-
-
Notifications
You must be signed in to change notification settings - Fork 487
Description
Background:
We need to add a hook point for cycle detection into jcasbin, while following the principles of “minimal intrusion” and “loose coupling.” DefaultRoleManager’s addLink method is the only place that updates the inheritance relationship
github.com
.
Tasks:
-
Add an optional
Detectorfield inDefaultRoleManager:private Detector detector;
public void setDetector(Detector detector) {
this.detector = detector;
}The default value is
nullto maintain backward compatibility. -
Modify
addLink(String name1, String name2, String... domain): after successfully adding the parent-child relationship, ifdetectoris notnull, calldetector.check(this).- If a non-empty error message is returned, immediately roll back the current
addRoleoperation (callremoveRoleor delete that link), and throw anIllegalArgumentExceptioncontaining the error description.
- If a non-empty error message is returned, immediately roll back the current
-
Create
src/test/java/org/casbin/jcasbin/main/DetectorTest.java:- Construct a
DefaultRoleManager, inject aDefaultDetector, add some valid inheritance chains, and assert that no exception is thrown. - Construct a chain containing a cycle, e.g., A→B→C→A, and assert that when adding the third link, an exception is thrown.
- Assert that after rollback, the state no longer contains the illegal link.
- If necessary, add more boundary tests (e.g., self-loop, complex graphs).
- Construct a
-
In tests, do not depend on
Enforcer; only useDefaultRoleManagerandDefaultDetector. This can speed up test execution and reduce coupling.
Constraints:
- Integration modifications must keep the existing public API unchanged. Inject
detectoronly via a setter; do not force all users to enable detection. - Unit tests must use JUnit 5 and ensure they pass within the project’s existing test suite.