Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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 days ago
JSON representation

Redis adapter for Casbin

Awesome Lists containing this project

README

        

Redis Adapter
====

[![Go Report Card](https://goreportcard.com/badge/github.com/casbin/redis-adapter)](https://goreportcard.com/report/github.com/casbin/redis-adapter)
[![Build](https://github.com/casbin/redis-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin/redis-adapter/actions/workflows/ci.yml)
[![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://pkg.go.dev/github.com/casbin/redis-adapter/v3)
[![Release](https://img.shields.io/github/release/casbin/redis-adapter.svg)](https://github.com/casbin/redis-adapter/releases/latest)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)
[![Sourcegraph](https://sourcegraph.com/github.com/casbin/redis-adapter/-/badge.svg)](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 main

import (
"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.