bunadapter

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

casbin-bun-adapter

Bun adapter for Casbin.

Testing locally

https://msales.atlassian.net/wiki/spaces/TECH/pages/3061972993/Testing+Same+service+address+in+Github+actions+as+in+local

Simple Example

package main

import (
	"database/sql"

	bunadapter "github.com/msales/casbin-bun-adapter"
	"github.com/casbin/casbin/v2"
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/pgdialect"
	"github.com/uptrace/bun/driver/pgdriver"
)

func main() {
	// Initialize a database connection (PostgreSQL in this example).
	dbDSN := "postgresql://username:password@postgres:5432/database?sslmode=disable"
	sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dbDSN)))
	db := bun.NewDB(sqldb, pgdialect.New())
	
	// Initialize an adapter.
	// The adapter will use the Postgres database named "casbin" and a table named "casbin_rule".
	// If it doesn't exist, the adapter will create it automatically.
	a, _ := bunadapter.NewAdapter(db)

	// Use the adapter when creating a new instance of an enforcer.
	e := casbin.NewEnforcer("examples/rbac_model.conf", a)

	// Load the policy from the DB.
	e.LoadPolicy()

	// Check the permission.
	e.Enforce("alice", "data1", "read")

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

	// Save the policy back to the DB.
	e.SavePolicy()
}

Support for FilteredAdapter interface

You can load a subset of policies with this adapter:

package main

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

func main() {
	db := bun.NewDB(...)
	a, _ := bunadapter.NewAdapter(db)
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

	e.LoadFilteredPolicy(&bunadapter.Filter{
		P: []string{"", "data1"},
		G: []string{"alice"},
	})
	...
}

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 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 interface{}) error

LoadFilteredPolicy loads adapter policy from the database that matches 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.casbin_rules,alias:cr"`

	ID    string `bun:",pk"`
	Ptype string
	V0    string
	V1    string
	V2    string
	V3    string
	V4    string
	V5    string
}

CasbinRule represents adapter rule in Casbin.

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