redisadapter

package module
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

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

  • This Adapter uses go-redis instead of Redigo
  • This Adapter uses the original CSV format for saving rules to Redis, instead of marshalling them to JSON.

License

This project is licensed under the Apache 2.0 license.

Documentation

Index

Constants

View Source
const (
	// The key under which the policies are stored in redis
	PolicyKey = "casbin:policy"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter is an adapter for policy storage based on Redis

func NewFromClient

func NewFromClient(redisCli *redis.Client) (adapter *Adapter)

NewFromClient returns a new instance of Adapter from an already existing go-redis client.

func NewFromURL

func NewFromURL(url string) (adapter *Adapter, err error)

NewFromDSN returns a new Adapter by using the given DSN. Format: redis://:{password}@{host}:{port}/{database} Example: redis://:123@localhost:6379/0

func (*Adapter) AddPolicy

func (a *Adapter) AddPolicy(_ string, ptype string, rule []string) (err error)

AddPolicy adds a policy rule to the storage.

func (*Adapter) LoadPolicy

func (a *Adapter) LoadPolicy(model model.Model) (err error)

LoadPolicy loads all policy rules from the storage.

func (*Adapter) RemoveFilteredPolicy

func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error

RemoveFilteredPolicy removes policy rules that match the filter from the storage.

func (*Adapter) RemovePolicy

func (a *Adapter) RemovePolicy(_ string, ptype string, rule []string) (err error)

RemovePolicy removes a policy rule from the storage.

func (*Adapter) SavePolicy

func (a *Adapter) SavePolicy(model model.Model) (err error)

SavePolicy saves all policy rules to the storage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL