casbinpgadapter

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

README

Casbin Postgres Adapter

Casbin Postgres Adapter is the postgres adapter for Casbin

Installation

$ go get github.com/nrfta/go-casbin-pg-adapter

Example

package main

import (
	"database/sql"
	"os"

	"github.com/casbin/casbin/v2"
	"github.com/nrfta/go-casbin-pg-adapter"
)

func main() {
	connectionString := "postgresql://postgres:@localhost:5432/postgres?sslmode=disable"
	db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
	if err != nil {
		panic(err)
	}

	tableName := "casbin"
	adapter, err := casbinpgadapter.NewAdapter(db, tableName)
	// If you are using db schema
	// myDBSchema := "mySchema"
	// adapter, err := casbinpgadapter.NewAdapterWithDBSchema(db, myDBSchema, tableName)
	if err != nil {
		panic(err)
	}

	enforcer, err := casbin.NewEnforcer("./examples/model.conf", adapter)
	if err != nil {
		panic(err)
	}

	// Load stored policy from database
	enforcer.LoadPolicy()

	// Do permission checking
	enforcer.Enforce("alice", "data1", "write")

	// Do some mutations
	enforcer.AddPolicy("alice", "data2", "write")
	enforcer.RemovePolicy("alice", "data1", "write")

	// Persist policy to database
	enforcer.SavePolicy()
}

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 is a postgresql adaptor for casbin

func NewAdapter

func NewAdapter(db *sql.DB, tableName string) (*Adapter, error)

NewAdapter returns a new casbin postgresql adapter

func NewAdapterWithDBSchema

func NewAdapterWithDBSchema(db *sql.DB, dbSchema string, tableName string) (*Adapter, error)

NewAdapterWithDBSchema returns a new casbin postgresql adapter with the schema named dbSchema

func (*Adapter) AddPolicies

func (adapter *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 (adapter *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) LoadPolicy

func (adapter *Adapter) LoadPolicy(cmodel casbinModel.Model) error

LoadPolicy loads all policy rules from the storage.

func (*Adapter) RemoveFilteredPolicy

func (adapter *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 (adapter *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 (adapter *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 (adapter *Adapter) SavePolicy(cmodel casbinModel.Model) error

SavePolicy saves all policy rules to the storage.

type FilteredAdapter

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

FilteredAdapter is the filtered file adapter for Casbin. It can load policy from file or save policy to file and supports loading of filtered policies.

func NewFilteredAdapter

func NewFilteredAdapter(db *sql.DB, tableName string) (*FilteredAdapter, error)

NewFilteredAdapter is the constructor for FilteredAdapter.

func NewFilteredAdapterWithDBSchema

func NewFilteredAdapterWithDBSchema(db *sql.DB, dbSchema string, tableName string) (*FilteredAdapter, error)

NewFilteredAdapterWithDBSchema return a pointer for FilteredAdapter which has schema dbSchema

func (*FilteredAdapter) IsFiltered

func (a *FilteredAdapter) IsFiltered() bool

IsFiltered returns true if the loaded policy has been filtered.

func (*FilteredAdapter) LoadFilteredPolicy

func (a *FilteredAdapter) LoadFilteredPolicy(mod casbinModel.Model, filter interface{}) error

LoadFilteredPolicy loads only policy rules that match the filter.

func (*FilteredAdapter) LoadPolicy

func (a *FilteredAdapter) LoadPolicy(model casbinModel.Model) error

LoadPolicy loads all policy rules from the storage.

func (*FilteredAdapter) SavePolicy

func (a *FilteredAdapter) SavePolicy(model casbinModel.Model) error

SavePolicy saves all policy rules to the storage.

Directories

Path Synopsis
db
pkg

Jump to

Keyboard shortcuts

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