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

Function parseConstraint

model/constraint.go:73–130  ·  view source on GitHub ↗

parseConstraint parses a constraint definition string.

(key, value string)

Source from the content-addressed store, hash-verified

71
72// parseConstraint parses a constraint definition string.
73func parseConstraint(key, value string) (*Constraint, error) {
74 value = strings.TrimSpace(value)
75
76 // Try to match sod pattern
77 if matches := sodPattern.FindStringSubmatch(value); matches != nil {
78 return &Constraint{
79 Key: key,
80 Type: ConstraintTypeSOD,
81 Roles: []string{matches[1], matches[2]},
82 }, nil
83 }
84
85 // Try to match sodMax pattern
86 if matches := sodMaxPattern.FindStringSubmatch(value); matches != nil {
87 maxCount, err := strconv.Atoi(matches[2])
88 if err != nil {
89 return nil, fmt.Errorf("invalid max count in sodMax: %w", err)
90 }
91
92 roles, err := parseRolesArray(matches[1])
93 if err != nil {
94 return nil, fmt.Errorf("sodMax: %w", err)
95 }
96
97 return &Constraint{
98 Key: key,
99 Type: ConstraintTypeSODMax,
100 Roles: roles,
101 MaxCount: maxCount,
102 }, nil
103 }
104
105 // Try to match roleMax pattern
106 if matches := roleMaxPattern.FindStringSubmatch(value); matches != nil {
107 maxCount, err := strconv.Atoi(matches[2])
108 if err != nil {
109 return nil, fmt.Errorf("invalid max count in roleMax: %w", err)
110 }
111 return &Constraint{
112 Key: key,
113 Type: ConstraintTypeRoleMax,
114 Role: matches[1],
115 MaxCount: maxCount,
116 }, nil
117 }
118
119 // Try to match rolePre pattern
120 if matches := rolePrePattern.FindStringSubmatch(value); matches != nil {
121 return &Constraint{
122 Key: key,
123 Type: ConstraintTypeRolePre,
124 Role: matches[1],
125 PreReqRole: matches[2],
126 }, nil
127 }
128
129 return nil, fmt.Errorf("unrecognized constraint format: %s", value)
130}

Callers 1

ValidateConstraintsMethod · 0.85

Calls 1

parseRolesArrayFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…