Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A Casbin policy persistance adapter backed by Bolt DB

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages