casbinbunadapter

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 12 Imported by: 0

README

casbin-bun-adapter

Bun adapter for Casbin

Installation

go get github.com/cuipeiyu/casbin-bun-adapter

How to use?

package main

import (
	bunadapter "github.com/cuipeiyu/casbin-bun-adapter"
)

var driverName = "pg" // Your can also use mysql and mssql
var sourceName = "user=postgres password=postgres host=localhost port=5432 database=casbin sslmode=disable"
var schemaName = "public"
var tableName = "casbin_rule"

func main() {
	a, err := bunadapter.NewAdapter(
		driverName,
		sourceName,
		bunadapter.WithTableName(schemaName, tableName),
	)
	handleError(err)
}

OR

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/jackc/pgx/v4/stdlib"

	bunadapter "github.com/cuipeiyu/casbin-bun-adapter"
)

var driverName = "pg" // Your can also use mysql and mssql
var sourceName = "user=postgres password=postgres host=localhost port=5432 database=casbin sslmode=disable" // demo for postgresql
var schemaName = "public"
var tableName = "casbin_rule"

func main() {
	// new sql driver
	db, err := sql.Open("pgx", sourceName)
	handleError(err)
	defer db.Close()

	// use custome driver
	a, err := bunadapter.NewAdapterWithClient(
		db,
		bunadapter.WithTableName(schemaName, tableName),
	)
	handleError(err)
}

License

MIT

Documentation

Index

Constants

View Source
const (
	DefaultSchemaName = "public"
	DefaultTableName  = "casbin_rule"
)

Variables

View Source
var (
	ErrUnknownDriver = errors.New("unknown driver")
)

Functions

func CasbinRuleToStringArray

func CasbinRuleToStringArray(rule *CasbinRule) []string

Types

type Adapter

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

func NewAdapter

func NewAdapter(driverName, dataSourceName string, options ...Option) (*Adapter, error)

NewAdapter returns an adapter by driver name and data source string.

func NewAdapterWithClient

func NewAdapterWithClient(client *bun.DB, options ...Option) (*Adapter, error)

NewAdapterWithClient create an adapter with client passed in. This method does not ensure the existence of database, user should create database manually.

func (*Adapter) AddPolicies

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

AddPolicies adds policy rules to the storage. This is part of the Auto-Save feature.

func (*Adapter) AddPolicy

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

AddPolicy adds a policy rule to the storage. This is part of the Auto-Save feature.

func (*Adapter) IsFiltered

func (a *Adapter) IsFiltered() bool

IsFiltered returns true if the loaded policy has been filtered.

func (*Adapter) LoadFilteredPolicy

func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) error

LoadFilteredPolicy loads only policy rules that match the filter. Filter parameter here is a Filter structure

func (*Adapter) LoadPolicy

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

LoadPolicy loads all policy rules from the storage.

func (*Adapter) RemoveFilteredPolicy

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

RemoveFilteredPolicy removes policy rules that match the filter from the storage. This is part of the Auto-Save feature.

func (*Adapter) RemovePolicies

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

RemovePolicies removes policy rules from the storage. This is part of the Auto-Save feature.

func (*Adapter) RemovePolicy

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

RemovePolicy removes a policy rule from the storage. This is part of the Auto-Save feature.

func (*Adapter) SavePolicy

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

SavePolicy saves all policy rules to the storage.

func (*Adapter) UpdateFilteredPolicies

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

UpdateFilteredPolicies deletes old rules and adds new rules.

func (*Adapter) UpdatePolicies

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

UpdatePolicies updates some policy rules to storage, like db, redis.

func (*Adapter) UpdatePolicy

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

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

func (*Adapter) WithTx

func (a *Adapter) WithTx(fn func(tx bun.Tx) error) error

type CasbinRule

type CasbinRule struct {
	Id    int64  `bun:"id,pk,autoincrement"`
	Ptype string `bun:",nullzero,notnull"`
	V0    string `bun:",nullzero,notnull"`
	V1    string `bun:",nullzero,notnull"`
	V2    string `bun:",nullzero,notnull"`
	V3    string `bun:",nullzero,notnull"`
	V4    string `bun:",nullzero,notnull"`
	V5    string `bun:",nullzero,notnull"`
	V6    string `bun:",nullzero,notnull"`
	V7    string `bun:",nullzero,notnull"`
}

type Filter

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

type Option

type Option func(a *Adapter) error

func WithTableName

func WithTableName(schema, table string) Option

Jump to

Keyboard shortcuts

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