Skip to content

Commit

Permalink
fix: change iteration logic for vectors and remove unwanted formating.
Browse files Browse the repository at this point in the history
  • Loading branch information
divy9881 committed Jun 27, 2020
1 parent d90e72d commit 4ff45f8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 84 deletions.
8 changes: 4 additions & 4 deletions casbin/model/assertion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<vector<string>> :: iterator it = this->policy.begin() ; it != this->policy.end() ; it++) {
vector<string> rule = *it;
for(int i = 0 ; i < this->policy.size() ; i++){
vector<string> rule = this->policy[i];

if (rule.size() < char_count)
throw IllegalArgumentException("grouping policy elements do not meet role definition");
Expand All @@ -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<vector<string>> :: iterator it = this->policy.begin() ; it != this->policy.end() ; it++) {
vector<string> rule = *it;
for(int i = 0 ; i < this->policy.size() ; i++){
vector<string> rule = policy[i];

if (rule.size() < char_count)
throw IllegalArgumentException("grouping policy elements do not meet role definition");
Expand Down
79 changes: 40 additions & 39 deletions casbin/model/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ vector<string> Model :: required_sections{"r","p","e","m"};
void Model :: LoadModelFromConfig(ConfigInterface *cfg) {
for(unordered_map <string, string> :: iterator it = section_name_map.begin() ; it != section_name_map.end() ; it++)
LoadSection(this, cfg, it->first);

vector<string> ms;
for(vector<string> :: 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, ","));
}
Expand Down Expand Up @@ -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;
ast->policy = vector<vector<string>>{};
m[sec].assertion_map[key] = ast;

return true;
}
Expand Down Expand Up @@ -167,13 +170,13 @@ void Model :: PrintPolicy() {

// ClearPolicy clears all current policy.
void Model :: ClearPolicy() {
for (unordered_map<string, Assertion*> :: iterator it = this->m["p"].assertion_map.begin() ; it != this->m["p"].assertion_map.end() ; it++) {
(it->second)->policy.clear();
}
for (unordered_map<string, Assertion*> :: 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<string, Assertion*> :: iterator it = this->m["g"].assertion_map.begin() ; it != this->m["g"].assertion_map.end() ; it++) {
(it->second)->policy.clear();
}
for (unordered_map<string, Assertion*> :: 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.
Expand All @@ -184,30 +187,28 @@ vector<vector<string>> Model :: GetPolicy(string sec, string p_type) {
// GetFilteredPolicy gets rules based on field filters from a policy.
vector<vector<string>> Model :: GetFilteredPolicy(string sec, string p_type, int field_index, vector<string> field_values) {
vector<vector<string>> res;

for (vector<vector<string>> :: iterator it = m[sec].assertion_map[p_type]->policy.begin() ; it != m[sec].assertion_map[p_type]->policy.end() ; it++){
vector<vector<string>> 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;
}

// HasPolicy determines whether a model has the specified policy rule.
bool Model :: HasPolicy(string sec, string p_type, vector<string> rule) {
for (vector<vector<string>> :: 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<vector<string>> 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;
}
Expand Down Expand Up @@ -267,21 +268,22 @@ bool Model :: RemovePolicies(string sec, string p_type, vector<vector<string>> r
pair<bool, vector<vector<string>>> Model :: RemoveFilteredPolicy(string sec, string p_type, int field_index, vector<string> field_values) {
vector<vector<string>> tmp;
vector<vector<string>> effects;
vector<vector<string>> policy(m[sec].assertion_map[p_type]->policy);
bool res = false;
for (vector<vector< string>> :: 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;
Expand All @@ -292,10 +294,9 @@ pair<bool, vector<vector<string>>> Model :: RemoveFilteredPolicy(string sec, str
// GetValuesForFieldInPolicy gets all values for a field for all rules in a policy, duplicated values are removed.
vector<string> Model :: GetValuesForFieldInPolicy(string sec, string p_type, int field_index) {
vector<string> values;

for (vector<vector<string>> :: 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<vector<string>> 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);

Expand All @@ -307,9 +308,9 @@ vector<string> Model :: GetValuesForFieldInPolicyAllTypes(string sec, int field_
vector<string> values;

for (unordered_map<string, Assertion*> :: iterator it = m[sec].assertion_map.begin() ; it != m[sec].assertion_map.end() ; it++) {
for (vector<string> :: 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<string> 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);
Expand Down
67 changes: 26 additions & 41 deletions casbin/rbac/default_role_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Role*> :: 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<Role*> :: 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;
}
Expand All @@ -70,22 +65,20 @@ string Role :: ToString() {

vector<string> Role :: GetRoles() {
vector<string> names;
for (vector<Role*> :: 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<string, Role*> :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++) {
if (this->has_pattern)
for (unordered_map<string, Role*> :: 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;
}
Expand Down Expand Up @@ -172,13 +165,11 @@ void DefaultRoleManager :: DeleteLink(string name1, string name2, vector<string>
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);
Expand All @@ -194,17 +185,14 @@ bool DefaultRoleManager :: HasLink(string name1, string name2, vector<string> 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);
Expand All @@ -216,23 +204,21 @@ bool DefaultRoleManager :: HasLink(string name1, string name2, vector<string> do
*/
vector<string> DefaultRoleManager :: GetRoles(string name, vector<string> 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<string> roles;
return roles;
}

vector<string> 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;
}

Expand All @@ -252,10 +238,10 @@ vector<string> DefaultRoleManager :: GetUsers(string name, vector<string> 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;
}

Expand All @@ -272,8 +258,7 @@ void DefaultRoleManager :: PrintRoles() {
string text = this->all_roles.begin()->second->ToString();
unordered_map<string, Role*> :: 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);
}

0 comments on commit 4ff45f8

Please sign in to comment.