Skip to content

Commit

Permalink
Merge pull request #6 from fengjihua/master
Browse files Browse the repository at this point in the history
support casbin v2 and go mod
  • Loading branch information
hsluoyz committed Jan 8, 2020
2 parents 8c56da4 + 0585737 commit 0ef7649
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
17 changes: 8 additions & 9 deletions README.md
@@ -1,5 +1,4 @@
Redis Adapter [![Build Status](https://travis-ci.org/casbin/redis-adapter.svg?branch=master)](https://travis-ci.org/casbin/redis-adapter) [![Coverage Status](https://coveralls.io/repos/github/casbin/redis-adapter/badge.svg?branch=master)](https://coveralls.io/github/casbin/redis-adapter?branch=master) [![Godoc](https://godoc.org/github.com/casbin/redis-adapter?status.svg)](https://godoc.org/github.com/casbin/redis-adapter)
====
# Redis Adapter [![Build Status](https://travis-ci.org/casbin/redis-adapter.svg?branch=master)](https://travis-ci.org/casbin/redis-adapter) [![Coverage Status](https://coveralls.io/repos/github/casbin/redis-adapter/badge.svg?branch=master)](https://coveralls.io/github/casbin/redis-adapter?branch=master) [![Godoc](https://godoc.org/github.com/casbin/redis-adapter?status.svg)](https://godoc.org/github.com/casbin/redis-adapter)

Redis Adapter is the [Redis](https://redis.io/) adapter for [Casbin](https://github.com/casbin/casbin). With this library, Casbin can load policy from Redis or save policy to it.

Expand All @@ -13,27 +12,27 @@ Redis Adapter is the [Redis](https://redis.io/) adapter for [Casbin](https://git
package main

import (
"github.com/casbin/casbin"
"github.com/casbin/redis-adapter"
"github.com/casbin/casbin/v2"
"github.com/casbin/redis-adapter/v2"
)

func main() {
// Initialize a Redis adapter and use it in a Casbin enforcer:
a := redisadapter.NewAdapter("tcp", "127.0.0.1:6379") // Your Redis network and address.
a := redisadapter.NewAdapter("tcp", "127.0.0.1:6379") // Your Redis network and address.
// Use the following if Redis has password like "123"
//a := redisadapter.NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")
e := casbin.NewEnforcer("examples/rbac_model.conf", a)

// Load the policy from DB.
e.LoadPolicy()

// Check the permission.
e.Enforce("alice", "data1", "read")

// Modify the policy.
// e.AddPolicy(...)
// e.RemovePolicy(...)

// Save the policy back to DB.
e.SavePolicy()
}
Expand Down
4 changes: 2 additions & 2 deletions adapter.go
Expand Up @@ -19,8 +19,8 @@ import (
"errors"
"runtime"

"github.com/casbin/casbin/model"
"github.com/casbin/casbin/persist"
"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
"github.com/gomodule/redigo/redis"
)

Expand Down
9 changes: 5 additions & 4 deletions adapter_test.go
Expand Up @@ -18,8 +18,8 @@ import (
"log"
"testing"

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

func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
Expand All @@ -35,7 +35,7 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
func TestAdapter(t *testing.T) {
// Because the DB is empty at first,
// so we need to load the policy from the file adapter (.CSV) first.
e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")

a := NewAdapter("tcp", "127.0.0.1:6379")
// Use the following if Redis has password like "123"
Expand Down Expand Up @@ -68,6 +68,7 @@ func TestAdapter(t *testing.T) {
a = NewAdapter("tcp", "127.0.0.1:6379")
// Use the following if Redis has password like "123"
//a := NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")
e = casbin.NewEnforcer("examples/rbac_model.conf", a)

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"}})
}
11 changes: 11 additions & 0 deletions go.mod
@@ -0,0 +1,11 @@
module github.com/fengjihua/redis-adapter/v2

go 1.12

require (
github.com/casbin/casbin v1.9.1
github.com/casbin/casbin/v2 v2.1.2
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/gomodule/redigo v2.0.0+incompatible
github.com/lib/pq v1.3.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
@@ -0,0 +1,12 @@
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 v1.9.1 h1:ucjbS5zTrmSLtH4XogqOG920Poe6QatdXtz1FEbApeM=
github.com/casbin/casbin v1.9.1/go.mod h1:z8uPsfBJGUsnkagrt3G8QvjgTKFMBJ32UP8HpZllfog=
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/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
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/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=

0 comments on commit 0ef7649

Please sign in to comment.