Documentation
¶
Index ¶
- Variables
- func KeyMatch2(path, pattern string) (bool, error)
- func Setup(db *gorm.DB, key string) *casbin.SyncedEnforcer
- func UpdateCallback(msg string)
- type Logger
- func (l *Logger) EnableLog(enable bool)
- func (l *Logger) IsEnabled() bool
- func (l *Logger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string)
- func (l *Logger) LogModel(model [][]string)
- func (l *Logger) LogPolicy(policy map[string][][]string)
- func (l *Logger) LogRole(roles []string)
Constants ¶
This section is empty.
Variables ¶
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
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) LogEnforce ¶
func (l *Logger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string)
LogEnforce log info related to enforce.