## 新增功能 ### 消息通知中心 (全新模块) - 新增 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
82 lines
2.7 KiB
Go
82 lines
2.7 KiB
Go
package model
|
||
|
||
import "time"
|
||
|
||
// User 用户模型
|
||
type User struct {
|
||
BaseModel
|
||
|
||
Email string `gorm:"type:varchar(255);not null" json:"email"`
|
||
PasswordHash string `gorm:"type:varchar(255);not null" json:"-"`
|
||
Username string `gorm:"type:varchar(16);uniqueIndex;not null" json:"username"`
|
||
Avatar string `gorm:"type:varchar(500);default:''" json:"avatar"`
|
||
Bio string `gorm:"type:text" json:"bio"`
|
||
|
||
Role string `gorm:"type:varchar(20);default:user;index;not null" json:"role"`
|
||
Status string `gorm:"type:varchar(20);default:active;index;not null" json:"status"`
|
||
TokenVersion int `gorm:"default:0;not null" json:"-"` // 令牌版本,+1 即时吊销所有 JWT
|
||
|
||
// 安全与审计
|
||
RegIP string `gorm:"type:varchar(45);default:''" json:"-"` // 注册 IP
|
||
LastLoginIP string `gorm:"type:varchar(45);default:''" json:"-"` // 最后登录 IP
|
||
LastLoginAt *time.Time `gorm:"default:null" json:"last_login_at,omitempty"` // 最后登录时间
|
||
DeleteReason string `gorm:"type:text;default:''" json:"-"` // 注销原因
|
||
}
|
||
|
||
// 角色常量(仅用作字符串标识符,层级和权限由配置驱动)
|
||
const (
|
||
RoleUser = "user"
|
||
RoleModerator = "moderator"
|
||
RoleAdmin = "admin"
|
||
RoleOwner = "owner"
|
||
)
|
||
|
||
// 状态常量
|
||
const (
|
||
StatusActive = "active"
|
||
StatusBanned = "banned"
|
||
StatusDeleted = "deleted" // 用户主动注销(7 天内登录可恢复)
|
||
StatusLocked = "locked" // 永久锁定(管理员删除或注销满 7 天)
|
||
)
|
||
|
||
// --- 以下由 InitRoles 从配置初始化,无硬编码默认值 ---
|
||
var roleLevel map[string]int
|
||
var operableRoles map[string][]string
|
||
var RoleDisplayNames map[string]string
|
||
|
||
// InitRoles 从配置初始化角色系统(唯一数据源)
|
||
// 必须在服务启动前调用,不提供默认值
|
||
func InitRoles(levels map[string]int, names map[string]string, operRoles map[string][]string) {
|
||
roleLevel = levels
|
||
RoleDisplayNames = names
|
||
operableRoles = operRoles
|
||
}
|
||
|
||
// HasMinRole 检查 role 是否达到 minRole 的权限级别
|
||
func HasMinRole(role, minRole string) bool {
|
||
return roleLevel[role] >= roleLevel[minRole]
|
||
}
|
||
|
||
// CanOperateRole 检查操作者是否可以操作目标角色
|
||
// 未在 operableRoles 中声明的角色默认可操作所有角色
|
||
func CanOperateRole(operatorRole, targetRole string) bool {
|
||
allowed, hasRule := operableRoles[operatorRole]
|
||
if !hasRule {
|
||
return true
|
||
}
|
||
for _, r := range allowed {
|
||
if r == targetRole {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// StatusDisplayNames 状态 → 中文名(服务端唯一数据源)
|
||
var StatusDisplayNames = map[string]string{
|
||
StatusActive: "正常",
|
||
StatusBanned: "已封禁",
|
||
StatusDeleted: "已注销",
|
||
StatusLocked: "已锁定",
|
||
}
|