Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support AddNamedPoliciesEx API #417

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ boolean addPolicy(String sec, String ptype, List<String> rule) {
/**
* addPolicies adds rules to the current policy.
*/
boolean addPolicies(String sec, String ptype, List<List<String>> rules) {
boolean addPolicies(String sec, String ptype, List<List<String>> rules, boolean autoRemoveRepeat) {
if(autoRemoveRepeat) {
for (List<String> rule : rules) {
if(model.hasPolicy(sec, ptype, rule)) {
model.removePolicy(sec, ptype, rule);
}
}
}

if (mustUseDispatcher()) {
dispatcher.addPolicies(sec, ptype, rules);
return true;
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,20 @@ public boolean addNamedPolicy(String ptype, List<String> params) {
* @return succeeds or not.
*/
public boolean addNamedPolicies(String ptype, List<List<String>> rules) {
return addPolicies("p", ptype, rules);
return addPolicies("p", ptype, rules, false);
}

/**
* addNamedPoliciesEx adds authorization rules to the current named policy.
* If the rule already exists, the rule will not be added.
* But unlike AddNamedPolicies, other non-existent rules are added instead of returning false directly
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @param rules the "p" policy rules.
* @return succeeds or not.
*/
public boolean addNamedPoliciesEx(String ptype, List<List<String>> rules) {
return addPolicies("p", ptype, rules, true);
}

/**
Expand Down Expand Up @@ -616,7 +629,7 @@ public boolean addNamedGroupingPolicy(String ptype, String... params) {
* @return succeeds or not.
*/
public boolean addNamedGroupingPolicies(String ptype, List<List<String>> rules) {
return addPolicies("g", ptype, rules);
return addPolicies("g", ptype, rules, false);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,20 @@ public boolean addNamedPolicies(String ptype, List<List<String>> rules) {
return runSynchronized(() -> super.addNamedPolicies(ptype, rules), READ_WRITE_LOCK.writeLock());
}

/**
* addNamedPoliciesEx adds authorization rules to the current named policy.
* If the rule already exists, the rule will not be added.
* But unlike AddNamedPolicies, other non-existent rules are added instead of returning false directly
*
* @param ptype the policy type, can be "p", "p2", "p3", ..
* @param rules the "p" policy rules.
* @return succeeds or not.
*/
@Override
public boolean addNamedPoliciesEx(String ptype, List<List<String>> rules) {
return runSynchronized(() -> super.addNamedPoliciesEx(ptype, rules), READ_WRITE_LOCK.writeLock());
}

/**
* updateNamedPolicy updates an authorization rule to the current named policy.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testAddPolicy() {

@Test
public void testAddPolicies() {
boolean result = enforcer.addPolicies(SEC, PTYPE, singletonList(RULE));
boolean result = enforcer.addPolicies(SEC, PTYPE, singletonList(RULE), false);
assertTrue(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ public void testModifyPolicyAPI() {

e.updateNamedPolicy("p", asList("eve", "data2", "read"), asList("eve", "data4", "read"));
testGetPolicy(e, asList(asList("eve", "data4", "read")));

e.addNamedPolicies("p", asList(asList("eve", "data4", "read"), asList("user1", "data1", "read")));
testGetPolicy(e, asList(asList("eve", "data4", "read")));

e.addNamedPoliciesEx("p", asList(asList("eve", "data4", "read"), asList("user1", "data1", "read")));
testGetPolicy(e, asList(asList("eve", "data4", "read"), asList("user1", "data1", "read")));



}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public void testModifyPolicyAPI() {
e.removeFilteredPolicy(1, "data2");

testGetPolicy(e, asList(asList("eve", "data3", "read")));

e.addNamedPolicies("p", asList(asList("eve", "data3", "read"), asList("user1", "data1", "read")));
testGetPolicy(e, asList(asList("eve", "data3", "read")));

e.addNamedPoliciesEx("p", asList(asList("eve", "data3", "read"), asList("user1", "data1", "read")));
testGetPolicy(e, asList(asList("eve", "data3", "read"), asList("user1", "data1", "read")));
}

@Test
Expand Down
Loading