Integrate Casbin for role-based access control with admin routes for role management. Add user activity logging service to track login and refresh events. Refactor audit service to use AI-based content moderation.
58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package model
|
|
|
|
import "time"
|
|
|
|
// Role 角色定义
|
|
type Role struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
Name string `gorm:"uniqueIndex;size:64;not null" json:"name"`
|
|
DisplayName string `gorm:"size:128;not null" json:"display_name"`
|
|
Description string `json:"description"`
|
|
ParentRole *string `gorm:"size:64" json:"parent_role"`
|
|
Priority int `gorm:"default:0" json:"priority"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (Role) TableName() string {
|
|
return "roles"
|
|
}
|
|
|
|
// UserRole 用户角色关联
|
|
type UserRole struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
UserID string `gorm:"index;size:64;not null" json:"user_id"`
|
|
Role string `gorm:"index;size:64;not null" json:"role"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (UserRole) TableName() string {
|
|
return "user_roles"
|
|
}
|
|
|
|
// CasbinRule Casbin 策略规则 (对应 casbin_rule 表)
|
|
type CasbinRule struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement"`
|
|
Ptype string `gorm:"size:255;not null;index"`
|
|
V0 string `gorm:"size:255;not null;index"`
|
|
V1 string `gorm:"size:255;not null;index"`
|
|
V2 string `gorm:"size:255;default:''"`
|
|
V3 string `gorm:"size:255;default:''"`
|
|
V4 string `gorm:"size:255;default:''"`
|
|
V5 string `gorm:"size:255;default:''"`
|
|
}
|
|
|
|
func (CasbinRule) TableName() string {
|
|
return "casbin_rule"
|
|
}
|
|
|
|
// 预定义角色常量
|
|
const (
|
|
RoleSuperAdmin = "super_admin"
|
|
RoleAdmin = "admin"
|
|
RoleModerator = "moderator"
|
|
RoleUser = "user"
|
|
RoleBanned = "banned"
|
|
)
|