From d475857ca687b7ca0240dc5aa8bd1703ad2ad158 Mon Sep 17 00:00:00 2001 From: liewstar <2935437378@qq.com> Date: Thu, 22 Aug 2024 19:22:37 +0800 Subject: [PATCH] feat: support AddNamedPoliciesEx API --- .../casbin/jcasbin/main/InternalEnforcer.java | 10 +++++++++- .../casbin/jcasbin/main/ManagementEnforcer.java | 17 +++++++++++++++-- .../org/casbin/jcasbin/main/SyncedEnforcer.java | 14 ++++++++++++++ .../InternalEnforcerWithDispatcherTest.java | 2 +- .../jcasbin/main/ManagementAPIUnitTest.java | 9 +++++++++ .../main/SyncedManagementAPIUnitTest.java | 6 ++++++ 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java b/src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java index 4fb1dead..47f3ef22 100644 --- a/src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java +++ b/src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java @@ -104,7 +104,15 @@ boolean addPolicy(String sec, String ptype, List rule) { /** * addPolicies adds rules to the current policy. */ - boolean addPolicies(String sec, String ptype, List> rules) { + boolean addPolicies(String sec, String ptype, List> rules, boolean autoRemoveRepeat) { + if(autoRemoveRepeat) { + for (List rule : rules) { + if(model.hasPolicy(sec, ptype, rule)) { + model.removePolicy(sec, ptype, rule); + } + } + } + if (mustUseDispatcher()) { dispatcher.addPolicies(sec, ptype, rules); return true; diff --git a/src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java b/src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java index bede02e3..584cb5d5 100644 --- a/src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java +++ b/src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java @@ -337,7 +337,20 @@ public boolean addNamedPolicy(String ptype, List params) { * @return succeeds or not. */ public boolean addNamedPolicies(String ptype, List> 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> rules) { + return addPolicies("p", ptype, rules, true); } /** @@ -616,7 +629,7 @@ public boolean addNamedGroupingPolicy(String ptype, String... params) { * @return succeeds or not. */ public boolean addNamedGroupingPolicies(String ptype, List> rules) { - return addPolicies("g", ptype, rules); + return addPolicies("g", ptype, rules, false); } /** diff --git a/src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java b/src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java index 46b410fa..c9bbce19 100644 --- a/src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java +++ b/src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java @@ -614,6 +614,20 @@ public boolean addNamedPolicies(String ptype, List> 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> rules) { + return runSynchronized(() -> super.addNamedPoliciesEx(ptype, rules), READ_WRITE_LOCK.writeLock()); + } + /** * updateNamedPolicy updates an authorization rule to the current named policy. * diff --git a/src/test/java/org/casbin/jcasbin/main/InternalEnforcerWithDispatcherTest.java b/src/test/java/org/casbin/jcasbin/main/InternalEnforcerWithDispatcherTest.java index 73560014..07a4dc0a 100644 --- a/src/test/java/org/casbin/jcasbin/main/InternalEnforcerWithDispatcherTest.java +++ b/src/test/java/org/casbin/jcasbin/main/InternalEnforcerWithDispatcherTest.java @@ -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); } diff --git a/src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java b/src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java index e1f1a576..970aefa1 100644 --- a/src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java +++ b/src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java @@ -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 diff --git a/src/test/java/org/casbin/jcasbin/main/SyncedManagementAPIUnitTest.java b/src/test/java/org/casbin/jcasbin/main/SyncedManagementAPIUnitTest.java index 0f543401..946b2946 100644 --- a/src/test/java/org/casbin/jcasbin/main/SyncedManagementAPIUnitTest.java +++ b/src/test/java/org/casbin/jcasbin/main/SyncedManagementAPIUnitTest.java @@ -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