Skip to content

Conversation

@Huggin423
Copy link
Contributor

Description

This PR refactors the implementation to match the Go Casbin approach, as requested by the maintainer.

It introduces *WithoutNotify internal methods that operate strictly on the memory model (bypassing both adapter and watcher) and updates self* methods to call them directly. This prevents infinite loops in distributed environments.

Related Issue

Fixes #506

Verification

  • Tested locally using the provided test script.
  • Verified that selfAddPolicy updates memory correctly without invoking adapter.addPolicy.

@coveralls
Copy link

coveralls commented Jan 6, 2026

Pull Request Test Coverage Report for Build 20877464519

Details

  • 54 of 62 (87.1%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 78.612%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/internalEnforcer.ts 47 55 85.45%
Totals Coverage Status
Change from base Build 20874899852: 0.1%
Covered Lines: 1711
Relevant Lines: 2069

💛 - Coveralls

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the self* methods in the Management API to use a new *WithoutNotify pattern, aligning with the Go Casbin implementation. The refactoring prevents infinite loops in distributed environments by ensuring that self-operations bypass both adapter persistence and watcher notifications, operating strictly on the in-memory model.

Changes:

  • Introduced new *WithoutNotify protected methods in InternalEnforcer that update only the in-memory model
  • Refactored all self* methods in ManagementEnforcer to delegate to the corresponding *WithoutNotify methods
  • Added comprehensive test coverage to verify self-operations bypass adapters while correctly updating memory

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/managementAPI_self.test.ts New comprehensive test suite validating that self* methods bypass adapters and update memory correctly, including edge cases
src/managementEnforcer.ts Refactored all self* methods to call *WithoutNotify variants; added selfUpdatePolicies method; updated selfRemoveFilteredPolicy signature to use rest parameters
src/internalEnforcer.ts Added six new *WithoutNotify protected methods that update the model without calling adapters or watchers; added comments clarifying when persistence occurs in existing methods

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Add *WithoutNotify internal methods that skip adapter and watcher
- Implement selfAddPolicy, selfRemovePolicy, selfUpdatePolicy, selfAddPolicies, selfRemovePolicies, selfRemoveFilteredPolicy, selfUpdatePolicies
- These methods only update in-memory model to prevent infinite loops in distributed watcher scenarios
- Add comprehensive unit tests with edge cases and grouping policy coverage
- Align with Go Casbin implementation pattern

Fixes casbin#506
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hsluoyz hsluoyz changed the title refactor: implement self* methods using WithoutNotify pattern (sync with Go) feat: implement self* methods using WithoutNotify pattern (sync with Go) Jan 10, 2026
@hsluoyz hsluoyz merged commit d086457 into casbin:master Jan 10, 2026
15 checks passed
@github-actions
Copy link

🎉 This PR is included in version 5.48.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Methods in enforcer for adding, updating and deleting policies without usage of adapter (even if autoSave is true)

3 participants