feat(auth): 完成租户用户角色资源核心模块
This commit is contained in:
60
backend/internal/models/resource.go
Normal file
60
backend/internal/models/resource.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Resource 资源模型(系统所有可访问的资源)
|
||||
type Resource struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
TenantID uint `gorm:"not null;index:idx_tenant_resource" json:"tenant_id"`
|
||||
Name string `gorm:"size:100;not null" json:"name"` // 资源名称
|
||||
DisplayName string `gorm:"size:200" json:"display_name"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
|
||||
// 资源标识
|
||||
Code string `gorm:"size:100;not null;uniqueIndex:idx_tenant_resource_code" json:"code"` // 资源代码
|
||||
Type string `gorm:"size:50;not null" json:"type"` // api, page, button, data 等
|
||||
Group string `gorm:"size:100" json:"group"` // 所属分组
|
||||
|
||||
// 权限配置
|
||||
Actions []string `gorm:"type:jsonb" json:"actions"` // 允许的操作:create, read, update, delete, export 等
|
||||
Path string `gorm:"size:500" json:"path"` // 资源路径或 API 端点
|
||||
ParentID *uint `gorm:"index" json:"parent_id,omitempty"` // 父资源 ID
|
||||
|
||||
// 状态
|
||||
Status string `gorm:"size:20;default:'enabled'" json:"status"` // enabled, disabled
|
||||
IsSystem bool `gorm:"default:false" json:"is_system"` // 是否系统资源
|
||||
|
||||
// 资源层级
|
||||
Level int `gorm:"default:0" json:"level"` // 层级深度
|
||||
SortOrder int `gorm:"default:0" json:"sort_order"` // 排序
|
||||
|
||||
// 时间戳
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `gorm:"index" json:"deleted_at,omitempty"`
|
||||
|
||||
// 关联
|
||||
Tenant Tenant `gorm:"foreignKey:TenantID" json:"tenant,omitempty"`
|
||||
Roles []Role `gorm:"many2many:role_resources;" json:"roles,omitempty"`
|
||||
Parent *Resource `gorm:"foreignKey:ParentID" json:"parent,omitempty"`
|
||||
Children []Resource `gorm:"foreignKey:ParentID" json:"children,omitempty"`
|
||||
}
|
||||
|
||||
// Permission 权限检查辅助结构
|
||||
type Permission struct {
|
||||
ResourceCode string `json:"resource_code"`
|
||||
Action string `json:"action"` // create, read, update, delete, manage 等
|
||||
TenantID uint `json:"tenant_id,omitempty"`
|
||||
UserID uint `json:"user_id,omitempty"`
|
||||
}
|
||||
|
||||
// HasPermission 检查用户是否有权限(需要在 handler 中实现)
|
||||
func HasPermission(userID, tenantID uint, resourceCode, action string) bool {
|
||||
// TODO: 实现权限检查逻辑
|
||||
// 1. 查询用户角色
|
||||
// 2. 查询角色资源
|
||||
// 3. 检查资源是否包含该操作
|
||||
return false
|
||||
}
|
||||
38
backend/internal/models/role.go
Normal file
38
backend/internal/models/role.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Role 角色模型(基于租户)
|
||||
type Role struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
TenantID uint `gorm:"not null;index:idx_tenant_role" json:"tenant_id"`
|
||||
Name string `gorm:"size:50;not null;uniqueIndex:idx_tenant_role_name" json:"name"` // admin, manager, agent, viewer 等
|
||||
DisplayName string `gorm:"size:100" json:"display_name"` // 显示名称
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
|
||||
// 权限配置
|
||||
Code string `gorm:"size:50;uniqueIndex" json:"code"` // 角色代码
|
||||
Level int `gorm:"default:1" json:"level"` // 权限等级 1-100
|
||||
Permissions JSONMap `gorm:"type:jsonb" json:"permissions"` // 权限列表
|
||||
|
||||
// 状态
|
||||
IsGlobal bool `gorm:"default:false" json:"is_global"` // 是否全局角色
|
||||
Status string `gorm:"size:20;default:'active'" json:"status"` // active, inactive
|
||||
|
||||
// 资源配置
|
||||
Resources []Resource `gorm:"many2many:role_resources;" json:"resources,omitempty"`
|
||||
|
||||
// 时间戳
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `gorm:"index" json:"deleted_at,omitempty"`
|
||||
|
||||
// 关联
|
||||
Tenant Tenant `gorm:"foreignKey:TenantID" json:"tenant,omitempty"`
|
||||
Users []User `gorm:"foreignKey:RoleID" json:"users,omitempty"`
|
||||
}
|
||||
|
||||
// RoleID 添加到 User 模型
|
||||
// 需要在 User 模型中添加 RoleID 字段
|
||||
@@ -19,7 +19,9 @@ type User struct {
|
||||
Bio string `gorm:"type:text" json:"bio"`
|
||||
|
||||
// 角色和权限
|
||||
Role string `gorm:"size:20;default:'user'" json:"role"` // super_admin, admin, agent, user
|
||||
Role string `gorm:"size:20;default:'user'" json:"role"` // super_admin, admin, agent, user (legacy)
|
||||
RoleID *uint `json:"role_id,omitempty"` // 关联的角色 ID (新的 RBAC)
|
||||
Roles []Role `gorm:"many2many:user_roles;" json:"roles,omitempty"` // 用户关联的角色(多对多)
|
||||
Status string `gorm:"size:20;default:'active'" json:"status"` // active, inactive, banned
|
||||
IsVerified bool `gorm:"default:false" json:"is_verified"`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user