Giter Site home page Giter Site logo

casbin / json-adapter Goto Github PK

View Code? Open in Web Editor NEW
16.0 5.0 7.0 21 KB

JSON adapter for Casbin

Home Page: https://github.com/casbin/casbin

License: Apache License 2.0

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

json-adapter's Introduction

JSON Adapter

Go Coverage Status Go Report Card 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/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.

json-adapter's People

Contributors

hsluoyz avatar mousedownmike avatar pjediny avatar yyy1000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

json-adapter's Issues

Update the installation docs

If you install the adapter using the command go get github.com/casbin/json-adapter, it's not compatible with the latest casbin v2. Also, the current version module name is github.com/casbin/json-adapter/v2. The installation guide should be updated to go get github.com/casbin/json-adapter/v2.

panic: runtime error: invalid memory address or nil pointer dereference

Getting this error for every enforcer method
please fix me if I'm doing something wrong.

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x10 pc=0x42722d]

goroutine 1 [running]:
github.com/casbin/casbin/v2.(*Enforcer).LoadPolicy(0x0)
        C:/Users/Admin/go/pkg/mod/github.com/casbin/casbin/[email protected]/enforcer.go:284 +0x4d
main.main()
        T:/dev/go_json_casbin_tesdt/main.go:16 +0xe7
exit status 2  
package main

import (
	"github.com/casbin/casbin/v2"
	"github.com/casbin/json-adapter/v2"
)

func main() {
	b := []byte{}
	a := jsonadapter.NewAdapter(&b)
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
	e.LoadPolicy()
}

Could not get this adapter working via the example, without an existing policy to load

I am using this adapter to essentially have an in-memory policy. I was unable to continue without pre-seeding the byte buffer with []:

func (a *Adapter) loadFromBuffer() error {
	var policy []CasbinRule
	err := json.Unmarshal(*a.source, &policy)
	if err == nil {
		a.policy = policy
	}
	return err
}

err is unexpected end of JSON input which is expected given the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.