Documentation ¶
Overview ¶
Package rbac provides role-based access control (RBAC) system
Index ¶
- Constants
- Variables
- func GetResName(resource any) string
- func GetResType(resource any) (res reflect.Type)
- func Included(base Role, testRole Role) bool
- func MatchName(pattern, name string) (ok bool, err error)
- func WithoutCustomCheck(obj any) error
- type Manager
- func (mng *Manager) ObjectByName(name string) any
- func (mng *Manager) ObjectPermissions(obj any, patterns ...string) []Permission
- func (mng *Manager) Permission(name string) Permission
- func (mng *Manager) Permissions(patterns ...string) []Permission
- func (mng *Manager) RegisterNewOwningPermissions(resType any, names []string, options ...Option) error
- func (mng *Manager) RegisterNewPermission(resType any, name string, options ...Option) error
- func (mng *Manager) RegisterNewPermissions(resType any, names []string, options ...Option) error
- func (mng *Manager) RegisterObject(objType, checkCallbac any) *Manager
- func (mng *Manager) RegisterPermission(perms ...Permission) *Manager
- func (mng *Manager) RegisterRole(ctx context.Context, roles ...Role) *Manager
- func (mng *Manager) Role(ctx context.Context, name string) Role
- func (mng *Manager) Roles(ctx context.Context, names ...string) []Role
- func (mng *Manager) RolesByFilter(ctx context.Context, filter RoleFilter) []Role
- type Option
- type Permission
- type ResourcePermission
- func (perm *ResourcePermission) CheckPermissions(ctx context.Context, resource any, patterns ...string) bool
- func (perm *ResourcePermission) CheckType(resource any) bool
- func (perm *ResourcePermission) CheckedPermissions(ctx context.Context, resource any, patterns ...string) Permission
- func (perm *ResourcePermission) ChildPermissions() []Permission
- func (perm *ResourcePermission) Ext() any
- func (perm *ResourcePermission) HasPermission(patterns ...string) bool
- func (perm *ResourcePermission) MatchPermissionPattern(patterns ...string) bool
- func (perm *ResourcePermission) Name() string
- func (perm *ResourcePermission) Permission(name string) Permission
- func (perm *ResourcePermission) Permissions(patterns ...string) []Permission
- func (perm *ResourcePermission) ResourceName() string
- func (perm *ResourcePermission) ResourceType() reflect.Type
- type Role
- type RoleAccessors
- type RoleFilter
- type RoleLoader
- type SimplePermission
- func (perm *SimplePermission) CheckPermissions(ctx context.Context, resource any, patterns ...string) bool
- func (perm *SimplePermission) CheckedPermissions(ctx context.Context, resource any, patterns ...string) Permission
- func (perm *SimplePermission) ChildPermissions() []Permission
- func (perm *SimplePermission) Description() string
- func (perm *SimplePermission) Ext() any
- func (perm *SimplePermission) HasPermission(patterns ...string) bool
- func (perm *SimplePermission) MatchPermissionPattern(patterns ...string) bool
- func (perm *SimplePermission) Name() string
- func (perm *SimplePermission) Permission(name string) Permission
- func (perm *SimplePermission) Permissions(patterns ...string) []Permission
Constants ¶
const ( OwnOwner = `owner` // The owner of the object (creator or user assigned as owner) OwnAccount = `account` // The account owner OwnAll = `all` // The system owner (can control all objects) *not recommended )
Variables ¶
var ( // ErrInvalidOption for this type ErrInvalidOption = errors.New(`invalid option`) // ErrInvalidOptionParam if param is not valid ErrInvalidOptionParam = errors.New(`invalid option param`) )
var ( // ErrInvalidCheckParams in case of empty permission check params ErrInvalidCheckParams = errors.New(`invalid check params`) // ErrInvalidResouceType if parameter is Nil ErrInvalidResouceType = errors.New(`invalid resource type`) )
var ( ErrEmptyPermissionName = errors.New(`empty permission name`) ErrInvalidPermissionName = errors.New(`invalid permission name`) ErrInvalidPattern = errors.New(`invalid pattern`) )
var ErrResourceTypeRequired = errors.New(`resource type required`)
Functions ¶
func Included ¶ added in v0.1.8
Included returns true if testRole is included in the base role or equal
func MatchName ¶ added in v0.1.5
MatchName permission pattern Example: `*` or `**` matches any string `test.*` matches `test.it`, `test.it.owner`, `test.it.admin `test.*.owner` matches `test.it.owner`, `test.object.owner` `test.*.*` matches `test.it.owner`, `test.object.owner` `test.*.?wner` matches `test.it.owner`, `test.object.owner `test.*.{owner|admin}` matches `test.it.owner`, `test.object.admin` `test.%r{[a-z]+}` matches `test.it.owner`, `test.object.admin` (regexp) `test.**` matches `test.it.owner`, `test.object.admin` (** must be at the end)
func WithoutCustomCheck ¶
WithoutCustomCheck remove custom check
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager of the roles and permissions
The manager is the main object of the system which contains all roles and permissions and provides methods to check permissions and roles for the object.
Default manager implements implies that all permissions will be defined in the code.
Default manager implements chain permission name type
Object permission name: `objectType.permissionName.owner|account|all` where objectType is the object type name, permissionName is the permission name and owner|account|all is the owner type
func NewManager ¶
func NewManager(roleAccessor RoleAccessors) *Manager
NewManager creates new manager
func NewManagerWithLoader ¶
func NewManagerWithLoader(roleLoader RoleLoader, lifetimeCache time.Duration) *Manager
NewManagerWithLoader creates new manager with role loader
func (*Manager) ObjectByName ¶ added in v0.1.3
ObjectByName returns object by name
func (*Manager) ObjectPermissions ¶
func (mng *Manager) ObjectPermissions(obj any, patterns ...string) []Permission
ObjectPermissions returns all or selected permissions for the object like .RBACResourceName() + `.` + pattern
func (*Manager) Permission ¶
func (mng *Manager) Permission(name string) Permission
AddRole to the manager
func (*Manager) Permissions ¶
func (mng *Manager) Permissions(patterns ...string) []Permission
Permissions returns all or selected permissions
func (*Manager) RegisterNewOwningPermissions ¶
func (mng *Manager) RegisterNewOwningPermissions(resType any, names []string, options ...Option) error
RegisterNewOwningPermissions modifies permissions for owning with extension of the name > name.owner, name.account and name.all
func (*Manager) RegisterNewPermission ¶
RegisterNewPermission in the system
func (*Manager) RegisterNewPermissions ¶
RegisterNewPermissions multiple related to the resource type
func (*Manager) RegisterObject ¶
RegisterObject for processing
func (*Manager) RegisterPermission ¶
func (mng *Manager) RegisterPermission(perms ...Permission) *Manager
RegisterPermission in the system
func (*Manager) RegisterRole ¶
Roles returns all or selected roles
func (*Manager) RolesByFilter ¶ added in v0.1.2
func (mng *Manager) RolesByFilter(ctx context.Context, filter RoleFilter) []Role
RolesByFilter returns roles by filter
type Option ¶
Option apply function to object
func WithCustomCheck ¶
WithCustomCheck function and additional data if need to use in checker Example:
callback := func(ctx context.Context, resource any, names ...string) bool { return ExtData(ctx).(*model.RoleContext).DebugMode } perm := NewResourcePermission(`view`, &model.User{}, WithCustomCheck(callback, &roleContext))
func WithDescription ¶ added in v0.1.8
WithDescription of the role or permission
func WithPermissions ¶
WithPermissions apply subpermission
type Permission ¶
type Permission interface { Name() string // Description of the permission Description() string // CheckPermissions to accept to resource CheckPermissions(ctx context.Context, resource any, patterns ...string) bool // CheckedPermission returns child permission for resource which has been checked as allowed CheckedPermissions(ctx context.Context, resource any, patterns ...string) Permission // ChildPermissions list returns list of child permissions ChildPermissions() []Permission // Permission returns permission by name Permission(name string) Permission // Permissions returns list of permissions by pattern Permissions(patterns ...string) []Permission // HasPermission returns true if permission has child permission HasPermission(patterns ...string) bool // MatchPermissionPattern returns true if permission matches any of the patterns MatchPermissionPattern(patterns ...string) bool // Ext returns additional user data Ext() any }
Permission object checker
type ResourcePermission ¶
type ResourcePermission struct { SimplePermission // contains filtered or unexported fields }
ResourcePermission implementation for some specific object type
func MustNewResourcePermission ¶
func MustNewResourcePermission(name string, resType any, options ...Option) *ResourcePermission
MustNewResourcePermission with name and resource type
func NewResourcePermission ¶
func NewResourcePermission(name string, resType any, options ...Option) (*ResourcePermission, error)
NewResourcePermission object with custom checker and base type
func (*ResourcePermission) CheckPermissions ¶
func (perm *ResourcePermission) CheckPermissions(ctx context.Context, resource any, patterns ...string) bool
CheckPermissions to accept to resource
func (*ResourcePermission) CheckType ¶
func (perm *ResourcePermission) CheckType(resource any) bool
CheckType of resource and target type
func (*ResourcePermission) CheckedPermissions ¶
func (perm *ResourcePermission) CheckedPermissions(ctx context.Context, resource any, patterns ...string) Permission
CheckedPermission returns child permission for resource which has been checked as allowed
func (*ResourcePermission) ChildPermissions ¶
func (perm *ResourcePermission) ChildPermissions() []Permission
ChildPermissions returns list of child permissions
func (*ResourcePermission) Ext ¶
func (perm *ResourcePermission) Ext() any
Ext returns additional user data
func (*ResourcePermission) HasPermission ¶
func (perm *ResourcePermission) HasPermission(patterns ...string) bool
HasPermission returns true if permission has permission
func (*ResourcePermission) MatchPermissionPattern ¶
func (perm *ResourcePermission) MatchPermissionPattern(patterns ...string) bool
MatchPermissionPattern returns true if permission matches any of the patterns
func (*ResourcePermission) Name ¶
func (perm *ResourcePermission) Name() string
Name returns permission name
func (*ResourcePermission) Permission ¶
func (perm *ResourcePermission) Permission(name string) Permission
Permission returns permission by name
func (*ResourcePermission) Permissions ¶
func (perm *ResourcePermission) Permissions(patterns ...string) []Permission
Permissions returns list of permissions by pattern
func (*ResourcePermission) ResourceName ¶
func (perm *ResourcePermission) ResourceName() string
ResourceName returns resource name
func (*ResourcePermission) ResourceType ¶
func (perm *ResourcePermission) ResourceType() reflect.Type
ResourceType returns resource type
type Role ¶
type Role interface { Permission // ChildRoles returns list of child roles ChildRoles() []Role // Role returns role by name Role(name string) Role // HasRole returns true if role has role HasRole(name string) bool }
Role base interface
func MustNewRole ¶
MustNewRole or produce panic
func NewDummyPermission ¶
NewDummyPermission permission with predefined check
type RoleAccessors ¶
type RoleAccessors interface { Role(ctx context.Context, name string) Role Roles(ctx context.Context, names ...string) []Role RolesByFilter(ctx context.Context, filter RoleFilter) []Role }
RoleAccessors interface for accessing roles
type RoleFilter ¶ added in v0.1.2
RoleLoader function for filling roles by custom rules
type RoleLoader ¶
RoleLoader interface for loading roles from the storage or other source
type SimplePermission ¶
type SimplePermission struct {
// contains filtered or unexported fields
}
SimplePermission implementation with simple functionality
func MustNewSimplePermission ¶
func MustNewSimplePermission(name string, options ...Option) *SimplePermission
MustNewSimplePermission with name and resource type
func NewSimplePermission ¶
func NewSimplePermission(name string, options ...Option) (*SimplePermission, error)
NewSimplePermission object with custom checker
func (*SimplePermission) CheckPermissions ¶
func (perm *SimplePermission) CheckPermissions(ctx context.Context, resource any, patterns ...string) bool
CheckPermissions to accept to resource
func (*SimplePermission) CheckedPermissions ¶
func (perm *SimplePermission) CheckedPermissions(ctx context.Context, resource any, patterns ...string) Permission
CheckedPermission returns child permission for resource which has been checked as allowed
func (*SimplePermission) ChildPermissions ¶
func (perm *SimplePermission) ChildPermissions() []Permission
ChildPermissions returns list of child permissions
func (*SimplePermission) Description ¶ added in v0.1.8
func (perm *SimplePermission) Description() string
Description of the permission
func (*SimplePermission) Ext ¶
func (perm *SimplePermission) Ext() any
Ext returns additional user data
func (*SimplePermission) HasPermission ¶
func (perm *SimplePermission) HasPermission(patterns ...string) bool
HasPermission returns true if permission has permission
func (*SimplePermission) MatchPermissionPattern ¶
func (perm *SimplePermission) MatchPermissionPattern(patterns ...string) bool
MatchPermissionPattern returns true if permission matches any of the patterns
func (*SimplePermission) Permission ¶
func (perm *SimplePermission) Permission(name string) Permission
Permission returns permission by name
func (*SimplePermission) Permissions ¶
func (perm *SimplePermission) Permissions(patterns ...string) []Permission
Permissions returns list of permissions by pattern