feat: respect AutoSave flag for grouping policy operations#395
Merged
hsluoyz merged 1 commit intoapache:masterfrom Nov 7, 2025
Merged
feat: respect AutoSave flag for grouping policy operations#395hsluoyz merged 1 commit intoapache:masterfrom
hsluoyz merged 1 commit intoapache:masterfrom
Conversation
The EnableAutoSave(false) setting was not being respected for RBAC/grouping policy operations. When AutoSave was disabled, policy operations (AddPolicy, RemovePolicy, UpdatePolicy) correctly skipped adapter saving, but grouping policy operations (AddGroupingPolicy, RemoveGroupingPolicy, etc.) still saved to the adapter. Root Cause: The SetAutoSave() method only iterated through policy assertions (section "p") to set the AutoSave flag. It never set the flag for role assertions (section "g"), even though RoleAssertion has its own PolicyManager with an AutoSave property. Solution: Modified SetAutoSave() to also configure the AutoSave flag for role/grouping assertions, with a safety check for models without role definitions. Changes: - Casbin/Extensions/Model/ModelExtension.cs: Updated SetAutoSave() to handle both policy and role assertions - Casbin.UnitTests/ModelTests/EnforcerTest.cs: Added comprehensive tests for sync and async grouping policy operations - Casbin.UnitTests/Mock/MockSingleAdapter.cs: Created test helper to track adapter save operations All 363 unit tests pass with no regressions.
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where the EnableAutoSave() method was not applying the AutoSave setting to role/grouping policy assertions (section "g"), causing grouping policy changes to still trigger adapter saves even when AutoSave was disabled.
- Extended
SetAutoSave()to include role/grouping assertions in addition to policy assertions - Added two new tests to verify AutoSave behavior for grouping policies (sync and async)
- Created
MockSingleAdapterto facilitate testing of AutoSave behavior
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Casbin/Extensions/Model/ModelExtension.cs | Added logic to set AutoSave on role assertion PolicyManagers when role section exists |
| Casbin.UnitTests/ModelTests/EnforcerTest.cs | Added tests TestAutoSaveGroupingPolicy() and TestAutoSaveGroupingPolicyAsync() to verify the fix |
| Casbin.UnitTests/Mock/MockSingleAdapter.cs | New mock adapter that tracks individual policy operations to test AutoSave behavior |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
🎉 This PR is included in version 2.19.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Based on the documentation for
EnableAutoSave(), which states it "controls whether to save a policy rule automatically to the adapter when it is added or removed," we believe AutoSave should apply uniformly to all policy operations, including both regular policies and grouping policies.However, we discovered that grouping policy operations were not respecting the AutoSave flag. This PR addresses that inconsistency.
Note: This issue was discovered while working on adding support for multiple
CasbinDbContextinstances in the EFCore adapter (see casbin-net/EFCore-Adapter#125), where proper AutoSave behavior is essential for correct adapter operation.Problem
The
EnableAutoSave(false)setting was not being respected for RBAC/grouping policy operations:AddPolicy(),RemovePolicy(),UpdatePolicy()did not save to adapter when AutoSave was disabledAddGroupingPolicy(),RemoveGroupingPolicy(),UpdateGroupingPolicy()still saved to adapter even when AutoSave was disabledRoot Cause
The
SetAutoSave()method inCasbin/Extensions/Model/ModelExtension.csonly iterated through policy assertions (section "p") to set the AutoSave flag. It never set the flag for role assertions (section "g"), even thoughRoleAssertionhas its ownPolicyManagerwith anAutoSaveproperty.Solution
Modified
SetAutoSave()to configure the AutoSave flag for both policy and role/grouping assertions:The fix includes a safety check (
ContainsSection) to handle models without role definitions.Changes
Modified Files
Casbin/Extensions/Model/ModelExtension.cs
SetAutoSave()method to configure AutoSave for both policy and role assertionsCasbin.UnitTests/ModelTests/EnforcerTest.cs
TestAutoSaveGroupingPolicy()- Verifies synchronous grouping policy operations respect AutoSaveTestAutoSaveGroupingPolicyAsync()- Verifies asynchronous grouping policy operations respect AutoSaveCasbin.UnitTests/Mock/MockSingleAdapter.cs (new file)
ISingleAdapterTesting
Impact
Affected Methods (Now Fixed)
All grouping policy methods now properly respect the AutoSave flag:
AddGroupingPolicy()/AddGroupingPolicyAsync()AddGroupingPolicies()/AddGroupingPoliciesAsync()RemoveGroupingPolicy()/RemoveGroupingPolicyAsync()RemoveGroupingPolicies()/RemoveGroupingPoliciesAsync()UpdateGroupingPolicy()/UpdateGroupingPolicyAsync()UpdateGroupingPolicies()/UpdateGroupingPoliciesAsync()RemoveFilteredGroupingPolicy()/RemoveFilteredGroupingPolicyAsync()Backward Compatibility
We believe this fix aligns with the documented behavior and design intent of the AutoSave feature. However, if there was an intentional reason for the previous behavior, we're happy to discuss alternative approaches.