Skip to content

Commit

Permalink
feat: Add batch_enforce() API (#298)
Browse files Browse the repository at this point in the history
* feat: Add batch_enforce() API

* feat: Add batch_enforce() API

* feat: Add batch_enforce() API

* Update core_enforcer.py

---------

Co-authored-by: hsluoyz <[email protected]>
  • Loading branch information
BustDot and hsluoyz authored May 24, 2023
1 parent 18ac56e commit 361af9b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions casbin/core_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,14 @@ def enforce_ex(self, *rvals):

return result, explain_rule

def batch_enforce(self, rvals):
"""batch_enforce enforce in batches"""
results = []
for request in rvals:
result = self.enforce(*request)
results.append(result)
return results

@staticmethod
def _get_expression(expr, functions=None):
expr = expr.replace("&&", "and")
Expand Down
7 changes: 7 additions & 0 deletions casbin/synced_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ def enforce_ex(self, *rvals):
with self._rl:
return self._e.enforce_ex(*rvals)

def batch_enforce(self, rvals):
"""batch_enforce enforce in batches,
input parameters are usually: [(sub, obj, act), (sub, obj, act), ...].
"""
with self._rl:
return self._e.batch_enforce(rvals)

def get_all_subjects(self):
"""gets the list of subjects that show up in the current policy."""
with self._rl:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ def test_enforce_ex_basic(self):
self.assertTupleEqual(e.enforce_ex("bob", "data2", "write"), (True, ["bob", "data2", "write"]))
self.assertTupleEqual(e.enforce_ex("bob", "data1", "write"), (False, []))

def test_batch_enforce(self):
e = self.get_enforcer(
get_examples("basic_model.conf"),
get_examples("basic_policy.csv"),
)
results = [True, False, True, False]
self.assertEqual(
e.batch_enforce(
[
("alice", "data1", "read"),
("alice", "data2", "read"),
("bob", "data2", "write"),
("bob", "data1", "write"),
]
),
results,
)

def test_model_set_load(self):
e = self.get_enforcer(
get_examples("basic_model.conf"),
Expand Down

0 comments on commit 361af9b

Please sign in to comment.