Package org.casbin.jcasbin.model
Class Model
- java.lang.Object
-
- org.casbin.jcasbin.model.Policy
-
- org.casbin.jcasbin.model.Model
-
public class Model extends Policy
Model represents the whole access control model.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Model.PolicyOperations
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
defaultDomain
private java.lang.String
defaultSeparator
private int
domainIndex
protected int
modCount
private static java.util.regex.Pattern
paramsPattern
static java.lang.String[]
requiredSections
static java.util.Map<java.lang.String,java.lang.String>
sectionNameMap
-
Constructor Summary
Constructors Constructor Description Model()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addDef(java.lang.String sec, java.lang.String key, java.lang.String value)
addDef adds an assertion to the model.private void
findHierarchy(java.util.Map<java.lang.String,java.lang.String> policyMap, java.util.Map<java.lang.String,java.lang.Integer> subjectHierarchyMap, java.util.List<java.lang.String> set, java.lang.String child)
private java.lang.String
getKeySuffix(int i)
int
getModCount()
java.lang.String
getNameWithDomain(java.lang.String domain, java.lang.String name)
private static java.util.List<java.lang.String>
getParamsToken(java.lang.String value)
getParamsToken Get ParamsToken from Assertion.Valuejava.util.Map<java.lang.String,java.lang.Integer>
getSubjectHierarchyMap(java.util.List<java.util.List<java.lang.String>> policies)
boolean
hasSection(java.lang.String sec)
hasSection checks if the section exists in the model.private boolean
loadAssertion(Model model, Config cfg, java.lang.String sec, java.lang.String key)
void
loadModel(java.lang.String path)
loadModel loads the model from model CONF file.void
loadModelFromConfig(Config cfg)
loadModelFromConfig loads the model from the configuration.void
loadModelFromText(java.lang.String text)
loadModelFromText loads the model from the text.private void
loadSection(Model model, Config cfg, java.lang.String sec)
private void
loadSections(Config cfg)
Helper function for loadModel and loadModelFromTextstatic Model
newModel()
NewModel creates an empty model.static Model
newModelFromFile(java.lang.String path)
NewModelFromString creates a model from a string which contains model text.static Model
newModelFromString(java.lang.String text)
NewModelFromString creates a model from a string which contains model text.void
printModel()
printModel prints the model to the log.java.lang.String
saveModelToText()
saveModelToText saves the model to the text.private java.lang.String
saveSectionToText(java.lang.String sec)
saveSectionToText saves the section to the text.void
setLogger(Logger logger)
SetLogger sets the model's logger.void
sortPoliciesByPriority()
sort policies by priority valuevoid
sortPoliciesBySubjectHieraichy()
sort policies by hieraichy mapjava.lang.String
toText()
private void
writeString(java.lang.StringBuilder s, java.lang.String sec, java.util.Map<java.lang.String,java.lang.String> tokenPatterns)
-
Methods inherited from class org.casbin.jcasbin.model.Policy
addPolicies, addPolicy, buildConditionalRoleLinks, buildIncrementalConditionalRoleLinks, buildIncrementalRoleLinks, buildRoleLinks, clearPolicy, getFilteredPolicy, getPolicy, getValuesForFieldInPolicy, hasPolicies, hasPolicy, printPolicy, removeFilteredPolicy, removeFilteredPolicyReturnsEffects, removePolicies, removePolicy, savePolicyToText, updatePolicy
-
-
-
-
Field Detail
-
sectionNameMap
public static final java.util.Map<java.lang.String,java.lang.String> sectionNameMap
-
requiredSections
public static final java.lang.String[] requiredSections
-
modCount
protected int modCount
-
domainIndex
private int domainIndex
-
defaultDomain
private java.lang.String defaultDomain
-
defaultSeparator
private java.lang.String defaultSeparator
-
paramsPattern
private static final java.util.regex.Pattern paramsPattern
-
-
Method Detail
-
getModCount
public int getModCount()
-
loadAssertion
private boolean loadAssertion(Model model, Config cfg, java.lang.String sec, java.lang.String key)
-
getParamsToken
private static java.util.List<java.lang.String> getParamsToken(java.lang.String value)
getParamsToken Get ParamsToken from Assertion.Value
-
addDef
public boolean addDef(java.lang.String sec, java.lang.String key, java.lang.String value)
addDef adds an assertion to the model.- Parameters:
sec
- the section, "p" or "g".key
- the policy type, "p", "p2", .. or "g", "g2", ..value
- the policy rule, separated by ", ".- Returns:
- succeeds or not.
-
getKeySuffix
private java.lang.String getKeySuffix(int i)
-
loadSections
private void loadSections(Config cfg)
Helper function for loadModel and loadModelFromText- Parameters:
cfg
- the configuration parser
-
setLogger
public void setLogger(Logger logger)
SetLogger sets the model's logger.- Parameters:
logger
- the logger to be set for the model.
-
newModel
public static Model newModel()
NewModel creates an empty model.- Returns:
- a new instance of the Model.
-
newModelFromFile
public static Model newModelFromFile(java.lang.String path)
NewModelFromString creates a model from a string which contains model text.- Parameters:
path
- the path of the model file.- Returns:
- the model loaded from file.
-
newModelFromString
public static Model newModelFromString(java.lang.String text)
NewModelFromString creates a model from a string which contains model text.- Parameters:
text
- the path of the file.- Returns:
- the model loaded from text.
-
loadModel
public void loadModel(java.lang.String path)
loadModel loads the model from model CONF file.- Parameters:
path
- the path of the model file.
-
loadModelFromText
public void loadModelFromText(java.lang.String text)
loadModelFromText loads the model from the text.- Parameters:
text
- the model text.
-
loadModelFromConfig
public void loadModelFromConfig(Config cfg)
loadModelFromConfig loads the model from the configuration.- Parameters:
cfg
- the model text.
-
hasSection
public boolean hasSection(java.lang.String sec)
hasSection checks if the section exists in the model.- Parameters:
sec
- the section name to check, such as "p" or "g".- Returns:
- whether the section exists in the model.
-
saveSectionToText
private java.lang.String saveSectionToText(java.lang.String sec)
saveSectionToText saves the section to the text.- Returns:
- the section text.
-
saveModelToText
public java.lang.String saveModelToText()
saveModelToText saves the model to the text.- Returns:
- the model text.
-
printModel
public void printModel()
printModel prints the model to the log.
-
sortPoliciesByPriority
public void sortPoliciesByPriority()
sort policies by priority value
-
sortPoliciesBySubjectHieraichy
public void sortPoliciesBySubjectHieraichy()
sort policies by hieraichy map
-
getSubjectHierarchyMap
public java.util.Map<java.lang.String,java.lang.Integer> getSubjectHierarchyMap(java.util.List<java.util.List<java.lang.String>> policies)
-
findHierarchy
private void findHierarchy(java.util.Map<java.lang.String,java.lang.String> policyMap, java.util.Map<java.lang.String,java.lang.Integer> subjectHierarchyMap, java.util.List<java.lang.String> set, java.lang.String child)
-
getNameWithDomain
public java.lang.String getNameWithDomain(java.lang.String domain, java.lang.String name)
-
toText
public java.lang.String toText()
-
writeString
private void writeString(java.lang.StringBuilder s, java.lang.String sec, java.util.Map<java.lang.String,java.lang.String> tokenPatterns)
-
-