Documentation
¶
Index ¶
- Variables
- func AllGranted(rbac *RBAC, roles []string, permission Permission, assert AssertionFunc) (res bool)
- func AnyGranted(rbac *RBAC, roles []string, permission Permission, assert AssertionFunc) (res bool)
- func InheritanceCircle(rbac *RBAC) (err error)
- func Walk(rbac *RBAC, h WalkHandler) (err error)
- type AssertionFunc
- type DeepPermission
- type Permission
- type Permissions
- type RBAC
- func (rbac *RBAC) Add(r Role) (err error)
- func (rbac *RBAC) GetParents(id string) ([]string, error)
- func (rbac *RBAC) GetRole(id string) (r Role, parents []string, err error)
- func (rbac *RBAC) GetRoles() []Role
- func (rbac *RBAC) IsGranted(id string, p Permission, assert AssertionFunc) (rslt bool)
- func (rbac *RBAC) Permissions(id string) Permissions
- func (rbac *RBAC) Remove(id string) (err error)
- func (rbac *RBAC) RemoveParent(id string, parent string) error
- func (rbac *RBAC) SetParent(id string, parent string) error
- func (rbac *RBAC) SetParents(id string, parents []string) error
- type Role
- type Roles
- type SimplePermission
- type SimpleRole
- type WalkHandler
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRoleNotExist occurred if a role cann't be found ErrRoleNotExist = errors.New("role does not exist") // ErrRoleExist occurred if a role shouldn't be found ErrRoleExist = errors.New("role has already existed") )
var ( // ErrFoundCircle throw error about looping into roles ErrFoundCircle = fmt.Errorf(`found circle`) )
Functions ¶
func AllGranted ¶
func AllGranted(rbac *RBAC, roles []string, permission Permission, assert AssertionFunc) (res bool)
AllGranted checks if all roles have the permission.
func AnyGranted ¶
func AnyGranted(rbac *RBAC, roles []string, permission Permission, assert AssertionFunc) (res bool)
AnyGranted checks if any role has the permission.
func InheritanceCircle ¶
InheritanceCircle returns an error when detecting any circle inheritance.
Types ¶
type AssertionFunc ¶
type AssertionFunc func(*RBAC, string, Permission) bool
AssertionFunc supplies more fine-grained permission controls.
type DeepPermission ¶
DeepPermission firstly checks the Id of permission. If the Id is matched, it can be considered having the permission. Otherwise, it checks every layers of permission. A role which has an upper layer granted, will be granted sub-layers permissions.
func (*DeepPermission) ID ¶
func (p *DeepPermission) ID() string
ID returns the identity of permission
func (*DeepPermission) Match ¶
func (p *DeepPermission) Match(a Permission) bool
Match another permission
type Permission ¶
type Permission interface {
ID() string
Match(Permission) bool
}
Permission exports `Id` and `Match`
func NewDeepPermission ¶
func NewDeepPermission(id string) Permission
NewDeepPermission returns an instance of layered permission with `id`
func NewPermission ¶
func NewPermission(id string) Permission
NewPermission returns a Permission instance with `id`
type RBAC ¶
type RBAC struct {
// contains filtered or unexported fields
}
RBAC object, in most cases it should be used as a singleton.
func (*RBAC) GetParents ¶
GetParents return `parents` of the role `id`. If the role is not existing, an error will be returned. Or the role doesn't have any parents, a nil slice will be returned.
func (*RBAC) IsGranted ¶
func (rbac *RBAC) IsGranted(id string, p Permission, assert AssertionFunc) (rslt bool)
IsGranted tests if the role `id` has Permission `p` with the condition `assert`.
func (*RBAC) Permissions ¶
func (rbac *RBAC) Permissions(id string) Permissions
Permissions get list of all permissions
func (*RBAC) RemoveParent ¶
RemoveParent unbind the `parent` with the role `id`. If the role or the parent is not existing, an error will be returned.
type Role ¶
type Role interface {
ID() string
Permit(Permission) bool
Permissions() []Permission
}
Role is an interface. You should implement this interface for your own role structures.
type SimplePermission ¶
type SimplePermission struct {
IDStr string
}
SimplePermission only checks if the Ids are fully matching.
func (*SimplePermission) ID ¶
func (p *SimplePermission) ID() string
ID returns the identity of permission
func (*SimplePermission) Match ¶
func (p *SimplePermission) Match(a Permission) bool
Match another permission
type SimpleRole ¶
type SimpleRole struct {
sync.RWMutex
// IDStr is the identity of role
IDStr string `json:"id"`
// contains filtered or unexported fields
}
SimpleRole is the default role implement. You can combine this struct into your own Role implement.
func (*SimpleRole) Assign ¶
func (role *SimpleRole) Assign(p Permission) *SimpleRole
Assign a permission to the role.
func (*SimpleRole) Permissions ¶
func (role *SimpleRole) Permissions() []Permission
Permissions returns all permissions into a slice.
func (*SimpleRole) Permit ¶
func (role *SimpleRole) Permit(p Permission) (res bool)
Permit returns true if the role has specific permission.
func (*SimpleRole) Revoke ¶
func (role *SimpleRole) Revoke(p Permission) error
Revoke the specific permission.
type WalkHandler ¶
WalkHandler is a function defined by user to handle role