github.com/casbin/json-adapter
go get github.com/casbin/json-adapter

github.com/casbin/json-adapter

JSON adapter for Casbin

by casbin

v1.0.0 (see all)License:Apache-2.0
go get github.com/casbin/json-adapter
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.

GitHub Stars

12

LAST COMMIT

3yrs ago

MAINTAINERS

0

CONTRIBUTORS

3

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
v1.0.0
3yrs ago
v0.0.0-20171220072155-98e846eed480
3yrs ago
No alternatives found
No tutorials found
Add a tutorial