https://github.com/casbin/redis-adapter
Redis adapter for Casbin
https://github.com/casbin/redis-adapter
access-control adapter auth authorization authz casbin redis storage-driver
Last synced: 2 months ago
JSON representation
Redis adapter for Casbin
- Host: GitHub
- URL: https://github.com/casbin/redis-adapter
- Owner: casbin
- License: apache-2.0
- Created: 2017-09-22T15:50:16.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-09T01:45:17.000Z (over 1 year ago)
- Last Synced: 2024-10-29T22:37:48.913Z (7 months ago)
- Topics: access-control, adapter, auth, authorization, authz, casbin, redis, storage-driver
- Language: Go
- Size: 40 KB
- Stars: 194
- Watchers: 8
- Forks: 26
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Redis Adapter
====[](https://goreportcard.com/report/github.com/casbin/redis-adapter)
[](https://github.com/casbin/redis-adapter/actions/workflows/ci.yml)
[](https://coveralls.io/github/casbin/redis-adapter?branch=master)
[](https://pkg.go.dev/github.com/casbin/redis-adapter/v3)
[](https://github.com/casbin/redis-adapter/releases/latest)
[](https://discord.gg/S5UjpzGZjN)
[](https://sourcegraph.com/github.com/casbin/redis-adapter?badge)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.
## Installation
go get github.com/casbin/redis-adapter/v3
## Simple Example
```go
package mainimport (
"github.com/casbin/casbin/v2"
"github.com/casbin/redis-adapter/v3"
)func main() {
// Direct Initialization:
// 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, err := redisadapter.NewAdapterWithPassword("tcp", "127.0.0.1:6379", "123")// Use the following if you use Redis with a specific user
// a, err := redisadapter.NewAdapterWithUser("tcp", "127.0.0.1:6379", "username", "password")// Use the following if you use Redis connections pool
// pool := &redis.Pool{}
// a, err := redisadapter.NewAdapterWithPool(pool)// Initialization with different user options:
// Use the following if you use Redis with passowrd like "123":
// a, err := redisadapter.NewAdapterWithOption(redisadapter.WithNetwork("tcp"), redisadapter.WithAddress("127.0.0.1:6379"), redisadapter.WithPassword("123"))// Use the following if you use Redis with username, password, and TLS option:
// var clientTLSConfig tls.Config
// ...
// a, err := redisadapter.NewAdapterWithOption(redisadapter.WithNetwork("tcp"), redisadapter.WithAddress("127.0.0.1:6379"), redisadapter.WithUsername("testAccount"), redisadapter.WithPassword("123456"), redisadapter.WithTls(&clientTLSConfig))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()
}
```## Getting Help
- [Casbin](https://github.com/casbin/casbin)
## License
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.