sqlxadapter

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

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

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)
e := casbin.NewEnforcer("examples/rbac_model.conf", a)

Notice

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 can rarely create a database, create a table or drop a table.

Thank

Special thanks to casin. 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.

func NewAdapterByDB

func NewAdapterByDB(db *sqlx.DB) *Adapter

NewAdapterByDB is the constructor for Adapter with existed connection

func NewAdapterFromOptions

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) 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

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

AdapterOptions contains all possible configuration options.

type CasbinRule

type CasbinRule struct {
	ID    uint   `db:"id"`
	PType string `db:"p_type"`
	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 ...

Jump to

Keyboard shortcuts

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