GithubHelp home page GithubHelp logo

casbin / ent-adapter Goto Github PK

View Code? Open in Web Editor NEW
49.0 5.0 15.0 78 KB

Ent adapter for Casbin

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

License: Apache License 2.0

Go 100.00%
adapter orm acl auth authorization rbac ent access-control abac storage-driver

ent-adapter's Introduction

Ent-Adapter

Go Report Card Go Coverage Status

Ent Adapter is the ent adapter for Casbin. With this library, Casbin can load policy from PostgresSQL/Mysql or save policy to it.

Installation

go get github.com/casbin/ent-adapter

Usage

    a, err := NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/casbin")
    //a, err := NewAdapter("postgres", "user=postgres password=postgres host=127.0.0.1 port=5432 dbname=casbin")
    if err != nil {
        panic(err)	
    }
    e, err := casbin.NewEnforcer("/path/to/model",a)

Notification

The database used in adapter(like casbin) should be created manually before NewAdapter calling.

Getting Help

License

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

ent-adapter's People

Contributors

bon10 avatar hsluoyz avatar huijiezheng avatar kilosonc avatar sevenwhite avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

ent-adapter's Issues

>go get github.com/casbin/ent-adapter error

go: downloading github.com/casbin/ent-adapter v0.0.2
go: downloading github.com/jackc/pgx/v4 v4.11.0
go: downloading github.com/mattn/go-sqlite3 v1.14.6
go: downloading github.com/jackc/pgconn v1.8.1
go: downloading github.com/jackc/pgtype v1.7.0
go: downloading github.com/mattn/go-sqlite3 v1.14.7

github.com/mattn/go-sqlite3

cgo: exec gcc: exec: "gcc": executable file not found in %PATH%

ent `DeleteOne()` will result in `NotFound` error

// RemovePolicies removes policy rules from the storage.
// This is part of the Auto-Save feature.
func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error {
	return a.WithTx(func(tx *ent.Tx) error {
		for _, rule := range rules {
			instance := a.toInstance(ptype, rule)
			if err := tx.CasbinRule.DeleteOne(instance).Exec(a.ctx); err != nil {
				return err
			}
		}
		return nil
	})
}

The instance will have a default id of 0, which will result a NotFound error even when other fields matches.

An exception occurs when mysql exceeds 30 records

An exception occurs when mysql exceeds 30 records

adapter, err := entAdapter.NewAdapter("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", config.Params.Mysql.DbUser, config.Params.Mysql.Password, config.Params.Mysql.Host, config.Params.Mysql.Port, config.Params.Mysql.DbName))
		if err != nil {
			log.Logger.Fatal(err.Error())
		}
		m, err := model.NewModelFromString(`
		[request_definition]
		r = sub, obj, act
		
		[policy_definition]
		p = sub, obj, act
		
		[role_definition]
		g = _, _
		
		[policy_effect]
		e = some(where (p.eft == allow))
		
		[matchers]
		m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
		`)
		if err != nil {
			return nil, err
		}
		auth, err = casbin.NewEnforcer(m, adapter)
		if err != nil {
			return nil, err
		}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x18d4878]

goroutine 89 [running]:
github.com/casbin/casbin/v2/persist.LoadPolicyLine(0xc00039d860, 0x20, 0xc0003cb0b0)
        /Users/xinyi/go/pkg/mod/github.com/casbin/casbin/[email protected]/persist/adapter.go:42 +0x358
github.com/casbin/ent-adapter.loadPolicyLine(0xc0003e5b80, 0xc0003cb0b0)
        /Users/xinyi/go/pkg/mod/github.com/casbin/[email protected]/adapter.go:336 +0x157
github.com/casbin/ent-adapter.(*Adapter).LoadPolicy(0xc000357400, 0xc0003cb0b0, 0xdc, 0x0)
        /Users/xinyi/go/pkg/mod/github.com/casbin/[email protected]/adapter.go:124 +0x1e5
