Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions casbin/internal_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ def _add_policies(self, sec, ptype, rules):

return rules_added

def _add_policies_Ex(self, sec, ptype, rules):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ex -> ex

"""adds rules to the current policy."""
rules_added = self.model.add_policies_Ex(sec, ptype, rules)
if not rules_added:
return rules_added

if self.adapter and self.auto_save:
if hasattr(self.adapter, "add_policies_Ex") is False:
return False

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

if self.watcher and self.auto_notify_watcher:
if callable(getattr(self.watcher, "update_for_add_policies_Ex", None)):
self.watcher.update_for_add_policies_Ex(sec, ptype, rules)
else:
self.watcher.update()

return rules_added

def _update_policy(self, sec, ptype, old_rule, new_rule):
"""updates a rule from the current policy."""
rule_updated = self.model.update_policy(sec, ptype, old_rule, new_rule)
Expand Down
16 changes: 16 additions & 0 deletions casbin/management_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ def add_policies(self, rules):
"""
return self.add_named_policies("p", rules)

def add_policies_Ex(self, rules):
"""add_policies_Ex adds authorization rules to the current policy.

If the rule already exists, the rule will not be added.
But unlike add_policies, other non-existent rules are added instead of returning false directly.
"""
return self.add_named_policies_Ex("p", rules)

def add_named_policy(self, ptype, *params):
"""adds an authorization rule to the current named policy.

Expand All @@ -139,6 +147,14 @@ def add_named_policies(self, ptype, rules):
Otherwise the function returns true for the corresponding by adding the new rule."""
return self._add_policies("p", ptype, rules)

def add_named_policies_Ex(self, ptype, rules):
"""add_named_policies_Ex adds authorization rules to the current policy.

If the rule already exists, the rule will not be added.
But unlike add_named_policies, other non-existent rules are added instead of returning false directly.
"""
return self._add_policies_Ex("p", ptype, rules)

def update_policy(self, old_rule, new_rule):
"""updates an authorization rule from the current policy."""
return self.update_named_policy("p", old_rule, new_rule)
Expand Down
14 changes: 14 additions & 0 deletions casbin/model/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ def add_policies(self, sec, ptype, rules):

return True

def add_policies_Ex(self, sec, ptype, rules):
"""
add_policies_Ex adds authorization rules to the current policy.
If the rule already exists, the rule will not be added.
But unlike add_policies, other non-existent rules are added instead of returning false directly.
"""
rules_added = False
for rule in rules:
if self.has_policy(sec, ptype, rule):
continue
self.add_policy(sec, ptype, rule)
rules_added = True
return rules_added

def update_policy(self, sec, ptype, old_rule, new_rule):
"""update a policy rule from the model."""

Expand Down
18 changes: 18 additions & 0 deletions casbin/synced_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,24 @@ def add_policies(self, rules):
with self._wl:
return self._e.add_policies(rules)

def add_policies_Ex(self, rules):
"""add_policies_Ex adds authorization rules to the current policy.

If the rule already exists, the rule will not be added.
But unlike add_policies, other non-existent rules are added instead of returning false directly.
"""
with self._wl:
return self._e.add_policies_Ex(rules)

def add_named_policies_Ex(self, ptype, rules):
"""add_named_policies_Ex adds authorization rules to the current policy.

If the rule already exists, the rule will not be added.
But unlike add_named_policies, other non-existent rules are added instead of returning false directly.
"""
with self._wl:
return self._e.add_named_policies_Ex(ptype, rules)

def add_named_policies(self, ptype, rules):
"""adds authorization rules to the current named policy.

Expand Down
19 changes: 19 additions & 0 deletions tests/test_management_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,25 @@ def test_modify_policy_api(self):
],
)

e.clear_policy()
e.add_policies_Ex([["user1", "data1", "read"], ["user1", "data1", "read"]])
self.assertEqual(
e.get_policy(),
[["user1", "data1", "read"]],
)
e.add_policies_Ex([["user1", "data1", "read"], ["user2", "data2", "read"]])
self.assertEqual(
e.get_policy(),
[["user1", "data1", "read"], ["user2", "data2", "read"]],
)
e.add_named_policies_Ex(
"p", [["user1", "data1", "read"], ["user2", "data2", "read"], ["user3", "data3", "read"]]
)
self.assertEqual(
e.get_policy(),
[["user1", "data1", "read"], ["user2", "data2", "read"], ["user3", "data3", "read"]],
)


class TestManagementApiSynced(TestManagementApi):
def get_enforcer(self, model=None, adapter=None):
Expand Down
Loading