Skip to content

Commit a1612c2

Browse files
committed
fix: role manager createRole bug.
1 parent 405b251 commit a1612c2

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

casbin/rbac/default_role_manager.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ void Role :: AddRole(Role* role) {
3838

3939
void Role :: DeleteRole(Role* role) {
4040
for (int i = 0; i < roles.size();i++) {
41-
if (!roles[i]->name.compare(role->name))
41+
if (roles[i]->name == role->name)
4242
roles.erase(roles.begin()+i);
4343
}
4444
}
4545

4646
bool Role :: HasRole(string name, int hierarchy_level) {
47-
if (!this->name.compare(name))
47+
if (this->name == name)
4848
return true;
4949

5050
if (hierarchy_level <= 0)
@@ -60,23 +60,32 @@ bool Role :: HasRole(string name, int hierarchy_level) {
6060

6161
bool Role :: HasDirectRole(string name) {
6262
for(int i = 0 ; i < roles.size() ; i++){
63-
if (!roles[i]->name.compare(name))
63+
if (roles[i]->name == name)
6464
return true;
6565
}
6666

6767
return false;
6868
}
6969

7070
string Role :: ToString() {
71+
if(this->roles.size()==0)
72+
return "";
73+
7174
string names = "";
75+
if(this->roles.size() != 1)
76+
names += "(";
77+
7278
for (int i = 0; i < roles.size(); i ++) {
7379
Role* role = roles[i];
74-
if (i == 0) {
75-
names.append(role->name);
76-
} else {
77-
names.append(", " + role->name);
78-
}
80+
if (i == 0)
81+
names += role->name;
82+
else
83+
names += ", " + role->name;
7984
}
85+
86+
if(this->roles.size() != 1)
87+
names += ")";
88+
8089
return name + " < " + names;
8190
}
8291

@@ -116,7 +125,7 @@ Role* DefaultRoleManager :: CreateRole(string name) {
116125
if (this->matching_func(name, it->first) && name!=it->first) {
117126
Role* role1;
118127
bool ok1 = this->all_roles.find(it->first) != this->all_roles.end();
119-
if (!ok) {
128+
if (!ok1) {
120129
all_roles[it->first] = Role :: NewRole(it->first);
121130
role1 = all_roles[it->first];
122131
} else
@@ -145,12 +154,11 @@ DefaultRoleManager* DefaultRoleManager :: NewRoleManager(int max_hierarchy_level
145154
// e.BuildRoleLinks must be called after AddMatchingFunc().
146155
//
147156
// example: e.GetRoleManager().(*defaultrolemanager.RoleManager).AddMatchingFunc('matcher', util.KeyMatch)
148-
void DefaultRoleManager :: AddMatchingFunc(string name, MatchingFunc fn) {
157+
void DefaultRoleManager :: AddMatchingFunc(MatchingFunc fn) {
149158
this->has_pattern = true;
150159
this->matching_func = fn;
151160
}
152161

153-
154162
/**
155163
* clear clears all stored data and resets the role manager to the initial state.
156164
*/
@@ -208,7 +216,6 @@ bool DefaultRoleManager :: HasLink(string name1, string name2, vector<string> do
208216

209217
if (!name1.compare(name2))
210218
return true;
211-
212219
if (!HasRole(name1) || !HasRole(name2))
213220
return false;
214221

casbin/rbac/default_role_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class DefaultRoleManager : public RoleManager {
7575
// e.BuildRoleLinks must be called after AddMatchingFunc().
7676
//
7777
// example: e.GetRoleManager().(*defaultrolemanager.RoleManager).AddMatchingFunc('matcher', util.KeyMatch)
78-
void AddMatchingFunc(string name, MatchingFunc fn);
78+
void AddMatchingFunc(MatchingFunc fn);
7979

8080
/**
8181
* clear clears all stored data and resets the role manager to the initial state.

0 commit comments

Comments
 (0)