Documentation
¶
Index ¶
- Variables
- type AuditLog
- type BulkEmployeePermission
- type BulkPermissionResult
- type Config
- type Department
- type EmployeeRole
- type Permission
- type RBAC
- func (r *RBAC) AddScopedPermission(roleID, permID uint, deptID, targetEmpID *uint) error
- func (r *RBAC) AssignRole(empID, roleID uint) error
- func (r *RBAC) BulkAssignRoles(assignments map[uint][]uint) error
- func (r *RBAC) BulkRemoveRoles(removals map[uint][]uint) error
- func (r *RBAC) CacheBulkPermissions(permissions map[string][]uint) error
- func (r *RBAC) CheckBulkPermissions(checks []BulkEmployeePermission) []BulkPermissionResult
- func (r *RBAC) CheckPermission(empID uint, permName string, deptID, targetEmpID *uint) error
- func (r *RBAC) ClearAllCache() error
- func (r *RBAC) Close()
- func (r *RBAC) CreateDepartment(name string) (*Department, error)
- func (r *RBAC) CreatePermission(name string, isGlobal bool) (*Permission, error)
- func (r *RBAC) CreateRole(name string, deptID uint, parentRoleID *uint, isGlobal bool) (*Role, error)
- func (r *RBAC) DeleteDepartment(id uint) error
- func (r *RBAC) DeleteEmployeeRole(empID, roleID uint) error
- func (r *RBAC) DeletePermission(id uint) error
- func (r *RBAC) DeleteRole(id uint) error
- func (r *RBAC) DeleteScopedPermission(id uint) error
- func (r *RBAC) GetAuditLog(id uint) (*AuditLog, error)
- func (r *RBAC) GetCacheStats() map[string]interface{}
- func (r *RBAC) GetContext() context.Context
- func (r *RBAC) GetDepartment(id uint) (*Department, error)
- func (r *RBAC) GetEmployeePermissionsBulk(employeeIDs []uint) map[uint][]string
- func (r *RBAC) GetEmployeeRole(empID, roleID uint) (*EmployeeRole, error)
- func (r *RBAC) GetPermission(id uint) (*Permission, error)
- func (r *RBAC) GetRole(id uint) (*Role, error)
- func (r *RBAC) GetScopedPermission(id uint) (*ScopedPermission, error)
- func (r *RBAC) GetSubordinateIDs(empID uint) ([]uint, error)
- func (r *RBAC) InvalidateBulkCache(employeeIDs []uint) error
- func (r *RBAC) ListAuditLogs(actorEmpID, targetID *uint) ([]AuditLog, error)
- func (r *RBAC) ListDepartments() ([]Department, error)
- func (r *RBAC) ListEmployeeRoles(empID uint) ([]EmployeeRole, error)
- func (r *RBAC) ListPermissions() ([]Permission, error)
- func (r *RBAC) ListRoles(deptID *uint) ([]Role, error)
- func (r *RBAC) ListScopedPermissions(roleID *uint) ([]ScopedPermission, error)
- func (r *RBAC) SetContext(ctx context.Context)
- func (r *RBAC) UpdateDepartment(id uint, name string) (*Department, error)
- func (r *RBAC) UpdateEmployeeRole(empID, oldRoleID, newRoleID uint) error
- func (r *RBAC) UpdatePermission(id uint, name string, isGlobal bool) (*Permission, error)
- func (r *RBAC) UpdateRole(id uint, name string, deptID uint, parentRoleID *uint, isGlobal bool) (*Role, error)
- func (r *RBAC) UpdateScopedPermission(id, roleID, permID uint, deptID, targetEmpID *uint) error
- func (r *RBAC) WarmCache() error
- type Role
- type ScopedPermission
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidInput = errors.New("invalid input") ErrNotFound = errors.New("resource not found") ErrPermissionDenied = errors.New("permission denied") )
Custom errors
Functions ¶
This section is empty.
Types ¶
type AuditLog ¶
type AuditLog struct { ID uint `gorm:"primaryKey"` ActorEmpID uint `gorm:"index;not null"` Action string `gorm:"not null"` TargetType string `gorm:"not null"` TargetID uint `gorm:"index;not null"` Details string CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` }
AuditLog tracks permission/role-related events.
type BulkEmployeePermission ¶
type BulkEmployeePermission struct { EmployeeID uint Permission string DepartmentID *uint TargetEmployeeID *uint }
BulkEmployeePermission represents employee-permission pairs for bulk operations
type BulkPermissionResult ¶
BulkPermissionResult represents the result of bulk permission checks
type Config ¶
type Config struct { DB *gorm.DB Redis *redis.Client // Optional; nil disables caching AppName string // For Redis key prefixing }
Config holds configuration for initializing the RBAC system.
type Department ¶
type Department struct { ID uint `gorm:"primaryKey"` Name string `gorm:"unique;not null"` CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` }
Department represents a logical group (e.g., Sales, HR).
type EmployeeRole ¶
type EmployeeRole struct { EmployeeID uint `gorm:"primaryKey;autoIncrement:false"` RoleID uint `gorm:"primaryKey;autoIncrement:false"` CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` }
EmployeeRole maps an employee to a role.
type Permission ¶
type Permission struct { ID uint `gorm:"primaryKey"` Name string `gorm:"unique;not null"` IsGlobal bool `gorm:"default:false"` CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` }
Permission represents a named access action.
type RBAC ¶
type RBAC struct {
// contains filtered or unexported fields
}
RBAC is the main struct for the RBAC system.
func (*RBAC) AddScopedPermission ¶
AddScopedPermission grants a permission to a role with optional scoping.
func (*RBAC) AssignRole ¶
AssignRole creates a new employee-role mapping.
func (*RBAC) BulkAssignRoles ¶
BulkAssignRoles assigns multiple roles to multiple employees efficiently
func (*RBAC) BulkRemoveRoles ¶
BulkRemoveRoles removes multiple roles from multiple employees efficiently
func (*RBAC) CacheBulkPermissions ¶
CacheBulkPermissions caches permission results for multiple employees
func (*RBAC) CheckBulkPermissions ¶
func (r *RBAC) CheckBulkPermissions(checks []BulkEmployeePermission) []BulkPermissionResult
CheckBulkPermissions checks multiple permissions for multiple employees efficiently
func (*RBAC) CheckPermission ¶
CheckPermission verifies if an employee has a specific permission.
func (*RBAC) ClearAllCache ¶
ClearAllCache clears all cache entries
func (*RBAC) CreateDepartment ¶
func (r *RBAC) CreateDepartment(name string) (*Department, error)
CreateDepartment creates a new department.
func (*RBAC) CreatePermission ¶
func (r *RBAC) CreatePermission(name string, isGlobal bool) (*Permission, error)
CreatePermission creates a new permission.
func (*RBAC) CreateRole ¶
func (r *RBAC) CreateRole(name string, deptID uint, parentRoleID *uint, isGlobal bool) (*Role, error)
CreateRole creates a new role in a department with optional parent role.
func (*RBAC) DeleteDepartment ¶
DeleteDepartment soft-deletes a department by ID.
func (*RBAC) DeleteEmployeeRole ¶
DeleteEmployeeRole soft-deletes an employee-role mapping.
func (*RBAC) DeletePermission ¶
DeletePermission soft-deletes a permission by ID.
func (*RBAC) DeleteRole ¶
DeleteRole soft-deletes a role by ID.
func (*RBAC) DeleteScopedPermission ¶
DeleteScopedPermission soft-deletes a scoped permission by ID.
func (*RBAC) GetAuditLog ¶
GetAuditLog retrieves an audit log by ID.
func (*RBAC) GetCacheStats ¶
GetCacheStats returns cache statistics
func (*RBAC) GetContext ¶
GetContext returns the current context
func (*RBAC) GetDepartment ¶
func (r *RBAC) GetDepartment(id uint) (*Department, error)
GetDepartment retrieves a department by ID.
func (*RBAC) GetEmployeePermissionsBulk ¶
GetEmployeePermissionsBulk efficiently retrieves permissions for multiple employees
func (*RBAC) GetEmployeeRole ¶
func (r *RBAC) GetEmployeeRole(empID, roleID uint) (*EmployeeRole, error)
GetEmployeeRole retrieves an employee-role mapping.
func (*RBAC) GetPermission ¶
func (r *RBAC) GetPermission(id uint) (*Permission, error)
GetPermission retrieves a permission by ID.
func (*RBAC) GetScopedPermission ¶
func (r *RBAC) GetScopedPermission(id uint) (*ScopedPermission, error)
GetScopedPermission retrieves a scoped permission by ID.
func (*RBAC) GetSubordinateIDs ¶
GetSubordinateIDs fetches IDs of employees whose roles are descendants of the caller's roles.
func (*RBAC) InvalidateBulkCache ¶
InvalidateBulkCache invalidates cache for multiple employees
func (*RBAC) ListAuditLogs ¶
ListAuditLogs retrieves audit logs, optionally filtered by actor or target.
func (*RBAC) ListDepartments ¶
func (r *RBAC) ListDepartments() ([]Department, error)
ListDepartments retrieves all departments.
func (*RBAC) ListEmployeeRoles ¶
func (r *RBAC) ListEmployeeRoles(empID uint) ([]EmployeeRole, error)
ListEmployeeRoles retrieves all roles for an employee.
func (*RBAC) ListPermissions ¶
func (r *RBAC) ListPermissions() ([]Permission, error)
ListPermissions retrieves all permissions.
func (*RBAC) ListScopedPermissions ¶
func (r *RBAC) ListScopedPermissions(roleID *uint) ([]ScopedPermission, error)
ListScopedPermissions retrieves all scoped permissions, optionally filtered by role.
func (*RBAC) SetContext ¶
SetContext allows setting a custom context
func (*RBAC) UpdateDepartment ¶
func (r *RBAC) UpdateDepartment(id uint, name string) (*Department, error)
UpdateDepartment updates a department's name.
func (*RBAC) UpdateEmployeeRole ¶
UpdateEmployeeRole updates an employee-role mapping (reassigns role).
func (*RBAC) UpdatePermission ¶
UpdatePermission updates a permission's details.
func (*RBAC) UpdateRole ¶
func (r *RBAC) UpdateRole(id uint, name string, deptID uint, parentRoleID *uint, isGlobal bool) (*Role, error)
UpdateRole updates a role's details.
func (*RBAC) UpdateScopedPermission ¶
UpdateScopedPermission updates a scoped permission's details.
type Role ¶
type Role struct { ID uint `gorm:"primaryKey"` Name string `gorm:"not null"` DepartmentID uint `gorm:"not null;index"` ParentRoleID *uint `gorm:"index"` // For role inheritance IsGlobal bool `gorm:"default:false"` CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` }
Role represents a hierarchical position within a department.
type ScopedPermission ¶
type ScopedPermission struct { ID uint `gorm:"primaryKey"` RoleID uint `gorm:"index;not null"` PermissionID uint `gorm:"index;not null"` DepartmentID *uint `gorm:"index"` // Optional department scope EmployeeID *uint `gorm:"index"` // Optional employee scope CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"` }
ScopedPermission grants a permission to a role with optional scoping.