mlsen / casbin-redis-adapter

Redis Adapter for Casbin
Apache License 2.0
4 stars 5 forks source link

Casbin Redis Adapter

Casbin Redis Adapter is an adapter for Casbin based on Redis.

Installation

go get github.com/mlsen/casbin-redis-adapter

Usage

package main

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

func main() {
    // The adapter can be initialized from a Redis URL
    a, _ := redisadapter.NewFromURL("redis://:123@localhost:6379/0")

    // Initialize a new Enforcer with the redis adapter
    e, _ := casbin.NewEnforcer("model.conf", a)

    // Load policy from redis
    e.LoadPolicy()

    // Add a policy to redis
    e.AddPolicy("alice", "data1", "read")

    // Check for permissions
    e.Enforce("alice", "data1", "read")

    // Delete a policy from redis
    e.RemovePolicy("alice", "data1", "read")

    // Save all policies to redis
    e.SavePolicy()
}

Initialize Adapter from existing go-redis Client

package main

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

func main() {
    // Initialize the redis client
    rc := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        DB:       0,
        Password: "123",
    })
    // ...

    // The adapter can be initialized from a Redis URL
    a := redisadapter.NewFromClient(rc)

    // ...
}

Main differences to the official Redis Adapter

License

This project is licensed under the Apache 2.0 license.