RezinaLMS-BackEnd/internal/domain/rbac.go
2026-07-03 05:07:48 -07:00

77 lines
2.0 KiB
Go

package domain
import (
"strings"
"time"
)
// NormalizeRBACRoleName canonicalizes custom RBAC role names (uppercase, underscores).
func NormalizeRBACRoleName(name string) string {
s := strings.TrimSpace(name)
s = strings.ReplaceAll(s, " ", "_")
s = strings.ReplaceAll(s, "-", "_")
return strings.ToUpper(s)
}
type Permission struct {
ID int64 `json:"id"`
Key string `json:"key"`
Name string `json:"name"`
Description string `json:"description"`
GroupName string `json:"group_name"`
CreatedAt time.Time `json:"created_at"`
}
type RoleRecord struct {
ID int64 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
IsSystem bool `json:"is_system"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
type RoleWithPermissions struct {
RoleRecord
Permissions []Permission `json:"permissions"`
}
type RoleListFilter struct {
Query string
IsSystem *bool
Page int64
PageSize int64
}
type CreateRoleReq struct {
Name string `json:"name" validate:"required,min=2,max=100"`
Description string `json:"description"`
}
type UpdateRoleReq struct {
Name string `json:"name" validate:"required,min=2,max=100"`
Description string `json:"description"`
}
type SetRolePermissionsReq struct {
PermissionIDs []int64 `json:"permission_ids" validate:"required"`
}
type PermissionSeed struct {
Key string
Name string
Description string
GroupName string
}
// Activity log constants for RBAC
const (
ActionRoleCreated ActivityAction = "ROLE_CREATED"
ActionRoleUpdated ActivityAction = "ROLE_UPDATED"
ActionRoleDeleted ActivityAction = "ROLE_DELETED"
ActionRolePermissionsSet ActivityAction = "ROLE_PERMISSIONS_SET"
ActionPermissionsSynced ActivityAction = "PERMISSIONS_SYNCED"
ResourceRole ResourceType = "ROLE"
ResourcePermission ResourceType = "PERMISSION"
)