99 "strings"
1010 "sync"
1111
12+ "github.com/casbin/casbin/v2"
1213 "github.com/casbin/casbin/v2/model"
13-
1414 "github.com/casbin/casbin/v2/persist"
1515 rds "github.com/go-redis/redis/v8"
1616)
@@ -25,14 +25,66 @@ type Watcher struct {
2525 ctx context.Context
2626}
2727
28+ func DefaultUpdateCallback (e casbin.IEnforcer ) func (string ) {
29+ return func (msg string ) {
30+ msgStruct := & MSG {}
31+
32+ err := msgStruct .UnmarshalBinary ([]byte (msg ))
33+ if err != nil {
34+ log .Println (err )
35+ return
36+ }
37+
38+ var res bool
39+ switch msgStruct .Method {
40+ case Update , UpdateForSavePolicy :
41+ err = e .LoadPolicy ()
42+ res = true
43+ case UpdateForAddPolicy :
44+ res , err = e .SelfAddPolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .Rule )
45+ case UpdateForAddPolicies :
46+ res , err = e .SelfAddPolicies (msgStruct .Sec , msgStruct .Ptype , msgStruct .Rules )
47+ case UpdateForRemovePolicy :
48+ res , err = e .SelfRemovePolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .Rule )
49+ case UpdateForRemoveFilteredPolicy :
50+ res , err = e .SelfRemoveFilteredPolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .FieldIndex , msgStruct .FieldValues ... )
51+ case UpdateForRemovePolicies :
52+ res , err = e .SelfRemovePolicies (msgStruct .Sec , msgStruct .Ptype , msgStruct .Rules )
53+ default :
54+ err = errors .New ("unknown update type" )
55+ }
56+ if err != nil {
57+ log .Println (err )
58+ }
59+ if ! res {
60+ log .Println ("callback update policy failed" )
61+ }
62+ }
63+ }
64+
2865type MSG struct {
29- Method string
30- ID string
31- Sec string
32- Ptype string
33- Params interface {}
66+ Method UpdateType
67+ ID string
68+ Sec string
69+ Ptype string
70+ Rule []string
71+ Rules [][]string
72+ FieldIndex int
73+ FieldValues []string
3474}
3575
76+ type UpdateType string
77+
78+ const (
79+ Update UpdateType = "Update"
80+ UpdateForAddPolicy UpdateType = "UpdateForAddPolicy"
81+ UpdateForRemovePolicy UpdateType = "UpdateForRemovePolicy"
82+ UpdateForRemoveFilteredPolicy UpdateType = "UpdateForRemoveFilteredPolicy"
83+ UpdateForSavePolicy UpdateType = "UpdateForSavePolicy"
84+ UpdateForAddPolicies UpdateType = "UpdateForAddPolicies"
85+ UpdateForRemovePolicies UpdateType = "UpdateForRemovePolicies"
86+ )
87+
3688func (m * MSG ) MarshalBinary () ([]byte , error ) {
3789 return json .Marshal (m )
3890}
@@ -105,7 +157,10 @@ func NewWatcherWithCluster(addrs string, option WatcherOptions) (persist.Watcher
105157 close : make (chan struct {}),
106158 }
107159
108- w .initConfig (option , true )
160+ err := w .initConfig (option , true )
161+ if err != nil {
162+ return nil , err
163+ }
109164
110165 if err := w .subClient .Ping (w .ctx ).Err (); err != nil {
111166 return nil , err
@@ -171,7 +226,7 @@ func NewPublishWatcher(addr string, option WatcherOptions) (persist.Watcher, err
171226 return w , nil
172227}
173228
174- // SetUpdateCallback SetUpdateCallBack sets the update callback function invoked by the watcher
229+ // SetUpdateCallback sets the update callback function invoked by the watcher
175230// when the policy is updated. Defaults to Enforcer.LoadPolicy()
176231func (w * Watcher ) SetUpdateCallback (callback func (string )) error {
177232 w .l .Lock ()
@@ -186,7 +241,14 @@ func (w *Watcher) Update() error {
186241 return w .logRecord (func () error {
187242 w .l .Lock ()
188243 defer w .l .Unlock ()
189- return w .pubClient .Publish (context .Background (), w .options .Channel , & MSG {"Update" , w .options .LocalID , "" , "" , "" }).Err ()
244+ return w .pubClient .Publish (
245+ context .Background (),
246+ w .options .Channel ,
247+ & MSG {
248+ Method : Update ,
249+ ID : w .options .LocalID ,
250+ },
251+ ).Err ()
190252 })
191253}
192254
@@ -196,7 +258,16 @@ func (w *Watcher) UpdateForAddPolicy(sec, ptype string, params ...string) error
196258 return w .logRecord (func () error {
197259 w .l .Lock ()
198260 defer w .l .Unlock ()
199- return w .pubClient .Publish (context .Background (), w .options .Channel , & MSG {"UpdateForAddPolicy" , w .options .LocalID , sec , ptype , params }).Err ()
261+ return w .pubClient .Publish (
262+ context .Background (),
263+ w .options .Channel ,
264+ & MSG {
265+ Method : UpdateForAddPolicy ,
266+ ID : w .options .LocalID ,
267+ Sec : sec ,
268+ Ptype : ptype ,
269+ Rule : params ,
270+ }).Err ()
200271 })
201272}
202273
@@ -206,7 +277,17 @@ func (w *Watcher) UpdateForRemovePolicy(sec, ptype string, params ...string) err
206277 return w .logRecord (func () error {
207278 w .l .Lock ()
208279 defer w .l .Unlock ()
209- return w .pubClient .Publish (context .Background (), w .options .Channel , & MSG {"UpdateForRemovePolicy" , w .options .LocalID , sec , ptype , params }).Err ()
280+ return w .pubClient .Publish (
281+ context .Background (),
282+ w .options .Channel ,
283+ & MSG {
284+ Method : UpdateForRemovePolicy ,
285+ ID : w .options .LocalID ,
286+ Sec : sec ,
287+ Ptype : ptype ,
288+ Rule : params ,
289+ },
290+ ).Err ()
210291 })
211292}
212293
@@ -216,11 +297,16 @@ func (w *Watcher) UpdateForRemoveFilteredPolicy(sec, ptype string, fieldIndex in
216297 return w .logRecord (func () error {
217298 w .l .Lock ()
218299 defer w .l .Unlock ()
219- return w .pubClient .Publish (context .Background (), w .options .Channel ,
220- & MSG {"UpdateForRemoveFilteredPolicy" , w .options .LocalID ,
221- sec ,
222- ptype ,
223- fmt .Sprintf ("%d %s" , fieldIndex , strings .Join (fieldValues , " " )),
300+ return w .pubClient .Publish (
301+ context .Background (),
302+ w .options .Channel ,
303+ & MSG {
304+ Method : UpdateForRemoveFilteredPolicy ,
305+ ID : w .options .LocalID ,
306+ Sec : sec ,
307+ Ptype : ptype ,
308+ FieldIndex : fieldIndex ,
309+ FieldValues : fieldValues ,
224310 },
225311 ).Err ()
226312 })
@@ -232,7 +318,14 @@ func (w *Watcher) UpdateForSavePolicy(model model.Model) error {
232318 return w .logRecord (func () error {
233319 w .l .Lock ()
234320 defer w .l .Unlock ()
235- return w .pubClient .Publish (context .Background (), w .options .Channel , & MSG {"UpdateForSavePolicy" , w .options .LocalID , "" , "" , model }).Err ()
321+ return w .pubClient .Publish (
322+ context .Background (),
323+ w .options .Channel ,
324+ & MSG {
325+ Method : UpdateForSavePolicy ,
326+ ID : w .options .LocalID ,
327+ },
328+ ).Err ()
236329 })
237330}
238331
@@ -242,7 +335,17 @@ func (w *Watcher) UpdateForAddPolicies(sec string, ptype string, rules ...[]stri
242335 return w .logRecord (func () error {
243336 w .l .Lock ()
244337 defer w .l .Unlock ()
245- return w .pubClient .Publish (context .Background (), w .options .Channel , & MSG {"UpdateForAddPolicies" , w .options .LocalID , sec , ptype , rules }).Err ()
338+ return w .pubClient .Publish (
339+ context .Background (),
340+ w .options .Channel ,
341+ & MSG {
342+ Method : UpdateForAddPolicies ,
343+ ID : w .options .LocalID ,
344+ Sec : sec ,
345+ Ptype : ptype ,
346+ Rules : rules ,
347+ },
348+ ).Err ()
246349 })
247350}
248351
@@ -252,7 +355,17 @@ func (w *Watcher) UpdateForRemovePolicies(sec string, ptype string, rules ...[]s
252355 return w .logRecord (func () error {
253356 w .l .Lock ()
254357 defer w .l .Unlock ()
255- return w .pubClient .Publish (context .Background (), w .options .Channel , & MSG {"UpdateForRemovePolicies" , w .options .LocalID , sec , ptype , rules }).Err ()
358+ return w .pubClient .Publish (
359+ context .Background (),
360+ w .options .Channel ,
361+ & MSG {
362+ Method : UpdateForRemovePolicies ,
363+ ID : w .options .LocalID ,
364+ Sec : sec ,
365+ Ptype : ptype ,
366+ Rules : rules ,
367+ },
368+ ).Err ()
256369 })
257370}
258371
0 commit comments