Skip to content

Commit e242fcc

Browse files
feat:update addNamedGroupingPoliciesEx() and addGroupingPoliciesEx() and Unit Test these methods.
1 parent 747eb33 commit e242fcc

File tree

5 files changed

+250
-1
lines changed

5 files changed

+250
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
<!-- Automatically close and deploy from OSSRH -->
131131
<groupId>org.sonatype.central</groupId>
132132
<artifactId>central-publishing-maven-plugin</artifactId>
133-
<version>0.6.0</version>
133+
<version>0.4.0</version>
134134
<extensions>true</extensions>
135135
<configuration>
136136
<publishingServerId>ossrh</publishingServerId>

src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,30 @@ public boolean addGroupingPolicies(String[][] rules) {
590590
return addGroupingPolicies(Arrays.stream(rules).map(Arrays::asList).collect(Collectors.toList()));
591591
}
592592

593+
/**
594+
* addGroupingPoliciesEx adds role inheritance rules to the current policy.
595+
* If the rule already exists, the rule will not be added.
596+
* But unlike addGroupingPolicies, other non-existent rules are added instead of returning false directly.
597+
*
598+
* @param rules the "g" policy rules, ptype "g" is implicitly used.
599+
* @return succeeds or not.
600+
*/
601+
public boolean addGroupingPoliciesEx(List<List<String>> rules) {
602+
return addNamedGroupingPoliciesEx("g", rules);
603+
}
604+
605+
/**
606+
* addGroupingPoliciesEx adds role inheritance rules to the current policy.
607+
* If the rule already exists, the rule will not be added.
608+
* But unlike addGroupingPolicies, other non-existent rules are added instead of returning false directly.
609+
*
610+
* @param rules the "g" policy rules, ptype "g" is implicitly used.
611+
* @return succeeds or not.
612+
*/
613+
public boolean addGroupingPoliciesEx(String[][] rules) {
614+
return addGroupingPoliciesEx(Arrays.stream(rules).map(Arrays::asList).collect(Collectors.toList()));
615+
}
616+
593617
/**
594618
* addNamedGroupingPolicy adds a named role inheritance rule to the current policy.
595619
* If the rule already exists, the function returns false and the rule will not be added.
@@ -645,6 +669,32 @@ public boolean addNamedGroupingPolicies(String ptype, String[][] rules) {
645669
return addNamedGroupingPolicies(ptype, Arrays.stream(rules).map(Arrays::asList).collect(Collectors.toList()));
646670
}
647671

672+
/**
673+
* addNamedGroupingPoliciesEx adds named role inheritance rules to the current policy.
674+
* If the rule already exists, the rule will not be added.
675+
* But unlike addNamedGroupingPolicies, other non-existent rules are added instead of returning false directly.
676+
*
677+
* @param ptype the policy type, can be "g", "g2", "g3", ..
678+
* @param rules the "g" policy rules.
679+
* @return succeeds or not.
680+
*/
681+
public boolean addNamedGroupingPoliciesEx(String ptype, List<List<String>> rules) {
682+
return addPolicies("g", ptype, rules, true);
683+
}
684+
685+
/**
686+
* addNamedGroupingPoliciesEx adds named role inheritance rules to the current policy.
687+
* If the rule already exists, the rule will not be added.
688+
* But unlike addNamedGroupingPolicies, other non-existent rules are added instead of returning false directly.
689+
*
690+
* @param ptype the policy type, can be "g", "g2", "g3", ..
691+
* @param rules the "g" policy rules.
692+
* @return succeeds or not.
693+
*/
694+
public boolean addNamedGroupingPoliciesEx(String ptype, String[][] rules) {
695+
return addNamedGroupingPoliciesEx(ptype, Arrays.stream(rules).map(Arrays::asList).collect(Collectors.toList()));
696+
}
697+
648698
/**
649699
* removeGroupingPolicy removes a role inheritance rule from the current policy.
650700
*

src/main/java/org/casbin/jcasbin/main/SyncedEnforcer.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,32 @@ public boolean addGroupingPolicies(String[][] rules) {
885885
return runSynchronized(() -> super.addGroupingPolicies(rules), READ_WRITE_LOCK.writeLock());
886886
}
887887

888+
/**
889+
* addGroupingPoliciesEx adds role inheritance rules to the current policy.
890+
* If the rule already exists, the rule will not be added.
891+
* But unlike addGroupingPolicies, other non-existent rules are added instead of returning false directly.
892+
*
893+
* @param rules the "g" policy rules, ptype "g" is implicitly used.
894+
* @return succeeds or not.
895+
*/
896+
@Override
897+
public boolean addGroupingPoliciesEx(List<List<String>> rules) {
898+
return runSynchronized(() -> super.addGroupingPoliciesEx(rules), READ_WRITE_LOCK.writeLock());
899+
}
900+
901+
/**
902+
* addGroupingPoliciesEx adds role inheritance rules to the current policy.
903+
* If the rule already exists, the rule will not be added.
904+
* But unlike addGroupingPolicies, other non-existent rules are added instead of returning false directly.
905+
*
906+
* @param rules the "g" policy rules, ptype "g" is implicitly used.
907+
* @return succeeds or not.
908+
*/
909+
@Override
910+
public boolean addGroupingPoliciesEx(String[][] rules) {
911+
return runSynchronized(() -> super.addGroupingPoliciesEx(rules), READ_WRITE_LOCK.writeLock());
912+
}
913+
888914
/**
889915
* addNamedGroupingPolicy adds a named role inheritance rule to the current policy.
890916
* If the rule already exists, the function returns false and the rule will not be added.
@@ -952,6 +978,34 @@ public boolean removeGroupingPolicy(List<String> params) {
952978
return runSynchronized(() -> super.removeGroupingPolicy(params), READ_WRITE_LOCK.writeLock());
953979
}
954980

981+
/**
982+
* addNamedGroupingPoliciesEx adds named role inheritance rules to the current policy.
983+
* If the rule already exists, the rule will not be added.
984+
* But unlike addNamedGroupingPolicies, other non-existent rules are added instead of returning false directly.
985+
*
986+
* @param ptype the policy type, can be "g", "g2", "g3", ..
987+
* @param rules the "g" policy rules.
988+
* @return succeeds or not.
989+
*/
990+
@Override
991+
public boolean addNamedGroupingPoliciesEx(String ptype, List<List<String>> rules) {
992+
return runSynchronized(() -> super.addNamedGroupingPoliciesEx(ptype, rules), READ_WRITE_LOCK.writeLock());
993+
}
994+
995+
/**
996+
* addNamedGroupingPoliciesEx adds named role inheritance rules to the current policy.
997+
* If the rule already exists, the rule will not be added.
998+
* But unlike addNamedGroupingPolicies, other non-existent rules are added instead of returning false directly.
999+
*
1000+
* @param ptype the policy type, can be "g", "g2", "g3", ..
1001+
* @param rules the "g" policy rules.
1002+
* @return succeeds or not.
1003+
*/
1004+
@Override
1005+
public boolean addNamedGroupingPoliciesEx(String ptype, String[][] rules) {
1006+
return runSynchronized(() -> super.addNamedGroupingPoliciesEx(ptype, rules), READ_WRITE_LOCK.writeLock());
1007+
}
1008+
9551009
/**
9561010
* removeGroupingPolicy removes a role inheritance rule from the current policy.
9571011
*
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2024 The casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package org.casbin.jcasbin.main;
16+
17+
import org.junit.Assert;
18+
import org.junit.Test;
19+
20+
import java.util.List;
21+
22+
import static java.util.Arrays.asList;
23+
import static org.casbin.jcasbin.main.TestUtil.*;
24+
25+
/**
26+
* Test for addNamedGroupingPoliciesEx method implementation
27+
*/
28+
public class AddNamedGroupingPoliciesExTest {
29+
30+
@Test
31+
public void testAddNamedGroupingPoliciesEx() {
32+
Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
33+
34+
// Test basic functionality
35+
String[][] rules = {
36+
{"alice", "admin"},
37+
{"bob", "user"},
38+
{"charlie", "moderator"}
39+
};
40+
41+
// Add rules for the first time - should succeed
42+
Assert.assertTrue(e.addNamedGroupingPoliciesEx("g", rules));
43+
44+
// Verify roles are added correctly
45+
testGetRoles(e, "alice", asList("data2_admin", "admin"));
46+
testGetRoles(e, "bob", asList("user"));
47+
testGetRoles(e, "charlie", asList("moderator"));
48+
49+
// Test Ex behavior - add some duplicate and some new rules
50+
String[][] mixedRules = {
51+
{"alice", "admin"}, // duplicate - should be ignored
52+
{"bob", "user"}, // duplicate - should be ignored
53+
{"david", "guest"}, // new rule - should be added
54+
{"eve", "supervisor"} // new rule - should be added
55+
};
56+
57+
// addNamedGroupingPoliciesEx should succeed even with duplicates
58+
Assert.assertTrue(e.addNamedGroupingPoliciesEx("g", mixedRules));
59+
60+
// Verify only new rules were added
61+
testGetRoles(e, "alice", asList("data2_admin", "admin")); // no duplicates
62+
testGetRoles(e, "bob", asList("user")); // no duplicates
63+
testGetRoles(e, "david", asList("guest")); // new rule added
64+
testGetRoles(e, "eve", asList("supervisor")); // new rule added
65+
66+
// Test with List<List<String>> version
67+
List<List<String>> listRules = asList(
68+
asList("alice", "admin"), // duplicate
69+
asList("frank", "editor") // new
70+
);
71+
72+
Assert.assertTrue(e.addNamedGroupingPoliciesEx("g", listRules));
73+
testGetRoles(e, "alice", asList("data2_admin", "admin")); // no duplicates
74+
testGetRoles(e, "frank", asList("editor")); // new rule added
75+
}
76+
77+
@Test
78+
public void testAddGroupingPoliciesEx() {
79+
Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
80+
81+
// Test basic functionality with default "g" ptype
82+
String[][] rules = {
83+
{"alice", "admin"},
84+
{"bob", "user"}
85+
};
86+
87+
Assert.assertTrue(e.addGroupingPoliciesEx(rules));
88+
testGetRoles(e, "alice", asList("data2_admin", "admin"));
89+
testGetRoles(e, "bob", asList("user"));
90+
91+
// Test with duplicates
92+
String[][] duplicateRules = {
93+
{"alice", "admin"}, // duplicate
94+
{"charlie", "guest"} // new
95+
};
96+
97+
Assert.assertTrue(e.addGroupingPoliciesEx(duplicateRules));
98+
testGetRoles(e, "alice", asList("data2_admin", "admin")); // no duplicates
99+
testGetRoles(e, "charlie", asList("guest")); // new rule added
100+
}
101+
102+
@Test
103+
public void testSyncedAddNamedGroupingPoliciesEx() {
104+
SyncedEnforcer e = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
105+
106+
String[][] rules = {
107+
{"alice", "admin"},
108+
{"bob", "user"}
109+
};
110+
111+
Assert.assertTrue(e.addNamedGroupingPoliciesEx("g", rules));
112+
testGetRoles(e, "alice", asList("data2_admin", "admin"));
113+
testGetRoles(e, "bob", asList("user"));
114+
115+
// Test with duplicates
116+
String[][] duplicateRules = {
117+
{"alice", "admin"}, // duplicate
118+
{"charlie", "guest"} // new
119+
};
120+
121+
Assert.assertTrue(e.addNamedGroupingPoliciesEx("g", duplicateRules));
122+
testGetRoles(e, "alice", asList("data2_admin", "admin")); // no duplicates
123+
testGetRoles(e, "charlie", asList("guest")); // new rule added
124+
}
125+
}

