badgeradapter

package module
v0.0.0-...-30ed567 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

casbin-badgerdb-adapter


package main

import (
	"fmt"

	"github.com/casbin/casbin/v2"
	"github.com/dgraph-io/badger/v2"
	badgerhold "github.com/inits/badgerholdv2"
	badgeradapter "github.com/inits/casbin-badgerdb-adapter"
)

// Ec ...
var Ec *casbin.Enforcer

func init() {

	// bts, err := ioutil.ReadFile("examples/rbac_policy.csv")
	// if err != nil {
	// 	fmt.Println(err)
	// }

	opts := badgerhold.DefaultOptions
	opts.Options = badger.DefaultOptions("/tmp/badgerhold/qms").WithEncryptionKey([]byte("1234567890123456"))
	store, err := badgerhold.Open(opts)
	if err != nil {
		fmt.Println(err)
	}

	a, err := badgeradapter.NewAdapter(store, "")
	fmt.Println("newadapter err:", err)

	Ec, err = casbin.NewEnforcer("examples/rbac_model.conf", a)
	fmt.Println("NewEnforcer err:", err)

	Ec.EnableAutoSave(true)
	// radd, err := Ec.AddPolicy("qiaos", "data1", "read")
	// fmt.Println("initial data:", radd, err)
	Ec.LoadPolicy()
}

func main() {

	rmul, err := Ec.AddPolicies([][]string{{"qiaos", "data1", "read"}, {"ming", "data2", "write"}, {"data2_admin1", "data2", "read"}, {"data2_admin1", "data2", "write"}, {"qiaos", "data1", "write"}})

	fmt.Println("rmul is:", rmul, err)
	Ec.LoadPolicy()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

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

Adapter ...

func NewAdapter

func NewAdapter(db *badgerhold.Store, builtinPolicy string) (res *Adapter, err1 error)

NewAdapter ...

func (*Adapter) AddPolicies

func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) (err error)

AddPolicies inserts or updates multiple rules by iterating over each one and inserting it into the badgerdb

func (*Adapter) AddPolicy

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

AddPolicy inserts or updates a rule.

func (*Adapter) LoadPolicy

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

LoadPolicy ...

func (*Adapter) RemoveFilteredPolicy

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

RemoveFilteredPolicy ...

func (*Adapter) RemovePolicies

func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) (err error)

RemovePolicies removes multiple policies.

func (*Adapter) RemovePolicy

func (a *Adapter) RemovePolicy(sec string, ptype string, line []string) (err error)

RemovePolicy removes a policy line that matches key

func (*Adapter) SavePolicy

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

SavePolicy is not supported for this Adapter. Auto-save should be used.

type CasbinRule

type CasbinRule struct {
	PType string `json:"p_type"`
	V0    string `json:"v0"`
	V1    string `json:"v1"`
	V2    string `json:"v2"`
	V3    string `json:"v3"`
	V4    string `json:"v4"`
	V5    string `json:"v5"`
}

CasbinRule represents a Casbin rule line.

Jump to

Keyboard shortcuts

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