Skip to content

Commit 06fcda7

Browse files
authored
feat: support default UpdateCallback (#34)
1 parent 0e70bf2 commit 06fcda7

File tree

6 files changed

+289
-220
lines changed

6 files changed

+289
-220
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func main() {
5959

6060
// Set callback to local example
6161
_ = w.SetUpdateCallback(updateCallback)
62+
63+
// Or use the default callback
64+
// _ = w.SetUpdateCallback(rediswatcher.DefaultUpdateCallback(e))
6265

6366
// Update the policy to test the effect.
6467
// You should see "[casbin rules updated]" in the log.
@@ -77,4 +80,3 @@ func main() {
7780
## License
7881

7982
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.
80-
>>>>>>> 243bd42 (refactor)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/casbin/redis-watcher/v2
33
go 1.18
44

55
require (
6-
github.com/casbin/casbin/v2 v2.47.2
6+
github.com/casbin/casbin/v2 v2.53.1
77
github.com/go-redis/redis/v8 v8.11.5
88
github.com/google/uuid v1.3.0
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
22
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
3-
github.com/casbin/casbin/v2 v2.47.2 h1:FVdlX0GEYWpYj7IdSThBpidLr8Bp+yfvlmVNec5INtw=
4-
github.com/casbin/casbin/v2 v2.47.2/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
3+
github.com/casbin/casbin/v2 v2.53.1 h1:uD/1LMHEPOkn1Xw5UmLnOJxdBPI7Zz85VbdPLJhivxo=
4+
github.com/casbin/casbin/v2 v2.53.1/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
55
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
66
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
77
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=

util.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

watcher.go

Lines changed: 132 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
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+
2865
type 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+
3688
func (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()
176231
func (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

Comments
 (0)