sqlxadapter

package module
v0.0.0-...-0a30309 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: Apache-2.0 Imports: 8 Imported by: 5

README

sqlx-adapter

Go Report Card Build Status Coverage Status PkgGoDev License

sqlx-adapter is a Sqlx Adapter for Casbin V2.

With this library, Casbin can load policy lines from Sqlx supported databases or save policy lines.

Tested Databases

master branch
oracle branch

Installation

go get github.com/Blank-Xu/sqlx-adapter

Simple Examples

SQLite3
package main

import (
	"log"

	sqlxadapter "github.com/Blank-Xu/sqlx-adapter"
	"github.com/casbin/casbin/v2"
	"github.com/jmoiron/sqlx"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	// connect to the database first.
	db, err := sqlx.Connect("sqlite3", "file:test.db")
	if err != nil {
		panic(err)
	}

	// Initialize a Sqlx adapter and use it in a Casbin enforcer:
	// The adapter will use the Sqlite3 table name "casbin_rule_test",
	// the default table name is "casbin_rule".
	// If it doesn't exist, the adapter will create it automatically.
	a, err := sqlxadapter.NewAdapter(db, "casbin_rule_test")
	if err != nil {
		panic(err)
	}

	e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
	if err != nil {
		panic(err)
	}

	// Load the policy from DB.
	if err = e.LoadPolicy(); err != nil {
		log.Println("LoadPolicy failed, err: ", err)
	}

	// Check the permission.
	has, err := e.Enforce("alice", "data1", "read")
	if err != nil {
		log.Println("Enforce failed, err: ", err)
	}
	if !has {
		log.Println("do not have permission")
	}

	// Modify the policy.
	// e.AddPolicy(...)
	// e.RemovePolicy(...)

	// Save the policy back to DB.
	if err = e.SavePolicy(); err != nil {
		log.Println("SavePolicy failed, err: ", err)
	}
}
MySQL
package main

import (
	"log"
	"runtime"
	"time"

	sqlxadapter "github.com/Blank-Xu/sqlx-adapter"
	"github.com/casbin/casbin/v2"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

func finalizer(db *sqlx.DB) {
	err := db.Close()
	if err != nil {
		panic(err)
	}
}

func main() {
	// connect to the database first.
	db, err := sqlx.Connect("mysql", "root:YourPassword@tcp(127.0.0.1:3306)/YourDBName")
	if err != nil {
		panic(err)
	}

	db.SetMaxOpenConns(20)
	db.SetMaxIdleConns(10)
	db.SetConnMaxLifetime(time.Minute * 10)

	// need to control by user, not the package
	runtime.SetFinalizer(db, finalizer)

	// Initialize a Sqlx adapter and use it in a Casbin enforcer:
	// The adapter will use the Sqlite3 table name "casbin_rule_test",
	// the default table name is "casbin_rule".
	// If it doesn't exist, the adapter will create it automatically.
	a, err := sqlxadapter.NewAdapter(db, "casbin_rule_test")
	if err != nil {
		panic(err)
	}

	e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
	if err != nil {
		panic(err)
	}

	// Load the policy from DB.
	if err = e.LoadPolicy(); err != nil {
		log.Println("LoadPolicy failed, err: ", err)
	}

	// Check the permission.
	has, err := e.Enforce("alice", "data1", "read")
	if err != nil {
		log.Println("Enforce failed, err: ", err)
	}
	if !has {
		log.Println("do not have permission")
	}

	// Modify the policy.
	// e.AddPolicy(...)
	// e.RemovePolicy(...)

	// Save the policy back to DB.
	if err = e.SavePolicy(); err != nil {
		log.Println("SavePolicy failed, err: ", err)
	}
}

Getting Help

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 define the sqlx adapter for Casbin. It can load policy lines or save policy lines from sqlx connected database.

func NewAdapter

func NewAdapter(db *sqlx.DB, tableName string) (*Adapter, error)

NewAdapter the constructor for Adapter. db should connected to database and controlled by user. If tableName == "", the Adapter will automatically create a table named "casbin_rule".

func NewAdapterContext

func NewAdapterContext(ctx context.Context, db *sqlx.DB, tableName string) (*Adapter, error)

NewAdapterContext the constructor for Adapter. db should connected to database and controlled by user. If tableName == "", the Adapter will automatically create a table named "casbin_rule".

func (*Adapter) AddPolicies

func (p *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error

AddPolicies add multiple policy rules to the storage.

func (*Adapter) AddPolicy

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

AddPolicy add one policy rule to the storage.

func (*Adapter) IsFiltered

func (p *Adapter) IsFiltered() bool

IsFiltered returns true if the loaded policy rules has been filtered.

func (*Adapter) LoadFilteredPolicy

func (p *Adapter) LoadFilteredPolicy(model model.Model, filterPtr interface{}) error

LoadFilteredPolicy load policy rules that match the filter. filterPtr must be a pointer.

func (*Adapter) LoadPolicy

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

LoadPolicy load all policy rules from the storage.

func (*Adapter) RemoveFilteredPolicy

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

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

func (*Adapter) RemovePolicies

func (p *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) (err error)

RemovePolicies remove policy rules.

func (*Adapter) RemovePolicy

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

RemovePolicy remove policy rules from the storage.

func (*Adapter) SavePolicy

func (p *Adapter) SavePolicy(model model.Model) error

SavePolicy save policy rules to the storage.

func (*Adapter) UpdateFilteredPolicies

func (p *Adapter) UpdateFilteredPolicies(sec, ptype string, newPolicies [][]string, fieldIndex int, fieldValues ...string) (oldPolicies [][]string, err error)

UpdateFilteredPolicies deletes old rules and adds new rules.

func (*Adapter) UpdatePolicies

func (p *Adapter) UpdatePolicies(sec, ptype string, oldRules, newRules [][]string) (err error)

UpdatePolicies updates policy rules to storage.

func (*Adapter) UpdatePolicy

func (p *Adapter) UpdatePolicy(sec, ptype string, oldRule, newPolicy []string) error

UpdatePolicy update a policy rule from storage. This is part of the Auto-Save feature.

type CasbinRule

type CasbinRule struct {
	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 defines the casbin rule model. It used for save or load policy lines from sqlx connected database.

type Filter

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

Filter defines the filtering rules for a FilteredAdapter's policy. Empty values are ignored, but all others must match the filter.

Jump to

Keyboard shortcuts

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