Skip to content

feat: add methods to retrieve domains for users and roles - #524

Merged
mserico merged 5 commits into
masterfrom
copilot/add-method-to-retrieve-domains
Dec 8, 2025
Merged

feat: add methods to retrieve domains for users and roles#524
mserico merged 5 commits into
masterfrom
copilot/add-method-to-retrieve-domains

Conversation

Copilot AI commented Dec 8, 2025

Copy link
Copy Markdown
Contributor

Users need to retrieve all domains associated with a user or role without manually iterating through policies. The Go Casbin library has getDomain functionality, but node-casbin lacked equivalent methods.

This implementation follows the exact Go Casbin pattern, architecture, and test cases to ensure compatibility.

Changes

RoleManager Interface - Added methods matching Go Casbin:

  • getDomains(name: string): Gets domains that a user has
  • getAllDomains(): Gets all domains

DefaultRoleManager - Implemented RoleManager methods:

  • Checks if a role has roles (inherits from) OR users (inherited by)
  • Filters out default domain when other domains exist (matching Go behavior)
  • Follows Go DomainManager implementation pattern

Enforcer - Added user-facing RBAC methods:

  • getDomainsForUser(user): Returns all domains for a user by calling RoleManager
  • getAllDomains(): Returns all domains by calling RoleManager
  • Methods are in Enforcer class (not ManagementEnforcer), matching Go Casbin's rbac_api.go

Test Coverage

  • Added rbac_with_domains_policy2.csv matching Go Casbin test data
  • Test cases match Go Casbin's test patterns and naming exactly
  • All 186 tests pass

Usage

const e = await newEnforcer('rbac_with_domains_model.conf', 'rbac_with_domains_policy2.csv');

// Get all domains in the system
const domains = await e.getAllDomains();  // ['domain1', 'domain2']

// Get domains for specific user
const userDomains = await e.getDomainsForUser('alice');  // ['domain1', 'domain2']

// Check domain access
if (userDomains.includes('domain1')) {
  // grant access
}

Policy example:

g, alice, admin, domain1
g, alice, admin, domain2
g, bob, admin, domain2
g, bob, user, domain3
Original prompt

This section details on the original issue you should resolve

<issue_title>Feature Request: Method to Retrieve All Domains for a User or a role</issue_title>
<issue_description>I noticed that the Casbin Go library includes a function getDomain. However, I'm looking for a method to retrieve all domains for a user without having to manually iterate over all policies.

Is there an existing method to get all domains for a user more efficiently, or is there a recommended approach to achieve this?

Thank you!</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@mserico
mserico marked this pull request as ready for review December 8, 2025 14:00
@coveralls

coveralls commented Dec 8, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 20033979759

Details

  • 28 of 29 (96.55%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 78.299%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/rbac/defaultRoleManager.ts 18 19 94.74%
Totals Coverage Status
Change from base Build 20034893874: 0.2%
Covered Lines: 1625
Relevant Lines: 1977

💛 - Coveralls

Copilot AI and others added 2 commits December 8, 2025 14:10
Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>
Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>
Copilot AI changed the title [WIP] Add method to retrieve all domains for a user Add methods to retrieve domains for users and roles Dec 8, 2025
Copilot AI requested a review from mserico December 8, 2025 14:17
@mserico

mserico commented Dec 8, 2025

Copy link
Copy Markdown
Contributor

@copilot should exactly use the same behavior as Go Casbin, including code and test cases and naming patterns

Copilot AI and others added 2 commits December 8, 2025 15:45
- Add getDomains and getAllDomains methods to RoleManager interface
- Implement methods in DefaultRoleManager following Go pattern
- Update Enforcer to call RoleManager methods (not ManagementEnforcer)
- Add rbac_with_domains_policy2.csv test file matching Go Casbin
- Update tests to match Go Casbin test patterns and naming
- Filter out default domain when other domains exist

Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>
- Update getDomainsForUser comment to be more descriptive
- Improve getDomains logic to check both roles and users
- Rename helper method for clarity (hasUserForRole)
- All tests pass

Co-authored-by: mserico <140243407+mserico@users.noreply.github.com>

Copilot AI commented Dec 8, 2025

Copy link
Copy Markdown
Contributor Author

@copilot should exactly use the same behavior as Go Casbin, including code and test cases and naming patterns

Updated to match Go Casbin exactly:

✅ Added getDomains() and getAllDomains() to RoleManager interface
✅ Implemented in DefaultRoleManager following Go pattern
✅ Methods are in Enforcer (rbac_api), not ManagementEnforcer
✅ Created rbac_with_domains_policy2.csv matching Go test data
✅ Test cases match Go Casbin test patterns and naming
✅ Filters default domain when other domains exist (Go behavior)

All 186 tests pass. See commit 2ba42d1 for the final implementation.

Copilot AI changed the title Add methods to retrieve domains for users and roles Add methods to retrieve domains for users and roles (Go Casbin compatible) Dec 8, 2025
@mserico mserico changed the title Add methods to retrieve domains for users and roles (Go Casbin compatible) feat: add methods to retrieve domains for users and roles Dec 8, 2025
@mserico
mserico merged commit 7cb2b2c into master Dec 8, 2025
11 of 13 checks passed
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.

Feature Request: Method to Retrieve All Domains for a User or a role

4 participants