Skip to content

Commit

Permalink
feat: fix the double quotes bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tx2002 committed Nov 30, 2024
1 parent 060c258 commit 0293faa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -629,6 +633,10 @@ private EnforceResult enforce(String matcher, Object... rvals) {

for (int i = 0; i < policy.size(); i++) {
List<String> pvals = policy.get(i);
if (hasEval) {
pvals = splitCommaDelimitedList(pvals);
}

Map<String, Object> parameters = new HashMap<>(rvals.length + pTokens.length);
getPTokens(parameters, pType, pvals, pTokens);
getRTokens(parameters, rType, rvals);
Expand Down
28 changes: 12 additions & 16 deletions src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ private boolean notifyWatcher(String sec, String ptype, List<List<String>> rules
* addPolicy adds a rule to the current policy.
*/
boolean addPolicy(String sec, String ptype, List<String> rule) {
List<String> 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;
}

Expand All @@ -96,11 +95,11 @@ boolean addPolicy(String sec, String ptype, List<String> 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);
}


Expand Down Expand Up @@ -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<String> rule) {
List<String> modifiedRule = splitCommaDelimitedList(rule);

if (mustUseDispatcher()) {
dispatcher.removePolicies(sec, ptype, singletonList(modifiedRule));
dispatcher.removePolicies(sec, ptype, singletonList(rule));
return true;
}

Expand All @@ -177,15 +175,15 @@ boolean removePolicy(String sec, String ptype, List<String> 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);
}

/**
Expand All @@ -198,11 +196,9 @@ boolean removePolicy(String sec, String ptype, List<String> rule) {
* @return succeeds or not.
*/
boolean updatePolicy(String sec, String ptype, List<String> oldRule, List<String> newRule) {
List<String> modifiedOldRule = splitCommaDelimitedList(oldRule);
List<String> modifiedNewRule = splitCommaDelimitedList(newRule);

if (mustUseDispatcher()) {
dispatcher.updatePolicy(sec, ptype, modifiedOldRule, modifiedNewRule);
dispatcher.updatePolicy(sec, ptype, oldRule, newRule);
return true;
}

Expand All @@ -219,7 +215,7 @@ boolean updatePolicy(String sec, String ptype, List<String> oldRule, List<String
}
}

boolean ruleUpdated = model.updatePolicy(sec, ptype, modifiedOldRule, modifiedNewRule);
boolean ruleUpdated = model.updatePolicy(sec, ptype, oldRule, newRule);

if (!ruleUpdated) {
return false;
Expand Down Expand Up @@ -250,7 +246,7 @@ boolean updatePolicy(String sec, String ptype, List<String> oldRule, List<String
if (watcher != null && autoNotifyWatcher) {
try {
if (watcher instanceof WatcherUpdatable) {
((WatcherUpdatable) watcher).updateForUpdatePolicy(modifiedOldRule, modifiedNewRule);
((WatcherUpdatable) watcher).updateForUpdatePolicy(oldRule, newRule);
} else {
watcher.update();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/casbin/jcasbin/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public static boolean setEquals(List<String> a, List<String> 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) {
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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"));
Expand Down

0 comments on commit 0293faa

Please sign in to comment.