feat: Support multiple policy definitions (p, p2)#518
Conversation
|
|
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Modified coreEnforcer to iterate over all policy types when evaluating matchers, enabling support for models with multiple policy definitions like Go Casbin. Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Added tests for p/p2/p3 scenarios, enforceEx, backward compatibility, and the exact GitHub issue example. Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
- Add effectDone flag to properly terminate evaluation across policy types - Use ptype for eft parameter naming (correct behavior for multi-policy) - Add test for explicit eft column with multiple policy types Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Use Array.from() and Array.some() for better readability and performance. Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
|
@copilot the newly added code and test case should refer to the implementation Golang Casbin's: https://github.com/casbin/casbin (aka use the same implementation) |
I've analyzed the Go Casbin implementation. There's a key difference in approach: Go Casbin: Uses separate matchers for each policy type (m for p, m2 for p2). You select which to use via EnforceContext. A matcher like My implementation: Allows a single matcher to reference multiple policy types (e.g., The original issue shows a matcher that ONLY references 'p', yet expects 'p2' policies to match. With Go's approach, this wouldn't work either - you'd need either:
My implementation enables option 2 by default. Should I instead:
Please clarify the expected behavior. |
|
@copilot should exactly use the same behavior as Go Casbin, including code and test cases and naming patterns |
Check if matcher contains policy type tokens (e.g., p_, p2_) before evaluating, consistent with Go Casbin implementation. Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Document the implementation details, comparison with Go Casbin, and usage guide. Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
- Reverted to single pType evaluation per enforce call - Added pType reference check like Go: expString.includes(pType+"_") - Added Go's example files: multiple_policy_definitions and rbac_with_multiple_policy - Added test matching Go's TestMultiplePolicyDefinitions - Removed custom implementation that iterated all policy types Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Done. Implementation now exactly matches Go Casbin: Changes (commit b5327b0):
Behavior: |
# [5.42.0](v5.41.0...v5.42.0) (2025-12-08) ### Features * configure CI workflow to trigger on master branch ([#521](#521)) ([02ec045](02ec045)) * fix duplicate CI runs on pull requests ([#513](#513)) ([67ab8ee](67ab8ee)) * fix escapeAssertion to preserve string literals ([#514](#514)) ([7801732](7801732)) * Support multiple policy definitions (p, p2) ([#518](#518)) ([1b1f1bb](1b1f1bb))
Node-casbin failed to enforce policies when models contained multiple policy definitions (p, p2, p3, etc.). The enforcer only evaluated policies from the default type, causing p2/p3 references to fail.
Changes
Core enforcement logic (
src/coreEnforcer.ts):p_,p2_)strings.Contains(expString, pType+"_")Go Casbin Compatibility:
Implementation now exactly matches Go Casbin's behavior:
Example Files Added (from Go Casbin):
multiple_policy_definitions_model.confand.csvrbac_with_multiple_policy_model.confand.csvHow It Works (Same as Go):
Tests: Added test matching Go's
TestMultiplePolicyDefinitionsand RBAC with multiple policy test. All 183 tests pass.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.