Skip to content

Commit

Permalink
feat: support use a specific user login redis (#34)
Browse files Browse the repository at this point in the history
* feat: support use a specific user login redis

* doc: add `NewAdapterWithUser()`
  • Loading branch information
imp2002 authored Nov 4, 2022
1 parent 03e1dee commit 3321b37
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ import (
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.

// Use the following if Redis has password like "123"
//a := redisadapter.NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")
//a := redisadapter.NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")

// Use the following if you use Redis with a specific user
// a := NewAdapterWithUser("tcp", "127.0.0.1:6379", "testaccount", "userpass")

e := casbin.NewEnforcer("examples/rbac_model.conf", a)

// Load the policy from DB.
Expand Down
30 changes: 25 additions & 5 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Adapter struct {
network string
address string
key string
username string
password string
conn redis.Conn
isFiltered bool
Expand All @@ -54,11 +55,13 @@ func finalizer(a *Adapter) {
a.conn.Close()
}

func newAdapter(network string, address string, key string, password string) *Adapter {
func newAdapter(network string, address string, key string,
username string, password string) *Adapter {
a := &Adapter{}
a.network = network
a.address = address
a.key = key
a.username = username
a.password = password

// Open the DB, create it if not existed.
Expand All @@ -72,17 +75,21 @@ func newAdapter(network string, address string, key string, password string) *Ad

// NewAdapter is the constructor for Adapter.
func NewAdapter(network string, address string) *Adapter {
return newAdapter(network, address, "casbin_rules", "")
return newAdapter(network, address, "casbin_rules", "", "")
}

func NewAdapterWithUser(network string, address string, username string, password string) *Adapter {
return newAdapter(network, address, "casbin_rules", username, password)
}

// NewAdapterWithPassword is the constructor for Adapter.
func NewAdapterWithPassword(network string, address string, password string) *Adapter {
return newAdapter(network, address, "casbin_rules", password)
return newAdapter(network, address, "casbin_rules", "", password)
}

// NewAdapterWithKey is the constructor for Adapter.
func NewAdapterWithKey(network string, address string, key string) *Adapter {
return newAdapter(network, address, key, "")
return newAdapter(network, address, key, "", "")
}

type Option func(*Adapter)
Expand All @@ -107,6 +114,12 @@ func WithAddress(address string) Option {
}
}

func WithUsername(username string) Option {
return func(a *Adapter) {
a.username = username
}
}

func WithPassword(password string) Option {
return func(a *Adapter) {
a.password = password
Expand All @@ -126,7 +139,14 @@ func WithKey(key string) Option {

func (a *Adapter) open() {
//redis.Dial("tcp", "127.0.0.1:6379")
if a.password == "" {
if a.username != "" {
conn, err := redis.Dial(a.network, a.address, redis.DialUsername(a.username), redis.DialPassword(a.password))
if err != nil {
panic(err)
}

a.conn = conn
} else if a.password == "" {
conn, err := redis.Dial(a.network, a.address)
if err != nil {
panic(err)
Expand Down
4 changes: 4 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,13 @@ func arrayEqualsWithoutOrder(a [][]string, b [][]string) bool {

func TestAdapters(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")

// Use the following if you use Redis with a account
// a := NewAdapterWithUser("tcp", "127.0.0.1:6379", "testaccount", "userpass")

testSaveLoad(t, a)
testAutoSave(t, a)
testFilteredPolicy(t, a)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.12

require (
github.com/casbin/casbin/v2 v2.40.6
github.com/gomodule/redigo v2.0.0+incompatible
github.com/gomodule/redigo v1.8.9
)
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/casbin/casbin/v2 v2.40.6 h1:Fy8UmYaLst1zjyQ7Uw/Kq9Vxgyk91EtZO/cUUSm3kpQ=
github.com/casbin/casbin/v2 v2.40.6/go.mod h1:sEL80qBYTbd+BPeL4iyvwYzFT3qwLaESq5aFKVLbLfA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
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/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
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.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 3321b37

Please sign in to comment.