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 WithoutCustomCheck(obj any) error
- type Manager
- 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
- 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 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) 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`) )
var ErrResourceTypeRequired = errors.New(`resource type required`)
Functions ¶
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) 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
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 WithPermissions ¶
WithPermissions apply subpermission
type Permission ¶
type Permission interface { Name() 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 { Roles(ctx context.Context, names ...string) []Role Role(ctx context.Context, name string) Role }
RoleAccessors interface for accessing roles
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) 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