From 0293faa72d03fe52d349388a7ab25141f77a237d Mon Sep 17 00:00:00 2001 From: TX <1428427011@qq.com> Date: Sat, 30 Nov 2024 20:54:50 +0800 Subject: [PATCH] feat: fix the double quotes bug --- .../org/casbin/jcasbin/main/CoreEnforcer.java | 8 ++++++ .../casbin/jcasbin/main/InternalEnforcer.java | 28 ++++++++----------- .../java/org/casbin/jcasbin/util/Util.java | 2 +- .../org/casbin/jcasbin/main/UtilTest.java | 8 ++++++ 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java b/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java index 60d88e9a..393bde97 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,10 @@ 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/main/InternalEnforcer.java b/src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java index c3596e3b..eb38e000 100644 --- a/src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java +++ b/src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java @@ -74,14 +74,13 @@ private boolean notifyWatcher(String sec, String ptype, List> rules * addPolicy adds a rule to the current policy. */ boolean addPolicy(String sec, String ptype, List rule) { - List modifiedRule = splitCommaDelimitedList(rule); if (mustUseDispatcher()) { - dispatcher.addPolicies(sec, ptype, singletonList(modifiedRule)); + dispatcher.addPolicies(sec, ptype, singletonList(rule)); return true; } - if (model.hasPolicy(sec, ptype, modifiedRule)) { + if (model.hasPolicy(sec, ptype, rule)) { return false; } @@ -96,11 +95,11 @@ boolean addPolicy(String sec, String ptype, List rule) { } } - model.addPolicy(sec, ptype, modifiedRule); + model.addPolicy(sec, ptype, rule); - buildIncrementalRoleLinks(sec, ptype, singletonList(modifiedRule), Model.PolicyOperations.POLICY_ADD); + buildIncrementalRoleLinks(sec, ptype, singletonList(rule), Model.PolicyOperations.POLICY_ADD); - return notifyWatcher(sec, ptype, singletonList(modifiedRule), WatcherEx.UpdateType.UpdateForAddPolicy); + return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForAddPolicy); } @@ -159,10 +158,9 @@ public void buildIncrementalRoleLinks(Model.PolicyOperations op, String ptype, L * removePolicy removes a rule from the current policy. */ boolean removePolicy(String sec, String ptype, List rule) { - List modifiedRule = splitCommaDelimitedList(rule); if (mustUseDispatcher()) { - dispatcher.removePolicies(sec, ptype, singletonList(modifiedRule)); + dispatcher.removePolicies(sec, ptype, singletonList(rule)); return true; } @@ -177,15 +175,15 @@ boolean removePolicy(String sec, String ptype, List rule) { } } - boolean ruleRemoved = model.removePolicy(sec, ptype, modifiedRule); + boolean ruleRemoved = model.removePolicy(sec, ptype, rule); if (!ruleRemoved) { return false; } - buildIncrementalRoleLinks(sec, ptype, singletonList(modifiedRule), Model.PolicyOperations.POLICY_REMOVE); + buildIncrementalRoleLinks(sec, ptype, singletonList(rule), Model.PolicyOperations.POLICY_REMOVE); - return notifyWatcher(sec, ptype, singletonList(modifiedRule), WatcherEx.UpdateType.UpdateForRemovePolicy); + return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForRemovePolicy); } /** @@ -198,11 +196,9 @@ boolean removePolicy(String sec, String ptype, List rule) { * @return succeeds or not. */ boolean updatePolicy(String sec, String ptype, List oldRule, List newRule) { - List modifiedOldRule = splitCommaDelimitedList(oldRule); - List modifiedNewRule = splitCommaDelimitedList(newRule); if (mustUseDispatcher()) { - dispatcher.updatePolicy(sec, ptype, modifiedOldRule, modifiedNewRule); + dispatcher.updatePolicy(sec, ptype, oldRule, newRule); return true; } @@ -219,7 +215,7 @@ boolean updatePolicy(String sec, String ptype, List oldRule, List oldRule, 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/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"));