MCPcopy
hub / github.com/apache/casbin / AddDef

Method AddDef

model/model.go:70–110  ·  view source on GitHub ↗

AddDef adds an assertion to the model.

(sec string, key string, value string)

Source from the content-addressed store, hash-verified

68
69// AddDef adds an assertion to the model.
70func (model Model) AddDef(sec string, key string, value string) bool {
71 if value == "" {
72 return false
73 }
74
75 ast := Assertion{}
76 ast.Key = key
77 ast.Value = value
78 ast.PolicyMap = make(map[string]int)
79 ast.FieldIndexMap = make(map[string]int)
80
81 if sec == "r" || sec == "p" {
82 ast.Tokens = strings.Split(ast.Value, ",")
83 for i := range ast.Tokens {
84 ast.Tokens[i] = key + "_" + strings.TrimSpace(ast.Tokens[i])
85 }
86 } else if sec == "g" {
87 ast.ParamsTokens = getParamsToken(ast.Value)
88 ast.Tokens = strings.Split(ast.Value, ",")
89 ast.Tokens = ast.Tokens[:len(ast.Tokens)-len(ast.ParamsTokens)]
90 } else {
91 ast.Value = util.RemoveComments(util.EscapeAssertion(ast.Value))
92 }
93
94 if sec == "m" {
95 // Escape backslashes in string literals to match CSV parsing behavior
96 ast.Value = util.EscapeStringLiterals(ast.Value)
97
98 if strings.Contains(ast.Value, "in") {
99 ast.Value = strings.Replace(strings.Replace(ast.Value, "[", "(", -1), "]", ")", -1)
100 }
101 }
102
103 _, ok := model[sec]
104 if !ok {
105 model[sec] = make(AssertionMap)
106 }
107
108 model[sec][key] = &ast
109 return true
110}
111
112func getKeySuffix(i int) string {
113 if i == 1 {

Callers 10

TestRBACModelInMemoryFunction · 0.95
TestInitEmptyFunction · 0.95
TestModel_AddDefFunction · 0.95
testModelToTextFunction · 0.95
loadAssertionFunction · 0.80

Calls 4

RemoveCommentsFunction · 0.92
EscapeAssertionFunction · 0.92
EscapeStringLiteralsFunction · 0.92
getParamsTokenFunction · 0.85

Tested by 9

TestRBACModelInMemoryFunction · 0.76
TestInitEmptyFunction · 0.76
TestModel_AddDefFunction · 0.76
testModelToTextFunction · 0.76