From 4ff45f814846f0a1ed99c6de97adaf5133823ecb Mon Sep 17 00:00:00 2001 From: DivyPatel9881 Date: Sat, 27 Jun 2020 19:42:27 +0530 Subject: [PATCH] fix: change iteration logic for vectors and remove unwanted formating. --- casbin/model/assertion.cpp | 8 +-- casbin/model/model.cpp | 79 ++++++++++++++-------------- casbin/rbac/default_role_manager.cpp | 67 +++++++++-------------- 3 files changed, 70 insertions(+), 84 deletions(-) diff --git a/casbin/model/assertion.cpp b/casbin/model/assertion.cpp index ecb331f2..c827e0b8 100644 --- a/casbin/model/assertion.cpp +++ b/casbin/model/assertion.cpp @@ -14,8 +14,8 @@ void Assertion :: BuildIncrementalRoleLinks(RoleManager* rm, policy_op op, vecto if (char_count < 2) throw IllegalArgumentException("the number of \"_\" in role definition should be at least 2"); - for (vector> :: iterator it = this->policy.begin() ; it != this->policy.end() ; it++) { - vector rule = *it; + for(int i = 0 ; i < this->policy.size() ; i++){ + vector rule = this->policy[i]; if (rule.size() < char_count) throw IllegalArgumentException("grouping policy elements do not meet role definition"); @@ -40,8 +40,8 @@ void Assertion :: BuildRoleLinks(RoleManager* rm) { if (char_count < 2) throw IllegalArgumentException("the number of \"_\" in role definition should be at least 2"); - for (vector> :: iterator it = this->policy.begin() ; it != this->policy.end() ; it++) { - vector rule = *it; + for(int i = 0 ; i < this->policy.size() ; i++){ + vector rule = policy[i]; if (rule.size() < char_count) throw IllegalArgumentException("grouping policy elements do not meet role definition"); diff --git a/casbin/model/model.cpp b/casbin/model/model.cpp index 4bb534f6..2f0f5e1d 100644 --- a/casbin/model/model.cpp +++ b/casbin/model/model.cpp @@ -24,11 +24,12 @@ vector Model :: required_sections{"r","p","e","m"}; void Model :: LoadModelFromConfig(ConfigInterface *cfg) { for(unordered_map :: iterator it = section_name_map.begin() ; it != section_name_map.end() ; it++) LoadSection(this, cfg, it->first); + vector ms; - for(vector :: iterator it = required_sections.begin() ; it != required_sections.end() ; it++){ - if(!this->HasSection(*it)) - ms.push_back(section_name_map[*it]); - } + for(int i=0 ; i < required_sections.size() ; i++) + if(!this->HasSection(required_sections[i])) + ms.push_back(section_name_map[required_sections[i]]); + if(ms.size() > 0) throw MissingRequiredSections("missing required sections: " + Join(ms, ",")); } @@ -66,20 +67,22 @@ bool Model :: LoadAssertion(Model* model, ConfigInterface* cfg, string sec, stri bool Model :: AddDef(string sec, string key, string value) { if(value == "") return false; - Assertion ast; - ast.key = key; - ast.value = value; + + Assertion* ast = new Assertion; + ast->key = key; + ast->value = value; if (sec == "r" || sec == "p") { - ast.tokens = Split(ast.value, ","); - for (int i = 0; i < ast.tokens.size() ; i++) - ast.tokens[i] = key + "_" + Trim(ast.tokens[i]); + ast->tokens = Split(ast->value, ","); + for (int i = 0; i < ast->tokens.size() ; i++) + ast->tokens[i] = key + "_" + Trim(ast->tokens[i]); } else - ast.value = RemoveComments(EscapeAssertion(ast.value)); + ast->value = RemoveComments(EscapeAssertion(ast->value)); if (m.find(sec) != m.end()) m[sec] = AssertionMap(); - m[sec].assertion_map[key] = * + ast->policy = vector>{}; + m[sec].assertion_map[key] = ast; return true; } @@ -167,13 +170,13 @@ void Model :: PrintPolicy() { // ClearPolicy clears all current policy. void Model :: ClearPolicy() { - for (unordered_map :: iterator it = this->m["p"].assertion_map.begin() ; it != this->m["p"].assertion_map.end() ; it++) { - (it->second)->policy.clear(); - } + for (unordered_map :: iterator it = this->m["p"].assertion_map.begin() ; it != this->m["p"].assertion_map.end() ; it++) + if((it->second)->policy.size() > 0) + (it->second)->policy.clear(); - for (unordered_map :: iterator it = this->m["g"].assertion_map.begin() ; it != this->m["g"].assertion_map.end() ; it++) { - (it->second)->policy.clear(); - } + for (unordered_map :: iterator it = this->m["g"].assertion_map.begin() ; it != this->m["g"].assertion_map.end() ; it++) + if((it->second)->policy.size() > 0) + (it->second)->policy.clear(); } // GetPolicy gets all rules in a policy. @@ -184,18 +187,17 @@ vector> Model :: GetPolicy(string sec, string p_type) { // GetFilteredPolicy gets rules based on field filters from a policy. vector> Model :: GetFilteredPolicy(string sec, string p_type, int field_index, vector field_values) { vector> res; - - for (vector> :: iterator it = m[sec].assertion_map[p_type]->policy.begin() ; it != m[sec].assertion_map[p_type]->policy.end() ; it++){ + vector> policy(m[sec].assertion_map[p_type]->policy); + for(int i = 0 ; i < policy.size() ; i++){ bool matched = true; for(int i = 0 ; i < field_values.size() ; i++){ - if(field_values[i] != "" && (*it)[field_index + i] != field_values[i] ){ + if(field_values[i] != "" && (policy[i])[field_index + i] != field_values[i] ){ matched = false; break; } } - if(matched) { - res.push_back(*it); - } + if(matched) + res.push_back(policy[i]); } return res; @@ -203,11 +205,10 @@ vector> Model :: GetFilteredPolicy(string sec, string p_type, int // HasPolicy determines whether a model has the specified policy rule. bool Model :: HasPolicy(string sec, string p_type, vector rule) { - for (vector> :: iterator it = m[sec].assertion_map[p_type]->policy.begin() ; it != m[sec].assertion_map[p_type]->policy.end() ; it++) { - if (ArrayEquals(rule, *it)) { + vector> policy(m[sec].assertion_map[p_type]->policy); + for(int i=0 ; i < policy.size() ; i++) + if (ArrayEquals(rule, policy[i])) return true; - } - } return false; } @@ -267,21 +268,22 @@ bool Model :: RemovePolicies(string sec, string p_type, vector> r pair>> Model :: RemoveFilteredPolicy(string sec, string p_type, int field_index, vector field_values) { vector> tmp; vector> effects; + vector> policy(m[sec].assertion_map[p_type]->policy); bool res = false; - for (vector> :: iterator it = m[sec].assertion_map[p_type]->policy.begin() ; it != m[sec].assertion_map[p_type]->policy.end() ; it++) { + for(int i = 0 ; i < policy.size() ; i++){ bool matched = true; for (int i = 0 ; i < field_values.size() ; i++) { - if (field_values[i] != "" && (*it)[field_index+i] != field_values[i]) { + if (field_values[i] != "" && (policy[i])[field_index+i] != field_values[i]) { matched = false; break; } } if (matched){ - effects.push_back(*it); + effects.push_back(policy[i]); res = true; } else - tmp.push_back(*it); + tmp.push_back(policy[i]); } m[sec].assertion_map[p_type]->policy = tmp; @@ -292,10 +294,9 @@ pair>> Model :: RemoveFilteredPolicy(string sec, str // GetValuesForFieldInPolicy gets all values for a field for all rules in a policy, duplicated values are removed. vector Model :: GetValuesForFieldInPolicy(string sec, string p_type, int field_index) { vector values; - - for (vector> :: iterator it = m[sec].assertion_map[p_type]->policy.begin() ; it != m[sec].assertion_map[p_type]->policy.end() ; it++){ - values.push_back((*it)[field_index]); - } + vector> policy(m[sec].assertion_map[p_type]->policy); + for(int i = 0 ; i < policy.size() ; i++) + values.push_back((policy[i])[field_index]); ArrayRemoveDuplicates(values); @@ -307,9 +308,9 @@ vector Model :: GetValuesForFieldInPolicyAllTypes(string sec, int field_ vector values; for (unordered_map :: iterator it = m[sec].assertion_map.begin() ; it != m[sec].assertion_map.end() ; it++) { - for (vector :: iterator it1 = this->GetValuesForFieldInPolicy(sec, it->first, field_index).begin() ; it1 != this->GetValuesForFieldInPolicy(sec, it->first, field_index).end() ; it1++) { - values.push_back(*it1); - } + vector values_for_field(this->GetValuesForFieldInPolicy(sec, it->first, field_index)); + for(int i = 0 ; i < values_for_field.size() ; i++) + values.push_back(values_for_field[i]); } ArrayRemoveDuplicates(values); diff --git a/casbin/rbac/default_role_manager.cpp b/casbin/rbac/default_role_manager.cpp index e6c227e6..c9680aed 100644 --- a/casbin/rbac/default_role_manager.cpp +++ b/casbin/rbac/default_role_manager.cpp @@ -29,28 +29,23 @@ void Role :: DeleteRole(Role* role) { } bool Role :: HasRole(string name, int hierarchy_level) { - if (!this->name.compare(name)) { + if (!this->name.compare(name)) return true; - } - if (hierarchy_level <= 0) { + if (hierarchy_level <= 0) return false; - } - for (vector :: iterator it = roles.begin() ; it != roles.end() ; it++) { - if ((*it)->HasRole(name, hierarchy_level - 1)) { + for(int i = 0 ; i < roles.size() ; i++) + if (roles[i]->HasRole(name, hierarchy_level - 1)) return true; - } - } + return false; } bool Role :: HasDirectRole(string name) { - for (vector :: iterator it = roles.begin() ; it != roles.end() ; it++) { - if (!(*it)->name.compare(name)) { + for(int i = 0 ; i < roles.size() ; i++) + if (!roles[i]->name.compare(name)) return true; - } - } return false; } @@ -70,22 +65,20 @@ string Role :: ToString() { vector Role :: GetRoles() { vector names; - for (vector :: iterator it = roles.begin() ; it != roles.end() ; it++) { - names.push_back((*it)->name); - } + for(int i = 0 ; i < roles.size() ; i++) + names.push_back(roles[i]->name); + return names; } bool DefaultRoleManager :: HasRole(string name) { bool ok = false; - if (this->has_pattern) { - for (unordered_map :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++) { + if (this->has_pattern) + for (unordered_map :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++) if (this->matching_func(name, it->first)) ok = true; - } - } else { + else ok = this->all_roles.find(name) != this->all_roles.end(); - } return ok; } @@ -172,13 +165,11 @@ void DefaultRoleManager :: DeleteLink(string name1, string name2, vector if (domain_length == 1) { name1 = domain[0] + "::" + name1; name2 = domain[0] + "::" + name2; - } else if (domain_length > 1) { + } else if (domain_length > 1) throw CasbinRBACException("error: domain should be 1 parameter"); - } - if (!HasRole(name1) || !HasRole(name2)) { + if (!HasRole(name1) || !HasRole(name2)) throw CasbinRBACException("error: name1 or name2 does not exist"); - } Role* role1 = this->CreateRole(name1); Role* role2 = this->CreateRole(name2); @@ -194,17 +185,14 @@ bool DefaultRoleManager :: HasLink(string name1, string name2, vector do if (domain_length == 1) { name1 = domain[0] + "::" + name1; name2 = domain[0] + "::" + name2; - } else if (domain_length > 1) { + } else if (domain_length > 1) throw CasbinRBACException("error: domain should be 1 parameter"); - } - if (!name1.compare(name2)) { + if (!name1.compare(name2)) return true; - } - if (!HasRole(name1) || !HasRole(name2)) { + if (!HasRole(name1) || !HasRole(name2)) return false; - } Role* role1 = this->CreateRole(name1); return role1->HasRole(name2, max_hierarchy_level); @@ -216,11 +204,10 @@ bool DefaultRoleManager :: HasLink(string name1, string name2, vector do */ vector DefaultRoleManager :: GetRoles(string name, vector domain) { unsigned int domain_length = int(domain.size()); - if (domain_length == 1) { + if (domain_length == 1) name = domain[0] + "::" + name; - } else if (domain_length > 1) { + else if (domain_length > 1) throw CasbinRBACException("error: domain should be 1 parameter"); - } if (!HasRole(name)) { vector roles; @@ -228,11 +215,10 @@ vector DefaultRoleManager :: GetRoles(string name, vector domain } vector roles = this->CreateRole(name)->GetRoles(); - if (domain_length == 1) { - for (int i = 0; i < roles.size(); i ++) { + if (domain_length == 1) + for (int i = 0; i < roles.size(); i ++) roles[i] = roles[i].substr(domain[0].length() + 2, roles[i].length() - domain[0].length() - 2); - } - } + return roles; } @@ -252,10 +238,10 @@ vector DefaultRoleManager :: GetUsers(string name, vector domain names.push_back(role->name); } - if (domain.size() == 1) { + if (domain.size() == 1) for (int i = 0 ; i < names.size() ; i++) names[i] = names[i].substr(domain[0].length() + 2, names[i].length() - domain[0].length() - 2); - } + return names; } @@ -272,8 +258,7 @@ void DefaultRoleManager :: PrintRoles() { string text = this->all_roles.begin()->second->ToString(); unordered_map :: iterator it = this->all_roles.begin(); it++; - for ( ; it != this->all_roles.end() ; it++) { + for ( ; it != this->all_roles.end() ; it++) text += ", " + it->second->ToString(); - } // LogUtil::LogPrint(text); } \ No newline at end of file