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

Function NewEnforcer

enforcer.go:85–138  ·  view source on GitHub ↗

NewEnforcer creates an enforcer via file or DB. File: e := casbin.NewEnforcer("path/to/basic_model.conf", "path/to/basic_policy.csv") MySQL DB: a := mysqladapter.NewDBAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") e := casbin.NewEnforcer("path/to/basic_model.conf", a)

(params ...interface{})

Source from the content-addressed store, hash-verified

83// a := mysqladapter.NewDBAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/")
84// e := casbin.NewEnforcer("path/to/basic_model.conf", a)
85func NewEnforcer(params ...interface{}) (*Enforcer, error) {
86 e := &Enforcer{}
87
88 parsedParamLen := 0
89 paramLen := len(params)
90
91 switch paramLen - parsedParamLen {
92 case 2:
93 switch p0 := params[0].(type) {
94 case string:
95 switch p1 := params[1].(type) {
96 case string:
97 err := e.InitWithFile(p0, p1)
98 if err != nil {
99 return nil, err
100 }
101 default:
102 err := e.InitWithAdapter(p0, p1.(persist.Adapter))
103 if err != nil {
104 return nil, err
105 }
106 }
107 default:
108 switch params[1].(type) {
109 case string:
110 return nil, errors.New("invalid parameters for enforcer")
111 default:
112 err := e.InitWithModelAndAdapter(p0.(model.Model), params[1].(persist.Adapter))
113 if err != nil {
114 return nil, err
115 }
116 }
117 }
118 case 1:
119 switch p0 := params[0].(type) {
120 case string:
121 err := e.InitWithFile(p0, "")
122 if err != nil {
123 return nil, err
124 }
125 default:
126 err := e.InitWithModelAndAdapter(p0.(model.Model), nil)
127 if err != nil {
128 return nil, err
129 }
130 }
131 case 0:
132 return e, nil
133 default:
134 return nil, errors.New("invalid parameters for enforcer")
135 }
136
137 return e, nil
138}
139
140// InitWithFile initializes an enforcer with a model file and a policy file.
141func (e *Enforcer) InitWithFile(modelPath string, policyPath string) error {

Callers 15

TestSetWatcherExFunction · 0.85
BenchmarkHasPolicySmallFunction · 0.85
BenchmarkHasPolicyMediumFunction · 0.85
BenchmarkHasPolicyLargeFunction · 0.85
BenchmarkAddPolicySmallFunction · 0.85
BenchmarkAddPolicyMediumFunction · 0.85
BenchmarkAddPolicyLargeFunction · 0.85
NewCachedEnforcerFunction · 0.85
TestConstraintSODFunction · 0.85

Calls 3

InitWithFileMethod · 0.95
InitWithAdapterMethod · 0.95

Tested by 15

TestSetWatcherExFunction · 0.68
BenchmarkHasPolicySmallFunction · 0.68
BenchmarkHasPolicyMediumFunction · 0.68
BenchmarkHasPolicyLargeFunction · 0.68
BenchmarkAddPolicySmallFunction · 0.68
BenchmarkAddPolicyMediumFunction · 0.68
BenchmarkAddPolicyLargeFunction · 0.68
TestConstraintSODFunction · 0.68
TestConstraintSODMaxFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…