authority

package
v0.0.0-...-54cc154 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2021 License: MIT, MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TablePrefix string

Functions

This section is empty.

Types

type Authority

type Authority struct {
	DB *gorm.DB
}

Authority helps deal with permissions

func New

func New(opts Options) *Authority

New initates authority

func Resolve

func Resolve() *Authority

Resolve returns the initiated instance

func (*Authority) AssignPermissions

func (a *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 (a *Authority) AssignRole(userID string, 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 data base an error is returned if the user have already a role assigned to him an error is returned

func (*Authority) CheckPermission

func (a *Authority) CheckPermission(userID string, permName string) (bool, error)

CheckPermission checks if a permission is assigned to a user it accepts the user id as the first parameter the permission as the second parameter it returns an error if the user donesn't have a rols assigned it returns an error if the user's role doesn't have the permission assigned it returns an error if the permission is not present in the database

func (*Authority) CheckRole

func (a *Authority) CheckRole(userID string, 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 (a *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 (a *Authority) CreatePermission(permName string) 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 (a *Authority) CreateRole(roleName string) error

CreateRole stores a role in the database it accepts the role name. it returns an error incase of any

func (*Authority) DeletePermission

func (a *Authority) DeletePermission(permName string) error

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

func (*Authority) DeleteRole

func (a *Authority) DeleteRole(roleName string) error

DeleteRole deletes a given role if the role is assigned to a user it returns an error

func (*Authority) GetPermissions

func (a *Authority) GetPermissions() ([]string, error)

GetPermissions returns all stored permissions

func (*Authority) GetRoles

func (a *Authority) GetRoles() ([]string, error)

GetRoles returns all stored roles

func (*Authority) RevokePermission

func (a *Authority) RevokePermission(userID uint, permName string) error

RevokePermission revokes a permission from the user's assigned role it returns an error in case of any

func (*Authority) RevokeRole

func (a *Authority) RevokeRole(userID uint, roleName string) error

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

func (*Authority) RevokeRolePermission

func (a *Authority) RevokeRolePermission(roleName string, permName string) error

RevokeRolePermission revokes a permission from a given role it returns an error in case of any

type Options

type Options struct {
	TablesPrefix string
	DB           *gorm.DB
}

Options has the options for initating the package

type Permission

type Permission struct {
	ID   uint
	Name string
}

Permission represents the database model of permissions

func (Permission) TableName

func (p Permission) TableName() string

TableName sets the table name

type Role

type Role struct {
	ID   uint
	Name string
}

Role represents the database model of roles

func (Role) TableName

func (r Role) TableName() string

TableName sets the table name

type RolePermission

type RolePermission struct {
	ID           uint
	RoleID       uint
	PermissionID uint
}

RolePermission stores the relationship between roles and permissions

func (RolePermission) TableName

func (r RolePermission) TableName() string

TableName sets the table name

type UserRole

type UserRole struct {
	ID     uint
	UserID string `json:"user_id" gorm:"type:varchar(50);not null;ForeignKey:ID"`
	RoleID uint
}

UserRole represents the relationship between users and roles

func (UserRole) TableName

func (u UserRole) TableName() string

TableName sets the table name

Jump to

Keyboard shortcuts

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