can

package module
v0.0.0-...-bb6dc8f Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 4 Imported by: 0

README

Can(Can)

This package is a simple implementation of a can/cannot method for authorization.

if can.Can(someAccount, "update", someResource) {
  // do something...
} else {
  // show an error maybe?
}

Or, if you only want to see if something is not allowed:

if can.Not(someAccount, "update", someResource) {
  // do something...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotAuthorized = errors.New("not authorized")
	ErrInvalidAction = errors.New("invalid action")
)
View Source
var (
	PolicyRegistry = make(map[string]Policy[any])
)

PolicyRegistry is a map of policy names to policies. Where the name is the type associated withthe policy.

Functions

func AddPolicy

func AddPolicy(p Policy[any])

AddPolicy adds a policy to the PolicyRegistry.

func Can

func Can[K any](actor K, action string, resources ...any) error

func Not

func Not[K any](actor K, action string, resources ...any) bool

func PolicyExists

func PolicyExists(k any) bool

PolicyExists returns true if a policy exists in the PolicyRegistry.

func RemovePolicy

func RemovePolicy(k any)

RemovePolicy removes a policy from the PolicyRegistry.

Types

type Canner

type Canner[K any] interface {
	Can(actor K, action string) error
}

Canner allows a resource to define if an account can perform a given action.

type Policy

type Policy[K any] map[string]func(K) error

Policy is a map of actions to functions that return an error if the action cannot be performed for the given account.

func (Policy[K]) Can

func (p Policy[K]) Can(actor K, action string) error

Can implements the Canner interface.

func (Policy[K]) Verbs

func (p Policy[K]) Verbs() []string

Verbs implements the Verber interface.

type Verber

type Verber interface {
	Verbs() []string
}

Verber is used to allow a resource to define the actions it supports.

Jump to

Keyboard shortcuts

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