roles

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package roles provides a simple, performant RBAC (Role-Based Access Control) system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gate

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

Gate stores named policy functions.

func NewGate

func NewGate() *Gate

NewGate creates a Gate.

func (*Gate) Allows

func (g *Gate) Allows(ctx context.Context, name string, userID int64, resource any) (bool, error)

Allows runs the named policy and returns whether it passes.

func (*Gate) Define

func (g *Gate) Define(name string, policy Policy)

Define registers a policy under a name.

type Manager

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

Manager is the central RBAC registry. Roles and their permissions are defined at startup and never change at runtime.

func New

func New(userRolesFn func(ctx context.Context, userID int64) ([]string, error)) *Manager

New creates a Manager with a function to resolve user roles.

roles.New(func(ctx context.Context, userID int64) ([]string, error) {
    var r []string
    err := db.Table("user_roles").Where("user_id = ?", userID).Pluck("role", &r)
    return r, err
})

func (*Manager) AllPermissions

func (m *Manager) AllPermissions(ctx context.Context, userID int64) ([]Permission, error)

AllPermissions returns all permissions granted to the user across all their roles.

func (*Manager) Can

func (m *Manager) Can(ctx context.Context, userID int64, permission Permission) (bool, error)

Can reports whether the user can perform the given permission. It checks all roles the user has and returns true if any grants the permission. Wildcard permissions (e.g. "users:*") match any sub-permission (e.g. "users:create").

func (*Manager) Cannot

func (m *Manager) Cannot(ctx context.Context, userID int64, permission Permission) (bool, error)

Cannot is the inverse of Can.

func (*Manager) Define

func (m *Manager) Define(name string, permissions ...Permission) *Manager

Define registers a role and its permissions.

m.Define("admin", "users:*", "posts:*")
m.Define("editor", "posts:create", "posts:update")

func (*Manager) HasRole

func (m *Manager) HasRole(ctx context.Context, userID int64, role string) (bool, error)

HasRole reports whether the user (by ID) has the given role.

func (*Manager) Preload

func (m *Manager) Preload(ctx context.Context, userID int64) (context.Context, error)

Preload resolves the user's roles once and caches them on the returned context. All subsequent Can/HasRole/AllPermissions calls with that context (and the same user ID) use the cached roles instead of hitting the userRolesFn (typically a DB query) on every check.

The cache is explicit and context-scoped: it lives exactly as long as the context (e.g. one HTTP request) and is never shared across requests, so role changes take effect on the next request with no invalidation dance.

ctx, err := rbac.Preload(ctx, userID)   // one DB query
rbac.Can(ctx, userID, "posts:update")   // cached
rbac.HasRole(ctx, userID, "admin")      // cached

type Permission

type Permission string

Permission is a string-based permission identifier (e.g. "users:create", "posts:delete").

type Policy

type Policy func(ctx context.Context, userID int64, resource any) (bool, error)

Policy is a named gate function for fine-grained authorization logic. Policies supplement role-based rules with model-level checks.

type Role

type Role struct {
	Name        string
	Permissions map[Permission]bool
}

Role groups a set of Permissions under a name.

Jump to

Keyboard shortcuts

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