pgxadapter

package module
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

PGX Adapter

Tests Coverage Status

PGX Adapter is the pgx adapter for Casbin. With this library, Casbin can load policy from PostgreSQL or save policy to it.

Installation

go get github.com/pckhoi/casbin-pgx-adapter

Simple Postgres Example

package main

import (
	pgxadapter "github.com/pckhoi/casbin-pgx-adapter"
	"github.com/casbin/casbin/v2"
)

func main() {
	// Initialize a PGX adapter and use it in a Casbin enforcer:
	// The adapter will use the Postgres database named "casbin".
	// If it doesn't exist, the adapter will create it automatically.
	a, _ := pgxadapter.NewAdapter("postgresql://username:password@postgres:5432/database?sslmode=disable") // Your driver and data source.
	// Alternatively, you can construct an adapter instance with *pgx.ConnConfig:
    // conf, _ := pgx.ParseConfig("postgresql://pguser:pgpassword@localhost:5432/pgdb?sslmode=disable")
	// a, _ := pgxadapter.NewAdapter(conf)

	// The adapter will use the table named "casbin_rule" by default.
	// If it doesn't exist, the adapter will create it automatically.

	e := casbin.NewEnforcer("examples/rbac_model.conf", a)

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

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

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

	// Save the policy back to 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"
	pgxadapter "github.com/pckhoi/casbin-pgx-adapter"
)

func main() {
	a, _ := pgxadapter.NewAdapter("postgresql://username:password@postgres:5432/database?sslmode=disable")
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

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

Custom database name and table name

You can provide a custom table or database name with option functions

package main

import (
	"github.com/casbin/casbin/v2"
	pgxadapter "github.com/pckhoi/casbin-pgx-adapter"
	"github.com/jackc/pgx/v4"
)

func main() {
    conf, _ := pgx.ParseConfig("postgresql://pguser:pgpassword@localhost:5432/pgdb?sslmode=disable")

    a, _ := pgxadapter.NewAdapter(conf,
        pgxadapter.WithDatabase("custom_database"),
        pgxadapter.WithTableName("custom_table"),
    )
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
    ...
}

Run all tests

PG_CONN=postgresql://user:password@localhost:5432/testdb?sslmode=disable go test github.com/pckhoi/casbin-pgx-adapter -coverpkg=./...

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
const (
	DefaultTableName    = "casbin_rule"
	DefaultDatabaseName = "casbin"
	DefaultTimeout      = time.Second * 10
)

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/jackc/pgx/v5 adapter for policy storage.

func NewAdapter

func NewAdapter(conn interface{}, opts ...Option) (*Adapter, error)

NewAdapter creates a new adapter with connection conn which must either be a PostgreSQL connection string or an instance of *pgx.ConnConfig from package github.com/jackc/pgx/v5.

func (*Adapter) AddPolicies

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

AddPolicies adds policy rules to the storage.

func (*Adapter) AddPolicy

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

AddPolicy adds a policy rule to the storage.

func (*Adapter) Close

func (a *Adapter) Close()

func (*Adapter) IsFiltered

func (a *Adapter) IsFiltered() bool

func (*Adapter) LoadFilteredPolicy

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

LoadFilteredPolicy can query policies with a filter. Make sure that filter is of type *pgxadapter.Filter

func (*Adapter) LoadPolicy

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

LoadPolicy loads policy from database.

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.

func (*Adapter) RemovePolicies

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

RemovePolicies removes policy rules from the storage.

func (*Adapter) RemovePolicy

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

RemovePolicy removes a policy rule from the storage.

func (*Adapter) SavePolicy

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

SavePolicy saves policy to database.

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.

type Filter

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

type Option

type Option func(a *Adapter)

func WithConnectionPool

func WithConnectionPool(pool *pgxpool.Pool) Option

WithConnectionPool can be used to pass an existing *pgxpool.Pool instance

func WithDatabase

func WithDatabase(dbname string) Option

WithDatabase can be used to pass custom database name for Casbin rules

func WithSchema

func WithSchema(s string) Option

WithSchema can be used to pass a custom schema name. Note that the schema name is case-sensitive. If you don't create the schema before hand, the schema will be created for you.

func WithSkipTableCreate

func WithSkipTableCreate() Option

WithSkipTableCreate skips the table creation step when the adapter starts If the Casbin rules table does not exist, it will lead to issues when using the adapter

func WithTableName

func WithTableName(tableName string) Option

WithTableName can be used to pass custom table name for Casbin rules

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout can be used to pass a different timeout than DefaultTimeout for each request to Postgres

Jump to

Keyboard shortcuts

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