-
-
Notifications
You must be signed in to change notification settings - Fork 464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to define new matcher function? #10
Comments
What you did now is correct, just call jcasbin/src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java Lines 547 to 556 in e621f73
It's very similar to Golang's Casbin, which is documented here: https://casbin.org/docs/en/syntax-for-models#how-to-add-a-customized-function |
I've used enforcer.addFunction() but got an error: |
Can you paste all your code here? including Enforcer setup, enforce() call, model text, policy text. |
This is set up and call : enforcer = new Enforcer("role.conf", "policy.csv");
enforcer.addFunction(InCollectionFunc.getNameStatic(),new InCollectionFunc());
enforcer.addFunction(TimeValidityFunc.getNameStatic(),new TimeValidityFunc());
if (enforcer.enforce("cathy", "order1", "view")) {
System.out.println("ok");
} And this is model: [request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.act == "View" && r.obj.branch isIn(r.sub.branch) || g(r.sub, "Manager") && r.act == "View" || isValidTimeForUser(r.sub) || g(r.sub, "Manager") && (r.act == "Confirm" || r.act == "Reject") || g(r.sub, "Operator") && (r.act == "Create" || r.act == "Edit") || g(r.sub, "Reviewer") && r.obj.status== "confirmed" Policy file is empty. |
Time function: import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;
import java.time.LocalTime;
import java.util.Map;
/**
* Created by Mahdi Razavi on 1/22/19-4:06 PM
*/
public class TimeValidityFunc extends AbstractFunction {
public TimeValidityFunc() {
}
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
String actionTime = FunctionUtils.getStringValue(arg1, env);
String validityTimeStart = FunctionUtils.getStringValue(arg2, env);
String validityTimeEnd = FunctionUtils.getStringValue(arg3, env);
return AviatorBoolean.valueOf(isTimeMatch(actionTime, validityTimeStart, validityTimeEnd));
// return AviatorBoolean.valueOf(BuiltInFunctions.keyMatch(key1, key2));
}
private boolean isTimeMatch(String actionTime, String validityTimeStart, String validityTimeEnd) {
LocalTime actTime = LocalTime.parse(actionTime);
LocalTime tStart = LocalTime.parse(validityTimeStart);
LocalTime tEnd = LocalTime.parse(validityTimeEnd);
return actTime.compareTo(tStart) >= 0 && actTime.compareTo(tEnd) <= 0;
}
private boolean isValidTimeForUser(String userName) {
System.out.println("User:" + userName + " wants to do action");
return true;
}
public String getName() {
return getNameStatic();
}
public static String getNameStatic() {
return "isValidTimeForUser";
}
} and import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;
import java.time.LocalTime;
import java.util.Map;
/**
* Created by Mahdi Razavi on 1/22/19-4:06 PM
*/
public class InCollectionFunc extends AbstractFunction {
public InCollectionFunc() {
}
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
String value = FunctionUtils.getStringValue(arg1, env);
String collection = FunctionUtils.getStringValue(arg3, env);
return AviatorBoolean.valueOf(isInCollection(value, collection));
}
private boolean isInCollection(String value,String collection) {
System.out.println("value:" + value + " isIn collection:" + collection);
return true;
}
public String getName() {
return getNameStatic();
}
public static String getNameStatic() {
return "isIn";
}
} |
Your matcher is not correct. From your function's code, it has three arguments: So your matcher should be: |
@hsluoyz Many Thanks |
I got this error: |
I think this is a bug in aviator which I fixed it |
What does that PR mean? I'm not a Java expert and didn't quite understand it. BTW, I already asked Aviator's author to review your PR :) |
It seems that recent commit merged in Aviator and it needed to use the latest version of it in JCasbin dependency. |
@M-Razavi Aviator needs to release a new version before jCasbin can import it. You can ask its author to release one and I will release a new version jCasbin based on it. |
@M-Razavi jCasbin |
I've written a function but now how should add it to function map?
The text was updated successfully, but these errors were encountered: