Documentation ¶
Overview ¶
Package utils: Modified version of https://github.com/harranali/authority. Special thanks to @harranali.
Index ¶
- Variables
- func Contains(haystack []string, needle string) bool
- func ParseAndValidate(c *fiber.Ctx, body interface{}) []errorResponse
- func ParseBody(c *fiber.Ctx, body interface{}) error
- func ValidateStruct(input interface{}) []errorResponse
- type Authority
- func (Authority) AssignPermissions(roleName string, permNames ...string) error
- func (Authority) AssignRole(userID int, roleName string) error
- func (Authority) CheckPermission(userID int, permName string) (bool, error)
- func (Authority) CheckRole(userID int, roleName string) (bool, error)
- func (Authority) CheckRolePermission(roleName string, permName string) (bool, error)
- func (Authority) CreatePermission(name string) (*ent.Permission, error)
- func (Authority) CreateRole(name string) (*ent.Role, error)
- func (Authority) DeletePermission(name string) error
- func (Authority) DeleteRole(name string) error
- func (Authority) GetPermissions() ([]*ent.Permission, error)
- func (Authority) GetRoles() ([]*ent.Role, error)
- func (Authority) GetUserRoles(userID int) ([]*ent.Role, error)
- func (Authority) RevokePermission(roleName string, permNames ...string) error
- func (Authority) RevokeRole(userID int, roleName string) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrRoleCreatedAlready = errors.New("authority: the role has created already") ErrPermCreatedAlready = errors.New("authority: the permission has created already") ErrRoleNotFound = errors.New("authority: the role not found") ErrUserNotFound = errors.New("authority: the user not found") ErrPermNotFound = errors.New("authority: the permission(s) not found") )
Public errors for Authority
Functions ¶
func ParseAndValidate ¶
func ParseAndValidate(c *fiber.Ctx, body interface{}) []errorResponse
func ValidateStruct ¶
func ValidateStruct(input interface{}) []errorResponse
Types ¶
type Authority ¶
type Authority struct{}
Authority helps deal with permissions
func (Authority) AssignPermissions ¶
AssignPermissions assigns a group of permissions to a given role it accepts in the first parameter the role name, it returns an error if there is not matching record of the role name in the database. the second parameter is a slice of strings which represents a group of permissions to be assigned to the role if any of these permissions doesn't have a matching record in the database the operations stops, changes reverted and error is returned in case of success nothing is returned
func (Authority) AssignRole ¶
AssignRole assigns a given role to a user the first parameter is the user id, the second parameter is the role name if the role name doesn't have a matching record in the database an error is returned if the user have already a role assigned to him an error is returned
func (Authority) CheckPermission ¶
CheckPermission checks if a permission is assigned to the role that's assigned to the user. it accepts the user id as the first parameter the permission as the second parameter it returns an error if the permission is not present in the database
func (Authority) CheckRole ¶
CheckRole checks if a role is assigned to a user it accepts the user id as the first parameter the role as the second parameter it returns an error if the role is not present in database
func (Authority) CheckRolePermission ¶
CheckRolePermission checks if a role has the permission assigned it accepts the role as the first parameter it accepts the permission as the second parameter it returns an error if the role is not present in database it returns an error if the permission is not present in database
func (Authority) CreatePermission ¶
func (Authority) CreatePermission(name string) (*ent.Permission, error)
CreatePermission stores a permission in the database it accepts the permission name. it returns an error in case of any
func (Authority) CreateRole ¶
CreateRole stores a role in the databaseADV it accepts the role name. it returns an error in case of any
func (Authority) DeletePermission ¶
DeletePermission deletes a given permission if the permission is assigned to a role it returns an error
func (Authority) DeleteRole ¶
DeleteRole deletes a given role if the role is assigned to a user it returns an error
func (Authority) GetPermissions ¶
func (Authority) GetPermissions() ([]*ent.Permission, error)
GetPermissions returns all stored permissions
func (Authority) GetUserRoles ¶
GetUserRoles returns all user assigned roles
func (Authority) RevokePermission ¶
RevokePermission revokes a permission from the role it returns an error in case of any