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: fix bug to make batchEnforce method take all passes policy arguments #415

Merged
merged 1 commit into from
Sep 28, 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
4 changes: 2 additions & 2 deletions src/main/java/org/casbin/jcasbin/main/Enforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public List<List<String>> getImplicitPermissionsForUserInDomain(String user, Str
public List<Boolean> batchEnforce(List<List<String>> rules) {
List<Boolean> results = new ArrayList<>();
for (List<String> rule:rules) {
boolean result = this.enforce(rule.get(0), rule.get(1), rule.get(2));
boolean result = this.enforce(rule.toArray());
results.add(result);
}
return results;
Expand All @@ -639,7 +639,7 @@ public List<Boolean> batchEnforce(List<List<String>> rules) {
public List<Boolean> batchEnforceWithMatcher(String matcher, List<List<String>> rules) {
List<Boolean> results = new ArrayList<>();
for (List<String> rule : rules) {
boolean result = this.enforceWithMatcher(matcher, rule.get(0), rule.get(1), rule.get(2));
boolean result = this.enforceWithMatcher(matcher, rule.toArray());
results.add(result);
}
return results;
Expand Down
59 changes: 41 additions & 18 deletions src/test/java/org/casbin/jcasbin/main/EnforcerUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,21 @@ public void testNotUsedRBACModelInMemory() {
}

@Test
public void testRBACModelInDomain(){
Enforcer e = new Enforcer("examples/keymatch_with_rbac_in_domain.conf","examples/keymatch_with_rbac_in_domain.csv");
testDomainEnforce(e,"Username==test2","engines/engine1","*","attach",true);
public void testRBACModelInDomain() {
Enforcer e = new Enforcer("examples/keymatch_with_rbac_in_domain.conf", "examples/keymatch_with_rbac_in_domain.csv");
testDomainEnforce(e, "Username==test2", "engines/engine1", "*", "attach", true);
}

@Test
public void testInOp() {
Enforcer e = new Enforcer("examples/in_op_sytanx.conf", "examples/in_op_sytanx.csv");

TestSub sub = new TestSub("alice");
TestSub sub2 = new TestSub("alice2");
TestObj obj = new TestObj(new String[]{"alice","bob"});
TestObj obj = new TestObj(new String[]{"alice", "bob"});

assertTrue(e.enforce(sub,obj));
assertFalse(e.enforce(sub2,obj));
assertTrue(e.enforce(sub, obj));
assertFalse(e.enforce(sub2, obj));
}

@Test
Expand Down Expand Up @@ -565,7 +566,7 @@ public void testInitEmptyByInputStream() {
}

@Test
public void testEnforceParamCheck(){
public void testEnforceParamCheck() {
Enforcer e = new Enforcer("examples/rbac_model.conf");
assertTrue(e.validateEnforce("user501", "data9", "read", "too many params")); //warn only
assertFalse(e.validateEnforce("data9", "read"));//throw error
Expand All @@ -578,15 +579,15 @@ public void testGetPermittedActions() {
String obj = "data1";
Set<String> actions = e.getPermittedActions(sub, obj);
Util.logPrint(sub + " has permissions to access " + obj + " with following actions:");
for (String action :actions) {
for (String action : actions) {
Util.logPrint(action); //alice should have read-only access to data1
}

obj = "data2";

actions = e.getPermittedActions(sub, obj);
Util.logPrint(sub + " has permissions to access " + obj + " with following actions:");
for (String action :actions) {
for (String action : actions) {
Util.logPrint(action); //alice should have read and write access to data2
}

Expand All @@ -595,7 +596,7 @@ public void testGetPermittedActions() {
actions = e.getPermittedActions(sub, data1);

Util.logPrint(sub + " has permissions to access " + data1.name + " with following actions:");
for (String action :actions) {
for (String action : actions) {
// The result shows that alice should have no access to data1,
// because no action defines.
// This method has no meaning for ABAC model,
Expand Down Expand Up @@ -670,6 +671,22 @@ public void testBatchEnforce() {
Assert.assertArrayEquals(myResults.toArray(new Boolean[0]), results.toArray(new Boolean[0]));
}

@Test
public void testDomainBatchEnforce() {
Enforcer e = new Enforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv");

List<Boolean> results = asList(true, true, false);
List<Boolean> myResults = e.batchEnforce(
asList(
asList("alice", "domain1", "data1", "read"),
asList("alice", "domain1", "data1", "write"),
asList("bob", "domain2", "data1", "write")
)
);

Assert.assertArrayEquals(myResults.toArray(new Boolean[0]), results.toArray(new Boolean[0]));
}

@Test
public void testBatchEnforceWithMatcher() {
Enforcer e = new Enforcer("examples/basic_model.conf", "examples/basic_policy.csv");
Expand All @@ -695,7 +712,7 @@ public void testMultiplePolicyDefinitions() {
}

@Test
public void testHasLinkSynchronized(){
public void testHasLinkSynchronized() {
File testingDir = null;
try {
testingDir = Files.createTempDirectory("testingDir").toFile();
Expand All @@ -721,7 +738,7 @@ public void testHasLinkSynchronized(){
String[][] gs = new String[1001][3];
gs[0] = new String[]{"[email protected]", "temp", "alice"};
for (int i = 0; i < 1000; i++) {
gs[i+1] = new String[]{i + "@alice.co", "temp", "alice"};
gs[i + 1] = new String[]{i + "@alice.co", "temp", "alice"};
}
e.addGroupingPolicies(gs);

Expand All @@ -736,7 +753,7 @@ public void testHasLinkSynchronized(){
int finalI = i;
new Thread(() -> {
boolean res = e.enforce("alice", "data");
if (!res){
if(!res) {
System.out.println("result failure: " + finalI);
}
countDownLatch.countDown();
Expand All @@ -752,18 +769,23 @@ public void testHasLinkSynchronized(){
System.out.println("Done!");
}

public static class TestSub{
public static class TestSub {
private String name;

public TestSub(String name) {
this.name = name;
}

public String getName() { return name; }
public String getName() {
return name;
}

public void setName(String name) { this.name = name; }
public void setName(String name) {
this.name = name;
}
}
public static class TestAdmins{

public static class TestAdmins {
private String name;

public TestAdmins(String name) {
Expand All @@ -778,7 +800,8 @@ public void setName(String name) {
this.name = name;
}
}
public static class TestObj{

public static class TestObj {
private String[] admins;

public TestObj(String[] admins) {
Expand Down
Loading