Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/casbin/json-adapter
- Owner: casbin
- License: apache-2.0
- Created: 2017-12-20T00:55:12.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-17T13:10:19.000Z (almost 2 years ago)
- Last Synced: 2024-10-29T16:58:13.975Z (3 months ago)
- Topics: abac, access-control, acl, adapter, auth, authoritization, casbin, json, rbac, storage-driver
- Language: Go
- Homepage: https://github.com/casbin/casbin
- Size: 20.5 KB
- Stars: 18
- Watchers: 5
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 mainimport (
"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.