Skip to content

Commit

Permalink
feat: add batch api to SyncedEnforcer
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Bichinger <[email protected]>
  • Loading branch information
abichinger authored and yyellowsun committed Jan 6, 2021
1 parent 9470c3e commit 9191ccd
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
8 changes: 7 additions & 1 deletion casbin/internal_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def _add_policies(self,sec,ptype,rules):
return rules_added

if self.adapter and self.auto_save:
if hasattr(self.adapter,'add_policies') is False:
return False

if self.adapter.add_policies(sec, ptype, rules) is False:
return False

Expand Down Expand Up @@ -57,6 +60,9 @@ def _remove_policies(self, sec, ptype, rules):
return rules_removed

if self.adapter and self.auto_save:
if hasattr(self.adapter,'remove_policies') is False:
return False

if self.adapter.remove_policies(sec, ptype, rules) is False:
return False

Expand All @@ -78,4 +84,4 @@ def _remove_filtered_policy(self, sec, ptype, field_index, *field_values):
if self.watcher:
self.watcher.update()

return rule_removed
return rule_removed
3 changes: 1 addition & 2 deletions casbin/management_enforcer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from casbin.internal_enforcer import InternalEnforcer


class ManagementEnforcer(InternalEnforcer):
"""
ManagementEnforcer = InternalEnforcer + Management API.
Expand Down Expand Up @@ -283,4 +282,4 @@ def remove_filtered_named_grouping_policy(self, ptype, field_index, *field_value

def add_function(self, name, func):
"""adds a customized function."""
self.fm.add_function(name, func)
self.fm.add_function(name, func)
3 changes: 2 additions & 1 deletion casbin/persist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .adapter import *
from .adapter_filtered import *
from .adapter_filtered import *
from .batch_adapter import *
58 changes: 56 additions & 2 deletions casbin/synced_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def value(self, value):
with self._lock:
self._value = value

class SyncedEnforcer(Enforcer):
class SyncedEnforcer():

"""SyncedEnforcer wraps Enforcer and provides synchronized access.
It's also a drop-in replacement for Enforcer"""
Expand Down Expand Up @@ -505,4 +505,58 @@ def enable_enforce(self, enabled=True):
def is_filtered(self):
"""returns true if the loaded policy has been filtered."""
with self._rl:
self._e.is_filtered()
self._e.is_filtered()

def add_policies(self,rules):
"""adds authorization rules to the current policy.
If the rule already exists, the function returns false for the corresponding rule and the rule will not be added.
Otherwise the function returns true for the corresponding rule by adding the new rule.
"""
with self._wl:
return self._e.add_policies(rules)

def add_named_policies(self,ptype,rules):
"""adds authorization rules to the current named policy.
If the rule already exists, the function returns false for the corresponding rule and the rule will not be added.
Otherwise the function returns true for the corresponding by adding the new rule."""
with self._wl:
return self._e.add_named_policies(ptype,rules)

def remove_policies(self,rules):
"""removes authorization rules from the current policy."""
with self._wl:
return self._e.remove_policies(rules)

def remove_named_policies(self,ptype,rules):
"""removes authorization rules from the current named policy."""
with self._wl:
return self._e.remove_named_policies(ptype,rules)

def add_grouping_policies(self,rules):
"""adds role inheritance rulea to the current policy.
If the rule already exists, the function returns false for the corresponding policy rule and the rule will not be added.
Otherwise the function returns true for the corresponding policy rule by adding the new rule.
"""
with self._wl:
return self._e.add_grouping_policies(rules)

def add_named_grouping_policies(self,ptype,rules):
""""adds named role inheritance rules to the current policy.
If the rule already exists, the function returns false for the corresponding policy rule and the rule will not be added.
Otherwise the function returns true for the corresponding policy rule by adding the new rule."""
with self._wl:
return self._e.add_named_grouping_policies(ptype,rules)

def remove_grouping_policies(self,rules):
"""removes role inheritance rulea from the current policy."""
with self._wl:
return self._e.addremove_grouping_policies_policies(rules)

def remove_named_grouping_policies(self,ptype,rules):
""" removes role inheritance rules from the current named policy."""
with self._wl:
return self._e.remove_named_grouping_policies(ptype,rules)
2 changes: 1 addition & 1 deletion test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ def test_filtered_adapter_invalid_filepath(self):
e = casbin.Enforcer("examples/rbac_with_domains_model.conf", adapter)

with self.assertRaises(FileNotFoundError):
e.load_filtered_policy(None)
e.load_filtered_policy(None)
5 changes: 2 additions & 3 deletions tests/test_management_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,12 @@ def test_modify_policy_api(self):
['eve', 'data3', 'read'],
['eve', 'data3', 'write'],
])
"""

class TestManagementApiSynced(TestManagementApi):

def get_enforcer(self, model=None, adapter=None, enable_log=False):
return casbin.SyncedEnforcer(
model,
adapter,
enable_log,
)
"""
)

0 comments on commit 9191ccd

Please sign in to comment.