Merge pull request #1 from pjediny/casbin-v2-go-mod

Add support for Casbin v2.0.0
tree: b09874b6c20d3d82dbb8453e5dab241d8aa6a731
  1. examples/
  2. .gitignore
  3. .travis.yml
  4. adapter.go
  5. adapter_test.go
  6. go.mod
  7. go.sum
  8. LICENSE
  9. README.md
README.md

JSON Adapter Build Status Coverage Status Godoc

JSON Adapter is the JSON (JavaScript Object Notation) adapter for Casbin. With this library, Casbin can load policy from JSON string or save policy to it.

Installation

go get github.com/casbin/json-adapter

Simple Example

package main

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

func main() {
	// Initialize a JSON adapter and use it in a Casbin enforcer:
	b := []byte{} // b stores Casbin policy in JSON bytes.
	a := jsonadapter.NewAdapter(&b) // Use b as the data source. 
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
	
	// Load the policy from JSON bytes b.
	e.LoadPolicy()
	
	// Check the permission.
	e.Enforce("alice", "data1", "read")
	
	// Modify the policy.
	// e.AddPolicy(...)
	// e.RemovePolicy(...)
	
	// Save the policy back to JSON bytes b.
	e.SavePolicy()
}

Getting Help

License

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