From 655221e98d89ae11fddd0b310180554e07d7767e Mon Sep 17 00:00:00 2001 From: dovics Date: Sun, 7 Jun 2020 17:47:04 +0800 Subject: [PATCH] fix:Update to casbin v2 --- README.md | 13 ++++++++----- go.mod | 5 +++++ go.sum | 4 ++++ role_manager.go | 6 +++--- role_manager_test.go | 16 ++++++++-------- 5 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/README.md b/README.md index 7f0216e..f3aca87 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,9 @@ Session Role Manager is the [Session-based](https://en.wikipedia.org/wiki/Sessio package main import ( - "github.com/casbin/casbin" - "github.com/casbin/casbin/file-adapter" - "github.com/casbin/session-role-manager" + "github.com/casbin/casbin/v2" + fileadapter "github.com/casbin/casbin/v2/persist/file-adapter" + sessionrolemanager "github.com/casbin/session-role-manager" ) func main() { @@ -23,7 +23,10 @@ func main() { // role manager when loading policy. So if we want to use a custom // role manager, and this role manager relies on Casbin policy, // we should manually set the role manager before loading policy. - e := casbin.NewEnforcer("examples/rbac_model_with_sessions.conf") + e, err := casbin.NewEnforcer("examples/rbac_model_with_sessions.conf") + if err != nil { + panic(err) + } // Manually set an adapter. a := fileadapter.NewAdapter("examples/rbac_policy_with_sessions.csv") @@ -40,7 +43,7 @@ func main() { // Otherwise, we can set the role manager at any time, because role // manager has nothing to do with the adapter. e.LoadPolicy() - + // Check the permission. // the 4th arg is the querying time in UNIX time format. e.Enforce("alice", "data1", "read", "1508503308708987131") diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b9affa6 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/casbin/session-role-manager/v2 + +go 1.13 + +require github.com/casbin/casbin/v2 v2.6.9 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1c2e3be --- /dev/null +++ b/go.sum @@ -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.6.9 h1:YwuOBkWA31CKWRkZKBmSjRkoRPsI9erYeB2CWDW1tb8= +github.com/casbin/casbin/v2 v2.6.9/go.mod h1:XXtYGrs/0zlOsJMeRteEdVi/FsB0ph7KgNfjoCoJUD8= \ No newline at end of file diff --git a/role_manager.go b/role_manager.go index faf2d4f..6d099fe 100644 --- a/role_manager.go +++ b/role_manager.go @@ -18,8 +18,8 @@ import ( "errors" "sort" - "github.com/casbin/casbin/rbac" - "github.com/casbin/casbin/util" + "github.com/casbin/casbin/v2/log" + "github.com/casbin/casbin/v2/rbac" ) type RoleManager struct { @@ -143,7 +143,7 @@ func (rm *RoleManager) GetUsers(name string, currentTime ...string) ([]string, e // PrintRoles prints all the roles to log. func (rm *RoleManager) PrintRoles() error { for _, role := range rm.allRoles { - util.LogPrint(role.toString()) + log.LogPrint(role.toString()) } return nil } diff --git a/role_manager_test.go b/role_manager_test.go index 3e0b725..cef1a03 100644 --- a/role_manager_test.go +++ b/role_manager_test.go @@ -19,15 +19,15 @@ import ( "testing" "time" - "github.com/casbin/casbin" - "github.com/casbin/casbin/persist/file-adapter" - "github.com/casbin/casbin/rbac" - "github.com/casbin/casbin/util" + "github.com/casbin/casbin/v2" + fileadapter "github.com/casbin/casbin/v2/persist/file-adapter" + "github.com/casbin/casbin/v2/rbac" + "github.com/casbin/casbin/v2/util" ) func testEnforce(t *testing.T, e *casbin.Enforcer, sub string, obj interface{}, act string, time string, res bool) { t.Helper() - if e.Enforce(sub, obj, act, time) != res { + if myRes, _ := e.Enforce(sub, obj, act, time); myRes != res { t.Errorf("%s, %v, %s, %s: %t, supposed to be %t", sub, obj, act, time, !res, res) } } @@ -55,7 +55,7 @@ func getCurrentTime() string { } func getAfterCurrentTime() string { - return strconv.FormatInt(time.Now().UnixNano() + 1, 10) + return strconv.FormatInt(time.Now().UnixNano()+1, 10) } func getOneHourAgo() string { @@ -67,7 +67,7 @@ func getInOneHour() string { } func getAfterOneHour() string { - return strconv.FormatInt(time.Now().Add(time.Hour).UnixNano() + 1, 10) + return strconv.FormatInt(time.Now().Add(time.Hour).UnixNano()+1, 10) } func TestSessionRole(t *testing.T) { @@ -267,7 +267,7 @@ func TestEnforcer(t *testing.T) { // role manager when loading policy. So if we want to use a custom // role manager, and this role manager relies on Casbin policy, // we should manually set the role manager before loading policy. - e := casbin.NewEnforcer("examples/rbac_model_with_sessions.conf") + e, _ := casbin.NewEnforcer("examples/rbac_model_with_sessions.conf") // Manually set an adapter. a := fileadapter.NewAdapter("examples/rbac_policy_with_sessions.csv")