JSON adapter for Casbin

Clone this repo:
  1. 65905f2 Update README.md by hsluoyz · 3 years, 1 month ago master
  2. f5ad382 fix: no Unmarshal when json is empty (#5) by Junhao Liu · 3 years, 2 months ago v2.1.1
  3. 202ad99 feat: Github Actions and semantic-release (#7) by Junhao Liu · 3 years, 2 months ago v2.1.0
  4. 97b017e Merge pull request #2 from mousedownmike/feature/json_example by Yang Luo · 6 years ago
  5. 8d2df58 Increased test coverage to include clearing, adding and saving policies. by Mike Dalrymple · 6 years ago

JSON Adapter

Go Coverage Status Go Report Card 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/v2

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()
}

Policy JSON

The following illustrates the expected JSON format for a policy. The rbac_policy.json has the same policy found in rbac_policy.csv.

[
  {"PType":"p","V0":"alice","V1":"data1","V2":"read"},
  {"PType":"p","V0":"bob","V1":"data2","V2":"write"},
  {"PType":"p","V0":"data2_admin","V1":"data2","V2":"read"},
  {"PType":"p","V0":"data2_admin","V1":"data2","V2":"write"},
  {"PType":"g","V0":"alice","V1":"data2_admin"}
]

Getting Help

License

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