JSON Adapter
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.