-
-
Notifications
You must be signed in to change notification settings - Fork 211
fix(policy): update policy_map by calling add_policy #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Victor-Bayim
wants to merge
3
commits into
casbin:master
from
Victor-Bayim:fix/add_policies_policy_map
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -154,13 +154,12 @@ def add_policies(self, sec, ptype, rules): | |||||||||||
| return False | ||||||||||||
|
|
||||||||||||
| for rule in rules: | ||||||||||||
| self[sec][ptype].policy.append(rule) | ||||||||||||
|
|
||||||||||||
| if not self.add_policy(sec, ptype, rule): | ||||||||||||
| return False | ||||||||||||
| return True | ||||||||||||
|
|
||||||||||||
| def update_policy(self, sec, ptype, old_rule, new_rule): | ||||||||||||
| """update a policy rule from the model.""" | ||||||||||||
|
|
||||||||||||
| if sec not in self.keys(): | ||||||||||||
| return False | ||||||||||||
| if ptype not in self[sec]: | ||||||||||||
|
|
@@ -173,20 +172,19 @@ def update_policy(self, sec, ptype, old_rule, new_rule): | |||||||||||
| else: | ||||||||||||
| return False | ||||||||||||
|
|
||||||||||||
| if "p_priority" in ast.tokens: | ||||||||||||
| priority_index = ast.tokens.index("p_priority") | ||||||||||||
| if old_rule[priority_index] == new_rule[priority_index]: | ||||||||||||
| ast.policy[rule_index] = new_rule | ||||||||||||
| else: | ||||||||||||
| raise Exception("New rule should have the same priority with old rule.") | ||||||||||||
| else: | ||||||||||||
| ast.policy[rule_index] = new_rule | ||||||||||||
| ast.policy[rule_index] = new_rule | ||||||||||||
|
|
||||||||||||
| old_key = DEFAULT_SEP.join(old_rule) | ||||||||||||
| new_key = DEFAULT_SEP.join(new_rule) | ||||||||||||
| if old_key in ast.policy_map: | ||||||||||||
| del ast.policy_map[old_key] | ||||||||||||
| ast.policy_map[new_key] = rule_index | ||||||||||||
|
|
||||||||||||
| return True | ||||||||||||
|
|
||||||||||||
| def update_policies(self, sec, ptype, old_rules, new_rules): | ||||||||||||
| """update policy rules from the model.""" | ||||||||||||
|
|
||||||||||||
| """update policy rules from the model using update_policy for each rule. | ||||||||||||
| If any update fails, roll back all changes.""" | ||||||||||||
| if sec not in self.keys(): | ||||||||||||
| return False | ||||||||||||
| if ptype not in self[sec]: | ||||||||||||
|
|
@@ -195,24 +193,15 @@ def update_policies(self, sec, ptype, old_rules, new_rules): | |||||||||||
| return False | ||||||||||||
|
|
||||||||||||
| ast = self[sec][ptype] | ||||||||||||
| old_rules_index = [] | ||||||||||||
|
|
||||||||||||
| for old_rule in old_rules: | ||||||||||||
| if old_rule in ast.policy: | ||||||||||||
| old_rules_index.append(ast.policy.index(old_rule)) | ||||||||||||
| else: | ||||||||||||
| return False | ||||||||||||
| original_policy = [rule[:] for rule in ast.policy] | ||||||||||||
| original_policy_map = ast.policy_map.copy() | ||||||||||||
|
|
||||||||||||
| if "p_priority" in ast.tokens: | ||||||||||||
| priority_index = ast.tokens.index("p_priority") | ||||||||||||
| for idx, old_rule, new_rule in zip(old_rules_index, old_rules, new_rules): | ||||||||||||
| if old_rule[priority_index] == new_rule[priority_index]: | ||||||||||||
| ast.policy[idx] = new_rule | ||||||||||||
| else: | ||||||||||||
| raise Exception("New rule should have the same priority with old rule.") | ||||||||||||
| else: | ||||||||||||
| for idx, old_rule, new_rule in zip(old_rules_index, old_rules, new_rules): | ||||||||||||
| ast.policy[idx] = new_rule | ||||||||||||
| for old_rule, new_rule in zip(old_rules, new_rules): | ||||||||||||
| if not self.update_policy(sec, ptype, old_rule, new_rule): | ||||||||||||
| ast.policy = original_policy | ||||||||||||
| ast.policy_map = original_policy_map | ||||||||||||
| return False | ||||||||||||
|
|
||||||||||||
| return True | ||||||||||||
|
|
||||||||||||
|
|
@@ -221,20 +210,21 @@ def remove_policy(self, sec, ptype, rule): | |||||||||||
| if not self.has_policy(sec, ptype, rule): | ||||||||||||
| return False | ||||||||||||
|
|
||||||||||||
| self[sec][ptype].policy.remove(rule) | ||||||||||||
| assertion = self[sec][ptype] | ||||||||||||
| assertion.policy.remove(rule) | ||||||||||||
|
|
||||||||||||
| return rule not in self[sec][ptype].policy | ||||||||||||
| new_map = {} | ||||||||||||
| for idx, r in enumerate(assertion.policy): | ||||||||||||
| new_map[DEFAULT_SEP.join(r)] = idx | ||||||||||||
| assertion.policy_map = new_map | ||||||||||||
|
Comment on lines
+216
to
+219
|
||||||||||||
| new_map = {} | |
| for idx, r in enumerate(assertion.policy): | |
| new_map[DEFAULT_SEP.join(r)] = idx | |
| assertion.policy_map = new_map | |
| assertion.policy_map = self._rebuild_policy_map(assertion.policy) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.