Skip to content

Commit bbe45ad

Browse files
authored
fix: matching_func works with multiply match (#172)
* fix: matching_func works with multipie match Closes #171 Signed-off-by: Zxilly <zhouxinyu1001@gmail.com> * chore: typo Closes #171 Signed-off-by: Zxilly <zhouxinyu1001@gmail.com>
1 parent af5659b commit bbe45ad

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

casbin/rbac/default_role_manager/role_manager.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,14 @@ def role_judge():
148148

149149
if self.has_pattern:
150150
if self.matching_func(role1.name, role.name):
151-
return role_judge()
151+
if role_judge():
152+
return True
153+
continue
152154
else:
153155
if role1.name == role.name:
154-
return role_judge()
156+
if role_judge():
157+
return True
158+
continue
155159
return False
156160

157161
def get_roles(self, name, *domain):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# https://github.com/casbin/pycasbin/issues/171
2+
[request_definition]
3+
r = sub, obj, act
4+
5+
[policy_definition]
6+
p = sub, obj, role
7+
8+
[role_definition]
9+
g = _, _
10+
g2 = _, _
11+
12+
[policy_effect]
13+
e = some(where (p.eft == allow))
14+
15+
[matchers]
16+
m = g2(r.act, p.role) && (g(r.sub, p.sub) || p.sub=='*') && keyMatch(r.obj, p.obj)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# https://github.com/casbin/pycasbin/issues/171
2+
p, root, *, owner
3+
4+
g, root@localhost, root
5+
6+
g2, *.read, viewer
7+
8+
g2, *.read, editor
9+
g2, *.update, editor
10+
11+
g2, *.read, admin
12+
g2, *.update, admin
13+
g2, *.create, admin
14+
g2, *.delete, admin
15+
16+
g2, *.*, owner

tests/test_enforcer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,16 @@ def test_enforce_rbac_with_pattern(self):
247247
self.assertTrue(e.enforce("bob", "/pen2/1", "GET"))
248248
self.assertTrue(e.enforce("bob", "/pen2/2", "GET"))
249249

250+
def test_rbac_with_multipy_matched_pattern(self):
251+
e = self.get_enforcer(
252+
get_examples("rbac_with_multiply_matched_pattern.conf"),
253+
get_examples("rbac_with_multiply_matched_pattern.csv"),
254+
)
255+
256+
e.add_named_matching_func("g2", casbin.util.glob_match)
257+
258+
self.assertTrue(e.enforce("root@localhost", "/", "org.create"))
259+
250260
def test_enforce_abac_log_enabled(self):
251261
e = self.get_enforcer(get_examples("abac_model.conf"))
252262
sub = "alice"

0 commit comments

Comments
 (0)