Package org.casbin.jcasbin.main
Class CoreEnforcer
java.lang.Object
org.casbin.jcasbin.main.CoreEnforcer
- Direct Known Subclasses:
ManagementEnforcer
CoreEnforcer defines the core functionality of an enforcer.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddNamedDomainLinkConditionFunc(String ptype, String user, String role, String domain, Function<String[], Boolean> fn) addNamedDomainLinkConditionFunc Add condition function fn for Link userName-> {roleName, domain}, when fn returns true, Link is valid, otherwise invalidbooleanaddNamedDomainMatchingFunc(String ptype, String name, BiPredicate<String, String> fn) addNamedMatchingFunc add MatchingFunc by ptype RoleManagerbooleanaddNamedLinkConditionFunc Add condition function fn for Link userName->roleName, when fn returns true, Link is valid, otherwise invalidbooleanaddNamedMatchingFunc(String ptype, String name, BiPredicate<String, String> fn) addNamedMatchingFunc add MatchingFunc by ptype RoleManagervoidvoidbuildRoleLinks manually rebuild the role inheritance relations.voidclearPolicy clears all policy.voidenableAcceptJsonRequest(boolean acceptJsonRequest) EnableAcceptJsonRequest controls whether to accept json as a request parametervoidenableAutoBuildRoleLinks(boolean autoBuildRoleLinks) enableAutoBuildRoleLinks controls whether to save a policy rule automatically to the adapter when it is added or removed.voidenableAutoSave(boolean autoSave) enableAutoSave controls whether to save a policy rule automatically to the adapter when it is added or removed.voidenableEnforce(boolean enable) enableEnforce changes the enforcing state of Casbin, when Casbin is disabled, all access will be allowed by the enforce() function.voidenableLog(boolean enable) enableLog changes whether to print Casbin log to the standard output.booleanenforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).enforceEx decides whether a "subject" can access "object" with the operation "action", input parameters are usually: (sub, obj, act).enforceExWithMatcher(String matcher, Object... rvals) enforceExWithMatcher use a custom matcher to decide whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "" or null.booleanenforceWithMatcher(String matcher, Object... rvals) enforceWithMatcher use a custom matcher to decide whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "" or null.getAdapter gets the current adapter.com.googlecode.aviator.AviatorEvaluatorInstancegets the current Aviator Evaluator instancegetModel()getModel gets the current model.getNamedRoleManager(String ptype) getNamedRoleManager gets the role manager for the named policy.getRmMap()getRmMap gets the current role manager map.getRoleManager gets the current role manager.booleanbooleanbooleanisFiltered returns true if the loaded policy has been filtered.voidloadFilteredPolicy(Object filter) loadFilteredPolicy reloads a filtered policy from file/database.voidloadModel reloads the model from the model CONF file.voidloadPolicy reloads the policy from file/database.protected booleanstatic ModelnewModel()newModel creates a model.static ModelnewModel creates a model.static ModelnewModel creates a model.voidInvalidate cache of compiled model matcher expression.voidsavePolicy saves the current policy (usually after changed with Casbin API) back to file/database.voidsetAdapter(Adapter adapter) setAdapter sets the current adapter.voidsetAutoNotifyDispatcher(boolean autoNotifyDispatcher) voidsetAutoNotifyWatcher(boolean autoNotifyWatcher) voidsetAviatorEvaluator(com.googlecode.aviator.AviatorEvaluatorInstance evaluator) set the aviator evaluatorvoidsetDispatcher(Dispatcher dispatcher) setDispatcher sets the current dispatcher.voidsetEffector(Effector eft) setEffector sets the current effector.voidsetModel sets the current model.booleansetNamedDomainLinkConditionFuncParams(String ptype, String user, String role, String domain, String... params) setNamedDomainLinkConditionFuncParams Sets the parameters of the condition function fn for Link userName->{roleName, domain}booleansetNamedLinkConditionFuncParams(String ptype, String user, String role, String... params) setNamedLinkConditionFuncParams Sets the parameters of the condition function fn for Link userName->roleNamevoidsetNamedRoleManager(String ptype, RoleManager rm) setNamedRoleManager sets the role manager for the named policy.voidsetRoleManager(String ptype, RoleManager rm) setRoleManager sets role manager for ptype.voidsetRoleManager sets the current role manager for g.voidsetWatcher(Watcher watcher) setWatcher sets the current watcher.booleanvalidateEnforce(Object... rvals)
-
Constructor Details
-
CoreEnforcer
public CoreEnforcer()
-
-
Method Details
-
newModel
newModel creates a model.- Returns:
- an empty model.
-
newModel
newModel creates a model.- Parameters:
text- the model text.- Returns:
- the model.
-
newModel
newModel creates a model.- Parameters:
modelPath- the path of the model file.unused- unused parameter, just for differentiating with newModel(String text).- Returns:
- the model.
-
loadModel
public void loadModel()loadModel reloads the model from the model CONF file. Because the policy is attached to a model, so the policy is invalidated and needs to be reloaded by calling LoadPolicy(). -
getModel
getModel gets the current model.- Returns:
- the model of the enforcer.
-
setModel
setModel sets the current model.- Parameters:
model- the model.
-
setAviatorEvaluator
public void setAviatorEvaluator(com.googlecode.aviator.AviatorEvaluatorInstance evaluator) set the aviator evaluator- Parameters:
evaluator- aviator evaluator
-
getAviatorEval
public com.googlecode.aviator.AviatorEvaluatorInstance getAviatorEval()gets the current Aviator Evaluator instance- Returns:
- Aviator Evaluator instance of enforcer
-
getAdapter
getAdapter gets the current adapter.- Returns:
- the adapter of the enforcer.
-
setAdapter
setAdapter sets the current adapter.- Parameters:
adapter- the adapter.
-
setWatcher
setWatcher sets the current watcher.- Parameters:
watcher- the watcher.
-
setDispatcher
setDispatcher sets the current dispatcher.- Parameters:
dispatcher- jCasbin dispatcher
-
getRmMap
getRmMap gets the current role manager map.- Returns:
- the role manager map of the enforcer.
-
getRoleManager
getRoleManager gets the current role manager.- Returns:
- the role manager.
-
getNamedRoleManager
getNamedRoleManager gets the role manager for the named policy.- Parameters:
ptype- the policy type.- Returns:
- the role manager.
-
setRoleManager
setRoleManager sets the current role manager for g.- Parameters:
rm- the role manager.
-
setNamedRoleManager
setNamedRoleManager sets the role manager for the named policy.- Parameters:
ptype- the policy type.rm- the role manager.
-
setEffector
setEffector sets the current effector.- Parameters:
eft- the effector.
-
clearPolicy
public void clearPolicy()clearPolicy clears all policy. -
loadPolicy
public void loadPolicy()loadPolicy reloads the policy from file/database. -
loadFilteredPolicy
loadFilteredPolicy reloads a filtered policy from file/database.- Parameters:
filter- the filter used to specify which type of policy should be loaded.
-
isFiltered
public boolean isFiltered()isFiltered returns true if the loaded policy has been filtered.- Returns:
- if the loaded policy has been filtered.
-
savePolicy
public void savePolicy()savePolicy saves the current policy (usually after changed with Casbin API) back to file/database. -
setRoleManager
setRoleManager sets role manager for ptype.- Parameters:
ptype- the policy type, can be "g", "g2", "g3", ..rm- the role manager.
-
enableEnforce
public void enableEnforce(boolean enable) enableEnforce changes the enforcing state of Casbin, when Casbin is disabled, all access will be allowed by the enforce() function.- Parameters:
enable- whether to enable the enforcer.
-
enableLog
public void enableLog(boolean enable) enableLog changes whether to print Casbin log to the standard output.- Parameters:
enable- whether to enable Casbin's log.
-
enableAutoSave
public void enableAutoSave(boolean autoSave) enableAutoSave controls whether to save a policy rule automatically to the adapter when it is added or removed.- Parameters:
autoSave- whether to enable the AutoSave feature.
-
enableAutoBuildRoleLinks
public void enableAutoBuildRoleLinks(boolean autoBuildRoleLinks) enableAutoBuildRoleLinks controls whether to save a policy rule automatically to the adapter when it is added or removed.- Parameters:
autoBuildRoleLinks- whether to automatically build the role links.
-
enableAcceptJsonRequest
public void enableAcceptJsonRequest(boolean acceptJsonRequest) EnableAcceptJsonRequest controls whether to accept json as a request parameter- Parameters:
acceptJsonRequest- a boolean that indicates whether JSON requests are accepted.
-
buildRoleLinks
public void buildRoleLinks()buildRoleLinks manually rebuild the role inheritance relations. -
buildConditionalRoleLinks
public void buildConditionalRoleLinks() -
enforce
enforce decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (sub, obj, act).- Parameters:
rvals- the request needs to be mediated, usually an array of strings, can be class instances if ABAC is used.- Returns:
- whether to allow the request.
-
enforceWithMatcher
enforceWithMatcher use a custom matcher to decide whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "" or null.- Parameters:
matcher- the custom matcher.rvals- the request needs to be mediated, usually an array of strings, can be class instances if ABAC is used.- Returns:
- whether to allow the request.
-
enforceEx
enforceEx decides whether a "subject" can access "object" with the operation "action", input parameters are usually: (sub, obj, act). the list explain, store matching rule.- Parameters:
rvals- the request needs to be mediated, usually an array of strings, can be class instances if ABAC is used.- Returns:
- whether to allow the request.
-
enforceExWithMatcher
enforceExWithMatcher use a custom matcher to decide whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "" or null. the list explain, store matching rule.- Parameters:
matcher- the custom matcher.rvals- the request needs to be mediated, usually an array of strings, can be class instances if ABAC is used.- Returns:
- whether to allow the request.
-
addNamedMatchingFunc
addNamedMatchingFunc add MatchingFunc by ptype RoleManager- Parameters:
ptype- the type of the role manager.name- the name of the matching function to be added.fn- the matching function.- Returns:
- whether the matching function was successfully added.
-
addNamedDomainMatchingFunc
addNamedMatchingFunc add MatchingFunc by ptype RoleManager- Parameters:
ptype- the type of the role manager.name- the name of the matching function to be added.fn- the domain matching function.- Returns:
- whether the matching function was successfully added.
-
addNamedLinkConditionFunc
public boolean addNamedLinkConditionFunc(String ptype, String user, String role, Function<String[], Boolean> fn) addNamedLinkConditionFunc Add condition function fn for Link userName->roleName, when fn returns true, Link is valid, otherwise invalid- Parameters:
ptype- the type of the role manager.user- the username for which the link condition is being added.role- the role associated with the user for which the condition is evaluated.fn- a function that takes an array of parameters (e.g., [user, role]) and returns a Boolean indicating the validity of the link.- Returns:
- whether the Link is valid.
-
addNamedDomainLinkConditionFunc
public boolean addNamedDomainLinkConditionFunc(String ptype, String user, String role, String domain, Function<String[], Boolean> fn) addNamedDomainLinkConditionFunc Add condition function fn for Link userName-> {roleName, domain}, when fn returns true, Link is valid, otherwise invalid- Parameters:
ptype- the type of the conditional role manager.user- the username for which the link condition is being added.role- the role associated with the user for which the condition is evaluated.domain- the domain associated with the role.fn- a function that takes an array of parameters (e.g., [user, role, domain]) and returns a Boolean indicating the validity of the link.- Returns:
- whether the Link is valid.
-
setNamedLinkConditionFuncParams
public boolean setNamedLinkConditionFuncParams(String ptype, String user, String role, String... params) setNamedLinkConditionFuncParams Sets the parameters of the condition function fn for Link userName->roleName- Parameters:
ptype- the type of the conditional role manager.user- the username for which the link condition parameters are being set.role- the role associated with the user for which the parameters are being configured.params- an array of parameters to be passed to the condition function.- Returns:
- whether the Link is valid.
-
setNamedDomainLinkConditionFuncParams
public boolean setNamedDomainLinkConditionFuncParams(String ptype, String user, String role, String domain, String... params) setNamedDomainLinkConditionFuncParams Sets the parameters of the condition function fn for Link userName->{roleName, domain}- Parameters:
ptype- the type of the conditional role manager.user- the username for which the link condition parameters are being set.role- the role associated with the user for which the parameters are being configured.domain- the domain associated with the role and user.params- an array of parameters to be passed to the condition function, allowing customization of the condition logic.- Returns:
- whether the parameters were successfully set.
-
validateEnforce
-
resetExpressionEvaluator
public void resetExpressionEvaluator()Invalidate cache of compiled model matcher expression. This is done automatically most of the time, but you may need to call it explicitly if you manipulate directly Model. -
isAutoNotifyWatcher
public boolean isAutoNotifyWatcher() -
setAutoNotifyWatcher
public void setAutoNotifyWatcher(boolean autoNotifyWatcher) -
isAutoNotifyDispatcher
public boolean isAutoNotifyDispatcher() -
setAutoNotifyDispatcher
public void setAutoNotifyDispatcher(boolean autoNotifyDispatcher) -
mustUseDispatcher
protected boolean mustUseDispatcher()
-