Skip to content

Commit

Permalink
feat: support map in ABAC (mainly add test case) (#366)
Browse files Browse the repository at this point in the history
* support map in ABAC

* support map in ABAC

* support map in ABAC
  • Loading branch information
LMay001 committed Dec 31, 2023
1 parent 5efe0a7 commit 4fb873f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/abac_rule_map_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == r.obj.Owner
24 changes: 24 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import org.casbin.jcasbin.util.Util;
import org.junit.Test;
import java.util.Map;
import java.util.HashMap;

import static org.casbin.jcasbin.main.TestUtil.testDomainEnforce;
import static org.casbin.jcasbin.main.TestUtil.testEnforce;
Expand Down Expand Up @@ -55,6 +57,28 @@ public void testEvalWithDomain() {
testDomainEnforce(e, "bob", "domain2", "data2", "read", true);
}

@Test
public void testABACMapRequest() {
Enforcer e = new Enforcer("examples/abac_rule_map_model.conf");

Map<String, Object> data1 = new HashMap<>();
data1.put("Name", "data1");
data1.put("Owner", "alice");

Map<String, Object> data2 = new HashMap<>();
data2.put("Name", "data2");
data2.put("Owner", "bob");

testEnforce(e, "alice", data1, "read", true);
testEnforce(e, "alice", data1, "write", true);
testEnforce(e, "alice", data2, "read", false);
testEnforce(e, "alice", data2, "write", false);
testEnforce(e, "bob", data1, "read", false);
testEnforce(e, "bob", data1, "write", false);
testEnforce(e, "bob", data2, "read", true);
testEnforce(e, "bob", data2, "write", true);
}

public static class TestEvalRule {
private String name;
private int age;
Expand Down

0 comments on commit 4fb873f

Please sign in to comment.