boltadapter

package module
v0.0.0-...-6ee457f Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

README

Bolt Adapter Build Status Godoc

Bolt Adapter is the BoltDB adapter for Casbin. With this library, Casbin can load a policy from a previously opened BoltDB or save the policy to it.

Installation

go get github.com/wirepair/bolt-adapter

Simple Example

package main

import (
	"flag"
	"log"

	"github.com/boltdb/bolt"
	"github.com/casbin/casbin"
	"github.com/casbin/casbin/persist"
	"github.com/wirepair/bolt-adapter"
)

var populate bool

func init() {
	flag.BoolVar(&populate, "populate", false, "populate the db from a file first")
}

func main() {
	flag.Parse()

	// Initialize a bolt DB adapter and use it in a Casbin enforcer:
	db, err := bolt.Open("db.dat", 0600, nil)
	if err != nil {
		log.Fatalf("error opening db: %s\n", err)
	}
	defer db.Close()

	adapter := boltadapter.NewAdapter(db) // Pass in the already open bolt DB.

	if populate {
		populateDB(adapter)
	}

	e := casbin.NewEnforcer("examples/rbac_model.conf", adapter)

	// Load the policy from DB.
	e.LoadPolicy()

	// Check the permission.
	e.Enforce("alice", "data1", "read")

	// Save the policy back to DB.
	e.SavePolicy()
}

// populateDB loads a policy from file and saves it to the DB
func populateDB(adapter persist.Adapter) {
	e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
	adapter.SavePolicy(e.GetModel())
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Author

Isaac Dawson

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyPolicy will be returned if the bucket doesn't have any policy data
	ErrEmptyPolicy = errors.New("policy was empty")
)

Functions

This section is empty.

Types

type Adapter

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

Adapter represents the BoltDB adapter for policy storage.

func NewAdapter

func NewAdapter(db *bolt.DB) *Adapter

NewAdapter is the constructor for Adapter. Assumes the bolt db is already opened.

func NewBoltAdapter

func NewBoltAdapter(db *bolt.DB, key string) *Adapter

NewBoltAdapter is the constructor for Adapter. Assumes the bolt db is already opened.

func (*Adapter) AddPolicy

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

AddPolicy adds a policy rule to the storage.

func (*Adapter) LoadPolicy

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

LoadPolicy loads policy from database.

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(sec string, ptype string, rule []string) error

RemovePolicy removes a policy rule from the storage.

func (*Adapter) SavePolicy

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

SavePolicy saves policy to database.

type CasbinRule

type CasbinRule struct {
	PType string
	V0    string
	V1    string
	V2    string
	V3    string
	V4    string
	V5    string
}

CasbinRule represents a policy type and their values

Jump to

Keyboard shortcuts

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