utils

package
v0.0.0-...-7fa7ca5 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package utils: Modified version of https://github.com/harranali/authority. Special thanks to @harranali.

Index

Constants

This section is empty.

Variables

View Source
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 Contains

func Contains(haystack []string, needle string) bool

func ParseAndValidate

func ParseAndValidate(c *fiber.Ctx, body interface{}) []errorResponse

func ParseBody

func ParseBody(c *fiber.Ctx, body interface{}) error

func ValidateStruct

func ValidateStruct(input interface{}) []errorResponse

Types

type Authority

type Authority struct{}

Authority helps deal with permissions

func (Authority) AssignPermissions

func (Authority) AssignPermissions(roleName string, permNames ...string) error

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

func (Authority) AssignRole(userID int, roleName string) error

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

func (Authority) CheckPermission(userID int, permName string) (bool, error)

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

func (Authority) CheckRole(userID int, roleName string) (bool, error)

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

func (Authority) CheckRolePermission(roleName string, permName string) (bool, error)

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

func (Authority) CreateRole(name string) (*ent.Role, error)

CreateRole stores a role in the databaseADV it accepts the role name. it returns an error in case of any

func (Authority) DeletePermission

func (Authority) DeletePermission(name string) error

DeletePermission deletes a given permission if the permission is assigned to a role it returns an error

func (Authority) DeleteRole

func (Authority) DeleteRole(name string) error

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) GetRoles

func (Authority) GetRoles() ([]*ent.Role, error)

GetRoles returns all stored roles

func (Authority) GetUserRoles

func (Authority) GetUserRoles(userID int) ([]*ent.Role, error)

GetUserRoles returns all user assigned roles

func (Authority) RevokePermission

func (Authority) RevokePermission(roleName string, permNames ...string) error

RevokePermission revokes a permission from the role it returns an error in case of any

func (Authority) RevokeRole

func (Authority) RevokeRole(userID int, roleName string) error

RevokeRole revokes a user's role it returns an error in case of any

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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