Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/casbin/json-adapter

JSON adapter for Casbin
https://github.com/casbin/json-adapter

abac access-control acl adapter auth authoritization casbin json rbac storage-driver

Last synced: 3 months ago
JSON representation

JSON adapter for Casbin

Awesome Lists containing this project

README

        

JSON Adapter
---

[![Go](https://github.com/casbin/json-adapter/actions/workflows/default.yml/badge.svg)](https://github.com/casbin/json-adapter/actions/workflows/default.yml)
[![Coverage Status](https://coveralls.io/repos/github/casbin/json-adapter/badge.svg?branch=master)](https://coveralls.io/github/casbin/json-adapter?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/casbin/json-adapter)](https://goreportcard.com/report/github.com/casbin/json-adapter)
[![Godoc](https://godoc.org/github.com/casbin/json-adapter?status.svg)](https://godoc.org/github.com/casbin/json-adapter)

JSON Adapter is the [JSON (JavaScript Object Notation)](https://www.json.org/) adapter for [Casbin](https://github.com/casbin/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

```go
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](examples/rbac_policy.json) has the same policy found in [rbac_policy.csv](examples/rbac_policy.csv).

```json
[
{"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

- [Casbin](https://github.com/casbin/casbin)

## License

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