@@ -41,15 +41,19 @@ func DefaultUpdateCallback(e casbin.IEnforcer) func(string) {
4141 err = e .LoadPolicy ()
4242 res = true
4343 case UpdateForAddPolicy :
44- res , err = e .SelfAddPolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .Rule )
44+ res , err = e .SelfAddPolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .NewRule )
4545 case UpdateForAddPolicies :
46- res , err = e .SelfAddPolicies (msgStruct .Sec , msgStruct .Ptype , msgStruct .Rules )
46+ res , err = e .SelfAddPolicies (msgStruct .Sec , msgStruct .Ptype , msgStruct .NewRules )
4747 case UpdateForRemovePolicy :
48- res , err = e .SelfRemovePolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .Rule )
48+ res , err = e .SelfRemovePolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .NewRule )
4949 case UpdateForRemoveFilteredPolicy :
5050 res , err = e .SelfRemoveFilteredPolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .FieldIndex , msgStruct .FieldValues ... )
5151 case UpdateForRemovePolicies :
52- res , err = e .SelfRemovePolicies (msgStruct .Sec , msgStruct .Ptype , msgStruct .Rules )
52+ res , err = e .SelfRemovePolicies (msgStruct .Sec , msgStruct .Ptype , msgStruct .NewRules )
53+ case UpdateForUpdatePolicy :
54+ res , err = e .SelfUpdatePolicy (msgStruct .Sec , msgStruct .Ptype , msgStruct .OldRule , msgStruct .NewRule )
55+ case UpdateForUpdatePolicies :
56+ res , err = e .SelfUpdatePolicies (msgStruct .Sec , msgStruct .Ptype , msgStruct .OldRules , msgStruct .NewRules )
5357 default :
5458 err = errors .New ("unknown update type" )
5559 }
@@ -67,8 +71,10 @@ type MSG struct {
6771 ID string
6872 Sec string
6973 Ptype string
70- Rule []string
71- Rules [][]string
74+ OldRule []string
75+ OldRules [][]string
76+ NewRule []string
77+ NewRules [][]string
7278 FieldIndex int
7379 FieldValues []string
7480}
@@ -83,6 +89,8 @@ const (
8389 UpdateForSavePolicy UpdateType = "UpdateForSavePolicy"
8490 UpdateForAddPolicies UpdateType = "UpdateForAddPolicies"
8591 UpdateForRemovePolicies UpdateType = "UpdateForRemovePolicies"
92+ UpdateForUpdatePolicy UpdateType = "UpdateForUpdatePolicy"
93+ UpdateForUpdatePolicies UpdateType = "UpdateForUpdatePolicies"
8694)
8795
8896func (m * MSG ) MarshalBinary () ([]byte , error ) {
@@ -262,11 +270,11 @@ func (w *Watcher) UpdateForAddPolicy(sec, ptype string, params ...string) error
262270 context .Background (),
263271 w .options .Channel ,
264272 & MSG {
265- Method : UpdateForAddPolicy ,
266- ID : w .options .LocalID ,
267- Sec : sec ,
268- Ptype : ptype ,
269- Rule : params ,
273+ Method : UpdateForAddPolicy ,
274+ ID : w .options .LocalID ,
275+ Sec : sec ,
276+ Ptype : ptype ,
277+ NewRule : params ,
270278 }).Err ()
271279 })
272280}
@@ -281,11 +289,11 @@ func (w *Watcher) UpdateForRemovePolicy(sec, ptype string, params ...string) err
281289 context .Background (),
282290 w .options .Channel ,
283291 & MSG {
284- Method : UpdateForRemovePolicy ,
285- ID : w .options .LocalID ,
286- Sec : sec ,
287- Ptype : ptype ,
288- Rule : params ,
292+ Method : UpdateForRemovePolicy ,
293+ ID : w .options .LocalID ,
294+ Sec : sec ,
295+ Ptype : ptype ,
296+ NewRule : params ,
289297 },
290298 ).Err ()
291299 })
@@ -339,11 +347,11 @@ func (w *Watcher) UpdateForAddPolicies(sec string, ptype string, rules ...[]stri
339347 context .Background (),
340348 w .options .Channel ,
341349 & MSG {
342- Method : UpdateForAddPolicies ,
343- ID : w .options .LocalID ,
344- Sec : sec ,
345- Ptype : ptype ,
346- Rules : rules ,
350+ Method : UpdateForAddPolicies ,
351+ ID : w .options .LocalID ,
352+ Sec : sec ,
353+ Ptype : ptype ,
354+ NewRules : rules ,
347355 },
348356 ).Err ()
349357 })
@@ -359,11 +367,53 @@ func (w *Watcher) UpdateForRemovePolicies(sec string, ptype string, rules ...[]s
359367 context .Background (),
360368 w .options .Channel ,
361369 & MSG {
362- Method : UpdateForRemovePolicies ,
363- ID : w .options .LocalID ,
364- Sec : sec ,
365- Ptype : ptype ,
366- Rules : rules ,
370+ Method : UpdateForRemovePolicies ,
371+ ID : w .options .LocalID ,
372+ Sec : sec ,
373+ Ptype : ptype ,
374+ NewRules : rules ,
375+ },
376+ ).Err ()
377+ })
378+ }
379+
380+ // UpdateForUpdatePolicy calls the update callback of other instances to synchronize their policy.
381+ // It is called after Enforcer.UpdatePolicy()
382+ func (w * Watcher ) UpdateForUpdatePolicy (sec string , ptype string , oldRule , newRule []string ) error {
383+ return w .logRecord (func () error {
384+ w .l .Lock ()
385+ defer w .l .Unlock ()
386+ return w .pubClient .Publish (
387+ context .Background (),
388+ w .options .Channel ,
389+ & MSG {
390+ Method : UpdateForUpdatePolicy ,
391+ ID : w .options .LocalID ,
392+ Sec : sec ,
393+ Ptype : ptype ,
394+ OldRule : oldRule ,
395+ NewRule : newRule ,
396+ },
397+ ).Err ()
398+ })
399+ }
400+
401+ // UpdateForUpdatePolicies calls the update callback of other instances to synchronize their policy.
402+ // It is called after Enforcer.UpdatePolicies()
403+ func (w * Watcher ) UpdateForUpdatePolicies (sec string , ptype string , oldRules , newRules [][]string ) error {
404+ return w .logRecord (func () error {
405+ w .l .Lock ()
406+ defer w .l .Unlock ()
407+ return w .pubClient .Publish (
408+ context .Background (),
409+ w .options .Channel ,
410+ & MSG {
411+ Method : UpdateForUpdatePolicies ,
412+ ID : w .options .LocalID ,
413+ Sec : sec ,
414+ Ptype : ptype ,
415+ OldRules : oldRules ,
416+ NewRules : newRules ,
367417 },
368418 ).Err ()
369419 })
0 commit comments