permissions

package
v0.60.8 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ResourceContentTypes = "content_types"
	ResourceBlocks       = "blocks"
	ResourceEnvironments = "environments"
	ResourceContent      = "content"
	ResourcePages        = "pages"
	ResourceMenus        = "menus"
)
View Source
const (
	ContentTypesRead    = "content_types:read"
	ContentTypesCreate  = "content_types:create"
	ContentTypesUpdate  = "content_types:update"
	ContentTypesDelete  = "content_types:delete"
	ContentTypesPublish = "content_types:publish"

	BlocksRead   = "blocks:read"
	BlocksCreate = "blocks:create"
	BlocksUpdate = "blocks:update"
	BlocksDelete = "blocks:delete"

	EnvironmentsRead   = "environments:read"
	EnvironmentsCreate = "environments:create"
	EnvironmentsUpdate = "environments:update"
	EnvironmentsDelete = "environments:delete"

	ContentRead   = "content:read"
	ContentCreate = "content:create"
	ContentUpdate = "content:update"
	ContentDelete = "content:delete"

	PagesRead   = "pages:read"
	PagesCreate = "pages:create"
	PagesUpdate = "pages:update"
	PagesDelete = "pages:delete"

	MenusRead   = "menus:read"
	MenusCreate = "menus:create"
	MenusUpdate = "menus:update"
	MenusDelete = "menus:delete"
)
View Source
const (
	StrategyEnvFirst    = "env_first"
	StrategyGlobalFirst = "global_first"
	StrategyCustom      = "custom"
)

Variables

View Source
var ErrPermissionDenied = errors.New("permissions: denied")

Functions

func Allowed

func Allowed(ctx context.Context, permission string) bool

Allowed reports whether the provided permission is allowed for the context.

func ConfigureEnvironmentScope added in v0.23.1

func ConfigureEnvironmentScope(cfg EnvironmentScopeConfig)

ConfigureEnvironmentScope updates the global environment permission scope configuration.

func EnvironmentKeyFromContext added in v0.23.1

func EnvironmentKeyFromContext(ctx context.Context) string

EnvironmentKeyFromContext returns the stored environment key, if any.

func Join

func Join(resource string, action Action) string

Join builds a permission token from resource and action.

func Require

func Require(ctx context.Context, permission string) error

Require enforces a permission requirement when a checker is available on the context.

func WithChecker

func WithChecker(ctx context.Context, checker Checker) context.Context

WithChecker stores a permission checker on the context.

func WithClaims

func WithClaims(ctx context.Context, claims interfaces.AuthClaims) context.Context

WithClaims stores auth claims on the context for permission checks.

func WithEnvironmentKey added in v0.23.1

func WithEnvironmentKey(ctx context.Context, key string) context.Context

WithEnvironmentKey stores the environment key on the context for permission checks.

func WithPermissions

func WithPermissions(ctx context.Context, perms ...string) context.Context

WithPermissions stores a static permission set on the context.

func WithSession

func WithSession(ctx context.Context, session interfaces.RoleCapableSession) context.Context

WithSession stores a role-capable session on the context for permission checks.

Types

type Action

type Action string
const (
	ActionRead    Action = "read"
	ActionCreate  Action = "create"
	ActionUpdate  Action = "update"
	ActionDelete  Action = "delete"
	ActionPublish Action = "publish"
)

type Checker

type Checker interface {
	Allowed(permission string) bool
}

func CheckerFromContext

func CheckerFromContext(ctx context.Context) Checker

CheckerFromContext returns the configured permission checker if available.

type CheckerFunc

type CheckerFunc func(permission string) bool

func (CheckerFunc) Allowed

func (fn CheckerFunc) Allowed(permission string) bool

type EnvironmentScopeConfig added in v0.23.1

type EnvironmentScopeConfig struct {
	Enabled  bool
	Strategy Strategy
}

EnvironmentScopeConfig configures environment-scoped permission resolution.

type Error

type Error struct {
	Permission string
}

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

type PermissionSet

type PermissionSet struct {
	Read    string `json:"read,omitempty"`
	Create  string `json:"create,omitempty"`
	Update  string `json:"update,omitempty"`
	Delete  string `json:"delete,omitempty"`
	Publish string `json:"publish,omitempty"`
}

PermissionSet captures common CRUD permission tokens.

func BlockLibraryPermissions

func BlockLibraryPermissions() PermissionSet

BlockLibraryPermissions returns permissions for block library actions.

func BuilderContentTypePermissions

func BuilderContentTypePermissions() PermissionSet

BuilderContentTypePermissions returns permissions for content type builder actions.

func ContentTypePermissions

func ContentTypePermissions(slug string) PermissionSet

ContentTypePermissions returns the permission set for a specific content type slug.

func ResourcePermissions

func ResourcePermissions(resource string, includePublish bool) PermissionSet

ResourcePermissions creates a permission set for a resource.

func (PermissionSet) List

func (p PermissionSet) List() []string

List returns the non-empty permissions in the set.

type Permissioner

type Permissioner interface {
	HasPermission(permission string) bool
}

type Set

type Set map[string]struct{}

func NewSet

func NewSet(perms ...string) Set

func (Set) Allowed

func (s Set) Allowed(permission string) bool

type Strategy added in v0.23.1

type Strategy interface {
	Resolve(permission, envKey string) []string
}

Strategy resolves permission tokens for an environment scope.

var (
	EnvFirstStrategy Strategy = StrategyFunc(func(permission, envKey string) []string {
		if permission == "" {
			return nil
		}
		if envKey == "" {
			return []string{permission}
		}
		scoped := scopePermission(permission, envKey)
		if scoped == permission {
			return []string{permission}
		}
		return []string{scoped, permission}
	})
	GlobalFirstStrategy Strategy = StrategyFunc(func(permission, envKey string) []string {
		if permission == "" {
			return nil
		}
		if envKey == "" {
			return []string{permission}
		}
		scoped := scopePermission(permission, envKey)
		if scoped == permission {
			return []string{permission}
		}
		return []string{permission, scoped}
	})
)

type StrategyFunc added in v0.23.1

type StrategyFunc func(permission, envKey string) []string

StrategyFunc adapts a function into a Strategy.

func (StrategyFunc) Resolve added in v0.23.1

func (fn StrategyFunc) Resolve(permission, envKey string) []string

Jump to

Keyboard shortcuts

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