casbun

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: MIT Imports: 8 Imported by: 0

README

CasBun

CasBun is a Bun ORM adapter for Casbin. With this library, Casbin can load policy from bun supported database or save policy to it.

Installation

go get github.com/mmikalsen/casbun

Simple Example

package main

import (
	"github.com/mmikalsen/casbun"
	"github.com/casbin/casbin/v2"
)

func main() {
	sqldb, _ := sql.Open(sqliteshim.ShimName, "file::memory:?cache=shared"")
	db := bun.NewDB(sqldb, sqlitedialect.New())

	a, _ := casbun.NewAdapter(ctx, db)
	e, _ := casbin.NewEnforcer("model.conf", a)

	// check the permission.
	_, _ = e.Enforce("alice", "data1", "read")

	// save the policy back to DB.
	_ = e.SavePolicy()
}

Context Adapter

casbun supports adapter with context, the following is a timeout control implemented using context

a, _ = casbun.NewAdapter(ctx, db)
// Limited time 300s
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
err := a.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"})
if err != nil {
    panic(err)
}

Credits

This adapter is a rewrite of the original junishimura/casbin-bun-adapter , with a focus on reducing unnecessary dependencies, preserving core functionality, and improving the integration with Bun.

License

casbin-bun-adapter is released under MIT License.

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 Bun adapter for policy storage.

func NewAdapter

func NewAdapter(ctx context.Context, db *bun.DB, opts ...CasbinBunOption) (*Adapter, error)

NewAdapter creates a new Casbin policy adapter using a Bun database connection.

Example:

db := bun.NewDB(sqlDB, pgdialect.New())
adapter, err := NewAdapter(ctx, db, WithAutoCreateTable())
if err != nil {
    log.Fatal("Failed to create adapter:", err)
}
enforcer, err := casbin.NewEnforcer("model.conf", adapter)

func (*Adapter) AddPolicies

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

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

func (*Adapter) AddPoliciesCtx

func (a *Adapter) AddPoliciesCtx(ctx context.Context, _, ptype string, rules [][]string) error

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

func (*Adapter) AddPolicy

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

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

func (*Adapter) AddPolicyCtx

func (a *Adapter) AddPolicyCtx(ctx context.Context, _, ptype string, rule []string) error

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

func (*Adapter) LoadPolicy

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

LoadPolicy loads all policy rules from the storage.

func (*Adapter) LoadPolicyCtx

func (a *Adapter) LoadPolicyCtx(ctx context.Context, model model.Model) error

LoadPolicyCtx loads all policy rules from the storage with context.

func (*Adapter) RemoveFilteredPolicy

func (a *Adapter) RemoveFilteredPolicy(
	sec, 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. This API is explained in the link below: https://casbin.org/docs/management-api/#removefilteredpolicy

func (*Adapter) RemoveFilteredPolicyCtx

func (a *Adapter) RemoveFilteredPolicyCtx(
	ctx context.Context,
	sec, ptype string,
	fieldIndex int,
	fieldValues ...string,
) error

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

func (*Adapter) RemovePolicies

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

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

func (*Adapter) RemovePoliciesCtx

func (a *Adapter) RemovePoliciesCtx(ctx context.Context, _, ptype string, rules [][]string) error

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

func (*Adapter) RemovePolicy

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

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

func (*Adapter) RemovePolicyCtx

func (a *Adapter) RemovePolicyCtx(ctx context.Context, _, ptype string, rule []string) error

RemovePolicyCtx removes a policy rule from the storage with context. 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) SavePolicyCtx

func (a *Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error

SavePolicyCtx saves all policy rules to the storage with context.

func (*Adapter) UpdateFilteredPolicies

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

UpdateFilteredPolicies deletes old rules and adds new rules.

func (*Adapter) UpdateFilteredPoliciesCtx

func (a *Adapter) UpdateFilteredPoliciesCtx(
	ctx context.Context,
	sec, ptype string,
	newRules [][]string,
	fieldIndex int,
	fieldValues ...string,
) ([][]string, error)

UpdateFilteredPoliciesCtx deletes old rules and adds new rules.

func (*Adapter) UpdatePolicies

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

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

func (*Adapter) UpdatePoliciesCtx

func (a *Adapter) UpdatePoliciesCtx(
	ctx context.Context,
	sec, ptype string,
	oldRules, newRules [][]string,
) error

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

func (*Adapter) UpdatePolicy

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

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

func (*Adapter) UpdatePolicyCtx

func (a *Adapter) UpdatePolicyCtx(
	ctx context.Context,
	sec, ptype string,
	oldRule, newRule []string,
) error

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

type CasbinBunOption

type CasbinBunOption func(*Adapter)

CasbinBunOption defines a functional option type for configuring a BunAdapter. These options are passed to NewAdapter to customize the adapter's behavior.

func DisableAutoCreateTable

func DisableAutoCreateTable() CasbinBunOption

DisableAutoCreateTable disables automatic creation of the Casbin policy storage table during adapter initialization. If used, the policy table must already exist in the database.

Example:

adapter, err := NewAdapter(db, DisableAutoCreateTable())

type CasbinPolicy

type CasbinPolicy struct {
	bun.BaseModel `bun:"casbin_policies,alias:cp"`
	ID            int64  `bun:"id,pk,autoincrement"`
	PType         string `bun:"ptype,type:varchar(100),notnull"`
	V0            string `bun:"v0,type:varchar(100)"`
	V1            string `bun:"v1,type:varchar(100)"`
	V2            string `bun:"v2,type:varchar(100)"`
	V3            string `bun:"v3,type:varchar(100)"`
	V4            string `bun:"v4,type:varchar(100)"`
	V5            string `bun:"v5,type:varchar(100)"`
}

CasbinPolicy defines the storage format following the definition below: https://casbin.org/docs/policy-storage#database-storage-format

Jump to

Keyboard shortcuts

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