chore: port GetUsers/GetNamedUsers API from Go to Java#516
Open
AlabLan wants to merge 3 commits into
Open
Conversation
- add getUsers() method to ManagementEnforcer that returns subjects excluding roles - add getValuesForFieldInPolicyAllTypes() and getFieldIndex() to Model for subject extraction - update test imports and organization in ManagementAPIUnitTest
Users = subjects from "p" - roles from "g" (replacing early-branch logic that included g column 0)
There was a problem hiding this comment.
Pull request overview
Ports the Casbin Go GetUsers/GetNamedUsers management APIs to the Java implementation to separate “users” (subjects) from “roles” (grouping targets), keeping behavior aligned across languages.
Changes:
- Added
getUsers()/getNamedUsers(ptype)toManagementEnforcer, with thread-safe wrappers inSyncedEnforcer. - Added supporting utilities:
Model.getFieldIndex(ptype, field)andUtil.setSubtract(a, b). - Added a unit test covering the new APIs with the RBAC example model/policy.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java | Adds unit test coverage for getUsers() and getNamedUsers() using RBAC example data. |
| src/main/java/org/casbin/jcasbin/util/Util.java | Adds setSubtract() helper used by the new user/role separation logic. |
| src/main/java/org/casbin/jcasbin/model/Model.java | Adds getFieldIndex() (and a helper to get field values across all policy types). |
| src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java | Adds synchronized wrappers for the new management APIs. |
| src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java | Implements getUsers() / getNamedUsers(ptype) using subjects minus roles. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Rename getUsers() to getAllUsers() to align with Go API - Remove getNamedUsers() as Go final version doesn't have it - Fix Model.getFieldIndex() to return -1 when not found (instead of 0) - Add null check in Model.getFieldIndex() to prevent NPE - Update Javadoc to accurately describe behavior - Update tests accordingly
Author
BackgroundIssue: apache/casbin#1621 Sync GetAllUsers API from Go Casbin (PR #1652) to Java version. Changes
Testing
|
Author
|
Hi @hsluoyz, I've addressed the Copilot review feedback in the latest commit. Could you please take another look when you have a chance? Thanks! |
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.
Background
Issue: apache/casbin#1621
This PR ports the users/roles separation feature (implemented in Go PR #1652) to the Java version,
ensuring API and implementation logic are synchronized across languages.
Changes
New APIs
getUsers()- Get all users in the current policygetNamedUsers(ptype)- Get all users in the specified policy typeAlgorithm (consistent with final Go implementation):
Users = subjects from "p" - roles from "g"
Ported Utilities
GetFieldIndexModel.getFieldIndex(ptype, field)SetSubtractUtil.setSubtract()Implementation Note
The initial port used the early-branch algorithm that included "g" column 0 entities.
This has been corrected to match the final Go version (PR #1652).
Files Changed
Model.java- AddgetFieldIndex()methodUtil.java- AddsetSubtract()methodManagementEnforcer.java- AddgetUsers()andgetNamedUsers(ptype)SyncedEnforcer.java- Add thread-safe wrappersTesting