Skip to content

Commit 8daa357

Browse files
committed
feat: add 'in' sytanx support
Signed-off-by: tangyang9464 <[email protected]>
1 parent 33ec97e commit 8daa357

File tree

6 files changed

+96
-1
lines changed

6 files changed

+96
-1
lines changed

examples/in_op_sytanx.conf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[request_definition]
2+
r = sub, obj
3+
4+
[policy_definition]
5+
p = sub
6+
7+
[policy_effect]
8+
e = some(where (p.eft == allow))
9+
10+
[matchers]
11+
m = r.sub.name == p.sub && r.sub.name in (r.obj.admins)
12+

examples/in_op_sytanx.csv

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
p, alice

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

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.googlecode.aviator.AviatorEvaluator;
1818
import com.googlecode.aviator.AviatorEvaluatorInstance;
1919
import com.googlecode.aviator.Expression;
20+
import com.googlecode.aviator.lexer.token.OperatorType;
2021
import com.googlecode.aviator.runtime.type.AviatorFunction;
2122
import org.casbin.jcasbin.effect.DefaultEffector;
2223
import org.casbin.jcasbin.effect.Effect;
@@ -436,6 +437,8 @@ private boolean enforce(String matcher, Object... rvals) {
436437
} else {
437438
expString = Util.removeComments(Util.escapeAssertion(matcher));
438439
}
440+
441+
expString = Util.convertInSyntax(expString);
439442
Expression expression = aviatorEval.compile(expString, true);
440443

441444
Effect[] policyEffects;

src/main/java/org/casbin/jcasbin/util/Util.java

+17
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ public static String escapeAssertion(String s) {
107107
return sb.toString();
108108
}
109109

110+
/**
111+
* convertInSyntax Convert 'in' to 'seq' to fit aviatorscript,because aviatorscript don't support native 'in' syntax
112+
*
113+
* @param expString the value of the matcher
114+
* @return the 'seq' expression.
115+
*/
116+
public static String convertInSyntax(String expString) {
117+
String reg = "((r|p)[0-9]*?_[.a-zA-Z]*?) *?in *?\\((.*?)\\)";
118+
Matcher m1 = Pattern.compile(reg).matcher(expString);
119+
StringBuffer sb = new StringBuffer();
120+
boolean flag=false;
121+
while (m1.find()) {
122+
flag=true;
123+
m1.appendReplacement(sb,"seq.some($3, fn(x) {x == $1}) != nil");
124+
}
125+
return flag?sb.toString():expString;
126+
}
110127
/**
111128
* removeComments removes the comments starting with # in the text.
112129
*

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

+56-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package org.casbin.jcasbin.main;
1616

17+
import com.googlecode.aviator.AviatorEvaluator;
18+
import com.googlecode.aviator.Expression;
1719
import org.casbin.jcasbin.model.Model;
1820
import org.casbin.jcasbin.persist.Adapter;
1921
import org.casbin.jcasbin.persist.file_adapter.FileAdapter;
@@ -23,7 +25,9 @@
2325

2426
import java.io.FileInputStream;
2527
import java.io.IOException;
28+
import java.util.HashMap;
2629
import java.util.List;
30+
import java.util.Map;
2731
import java.util.Set;
2832

2933
import static java.util.Arrays.asList;
@@ -218,10 +222,19 @@ public void testNotUsedRBACModelInMemory() {
218222
testEnforce(e, "bob", "data2", "write", true);
219223
}
220224

225+
@Test
226+
public void testInOp() {
227+
Enforcer e = new Enforcer("examples/in_op_sytanx.conf", "examples/in_op_sytanx.csv");
228+
229+
TestSub sub = new TestSub("alice");
230+
TestObj obj = new TestObj(new String[]{"alice","bob"});
231+
232+
assertTrue(e.enforce(sub,obj));
233+
}
234+
221235
@Test
222236
public void testReloadPolicy() {
223237
Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
224-
225238
e.loadPolicy();
226239
testGetPolicy(e, asList(asList("alice", "data1", "read"), asList("bob", "data2", "write"), asList("data2_admin", "data2", "read"), asList("data2_admin", "data2", "write")));
227240
}
@@ -572,4 +585,46 @@ public void testBatchEnforceWithMatcher() {
572585
asList("root", "data3", "read"), asList("jack", "data3", "read")));
573586
Assert.assertArrayEquals(myResults.toArray(new Boolean[0]), results.toArray(new Boolean[0]));
574587
}
588+
589+
public static class TestSub{
590+
private String name;
591+
592+
public TestSub(String name) {
593+
this.name = name;
594+
}
595+
596+
public String getName() { return name; }
597+
598+
public void setName(String name) { this.name = name; }
599+
}
600+
public static class TestAdmins{
601+
private String name;
602+
603+
public TestAdmins(String name) {
604+
this.name = name;
605+
}
606+
607+
public String getName() {
608+
return name;
609+
}
610+
611+
public void setName(String name) {
612+
this.name = name;
613+
}
614+
}
615+
public static class TestObj{
616+
private String[] admins;
617+
618+
public TestObj(String[] admins) {
619+
this.admins = admins;
620+
}
621+
622+
public String[] getAdmins() {
623+
return admins;
624+
}
625+
626+
public void setAdmins(String[] admins) {
627+
this.admins = admins;
628+
}
629+
}
575630
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public void testEscapeAssertion(){
4040
assertEquals("(r_attp.value || p_attr)p_u", Util.escapeAssertion("(r.attp.value || p.attr)p.u"));
4141
}
4242

43+
@Test
44+
public void testConvertInSyntax(){
45+
assertEquals("seq.some(r_obj, fn(x) {x == r_sub}) != nil", Util.convertInSyntax("r_sub in (r_obj)"));
46+
assertEquals("seq.some(r_obj, fn(x) {x == r_sub.name}) != nil", Util.convertInSyntax("r_sub.name in (r_obj)"));
47+
assertEquals("seq.some(r_obj.name, fn(x) {x == r_sub.name}) != nil", Util.convertInSyntax("r_sub.name in (r_obj.name)"));
48+
}
49+
4350
@Test
4451
public void testRemoveComments(){
4552
assertEquals("r.act == p.act", Util.removeComments("r.act == p.act # comments"));

0 commit comments

Comments
 (0)