Skip to content

Commit c35739e

Browse files
fix: correct supported matcher types (casbin#320)
* fix: correct supported matcher types Aviator script used to evaluate matcher expression does not support Float and Int types. It uses Double and Long instead. * fix: correct supported matcher types Aviator script used to evaluate matcher expression does not support Float and Int types. It uses Double and Long instead.
1 parent 1fbc062 commit c35739e

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

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

+21-21
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public static Model newModel(String text) {
103103
* newModel creates a model.
104104
*
105105
* @param modelPath the path of the model file.
106-
* @param unused unused parameter, just for differentiating with
107-
* newModel(String text).
106+
* @param unused unused parameter, just for differentiating with
107+
* newModel(String text).
108108
* @return the model.
109109
*/
110110
public static Model newModel(String modelPath, String unused) {
@@ -227,7 +227,7 @@ public void setRoleManager(RoleManager rm) {
227227
* setNamedRoleManager sets the role manager for the named policy.
228228
*
229229
* @param ptype the policy type.
230-
* @param rm the role manager.
230+
* @param rm the role manager.
231231
*/
232232
public void setNamedRoleManager(String ptype, RoleManager rm) {
233233
setRoleManager(ptype, rm);
@@ -355,11 +355,11 @@ private void initRmMap() {
355355
}
356356
}
357357

358-
private void initBuiltInFunction(){
358+
private void initBuiltInFunction() {
359359
for (Map.Entry<String, AviatorFunction> entry : fm.fm.entrySet()) {
360360
AviatorFunction function = entry.getValue();
361361

362-
if(aviatorEval.containsFunction(function.getName())){
362+
if (aviatorEval.containsFunction(function.getName())) {
363363
aviatorEval.removeFunction(function.getName());
364364
}
365365
aviatorEval.addFunction(function);
@@ -444,10 +444,10 @@ private EnforceResult enforce(String matcher, Object... rvals) {
444444
}
445445

446446
boolean compileCached = true;
447-
if(fm.isModify){
447+
if (fm.isModify) {
448448
compileCached = false;
449449
initBuiltInFunction();
450-
fm.isModify=false;
450+
fm.isModify = false;
451451
}
452452
Map<String, AviatorFunction> gFunctions = new HashMap<>();
453453
if (model.model.containsKey("g")) {
@@ -489,7 +489,7 @@ private EnforceResult enforce(String matcher, Object... rvals) {
489489

490490
expString = Util.convertInSyntax(expString);
491491
// Use md5 encryption as cacheKey to prevent expString from being too long
492-
Expression expression = aviatorEval.compile(Util.md5(expString),expString, compileCached);
492+
Expression expression = aviatorEval.compile(Util.md5(expString), expString, compileCached);
493493

494494
StreamEffector streamEffector = null;
495495
try {
@@ -510,8 +510,8 @@ private EnforceResult enforce(String matcher, Object... rvals) {
510510
for (int i = 0; i < model.model.get("p").get(pType).policy.size(); i++) {
511511
List<String> pvals = model.model.get("p").get(pType).policy.get(i);
512512
if (model.model.get("p").get(pType).tokens.length != pvals.size()) {
513-
throw new CasbinMatcherException("invalid request size: expected "+model.model.get("p").get(pType).tokens.length+
514-
", got "+pvals.size()+", rvals: "+ Arrays.toString(rvals));
513+
throw new CasbinMatcherException("invalid request size: expected " + model.model.get("p").get(pType).tokens.length +
514+
", got " + pvals.size() + ", rvals: " + Arrays.toString(rvals));
515515
}
516516

517517
// Util.logPrint("Policy Rule: " + pvals);
@@ -535,18 +535,18 @@ private EnforceResult enforce(String matcher, Object... rvals) {
535535
if (streamEffector == null) {
536536
continue;
537537
}
538-
} else if (result instanceof Float) {
539-
if ((float) result == 0) {
538+
} else if (result instanceof Double || result instanceof Long) {
539+
if (((Number) result).floatValue() == 0) {
540540
policyEffects[i] = Effect.Indeterminate;
541541
} else {
542-
matcherResults[i] = (float) result;
542+
matcherResults[i] = ((Number) result).floatValue();
543543
policyEffects[i] = Effect.Allow;
544544
}
545545
if (streamEffector == null) {
546546
continue;
547547
}
548548
} else {
549-
throw new CasbinMatcherException("matcher result should be bool, int or float");
549+
throw new CasbinMatcherException("matcher result should be Boolean, Double or Long");
550550
}
551551
if (policyEffects[i] == Effect.Allow && parameters.containsKey(pType + "_eft")) {
552552
String eft = (String) parameters.get(pType + "_eft");
@@ -675,12 +675,12 @@ public EnforceResult enforceExWithMatcher(String matcher, Object... rvals) {
675675
/**
676676
* addNamedMatchingFunc add MatchingFunc by ptype RoleManager
677677
*/
678-
public boolean addNamedMatchingFunc(String ptype, String name, BiPredicate<String, String> fn){
679-
if(rmMap.containsKey(ptype)){
678+
public boolean addNamedMatchingFunc(String ptype, String name, BiPredicate<String, String> fn) {
679+
if (rmMap.containsKey(ptype)) {
680680
DomainManager rm = (DomainManager) rmMap.get(ptype);
681681
rm.addMatchingFunc(name, fn);
682682
clearRmMap();
683-
if(autoBuildRoleLinks){
683+
if (autoBuildRoleLinks) {
684684
buildRoleLinks();
685685
}
686686
return true;
@@ -691,12 +691,12 @@ public boolean addNamedMatchingFunc(String ptype, String name, BiPredicate<Strin
691691
/**
692692
* addNamedMatchingFunc add MatchingFunc by ptype RoleManager
693693
*/
694-
public boolean addNamedDomainMatchingFunc(String ptype, String name, BiPredicate<String, String> fn){
695-
if(rmMap.containsKey(ptype)){
694+
public boolean addNamedDomainMatchingFunc(String ptype, String name, BiPredicate<String, String> fn) {
695+
if (rmMap.containsKey(ptype)) {
696696
DomainManager rm = (DomainManager) rmMap.get(ptype);
697697
rm.addDomainMatchingFunc(name, fn);
698698
clearRmMap();
699-
if(autoBuildRoleLinks){
699+
if (autoBuildRoleLinks) {
700700
buildRoleLinks();
701701
}
702702
return true;
@@ -763,6 +763,6 @@ public void setAutoNotifyDispatcher(boolean autoNotifyDispatcher) {
763763
}
764764

765765
protected boolean mustUseDispatcher() {
766-
return this.dispatcher != null && this.autoNotifyDispatcher;
766+
return this.dispatcher != null && this.autoNotifyDispatcher;
767767
}
768768
}

0 commit comments

Comments
 (0)