sqlxadapter

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2023 License: Apache-2.0 Imports: 5 Imported by: 4

README

casbin-sqlx-adapter

sqlx adapter for Casbin https://github.com/casbin/casbin

Based on sqlx, and tested in MySQL.

Installation

go get github.com/memwey/casbin-sqlx-adapter

NOTICE

The v2 version of Casbin has some break change, check for the detail. If you are still using v1 version, use 0.1.x of this project. And if you have been using the package before, and wanting to upgrade, check the following massage.

The adapter maintained by Casbin team introduce a BREAKING CHANGE by changing the column name from p_type to ptype, see the issue in gorm-adapter and xorm-adapter. Thought I think the adapter layer is able to imply its own storage detail, I follow this BREAKING CHANGE in version 0.3.0 in case of developers may port to different adapters.

The implement is kind of different from the official one. In this implement you should create the database and table on your own.

In my opinion, in a general PRODUCTION environment, the business code is only allow to do DML but not DDL.

The filtered adapter feature has been added in some adapter implements such as Xorm Adapter. However, it's not yet documented in the doc. I will add it after it's documented. See this issue.

Usage example

opts := &AdapterOptions{
    DriverName: "mysql",
    DataSourceName: "root:1234@tcp(127.0.0.1:3306)/yourdb",
    TableName: "casbin_rule",
    // or reuse an existing connection:
    // DB: myDBConn,
}

a := NewAdapterFromOptions(opts)
// Casbin v2 may return an error
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
    panic(err)
}

Thank

Special thanks to Casbin. They provide a superb authorization library.

Special thanks to sqlx. It provides a brilliant set of extensions on go's standard database/sql library.

And this project is inspected by Xorm Adapter, Gorm Adapter, Beego ORM Adapter and MySQL adapter . Thanks all of them.

Others

This is a very first opensource of me and if this project violates any of the opensource guidelines, please contact me. The project is far from perfect, issues and pull requesets are very welcome.

License

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter represents the sqlx adapter for policy storage.

func NewAdapter

func NewAdapter(driverName string, dataSourceName string) *Adapter

NewAdapter is the constructor for Adapter Deprecated: Use NewAdapterFromOptions instead

func NewAdapterByDB

func NewAdapterByDB(db *sqlx.DB) *Adapter

NewAdapterByDB is the constructor for Adapter with existed connection Deprecated: Use NewAdapterFromOptions instead

func NewAdapterFromOptions added in v0.1.2

func NewAdapterFromOptions(opts *AdapterOptions) *Adapter

NewAdapterFromOptions is the constructor for Adapter with existed connection

func (*Adapter) AddPolicy

func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) (err error)

AddPolicy adds a policy rule to the storage.

func (*Adapter) IsFiltered added in v0.2.1

func (a *Adapter) IsFiltered() bool

IsFiltered returns true if the loaded policy has been filtered.

func (*Adapter) LoadPolicy

func (a *Adapter) LoadPolicy(model model.Model) error

LoadPolicy loads policy from database.

func (*Adapter) RemoveFilteredPolicy

func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) (err error)

RemoveFilteredPolicy removes policy rules that match the filter from the storage.

func (*Adapter) RemovePolicy

func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) (err error)

RemovePolicy removes a policy rule from the storage.

func (*Adapter) SavePolicy

func (a *Adapter) SavePolicy(model model.Model) (err error)

SavePolicy saves policy to database.

type AdapterOptions added in v0.1.2

type AdapterOptions struct {
	DriverName     string
	DataSourceName string
	TableName      string
	DB             *sqlx.DB
}

AdapterOptions contains all possible configuration options.

type CasbinRule

type CasbinRule struct {
	ID    int64  `db:"id"`
	PType string `db:"ptype"`
	V0    string `db:"v0"`
	V1    string `db:"v1"`
	V2    string `db:"v2"`
	V3    string `db:"v3"`
	V4    string `db:"v4"`
	V5    string `db:"v5"`
}

CasbinRule ...

type Filter added in v0.2.1

type Filter struct {
	PType []string
	V0    []string
	V1    []string
	V2    []string
	V3    []string
	V4    []string
	V5    []string
}

Filter ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL