mycasbin

package
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ReloadInterval = time.Minute

ReloadInterval is how often an enforcer reloads its policy from the database.

A policy written on one instance reaches the others no other way: the adapter writes to the database, and every other process keeps serving what it loaded at startup. Set this to zero before the first Setup to opt out — a single-instance deployment gains nothing from the query.

Functions

func KeyMatch2 added in v2.3.0

func KeyMatch2(path, pattern string) (bool, error)

KeyMatch2 reports whether path matches the route pattern, e.g. "/api/v1/dept/7" against "/api/v1/dept/:id".

It answers exactly what casbin's util.KeyMatch2 answers, without recompiling the pattern on every call. Callers that match a request path against a fixed list of route patterns - an authorization allowlist, say - pay a regexp compilation per entry per request otherwise.

The error reports a pattern that will not compile; util.KeyMatch2 panics on those.

func Setup

func Setup(db *gorm.DB, key string) *casbin.SyncedEnforcer

Setup returns the enforcer for key, building it from db on first use.

key names the policy source. An enforcer serves the policy it loaded, so a deployment that reads from more than one database - the multi-tenant configuration, where each host has its own - has to pass a distinct key per database. Passing one key for all of them hands every caller the enforcer built from whichever database arrived first, and the rest are then authorized against a policy table that is not theirs. "" is the right key for a single database.

func UpdateCallback

func UpdateCallback(msg string)

UpdateCallback reloads the policy, for use as a watcher's update callback.

Setup polls instead, which needs no broker. A deployment that already has one can register a watcher against this and have the change arrive at once rather than within ReloadInterval:

enforcer.SetWatcher(w)
w.SetUpdateCallback(mycasbin.UpdateCallback)

The message carries no tenant, so every enforcer reloads. That is a wasted query for the tenants whose policy did not change, and the alternative - reloading whichever one happens to be first - is wrong rather than slow.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is the implementation for a Logger using golang log.

func (*Logger) EnableLog

func (l *Logger) EnableLog(enable bool)

EnableLog controls whether print the message.

func (*Logger) IsEnabled

func (l *Logger) IsEnabled() bool

IsEnabled returns if logger is enabled.

func (*Logger) LogEnforce

func (l *Logger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string)

LogEnforce log info related to enforce.

func (*Logger) LogModel

func (l *Logger) LogModel(model [][]string)

LogModel log info related to model.

func (*Logger) LogPolicy

func (l *Logger) LogPolicy(policy map[string][][]string)

LogPolicy log info related to policy.

func (*Logger) LogRole

func (l *Logger) LogRole(roles []string)

LogRole log info related to role.

Jump to

Keyboard shortcuts

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