Fix setPermission() to support enforcer format from Go backend's CasbinJsGetPermissionForUser #285
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.
Problem
Users integrating casbin.js with Go backends using
CasbinJsGetPermissionForUserwere unable to use manual mode correctly. The Go function exports permissions in an "enforcer format" containing model (m) and policies (p), butsetPermission()only supported a simple action-object format like{"read": ["data1", "data2"], "write": ["data1"]}.This caused permission checks to return
falsewhen they should returntrue:Solution
Enhanced
setPermission()to automatically detect and handle both permission formats:m(model) orp(policies) keys to identify enforcer formatinitEnforcer()for enforcer format, orPermission.load()for simple formatcan()method now checks if an enforcer is available in manual modesetUser()now sets the user property in all modes (required for enforcer-based permissions)Changes
setPermission()async to support enforcer initializationcan()to check for enforcer in manual mode before falling back to simple permission checksetUser()to work properly in manual mode with enforcer formatUsage
Breaking Change
setPermission()is now async and returnsPromise<void>instead ofvoid. Existing code needs to addawait:Testing
Fixes #[issue_number]
Original prompt
Fixes #284
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.