Categorygithub.com/casbin/json-adapter/v2
modulepackage
2.1.1
Repository: https://github.com/casbin/json-adapter.git
Documentation: pkg.go.dev

# README

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

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.

# Functions

NewAdapter is the constructor for Adapter.

# Structs

Adapter represents the Redis adapter for policy storage.
No description provided by the author