github.com/casbin/casbin/v2.(*Enforcer).LoadPolicy(0xc0003fe080, 0x0, 0x0)
        /Users/xinyi/go/pkg/mod/github.com/casbin/casbin/[email protected]/enforcer.go:288 +0x1dd
github.com/casbin/casbin/v2.(*Enforcer).InitWithModelAndAdapter(0xc0003fe080, 0xc0003ca930, 0xb007120, 0xc000357400, 0xc000357400, 0x18c9c00)
        /Users/xinyi/go/pkg/mod/github.com/casbin/casbin/[email protected]/enforcer.go:169 +0x112
github.com/casbin/casbin/v2.NewEnforcer(0xc00031d210, 0x2, 0x2, 0x0, 0x0, 0x0)
        /Users/xinyi/go/pkg/mod/github.com/casbin/casbin/[email protected]/enforcer.go:105 +0x49d

NewAdapterWithClient won't work with local ent client

Hi,

I'm trying to use NewAdapterWithClient so that I can use the same client for the enforcer (i.e. for casbin_rules table) and for other tables of my project, it would be nice because I can then perhaps wrap the modification to casbin_rules and other tables in the same transaction. For example, when deleting a role, not only do I want to delete policies related to this role stored in casbin_rules, I want to delete info about this role stored in other tables. It's better if they can be done in a single transaction.

But, it's not letting me use my own ent client because it comes from my own ent package, instead of ent package from your repo.

I'm not exactly sure how to fix this, maybe instead of using the ent.client struct as the parameter of NewAdapterWithClient, we could use an interface instead?

I'm not sure wether this repo is still under maintenance or not. Though thank you for this anyway.

Implement `UpdatableAdapter` interface

when use:

updated, err := e.UpdatePolicy([]string{"eve", "data3", "read"}, []string{"eve", "data3", "write"})

got fatal error:

interface conversion: *entadapter.Adapter is not persist.UpdatableAdapter: missing method UpdateFilteredPolicies

happens in github.com/casbin/casbin/[email protected]/internal_api.go line 156

UpdateFilteredPolicies without match ptype when delete old rule with 0 fieldValues, so it will delete all policies

func (a *Adapter) UpdateFilteredPolicies(sec string, ptype string, newPolicies [][]string, fieldIndex int, fieldValues ...string) ([][]string, error) {
	oldPolicies := make([][]string, 0)
	err := a.WithTx(func(tx *ent.Tx) error {
		line := tx.CasbinRule.Query()
		if fieldIndex <= 0 && 0 < fieldIndex+len(fieldValues) {
			line = line.Where(casbinrule.V0EQ(fieldValues[0-fieldIndex]))
		}
		if fieldIndex <= 1 && 1 < fieldIndex+len(fieldValues) {
			line = line.Where(casbinrule.V1EQ(fieldValues[1-fieldIndex]))
		}
		if fieldIndex <= 2 && 2 < fieldIndex+len(fieldValues) {
			line = line.Where(casbinrule.V2EQ(fieldValues[2-fieldIndex]))
		}
		if fieldIndex <= 3 && 3 < fieldIndex+len(fieldValues) {
			line = line.Where(casbinrule.V3EQ(fieldValues[3-fieldIndex]))
		}
		if fieldIndex <= 4 && 4 < fieldIndex+len(fieldValues) {
			line = line.Where(casbinrule.V4EQ(fieldValues[4-fieldIndex]))
		}
		if fieldIndex <= 5 && 5 < fieldIndex+len(fieldValues) {
			line = line.Where(casbinrule.V5EQ(fieldValues[5-fieldIndex]))
		}
		rules, err := line.All(a.ctx)
		if err != nil {
			return err
		}
		for _, rule := range rules {
			if _, err := tx.CasbinRule.Delete().Where(
				casbinrule.IDEQ(rule.ID),
			).Exec(a.ctx); err != nil {
				return err
			}
		}

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.