Skip to content

Commit f405223

Browse files
committed
DefaultRoleManager added.
DefaultRoleManager added. Removed unnecessary includeFiles. Made changes in ordered to unorder_map, enum to enum-class and declaring iterators inside for loop. h to hpp
1 parent 0ebdb4b commit f405223

14 files changed

+331
-29
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ StyleCopReport.xml
8585
*.tmp
8686
*.tmp_proj
8787
*_wpftmp.csproj
88-
*.log
88+
# *.log
8989
*.vspscc
9090
*.vssscc
9191
.builds

src/effect/DefaultEffector.h renamed to src/effect/DefaultEffector.hpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#include <string>
2-
3-
#include "Effect.h"
4-
#include "Effector.h"
5-
#include "exception/UnsupportedOperationException.h"
1+
#include "Effect.hpp"
2+
#include "Effector.hpp"
3+
#include "exception/UnsupportedOperationException.hpp"
64

75
/**
86
* DefaultEffector is default effector for Casbin.
@@ -20,34 +18,34 @@ class DefaultEffector : public Effector{
2018
if (!expr.compare("some(where (p_eft == allow))")) {
2119
result = false;
2220
for(unsigned int index = 0 ; index < number_of_effects ; index++){
23-
if (effects[index] == Allow) {
21+
if (effects[index] == Effect::Allow) {
2422
result = true;
2523
break;
2624
}
2725
}
2826
} else if (!expr.compare("!some(where (p_eft == deny))")) {
2927
result = true;
3028
for(unsigned int index = 0 ; index < number_of_effects ; index++){
31-
if (effects[index] == Deny) {
29+
if (effects[index] == Effect::Deny) {
3230
result = false;
3331
break;
3432
}
3533
}
3634
} else if (!expr.compare("some(where (p_eft == allow)) && !some(where (p_eft == deny))")) {
3735
result = false;
3836
for(unsigned int index = 0 ; index < number_of_effects ; index++){
39-
if (effects[index] == Allow) {
37+
if (effects[index] == Effect::Allow) {
4038
result = true;
41-
} else if (effects[index] == Deny) {
39+
} else if (effects[index] == Effect::Deny) {
4240
result = false;
4341
break;
4442
}
4543
}
4644
} else if (!expr.compare("priority(p_eft) || deny")) {
4745
result = false;
4846
for(unsigned int index = 0 ; index < number_of_effects ; index++){
49-
if (effects[index] != Indeterminate) {
50-
if (effects[index] == Allow) {
47+
if (effects[index] != Effect::Indeterminate) {
48+
if (effects[index] == Effect::Allow) {
5149
result = true;
5250
} else {
5351
result = false;

src/effect/Effect.h

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/effect/Effect.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum class Effect{
2+
Allow, Indeterminate, Deny
3+
};
4+
5+
typedef enum class Effect Effect;

src/effect/Effector.h renamed to src/effect/Effector.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <string>
22

3-
#include "Effect.h"
3+
#include "Effect.hpp"
44

55
/**
66
* Effector is the abstract class for Casbin effectors.

src/log/DefaultLogger.h renamed to src/log/DefaultLogger.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
#include <string>
2-
3-
#include "Logger.h"
4-
#include "Log.h"
1+
#include "Logger.hpp"
2+
#include "Log.hpp"
53

64
class DefaultLogger : public Logger{
7-
private:
8-
bool enable;
9-
105
public:
6+
117
void EnableLog(bool enable) {
128
this->enable = enable;
139
}
File renamed without changes.

src/log/LogUtil.h renamed to src/log/LogUtil.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
#include <string>
2-
3-
#include "DefaultLogger.h"
4-
#include "Logger.h"
1+
#include "DefaultLogger.hpp"
52

63
class LogUtil{
74
private:

src/log/Logger.h renamed to src/log/Logger.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#include <string>
22

33
class Logger{
4+
protected:
5+
bool enable;
6+
47
public:
8+
59
//EnableLog controls whether print the message.
610
void EnableLog(bool enable);
711

src/rbac/DefaultRoleManager.hpp

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
#include <unordered_map>
2+
3+
#include "RoleManager.hpp"
4+
#include "exception/IllegalArgumentException.hpp"
5+
#include "log/LogUtil.hpp"
6+
7+
/**
8+
* Role represents the data structure for a role in RBAC.
9+
*/
10+
class Role {
11+
12+
private:
13+
vector <Role> roles;
14+
15+
public:
16+
string name;
17+
18+
Role(string name) {
19+
this->name = name;
20+
}
21+
22+
void addRole(Role role) {
23+
for (Role r : roles) {
24+
if (!r.name.compare(role.name)) {
25+
return;
26+
}
27+
}
28+
29+
roles.push_back(role);
30+
}
31+
32+
void deleteRole(Role role) {
33+
for (vector <Role> :: iterator it = roles.begin() ; it != roles.end() ; it++) {
34+
if (!(*it).name.compare(role.name)) {
35+
roles.erase(it);
36+
}
37+
}
38+
}
39+
40+
bool hasRole(string name, int hierarchyLevel) {
41+
if (!this->name.compare(name)) {
42+
return true;
43+
}
44+
45+
if (hierarchyLevel <= 0) {
46+
return false;
47+
}
48+
49+
for (vector <Role> :: iterator it = roles.begin() ; it != roles.end() ; it++) {
50+
if ((*it).hasRole(name, hierarchyLevel - 1)) {
51+
return true;
52+
}
53+
}
54+
return false;
55+
}
56+
57+
bool hasDirectRole(string name) {
58+
for (vector <Role> :: iterator it = roles.begin() ; it != roles.end() ; it++) {
59+
if (!(*it).name.compare(name)) {
60+
return true;
61+
}
62+
}
63+
64+
return false;
65+
}
66+
67+
string toString() {
68+
string names = "";
69+
for (int i = 0; i < roles.size(); i ++) {
70+
Role role = roles[i];
71+
if (i == 0) {
72+
names.append(role.name);
73+
} else {
74+
names.append(", " + role.name);
75+
}
76+
}
77+
return name + " < " + names;
78+
}
79+
80+
vector<string> getRoles() {
81+
vector <string> names;
82+
for (vector <Role> :: iterator it = roles.begin() ; it != roles.end() ; it++) {
83+
names.push_back((*it).name);
84+
}
85+
return names;
86+
}
87+
};
88+
89+
class DefaultRoleManager : public RoleManager {
90+
private:
91+
unordered_map <string, Role> allRoles;
92+
int maxHierarchyLevel;
93+
94+
bool hasRole(string name) {
95+
return allRoles.find(name) != allRoles.end();
96+
}
97+
98+
Role createRole(string name) {
99+
if (hasRole(name)) {
100+
return allRoles.at(name);
101+
} else {
102+
Role role = Role(name);
103+
allRoles[name] = role;
104+
return role;
105+
}
106+
}
107+
108+
public:
109+
/**
110+
* DefaultRoleManager is the constructor for creating an instance of the
111+
* default RoleManager implementation.
112+
*
113+
* @param maxHierarchyLevel the maximized allowed RBAC hierarchy level.
114+
*/
115+
DefaultRoleManager(int maxHierarchyLevel) {
116+
this->maxHierarchyLevel = maxHierarchyLevel;
117+
}
118+
119+
120+
121+
/**
122+
* clear clears all stored data and resets the role manager to the initial state.
123+
*/
124+
void clear() {
125+
allRoles.clear();
126+
}
127+
128+
/**
129+
* addLink adds the inheritance link between role: name1 and role: name2.
130+
* aka role: name1 inherits role: name2.
131+
* domain is a prefix to the roles.
132+
*/
133+
void addLink(string name1, string name2, string domain[]) {
134+
unsigned int domain_length = sizeof(domain)/sizeof(domain[0]);
135+
if (domain_length == 1) {
136+
name1 = domain[0] + "::" + name1;
137+
name2 = domain[0] + "::" + name2;
138+
} else if (domain_length > 1) {
139+
throw new IllegalArgumentException("error: domain should be 1 parameter");
140+
}
141+
142+
Role role1 = createRole(name1);
143+
Role role2 = createRole(name2);
144+
role1.addRole(role2);
145+
}
146+
147+
/**
148+
* deleteLink deletes the inheritance link between role: name1 and role: name2.
149+
* aka role: name1 does not inherit role: name2 any more.
150+
* domain is a prefix to the roles.
151+
*/
152+
void deleteLink(string name1, string name2, string domain[]) {
153+
unsigned int domain_length = sizeof(domain)/sizeof(domain[0]);
154+
if (domain_length == 1) {
155+
name1 = domain[0] + "::" + name1;
156+
name2 = domain[0] + "::" + name2;
157+
} else if (domain_length > 1) {
158+
throw new IllegalArgumentException("error: domain should be 1 parameter");
159+
}
160+
161+
if (!hasRole(name1) || !hasRole(name2)) {
162+
throw new IllegalArgumentException("error: name1 or name2 does not exist");
163+
}
164+
165+
Role role1 = createRole(name1);
166+
Role role2 = createRole(name2);
167+
role1.deleteRole(role2);
168+
}
169+
170+
/**
171+
* hasLink determines whether role: name1 inherits role: name2.
172+
* domain is a prefix to the roles.
173+
*/
174+
bool hasLink(string name1, string name2, string domain[]) {
175+
unsigned int domain_length = sizeof(domain)/sizeof(domain[0]);
176+
if (domain_length == 1) {
177+
name1 = domain[0] + "::" + name1;
178+
name2 = domain[0] + "::" + name2;
179+
} else if (domain_length > 1) {
180+
throw new IllegalArgumentException("error: domain should be 1 parameter");
181+
}
182+
183+
if (!name1.compare(name2)) {
184+
return true;
185+
}
186+
187+
if (!hasRole(name1) || !hasRole(name2)) {
188+
return false;
189+
}
190+
191+
Role role1 = createRole(name1);
192+
return role1.hasRole(name2, maxHierarchyLevel);
193+
}
194+
195+
/**
196+
* getRoles gets the roles that a subject inherits.
197+
* domain is a prefix to the roles.
198+
*/
199+
vector <string> getRoles(string name, string domain[]) {
200+
unsigned int domain_length = sizeof(domain)/sizeof(domain[0]);
201+
if (domain_length == 1) {
202+
name = domain[0] + "::" + name;
203+
} else if (domain_length > 1) {
204+
throw new IllegalArgumentException("error: domain should be 1 parameter");
205+
}
206+
207+
if (!hasRole(name)) {
208+
throw new IllegalArgumentException("error: name does not exist");
209+
}
210+
vector <string> roles = createRole(name).getRoles();
211+
if (domain_length == 1) {
212+
for (int i = 0; i < roles.size(); i ++) {
213+
roles[i] = roles[i].substr(domain[0].length() + 2, roles[i].length());
214+
}
215+
}
216+
return roles;
217+
}
218+
219+
/**
220+
* getUsers gets the users that inherits a subject.
221+
* domain is an unreferenced parameter here, may be used in other implementations.
222+
*/
223+
vector <string> getUsers(string name) {
224+
if (!hasRole(name)) {
225+
throw new IllegalArgumentException("error: name does not exist");
226+
}
227+
228+
vector <string> names;
229+
for (unordered_map <string, Role> :: iterator it = allRoles.begin() ; it != allRoles.end() ; it++) {
230+
if ((it->second).hasDirectRole(name)) {
231+
names.push_back((it->second).name);
232+
}
233+
}
234+
return names;
235+
}
236+
237+
/**
238+
* printRoles prints all the roles to log.
239+
*/
240+
void printRoles() {
241+
DefaultLogger df_logger;
242+
df_logger.EnableLog(true);
243+
244+
Logger *logger = &df_logger;
245+
LogUtil::SetLogger(*logger);
246+
247+
for (unordered_map <string, Role> :: iterator it = allRoles.begin() ; it != allRoles.end() ; it++) {
248+
LogUtil::LogPrint((it->second).toString());
249+
}
250+
}
251+
};
252+
253+

0 commit comments

Comments
 (0)