60 lines
2.2 KiB
Go
60 lines
2.2 KiB
Go
|
|
package model
|
||
|
|
|
||
|
|
import "time"
|
||
|
|
|
||
|
|
// Role 角色模型
|
||
|
|
// RBAC核心: 用户 -> 角色 -> 权限
|
||
|
|
type Role struct {
|
||
|
|
ID uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
||
|
|
Name string `json:"name" gorm:"uniqueIndex;size:64;not null"` // 角色标识: admin/editor/viewer
|
||
|
|
DisplayName string `json:"display_name" gorm:"size:64;not null"` // 显示名称: 管理员/编辑者/查看者
|
||
|
|
Description string `json:"description" gorm:"size:256"` // 角色描述
|
||
|
|
IsSystem int8 `json:"is_system" gorm:"default:0"` // 是否系统内置角色(不可删除)
|
||
|
|
Status int8 `json:"status" gorm:"default:1"` // 1=启用 0=禁用
|
||
|
|
CreatedAt time.Time `json:"created_at"`
|
||
|
|
UpdatedAt time.Time `json:"updated_at"`
|
||
|
|
|
||
|
|
// 关联
|
||
|
|
Permissions []Permission `json:"permissions,omitempty" gorm:"many2many:fs_role_permission;"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (Role) TableName() string {
|
||
|
|
return "fs_role"
|
||
|
|
}
|
||
|
|
|
||
|
|
// Permission 权限模型
|
||
|
|
// 定义系统中所有可控制的操作权限
|
||
|
|
type Permission struct {
|
||
|
|
ID uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
||
|
|
Code string `json:"code" gorm:"uniqueIndex;size:64;not null"` // 权限标识: file:upload, file:delete, user:manage
|
||
|
|
Name string `json:"name" gorm:"size:64;not null"` // 显示名称: 上传文件, 删除文件, 用户管理
|
||
|
|
Resource string `json:"resource" gorm:"size:32;not null"` // 资源类型: file, folder, share, user, system
|
||
|
|
Action string `json:"action" gorm:"size:32;not null"` // 操作类型: create, read, update, delete, manage
|
||
|
|
Description string `json:"description" gorm:"size:256"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (Permission) TableName() string {
|
||
|
|
return "fs_permission"
|
||
|
|
}
|
||
|
|
|
||
|
|
// RolePermission 角色-权限关联表
|
||
|
|
type RolePermission struct {
|
||
|
|
RoleID uint64 `json:"role_id" gorm:"primaryKey"`
|
||
|
|
PermissionID uint64 `json:"permission_id" gorm:"primaryKey"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (RolePermission) TableName() string {
|
||
|
|
return "fs_role_permission"
|
||
|
|
}
|
||
|
|
|
||
|
|
// UserRole 用户-角色关联表
|
||
|
|
// 一个用户可以有多个角色, 权限取并集
|
||
|
|
type UserRole struct {
|
||
|
|
UserID uint64 `json:"user_id" gorm:"primaryKey"`
|
||
|
|
RoleID uint64 `json:"role_id" gorm:"primaryKey"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (UserRole) TableName() string {
|
||
|
|
return "fs_user_role"
|
||
|
|
}
|