diff --git a/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java b/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java index 60d88e9a..ec3c7ace 100644 --- a/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java +++ b/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java @@ -42,6 +42,9 @@ import java.util.function.BiPredicate; import java.util.function.Function; +import static org.casbin.jcasbin.util.Util.hasEval; +import static org.casbin.jcasbin.util.Util.splitCommaDelimitedList; + /** * CoreEnforcer defines the core functionality of an enforcer. */ @@ -580,6 +583,7 @@ private EnforceResult enforce(String matcher, Object... rvals) { } else { expString = Util.removeComments(Util.escapeAssertion(matcher)); } + boolean hasEval = hasEval(expString); // json process if (acceptJsonRequest) { @@ -629,6 +633,9 @@ private EnforceResult enforce(String matcher, Object... rvals) { for (int i = 0; i < policy.size(); i++) { List pvals = policy.get(i); + if (hasEval) { + pvals = splitCommaDelimitedList(pvals); + } Map parameters = new HashMap<>(rvals.length + pTokens.length); getPTokens(parameters, pType, pvals, pTokens); getRTokens(parameters, rType, rvals); diff --git a/src/main/java/org/casbin/jcasbin/util/Util.java b/src/main/java/org/casbin/jcasbin/util/Util.java index 4077ee18..6f739025 100644 --- a/src/main/java/org/casbin/jcasbin/util/Util.java +++ b/src/main/java/org/casbin/jcasbin/util/Util.java @@ -284,6 +284,21 @@ public static String[] splitCommaDelimited(String s) { return records; } + /** + * splits each string in the given list by commas according to CSV format + * and removes any extra double quotes + * @param rule the rule to be modified + * @return the modified rule + */ + public static List splitCommaDelimitedList(List rule) { + List modifiedRule = new ArrayList<>(); + for (String s : rule) { + String[] strings = splitCommaDelimited(s); + modifiedRule.add(strings[0]); + } + return modifiedRule; + } + /** * setEquals determines whether two string sets are identical. * @@ -314,7 +329,7 @@ public static boolean setEquals(List a, List b) { } public static boolean hasEval(String exp) { - return evalReg.matcher(exp).matches(); + return evalReg.matcher(exp).find(); } public static String replaceEval(String s, String replacement) { diff --git a/src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java b/src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java index d0f8a65b..7a252eb0 100644 --- a/src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java +++ b/src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java @@ -14,8 +14,10 @@ package org.casbin.jcasbin.main; -import org.casbin.jcasbin.util.Util; import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.HashMap; @@ -42,6 +44,15 @@ public void testEval() { alice.setAge(60); testEnforce(e, alice, "/data2", "read", false); testEnforce(e, alice, "/data2", "write", false); + + List rule = new ArrayList<>(); + rule.add("\"r.sub.name == 'alice,green'\""); + rule.add("data1"); + rule.add("read"); + e.addPolicy(rule); + + TestEvalRule aliceGreen = new TestEvalRule("alice,green", 18); + testEnforce(e, aliceGreen, "data1", "read", true); } @Test diff --git a/src/test/java/org/casbin/jcasbin/main/UtilTest.java b/src/test/java/org/casbin/jcasbin/main/UtilTest.java index 545bc373..187951fd 100644 --- a/src/test/java/org/casbin/jcasbin/main/UtilTest.java +++ b/src/test/java/org/casbin/jcasbin/main/UtilTest.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.StringReader; +import static org.casbin.jcasbin.util.Util.hasEval; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.*; @@ -84,6 +85,13 @@ public void testSplitCommaDelimited(){ assertArrayEquals(new String[]{"a b", "c", "d"}, Util.splitCommaDelimited("\"a b\", c, d")); } + @Test + public void testHasEval() { + assertTrue(hasEval("eval(test)")); + assertTrue(hasEval("r_act == p_act && eval(p_sub_rule) && eval(p_obj_rule)")); + assertFalse(hasEval("evaltest")); + } + @Test public void testReplaceEval() { Util.logPrint(Util.replaceEval("eval(test)", "testEval"));