Skip to content

Commit

Permalink
feat: support in operator on list/set and other compound types
Browse files Browse the repository at this point in the history
Signed-off-by: ffyuanda <46557895+ffyuanda@users.noreply.github.com>
  • Loading branch information
ffyuanda committed Aug 29, 2021
1 parent 2d0fa8c commit 3f6ee8f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions casbin/util/expression.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from simpleeval import SimpleEval
from simpleeval import EvalWithCompoundTypes
import ast


class SimpleEval(SimpleEval):
class SimpleEval(EvalWithCompoundTypes):
"""Rewrite SimpleEval.
>>> s = SimpleEval("20 + 30 - ( 10 * 5)")
>>> s.eval()
Expand Down
14 changes: 14 additions & 0 deletions examples/rbac_model_matcher_using_in_op_bracket.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

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

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act || r.obj in ['data2', 'data3']
2 changes: 1 addition & 1 deletion examples/rbac_policy.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write

g, alice, data2_admin
g, alice, data2_admin
11 changes: 11 additions & 0 deletions tests/test_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ def test_abac_with_multiple_sub_rules(self):
self.assertFalse(e.enforce(sub4, "/data1", "write"))
self.assertTrue(e.enforce(sub4, "/data2", "write"))

def test_matcher_using_in_operator_bracket(self):
e = self.get_enforcer(
get_examples("rbac_model_matcher_using_in_op_bracket.conf"),
get_examples("rbac_policy.csv"),
)

self.assertTrue(e.enforce("alice", "data1", "read"))
self.assertTrue(e.enforce("alice", "data2", "read"))
self.assertTrue(e.enforce("alice", "data3", "scribble"))
self.assertFalse(e.enforce("alice", "data4", "scribble"))


class TestConfigSynced(TestConfig):
def get_enforcer(self, model=None, adapter=None):
Expand Down

0 comments on commit 3f6ee8f

Please sign in to comment.