rbac

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package rbac checks permissions by role:

var policy = rbac.New(map[string][]string{
	"viewer": {"projects:read"},
	"editor": {"projects:read", "projects:write"},
	"admin":  {"*"},
}, func(ctx *kit.Ctx) []string {
	return sessions.Get(ctx).Strings("roles") // however your app stores them
})

func Load(ctx *kit.Ctx) (Page, error) {
	if err := policy.Require(ctx, "projects:read"); err != nil {
		return Page{}, err // 401 when signed out, 403 without the permission
	}
	...
}

Permissions are strings; "projects:*" grants every permission that starts with "projects:", and "*" grants everything. A role can include other roles with "role:<name>".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Policy

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

Policy maps roles to permissions.

func New

func New(roles map[string][]string, rolesOf func(*kit.Ctx) []string) *Policy

New returns a policy. roles maps each role to its permissions; rolesOf returns the current user's roles, or nil when nobody is signed in.

func (*Policy) Allows

func (p *Policy) Allows(roles []string, perm string) bool

Allows reports whether any of roles grants perm.

func (*Policy) Can

func (p *Policy) Can(ctx *kit.Ctx, perm string) bool

Can reports whether the current user has perm.

func (*Policy) Hook

func (p *Policy) Hook(prefix, perm string) kit.HookFunc

Hook returns a kit hook that requires perm for every request whose path starts with prefix, such as rbac.Hook("/admin", "admin:access").

func (*Policy) Permissions

func (p *Policy) Permissions(roles ...string) []string

Permissions returns everything the roles grant, including included roles.

func (*Policy) Require

func (p *Policy) Require(ctx *kit.Ctx, perms ...string) error

Require returns nil if the current user has every perm, a 401 error if nobody is signed in, and a 403 error otherwise.

Jump to

Keyboard shortcuts

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