Skip to content

Commit eb7f931

Browse files
authored
feat: support comment ("#",";") in model (testCommentModel) (#381)
1 parent 1723946 commit eb7f931

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

examples/comment_model.conf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[request_definition]
2+
r = sub, obj, act ; Request definition
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[policy_effect]
8+
e = some(where (p.eft == allow)) # This is policy effect.
9+
10+
# Matchers
11+
[matchers]
12+
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act

src/main/java/org/casbin/jcasbin/config/Config.java

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ private void parseBuffer(BufferedReader buf) throws IOException {
120120
} else if (line.startsWith("[") && line.endsWith("]")) {
121121
section = line.substring(1, line.length() - 1);
122122
} else {
123+
int index1 = line.indexOf(DEFAULT_COMMENT);
124+
int index2 = line.indexOf(DEFAULT_COMMENT_SEM);
125+
int index = (index1 != -1 && index2 != -1) ? Math.min(index1, index2) : (index1 != -1) ? index1 : (index2 != -1) ? index2 : -1;
126+
if (index != -1)
127+
line = line.substring(0, index);
128+
123129
String[] optionVal = line.split("=", 2);
124130
if (optionVal.length != 2) {
125131
throw new IllegalArgumentException(String.format("parse the content error : line %d , %s = ? ", lineNum, optionVal[0]));

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

+14
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,20 @@ public void testABACPolicy(){
787787
testEnforce(e, sub3, "/data2", "write", false);
788788
}
789789

790+
@Test
791+
public void testCommentModel(){
792+
Enforcer e = new Enforcer("examples/comment_model.conf", "examples/basic_policy.csv");
793+
794+
testEnforce(e, "alice", "data1", "read", true);
795+
testEnforce(e, "alice", "data1", "write", false);
796+
testEnforce(e, "alice", "data2", "read", false);
797+
testEnforce(e, "alice", "data2", "write", false);
798+
testEnforce(e, "bob", "data1", "read", false);
799+
testEnforce(e, "bob", "data1", "write", false);
800+
testEnforce(e, "bob", "data2", "read", false);
801+
testEnforce(e, "bob", "data2", "write", true);
802+
}
803+
790804
@Test
791805
public void testSubjectPriorityWithDomain() {
792806
Enforcer e = new Enforcer("examples/subject_priority_model_with_domain.conf", "examples/subject_priority_policy_with_domain.csv");

0 commit comments

Comments
 (0)