Skip to content

Commit

Permalink
fix: matching_func works with multiply match (#172)
Browse files Browse the repository at this point in the history
* fix: matching_func works with multipie match

Closes #171

Signed-off-by: Zxilly <[email protected]>

* chore: typo

Closes #171

Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly authored Jun 30, 2021
1 parent af5659b commit bbe45ad
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions casbin/rbac/default_role_manager/role_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ def role_judge():

if self.has_pattern:
if self.matching_func(role1.name, role.name):
return role_judge()
if role_judge():
return True
continue
else:
if role1.name == role.name:
return role_judge()
if role_judge():
return True
continue
return False

def get_roles(self, name, *domain):
Expand Down
16 changes: 16 additions & 0 deletions examples/rbac_with_multiply_matched_pattern.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://github.com/casbin/pycasbin/issues/171
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, role

[role_definition]
g = _, _
g2 = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g2(r.act, p.role) && (g(r.sub, p.sub) || p.sub=='*') && keyMatch(r.obj, p.obj)
16 changes: 16 additions & 0 deletions examples/rbac_with_multiply_matched_pattern.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://github.com/casbin/pycasbin/issues/171
p, root, *, owner

g, root@localhost, root

g2, *.read, viewer

g2, *.read, editor
g2, *.update, editor

g2, *.read, admin
g2, *.update, admin
g2, *.create, admin
g2, *.delete, admin

g2, *.*, owner
10 changes: 10 additions & 0 deletions tests/test_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ def test_enforce_rbac_with_pattern(self):
self.assertTrue(e.enforce("bob", "/pen2/1", "GET"))
self.assertTrue(e.enforce("bob", "/pen2/2", "GET"))

def test_rbac_with_multipy_matched_pattern(self):
e = self.get_enforcer(
get_examples("rbac_with_multiply_matched_pattern.conf"),
get_examples("rbac_with_multiply_matched_pattern.csv"),
)

e.add_named_matching_func("g2", casbin.util.glob_match)

self.assertTrue(e.enforce("root@localhost", "/", "org.create"))

def test_enforce_abac_log_enabled(self):
e = self.get_enforcer(get_examples("abac_model.conf"))
sub = "alice"
Expand Down

0 comments on commit bbe45ad

Please sign in to comment.