## 新增功能 ### 消息通知中心 (全新模块) - 新增 MessageController / NotificationService / NotificationRepo - SSR 消息页面 (/messages):左侧边栏 + 右侧卡片列表,noindex 元标签 - 通知能力:列表分页、单条标为已读、一键全部标为已读 - 未读数角标 (1/66/99+):侧边栏 + 导航栏铃铛图标 - 导航栏轮询 /api/messages/unread 每 60 秒刷新未读数 - 审核通过/驳回时自动 fire-and-forget 推送通知 ### 密码修改 - ChangePassword:验证当前密码 → 新密码强度校验(8位+字母+数字) → 哈希更新 - 修改后递增 token_version 强制所有设备退登 ### 账号自助注销 - DeleteAccount:验证密码 → 设置 deleted 状态 → 记录原因 → 吊销 JWT - 注销后登录二次确认:Login 检测 deleted → 返回 confirm_restore - ConfirmRestore 恢复账号,重新签发 token - 注销页文案:"账号将在 7 天后正式注销,期间可随时重新登录恢复" ### IP 审计记录 - 注册时记录 RegIP,登录时记录 LastLoginIP + LastLoginAt - clientIP() 支持 X-Forwarded-For / X-Real-IP 反向代理 ### 安全加固 - Login 防时序攻击:用户不存在时仍执行完整 bcrypt 比对 - FindByEmail 改用 Unscoped() 覆盖软删除用户 - 站长 (owner) 不允许自主注销,避免权限体系死锁 ## Bug 修复 1. 注销按钮不触发:JS IIFE 中 profile 代码 return 阻塞了 account 标签页处理器注册 → 拆分为两层 IIFE,profile 放在内层 2. label 缺少 for 属性导致控制台警告 → 全部补充 for 属性 3. 注销后未自动退登:DeleteAccount 只设状态未吊销 JWT → 末尾加 InvalidateSessions 4. 退登后重定向 500 panic:authenticateToken 中 err||versionMismatch 合并判断 在 err=nil 但版本不匹配时返回 (nil,nil) → 拆为两个独立判断 injectUserContext 增加 claims==nil / 类型断言空安全守卫 5. 注销后登录直接提示"登录成功":FindByEmail 默认 scope 排除软删除记录 → 改用 Unscoped() ## 文件变更 - 新建 17 个文件 (消息/审核/站点设置完整模块) - 修改 25 个文件 (认证/设置/中间件/前端) - 统计:+3229 / -161,42 files changed
138 lines
4.1 KiB
Go
138 lines
4.1 KiB
Go
package service
|
||
|
||
import (
|
||
"metazone.cc/metalab/internal/common"
|
||
"metazone.cc/metalab/internal/model"
|
||
)
|
||
type AdminService struct {
|
||
userRepo userAdminStore
|
||
}
|
||
|
||
// NewAdminService 构造函数
|
||
func NewAdminService(userRepo userAdminStore) *AdminService {
|
||
return &AdminService{userRepo: userRepo}
|
||
}
|
||
|
||
// ListUsersParams 用户列表查询参数
|
||
type ListUsersParams struct {
|
||
Keyword string // 搜索关键字(邮箱/用户名)
|
||
Role string // 角色筛选
|
||
Status string // 状态筛选
|
||
Page int // 页码
|
||
PageSize int // 每页条数
|
||
}
|
||
|
||
// ListUsersResult 用户列表查询结果
|
||
type ListUsersResult struct {
|
||
Users []model.User `json:"users"`
|
||
Total int64 `json:"total"`
|
||
Page int `json:"page"`
|
||
}
|
||
|
||
// ListUsers 分页搜索用户列表
|
||
func (s *AdminService) ListUsers(params ListUsersParams) (*ListUsersResult, error) {
|
||
p := common.Pagination{Page: params.Page, PageSize: params.PageSize}
|
||
p.DefaultPagination()
|
||
|
||
total, err := s.userRepo.CountSearchUsers(params.Keyword, params.Role, params.Status)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
users, err := s.userRepo.SearchUsers(params.Keyword, params.Role, params.Status, p.Offset(), p.PageSize)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &ListUsersResult{
|
||
Users: users,
|
||
Total: total,
|
||
Page: p.Page,
|
||
}, nil
|
||
}
|
||
|
||
// UpdateUserStatus 修改用户状态
|
||
// 权限规则:
|
||
// - 不可操作自己
|
||
// - Admin 只能设置 active/banned,且只能操作 RoleUser
|
||
// - Owner 可设置 active/banned/locked,可操作任何人(除自己)
|
||
func (s *AdminService) UpdateUserStatus(operatorUID, targetUID uint, newStatus string) error {
|
||
// 校验状态值合法性
|
||
switch newStatus {
|
||
case model.StatusActive, model.StatusBanned, model.StatusLocked:
|
||
default:
|
||
return common.ErrPermissionDenied
|
||
}
|
||
|
||
return s.checkAndOperate(operatorUID, targetUID, func(operator, target *model.User) error {
|
||
// locked 相关操作仅 owner
|
||
if target.Status == model.StatusLocked || newStatus == model.StatusLocked {
|
||
if !model.HasMinRole(operator.Role, model.RoleOwner) {
|
||
return common.ErrPermissionDenied
|
||
}
|
||
}
|
||
if err := s.userRepo.UpdateStatus(target.ID, newStatus); err != nil {
|
||
return err
|
||
}
|
||
// 锁定(删除)→ GORM 软删除释放邮箱;解锁 → 先查邮箱冲突再恢复
|
||
if newStatus == model.StatusLocked {
|
||
if err := s.userRepo.SoftDelete(target.ID); err != nil {
|
||
return err
|
||
}
|
||
} else if target.Status == model.StatusLocked && newStatus == model.StatusActive {
|
||
// 检查邮箱是否已被新用户注册(软删期间邮箱释放了)
|
||
collision, err := s.userRepo.ExistsByEmailExclude(target.Email, target.ID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if collision {
|
||
return common.ErrEmailExists
|
||
}
|
||
if err := s.userRepo.Restore(target.ID); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
// 修改状态必须立刻让 JWT 失效
|
||
return s.userRepo.IncrementTokenVersion(target.ID)
|
||
})
|
||
}
|
||
|
||
// ResetUserToken 强制下线(递增 token_version)
|
||
func (s *AdminService) ResetUserToken(operatorUID, targetUID uint) error {
|
||
return s.checkAndOperate(operatorUID, targetUID, func(_ *model.User, target *model.User) error {
|
||
return s.userRepo.IncrementTokenVersion(target.ID)
|
||
})
|
||
}
|
||
|
||
// CountUsers 统计用户总数(管理首页用)
|
||
func (s *AdminService) CountUsers() (int64, error) {
|
||
return s.userRepo.CountSearchUsers("", "", "")
|
||
}
|
||
|
||
// checkAndOperate 通用权限检查 + 执行操作
|
||
func (s *AdminService) checkAndOperate(operatorUID, targetUID uint, operate func(*model.User, *model.User) error) error {
|
||
// 1. 不可操作自己
|
||
if operatorUID == targetUID {
|
||
return common.ErrPermissionDenied
|
||
}
|
||
|
||
// 2. 查操作者
|
||
operator, err := s.userRepo.FindByIDUnscoped(operatorUID)
|
||
if err != nil {
|
||
return common.ErrUserNotFound
|
||
}
|
||
|
||
// 3. 查目标(含软删除用户,locked 解锁需要查到)
|
||
target, err := s.userRepo.FindByIDUnscoped(targetUID)
|
||
if err != nil {
|
||
return common.ErrUserNotFound
|
||
}
|
||
|
||
// 4. 权限检查:操作者是否有权限操作目标角色
|
||
if !model.CanOperateRole(operator.Role, target.Role) {
|
||
return common.ErrPermissionDenied
|
||
}
|
||
|
||
return operate(operator, target)
|
||
}
|