src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ public void testModifyGroupingPolicyAPI() {
173173
e.removeNamedGroupingPolicies("g", groupingRules);
174174
e.removeNamedGroupingPolicies("g", groupingRules);
175175

176+
// Test addNamedGroupingPoliciesEx
177+
String[][] groupingRulesEx = {
178+
{"ham", "data4_admin"},
179+
{"jack", "data5_admin"},
180+
{"duplicate", "data6_admin"}
181+
};
182+
183+
// First add some duplicate rules to test Ex behavior
184+
e.addNamedGroupingPolicy("g", "ham", "data4_admin");
185+
testGetRoles(e, "ham", asList("data4_admin"));
186+
187+
// addNamedGroupingPoliciesEx should succeed even with duplicates
188+
Assert.assertTrue(e.addNamedGroupingPoliciesEx("g", groupingRulesEx));
189+
testGetRoles(e, "ham", asList("data4_admin")); // Still only one rule
190+
testGetRoles(e, "jack", asList("data5_admin")); // New rule added
191+
testGetRoles(e, "duplicate", asList("data6_admin")); // New rule added
192+
193+
// Clean up
194+
e.removeNamedGroupingPolicies("g", groupingRulesEx);
195+
176196
testGetRoles(e, "alice", asList());
177197
testGetRoles(e, "bob", asList("data1_admin"));
178198
testGetRoles(e, "eve", asList("data3_admin"));

0 commit comments

Comments
 (0)