Skip to content

Commit

Permalink
Merge pull request #5 from gonsuke/feature/casbin_v230
Browse files Browse the repository at this point in the history
Casbin v2.30 support
  • Loading branch information
hsluoyz authored May 11, 2021
2 parents 5b6bd1c + b6c21c5 commit 29f9ce9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module github.com/casbin/redis-watcher/v2
go 1.14

require (
github.com/casbin/casbin/v2 v2.26.1
github.com/casbin/casbin/v2 v2.30.0
github.com/go-redis/redis/v8 v8.8.0
github.com/google/uuid v1.2.0
)

2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/casbin/casbin/v2 v2.26.1 h1:82ruJHV5ZLJyfca8M+d4TsrZfGvC3FHUcLvQMqzb9LQ=
github.com/casbin/casbin/v2 v2.26.1/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
github.com/casbin/casbin/v2 v2.30.0 h1:bBUyn1xgI+AApUDAPK+G7aw1Ln+dbKMIdqKFwOzhz/4=
github.com/casbin/casbin/v2 v2.30.0/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
21 changes: 13 additions & 8 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/casbin/casbin/v2/model"
"log"
"strings"
"sync"

"github.com/casbin/casbin/v2/model"

"github.com/casbin/casbin/v2/persist"
rds "github.com/go-redis/redis/v8"
)
Expand All @@ -26,6 +27,8 @@ type Watcher struct {
type MSG struct {
Method string
ID string
sec string
ptype string
Params interface{}
}

Expand Down Expand Up @@ -102,38 +105,40 @@ func (w *Watcher) Update() error {
return w.logRecord(func() error {
w.l.Lock()
defer w.l.Unlock()
return w.pubClient.Publish(context.Background(), w.options.Channel, &MSG{"Update", w.options.LocalID, ""}).Err()
return w.pubClient.Publish(context.Background(), w.options.Channel, &MSG{"Update", w.options.LocalID, "", "", ""}).Err()
})
}

// UpdateForAddPolicy calls the update callback of other instances to synchronize their policy.
// It is called after Enforcer.AddPolicy()
func (w *Watcher) UpdateForAddPolicy(params ...string) error {
func (w *Watcher) UpdateForAddPolicy(sec, ptype string, params ...string) error {
return w.logRecord(func() error {
w.l.Lock()
defer w.l.Unlock()
return w.pubClient.Publish(context.Background(), w.options.Channel, &MSG{"UpdateForAddPolicy", w.options.LocalID, params}).Err()
return w.pubClient.Publish(context.Background(), w.options.Channel, &MSG{"UpdateForAddPolicy", w.options.LocalID, sec, ptype, params}).Err()
})
}

// UpdateForRemovePolicy UPdateForRemovePolicy calls the update callback of other instances to synchronize their policy.
// It is called after Enforcer.RemovePolicy()
func (w *Watcher) UpdateForRemovePolicy(params ...string) error {
func (w *Watcher) UpdateForRemovePolicy(sec, ptype string, params ...string) error {
return w.logRecord(func() error {
w.l.Lock()
defer w.l.Unlock()
return w.pubClient.Publish(context.Background(), w.options.Channel, &MSG{"UpdateForRemovePolicy", w.options.LocalID, params}).Err()
return w.pubClient.Publish(context.Background(), w.options.Channel, &MSG{"UpdateForRemovePolicy", w.options.LocalID, sec, ptype, params}).Err()
})
}

// UpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy.
// It is called after Enforcer.RemoveFilteredNamedGroupingPolicy()
func (w *Watcher) UpdateForRemoveFilteredPolicy(fieldIndex int, fieldValues ...string) error {
func (w *Watcher) UpdateForRemoveFilteredPolicy(sec, ptype string, fieldIndex int, fieldValues ...string) error {
return w.logRecord(func() error {
w.l.Lock()
defer w.l.Unlock()
return w.pubClient.Publish(context.Background(), w.options.Channel,
&MSG{"UpdateForRemoveFilteredPolicy", w.options.LocalID,
sec,
ptype,
fmt.Sprintf("%d %s", fieldIndex, strings.Join(fieldValues, " ")),
},
).Err()
Expand All @@ -146,7 +151,7 @@ func (w *Watcher) UpdateForSavePolicy(model model.Model) error {
return w.logRecord(func() error {
w.l.Lock()
defer w.l.Unlock()
return w.pubClient.Publish(context.Background(), w.options.Channel, &MSG{"UpdateForSavePolicy", w.options.LocalID, model}).Err()
return w.pubClient.Publish(context.Background(), w.options.Channel, &MSG{"UpdateForSavePolicy", w.options.LocalID, "", "", model}).Err()
})
}

Expand Down

0 comments on commit 29f9ce9

Please sign in to comment.