-
-
Notifications
You must be signed in to change notification settings - Fork 228
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Feature Request
Add the Ex variant methods that exist in the Go implementation but are missing in node-casbin.
Missing Methods
addPoliciesExaddGroupingPoliciesExaddNamedPoliciesExaddNamedGroupingPoliciesEx
Why This Is Needed
The current addGroupingPolicies method returns false immediately if any rule in the batch already exists, and no rules are added (atomic behavior).
From the Casbin Management API docs:
The difference between these methods and the methods without the Ex suffix is that if one of the rules already exists, they will continue checking the next rule instead of returning false immediately.
This causes issues in real-world scenarios:
- Partial sync failures: When syncing group memberships, if even one rule already exists, the entire batch fails silently
- Race conditions: Concurrent requests may cause partial failures
- Idempotency: Operations cannot be safely retried without pre-filtering
Current Workaround
We have to manually query existing rules and filter them out before calling addGroupingPolicies:
const existingMembers = await getGroupMembers(groupId, companyId);
const { toAdd } = computeToAddAndRemove(existingMembers, newUserIds);
if (toAdd.length > 0) {
await enforcer.addGroupingPolicies(toAdd);
}This adds extra queries and still doesn't handle race conditions.
Expected Behavior
// Should skip existing rules and add the rest, returning true
const result = await enforcer.addGroupingPoliciesEx([
['user1', 'group1', 'company1'], // already exists - skip
['user2', 'group1', 'company1'], // new - add
['user3', 'group1', 'company1'], // new - add
]);
// result = true (because some rules were added)Reference
- Go implementation: https://github.com/casbin/casbin/blob/master/management_api.go (lines 366-368)
- Documentation: https://casbin.org/docs/management-api/
Environment
- node-casbin version: 5.38.0
- Node.js version: 20.x
Thank you!
Copilot
Metadata
Metadata
Labels
enhancementNew feature or requestNew feature or request