Skip to content

Commit

Permalink
Merge c99d2bf into c40c24b
Browse files Browse the repository at this point in the history
  • Loading branch information
uran0sH committed Jan 8, 2021
2 parents c40c24b + c99d2bf commit 9a788e0
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 18 deletions.
84 changes: 69 additions & 15 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,28 @@ func loadPolicyLine(line CasbinRule, model model.Model) {

// LoadPolicy loads policy from database.
func (a *Adapter) LoadPolicy(model model.Model) error {
text, err := redis.String(a.conn.Do("GET", a.key))
num, err := redis.Int(a.conn.Do("LLEN", a.key))
if err == redis.ErrNil {
return nil
}
if err != nil {
return err
}
var lines []CasbinRule
err = json.Unmarshal([]byte(text), &lines)
values, err := redis.Values(a.conn.Do("LRANGE", a.key, 0, num))
if err != nil {
return err
}

for _, line := range lines {
var line CasbinRule
for _, value := range values {
text, ok := value.([]byte)
if !ok {
return errors.New("the type is wrong")
}
err = json.Unmarshal(text, &line)
if err != nil {
return err
}
loadPolicyLine(line, model)
}

Expand Down Expand Up @@ -186,42 +194,88 @@ func (a *Adapter) SavePolicy(model model.Model) error {
a.dropTable()
a.createTable()

var lines []CasbinRule
var texts [][]byte

for ptype, ast := range model["p"] {
for _, rule := range ast.Policy {
line := savePolicyLine(ptype, rule)
lines = append(lines, line)
text, err := json.Marshal(line)
if err != nil {
return err
}
texts = append(texts, text)
}
}

for ptype, ast := range model["g"] {
for _, rule := range ast.Policy {
line := savePolicyLine(ptype, rule)
lines = append(lines, line)
text, err := json.Marshal(line)
if err != nil {
return err
}
texts = append(texts, text)
}
}

text, err := json.Marshal(lines)
if err != nil {
return err
}

_, err = a.conn.Do("SET", a.key, text)
_, err := a.conn.Do("RPUSH", redis.Args{}.Add(a.key).AddFlat(texts)...)
return err
}

// AddPolicy adds a policy rule to the storage.
func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
line := savePolicyLine(ptype, rule)
text, err := json.Marshal(line)
if err != nil {
return err
}
_, err = a.conn.Do("RPUSH", a.key, text)
return err
}

// RemovePolicy removes a policy rule from the storage.
func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
line := savePolicyLine(ptype, rule)
text, err := json.Marshal(line)
if err != nil {
return err
}
_, err = a.conn.Do("LREM", a.key, 1, text)
return err
}

// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
return errors.New("not implemented")
}

// AddPolicies adds policy rules to the storage.
func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error {
var texts [][]byte
for _, rule := range rules {
line := savePolicyLine(ptype, rule)
text, err := json.Marshal(line)
if err != nil {
return err
}
texts = append(texts, text)
}
_, err := a.conn.Do("RPUSH", redis.Args{}.Add(a.key).AddFlat(texts)...)
return err
}

// RemovePolicies removes policy rules from the storage.
func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error {
for _, rule := range rules {
line := savePolicyLine(ptype, rule)
text, err := json.Marshal(line)
if err != nil {
return err
}
_, err = a.conn.Do("LREM", a.key, 1, text)
if err != nil {
return err
}
}
return nil
}
37 changes: 37 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,41 @@ func TestAdapter(t *testing.T) {

e, _ = casbin.NewEnforcer("examples/rbac_model.conf", a)
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})

// Add one policy to DB
a.AddPolicy("p", "p", []string{"paul", "data2", "read"})
e.ClearPolicy()
a.LoadPolicy(e.GetModel())
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"paul", "data2", "read"}})

// Remove one policy from DB
a.RemovePolicy("p", "p", []string{"paul", "data2", "read"})
e.ClearPolicy()
a.LoadPolicy(e.GetModel())
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})

// Add policies to DB
a.AddPolicies("p", "p", [][]string{
{"curry", "data1", "write"},
{"kobe", "data2", "read"},
})
e.ClearPolicy()
a.LoadPolicy(e.GetModel())
testGetPolicy(t, e, [][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"curry", "data1", "write"},
{"kobe", "data2", "read"},
})

// Remove polices from DB
a.RemovePolicies("p", "p", [][]string{
{"curry", "data1", "write"},
{"kobe", "data2", "read"},
})
e.ClearPolicy()
a.LoadPolicy(e.GetModel())
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/casbin/redis-adapter/v2
go 1.12

require (
github.com/casbin/casbin/v2 v2.1.2
github.com/casbin/casbin/v2 v2.19.8
github.com/gomodule/redigo v2.0.0+incompatible
)
18 changes: 16 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/casbin/casbin/v2 v2.1.2 h1:bTwon/ECRx9dwBy2ewRVr5OiqjeXSGiTUY74sDPQi/g=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/casbin/casbin/v2 v2.19.8 h1:hmlbARamCCE9hmcE7N/NRQqzti6Wu8CdF+QwpXzhFQU=
github.com/casbin/casbin/v2 v2.19.8/go.mod h1:wUgota0cQbTXE6Vd+KWpg41726jFRi7upxio0sR+Xd0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 9a788e0

Please sign in to comment.