Skip to content

Feature Request: Add addGroupingPoliciesEx and other Ex variant methods #534

@hisabimbola

Description

@hisabimbola

Feature Request

Add the Ex variant methods that exist in the Go implementation but are missing in node-casbin.

Missing Methods

  • addPoliciesEx
  • addGroupingPoliciesEx
  • addNamedPoliciesEx
  • addNamedGroupingPoliciesEx

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:

  1. Partial sync failures: When syncing group memberships, if even one rule already exists, the entire batch fails silently
  2. Race conditions: Concurrent requests may cause partial failures
  3. 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

Environment

  • node-casbin version: 5.38.0
  • Node.js version: 20.x

Thank you!

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions