Skip to content

Commit 3b77336

Browse files
committed
feat: add add_policies_Ex and add_named_policies_Ex APIs
1 parent b42b0be commit 3b77336

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

casbin/internal_enforcer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ def _add_policies(self, sec, ptype, rules):
5959

6060
return rules_added
6161

62+
def _add_policies_Ex(self, sec, ptype, rules):
63+
"""adds rules to the current policy."""
64+
rules_added = self.model.add_policies_Ex(sec, ptype, rules)
65+
if not rules_added:
66+
return rules_added
67+
68+
if self.adapter and self.auto_save:
69+
if hasattr(self.adapter, "add_policies_Ex") is False:
70+
return False
71+
72+
if self.adapter.add_policies_Ex(sec, ptype, rules) is False:
73+
return False
74+
75+
if self.watcher and self.auto_notify_watcher:
76+
if callable(getattr(self.watcher, "update_for_add_policies_Ex", None)):
77+
self.watcher.update_for_add_policies_Ex(sec, ptype, rules)
78+
else:
79+
self.watcher.update()
80+
81+
return rules_added
82+
6283
def _update_policy(self, sec, ptype, old_rule, new_rule):
6384
"""updates a rule from the current policy."""
6485
rule_updated = self.model.update_policy(sec, ptype, old_rule, new_rule)

casbin/management_enforcer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ def add_policies(self, rules):
117117
"""
118118
return self.add_named_policies("p", rules)
119119

120+
def add_policies_Ex(self, rules):
121+
"""add_policies_Ex adds authorization rules to the current policy.
122+
123+
If the rule already exists, the rule will not be added.
124+
But unlike add_policies, other non-existent rules are added instead of returning false directly.
125+
"""
126+
return self.add_named_policies_Ex("p", rules)
127+
120128
def add_named_policy(self, ptype, *params):
121129
"""adds an authorization rule to the current named policy.
122130
@@ -139,6 +147,14 @@ def add_named_policies(self, ptype, rules):
139147
Otherwise the function returns true for the corresponding by adding the new rule."""
140148
return self._add_policies("p", ptype, rules)
141149

150+
def add_named_policies_Ex(self, ptype, rules):
151+
"""add_named_policies_Ex adds authorization rules to the current policy.
152+
153+
If the rule already exists, the rule will not be added.
154+
But unlike add_named_policies, other non-existent rules are added instead of returning false directly.
155+
"""
156+
return self._add_policies_Ex("p", ptype, rules)
157+
142158
def update_policy(self, old_rule, new_rule):
143159
"""updates an authorization rule from the current policy."""
144160
return self.update_named_policy("p", old_rule, new_rule)

casbin/model/policy.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ def add_policies(self, sec, ptype, rules):
158158

159159
return True
160160

161+
def add_policies_Ex(self, sec, ptype, rules):
162+
"""
163+
add_policies_Ex adds authorization rules to the current policy.
164+
If the rule already exists, the rule will not be added.
165+
But unlike add_policies, other non-existent rules are added instead of returning false directly.
166+
"""
167+
rules_added = False
168+
for rule in rules:
169+
if self.has_policy(sec, ptype, rule):
170+
continue
171+
self.add_policy(sec, ptype, rule)
172+
rules_added = True
173+
return rules_added
174+
161175
def update_policy(self, sec, ptype, old_rule, new_rule):
162176
"""update a policy rule from the model."""
163177

0 commit comments

Comments
 (0)