psqlwatcher

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2022 License: MIT Imports: 9 Imported by: 2

README

Casbin Postgres Watcher


A WatcherEX implementation for Casbin based on PostgreSQL

Installation

go get github.com/IguteChung/casbin-psql-watcher

Example

package main

import (
	"context"
	"fmt"

	psqlwatcher "github.com/IguteChung/casbin-psql-watcher"
	"github.com/casbin/casbin/v2"
)

func main() {
	// prepare the watcher with local psql server.
	// for demo purpose, enable NotifySelf to receive a local change callback.
	conn := "host=localhost user=postgres password=postgres dbname=postgres port=5432"
	w, _ := psqlwatcher.NewWatcherWithConnString(context.Background(), conn,
		psqlwatcher.Option{NotifySelf: true, Verbose: true})

	// prepare the enforcer.
	e, _ := casbin.NewEnforcer("../testdata/rbac_model.conf", "../testdata/rbac_policy.csv")

	// set the watcher for enforcer.
	_ = e.SetWatcher(w)

	// set the default callback to handle policy changes.
	_ = w.SetUpdateCallback(psqlwatcher.DefaultCallback(e))

	// update the policy and notify other enforcers.
	_ = e.SavePolicy()

	// wait for callback.
	fmt.Scanln()
}

Dependency

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultCallback

func DefaultCallback(e casbin.IEnforcer) func(string)

DefaultCallback defines the generic implementation for WatcherEX interface.

Types

type MSG

type MSG struct {
	Method      UpdateType `json:"method"`
	ID          string     `json:"id"`
	Sec         string     `json:"sec,omitempty"`
	Ptype       string     `json:"ptype,omitempty"`
	OldRules    [][]string `json:"old_rules,omitempty"`
	NewRules    [][]string `json:"new_rules,omitempty"`
	FieldIndex  int        `json:"field_index,omitempty"`
	FieldValues []string   `json:"field_values,omitempty"`
}

MSG defines the payload for message.

type Option

type Option struct {
	// Channel defines which psql channel to use.
	// use default channel if not specified.
	Channel string
	// Verbose indicate whether to output info log.
	Verbose bool
	// LocalID indicates the watcher's local ID, used to ignore self update event.
	// generate a random id if not specified.
	LocalID string
	// NotifySelf will notify change to the same watcher to do the update.
	// only for testing or debug usage.
	NotifySelf bool
}

Option is used for configure watcher.

type UpdateType

type UpdateType string

UpdateType defines the type of update operation.

const (
	Update                        UpdateType = "Update"
	UpdateForAddPolicy            UpdateType = "UpdateForAddPolicy"
	UpdateForRemovePolicy         UpdateType = "UpdateForRemovePolicy"
	UpdateForRemoveFilteredPolicy UpdateType = "UpdateForRemoveFilteredPolicy"
	UpdateForSavePolicy           UpdateType = "UpdateForSavePolicy"
	UpdateForAddPolicies          UpdateType = "UpdateForAddPolicies"
	UpdateForRemovePolicies       UpdateType = "UpdateForRemovePolicies"
	UpdateForUpdatePolicy         UpdateType = "UpdateForUpdatePolicy"
	UpdateForUpdatePolicies       UpdateType = "UpdateForUpdatePolicies"
)

all types of Update.

type Watcher

type Watcher struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Watcher implements casbin Watcher and WatcherEX to sync multiple casbin enforcer.

func NewWatcherWithConnString

func NewWatcherWithConnString(ctx context.Context, connString string, opt Option) (*Watcher, error)

NewWatcherWithConnString creates a Watcher with pgx connection string.

func NewWatcherWithPool

func NewWatcherWithPool(ctx context.Context, pool *pgxpool.Pool, opt Option) (*Watcher, error)

NewWatcherWithPool creates a Watcher with pgx pool.

func (*Watcher) Close

func (w *Watcher) Close()

Close stops and releases the watcher, the callback function will not be called any more.

func (*Watcher) GetChannel

func (w *Watcher) GetChannel() string

GetChannel gets the channel for the option.

func (*Watcher) GetLocalID

func (w *Watcher) GetLocalID() string

GetLocalID gets the local ID for the option.

func (*Watcher) GetNotifySelf

func (w *Watcher) GetNotifySelf() bool

GetNotifySelf gets the NotifySelf for the option.

func (*Watcher) GetVerbose

func (w *Watcher) GetVerbose() bool

GetVerbose gets the verbose for the option.

func (*Watcher) SetUpdateCallback

func (w *Watcher) SetUpdateCallback(callback func(string)) error

SetUpdateCallback sets the callback function that the watcher will call when the policy in DB has been changed by other instances. A classic callback is Enforcer.LoadPolicy().

func (*Watcher) Update

func (w *Watcher) Update() error

Update calls the update callback of other instances to synchronize their policy. It is usually called after changing the policy in DB, like Enforcer.SavePolicy(), Enforcer.AddPolicy(), Enforcer.RemovePolicy(), etc.

func (*Watcher) UpdateForAddPolicies

func (w *Watcher) UpdateForAddPolicies(sec string, ptype string, rules ...[]string) error

UpdateForAddPolicies calls the update callback of other instances to synchronize their policy. It is called after Enforcer.AddPolicies()

func (*Watcher) UpdateForAddPolicy

func (w *Watcher) UpdateForAddPolicy(sec, ptype string, params ...string) error

UpdateForAddPolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.AddPolicy()

func (*Watcher) UpdateForRemoveFilteredPolicy

func (w *Watcher) UpdateForRemoveFilteredPolicy(sec, ptype string, fieldIndex int, fieldValues ...string) error

UpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemoveFilteredNamedGroupingPolicy()

func (*Watcher) UpdateForRemovePolicies

func (w *Watcher) UpdateForRemovePolicies(sec string, ptype string, rules ...[]string) error

UpdateForRemovePolicies calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemovePolicies()

func (*Watcher) UpdateForRemovePolicy

func (w *Watcher) UpdateForRemovePolicy(sec, ptype string, params ...string) error

UpdateForRemovePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemovePolicy()

func (*Watcher) UpdateForSavePolicy

func (w *Watcher) UpdateForSavePolicy(model model.Model) error

UpdateForSavePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemoveFilteredNamedGroupingPolicy()

func (*Watcher) UpdateForUpdatePolicies

func (w *Watcher) UpdateForUpdatePolicies(sec string, ptype string, oldRules, newRules [][]string) error

UpdateForUpdatePolicies calls the update callback of other instances to synchronize their policy. It is called after Enforcer.UpdatePolicies()

func (*Watcher) UpdateForUpdatePolicy

func (w *Watcher) UpdateForUpdatePolicy(sec string, ptype string, oldRule, newRule []string) error

UpdateForUpdatePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.UpdatePolicy()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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