Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Victor-Bayim
Copy link

This Pull Request fixes the issue where add_policies does not update the internal policy_map when adding new policies (see #356). Previously, add_policies simply appended new rules to the policy list without invoking add_policy, causing priority and other related information to be missing from policy_map. The main changes include:

Invoke add_policy for Each Rule

In add_policies, each new rule is added via the add_policy method to ensure that both the policy list and the policy_map are updated.
This approach is aligned with the Go Casbin implementation for consistency.
Duplicate Check

If any rule already exists, the function returns False to prevent adding duplicates.
Testing

All existing test cases have been run locally, and they pass successfully.
Additional tests for priority-based rules can be added in the future to further confirm that policy_map is correctly updated.
By applying this fix, add_policies now properly maintains the internal data structures, preventing access control logic errors due to an out-of-sync policy_map. Feedback and reviews are welcome!

@CLAassistant
Copy link

CLAassistant commented Mar 20, 2025

CLA assistant check
All committers have signed the CLA.

@Victor-Bayim Victor-Bayim changed the title Fix add_policies: update policy_map by calling add_policy for each rule (#356) fix(policy): update policy_map by calling add_policy (#356) Mar 20, 2025
@Victor-Bayim Victor-Bayim changed the title fix(policy): update policy_map by calling add_policy (#356) fix(policy): update policy_map by calling add_policy Mar 20, 2025
@Victor-Bayim Victor-Bayim force-pushed the fix/add_policies_policy_map branch from 0fdb343 to c3c3c78 Compare March 20, 2025 16:24
@@ -154,7 +154,8 @@ 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):
Copy link
Member

Choose a reason for hiding this comment

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

You should fix all similar issues, including but not limited to: update_policies, remove_policies, remove_filtered_policy

@Victor-Bayim Victor-Bayim force-pushed the fix/add_policies_policy_map branch from c3c3c78 to 46ba668 Compare March 21, 2025 13:27
@Victor-Bayim Victor-Bayim force-pushed the fix/add_policies_policy_map branch from 068de06 to 11079f3 Compare March 23, 2025 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants