Skip to content

Commit

Permalink
fix:Update to casbin v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dovics committed Jun 7, 2020
1 parent 813bf0d commit 1a2abec
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
13 changes: 8 additions & 5 deletions README.md
Expand Up @@ -13,17 +13,20 @@ 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() {
// NewEnforcer(modelPath, policyPath) automatically uses the default
// 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")
Expand All @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions go.mod
@@ -0,0 +1,5 @@
module github.com/dovics/session-role-manager

go 1.13

require github.com/casbin/casbin/v2 v2.6.9
4 changes: 4 additions & 0 deletions 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=
6 changes: 3 additions & 3 deletions role_manager.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions role_manager_test.go
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 1a2abec

Please sign in to comment.