bunadapter

package
v0.0.0-...-3400737 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

Forked Adapter

Internal fork of the Bun Casbin adapter so the CasbinRule model no longer hard-codes the casbin. schema, keeping the original API but allowing the same table name to work in Postgres (public search path) and SQLite.

Bug Fix: Support Empty Fields

Comprehensive Review of bunadapter/adapter.go

Methods Reviewed:

  1. LoadPolicy() (lines 44-57) ✅
    • Uses String() method - now fixed
  2. RemoveFilteredPolicy() (lines 122-149) ✅ CORRECT
    • Skips empty values in WHERE clauses - appropriate behavior
  3. LoadFilteredPolicy() (lines 153-190) ✅
    • Uses String() method - now fixed
    • Uses buildQuery() - correct behavior
  4. UpdateFilteredPolicies() (lines 238-297) ✅ NOW FIXED
    • Uses toStringPolicy() - now fixed to preserve empty fields
  5. buildQuery() (lines 365-387) ✅ CORRECT
    • Skips empty values for SQL filters - appropriate behavior
  6. String() (lines 432-464) ✅ FIXED
    • Converts to CSV string for Casbin
    • Now preserves empty fields
  7. QueryWhereGroup() (lines 467-489) ✅ CORRECT
    • Skips empty values in SQL WHERE clauses - appropriate behavior
  8. toStringPolicy() (lines 491-516) ✅ FIXED
    • Returns policy as string slice
    • Now preserves empty fields

Two methods fixed:

  1. String() - CSV serialization for Casbin parser
  2. toStringPolicy() - String slice serialization for Casbin API

The Bug: The methods were skipping empty string fields when building the CSV string for Casbin, causing field position misalignment when parsing policies with empty scopeExpr values.

The Fix: Modified the methods to preserve empty fields up to the last non-empty field, ensuring Casbin correctly parses all 5 fields defined in the model: p = role, obj, act, scopeExpr, eft.

This now allows wildcard policies like role:platform-engineer, *, *, , allow to work correctly!

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 github.com/uptrace/bun adapter for policy storage.

func NewAdapter

func NewAdapter(db *bun.DB) (*Adapter, error)

NewAdapter creates new Adapter by using bun's database connection. Expects DB table to be created in database.

func (*Adapter) AddPolicies

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

AddPolicies adds policy rules to the database.

func (*Adapter) AddPolicy

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

AddPolicy adds adapter policy rule to the database.

func (*Adapter) Close

func (a *Adapter) Close() error

Close closes adapter database connection.

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

LoadFilteredPolicy loads only policies that match the filter.

func (*Adapter) LoadPolicy

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

LoadPolicy loads policy from the database.

func (*Adapter) RemoveFilteredPolicy

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

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

func (*Adapter) RemovePolicies

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

RemovePolicies removes policy rules from the database.

func (*Adapter) RemovePolicy

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

RemovePolicy removes adapter policy rule from the database.

func (*Adapter) SavePolicy

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

SavePolicy saves policy to the database removing any policies already present.

func (*Adapter) UpdateFilteredPolicies

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

UpdateFilteredPolicies updates some policy rules in the database.

func (*Adapter) UpdatePolicies

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

UpdatePolicies updates some policy rules to the database.

func (*Adapter) UpdatePolicy

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

UpdatePolicy updates adapter policy rule from the database. This is part of the Auto-Save feature.

type CasbinRule

type CasbinRule struct {
	bun.BaseModel `bun:"table:casbin_rules,alias:cr"`

	// Removed ID field,
	// defined a composite primary key on all fields instead.
	// strongly typed fields to varchar with length limits
	Ptype string `bun:",pk,type:varchar(100),notnull"` // Policy type: 'p' (policy), 'g' (grouping/role)
	V0    string `bun:",pk,type:varchar(255)"`         // Role name (policies) or user ID (groupings)
	V1    string `bun:",pk,type:varchar(255)"`         // Object type or role name
	V2    string `bun:",pk,type:varchar(255)"`         // Action
	V3    string `bun:",pk,type:varchar(255)"`         // Scope expression (go-bexpr string)
	V4    string `bun:",pk,type:varchar(255)"`         // Effect (allow/deny)
	V5    string `bun:",pk,type:varchar(255)"`         // Reserved
}

CasbinRule represents adapter rule in Casbin.

func (*CasbinRule) QueryWhereGroup

func (r *CasbinRule) QueryWhereGroup(q bun.QueryBuilder) bun.QueryBuilder

QueryWhereGroup extends query builder with another OR group. Group contains all non-empty fields of the CasbinRule.

func (*CasbinRule) String

func (r *CasbinRule) String() string

type Filter

type Filter struct {
	P []string
	G []string
}

Filter represents adapter filter.

Jump to

Keyboard shortcuts

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