Skip to content

Commit

Permalink
update casbin v2
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangmzsx committed Oct 29, 2019
1 parent 38f2530 commit 6f22085
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.idea/
9 changes: 5 additions & 4 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package string_adapter
import (
"bytes"
"errors"
"github.com/casbin/casbin/model"
"github.com/casbin/casbin/persist"
"github.com/casbin/casbin/util"
"strings"

"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
"github.com/casbin/casbin/v2/util"
)

/*type Adapter interface {
Expand Down Expand Up @@ -42,7 +43,7 @@ func (sa *Adapter) LoadPolicy(model model.Model) error {
}
strs := strings.Split(sa.Line, "\n")
for _, str := range strs {
if str=="" {
if str == "" {
continue
}
persist.LoadPolicyLine(str, model)
Expand Down
28 changes: 21 additions & 7 deletions adapter_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package string_adapter

import (
"github.com/casbin/casbin"
"testing"

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

func Test_KeyMatchRbac(t *testing.T) {
Expand Down Expand Up @@ -30,11 +32,17 @@ p, data_group_admin, /bob_data/*, POST
g, alice, data_group_admin
`
sa := NewAdapter(line)
e := casbin.NewEnforcer(casbin.NewModel(conf), sa)
md := model.NewModel()
err := md.LoadModelFromText(conf)
if err != nil {
t.Error(err)
return
}
e, _ := casbin.NewEnforcer(md, sa)
sub := "alice"
obj := "/alice_data/login"
act := "POST"
if e.Enforce(sub, obj, act) != true {
if res, _ := e.Enforce(sub, obj, act); res != true {
t.Error("**error**")
}
}
Expand Down Expand Up @@ -63,11 +71,17 @@ p, data_group_admin, data3, write
g, alice, data_group_admin
`
sa := NewAdapter(line)
e := casbin.NewEnforcer(casbin.NewModel(conf), sa)
md := model.NewModel()
err := md.LoadModelFromText(conf)
if err != nil {
t.Error(err)
return
}
e, _ := casbin.NewEnforcer(md, sa)
sub := "alice" // the user that wants to access a resource.
obj := "data1" // the resource that is going to be accessed.
act := "read" // the operation that the user performs on the resource.
if e.Enforce(sub, obj, act) != true {
act := "read" // the operation that the user performs on the resource.
if res, _ := e.Enforce(sub, obj, act); res != true {
t.Error("**error**")
}
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/qiangmzsx/string-adapter

go 1.12

require github.com/casbin/casbin/v2 v2.1.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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.0 h1:FqE47qR7PNFrhh/mQFRqlXWdAM0lObvn/cl8ydyxi1c=
github.com/casbin/casbin/v2 v2.1.0/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=

0 comments on commit 6f22085

Please sign in to comment.