feat: 消息通知中心 + 账号安全体系 + Bug修复
## 新增功能 ### 消息通知中心 (全新模块) - 新增 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
This commit is contained in:
@ -11,26 +11,38 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// profileProvider SettingsController 对 Service 层的最小依赖(ISP:2 个方法)
|
||||
// profileProvider SettingsController 对 Service 层的最小依赖(ISP:4 个方法)
|
||||
type profileProvider interface {
|
||||
GetProfile(userID uint) (*model.User, error)
|
||||
UpdateProfile(userID uint, username, bio string) error
|
||||
ChangePassword(userID uint, currentPassword, newPassword string) error
|
||||
DeleteAccount(userID uint, password, reason string) error
|
||||
}
|
||||
|
||||
// avatarProvider 头像上传对 Service 层的最小依赖(ISP:1 个方法)
|
||||
// avatarProvider 头像上传对 Service 层的最小依赖(ISP:2 个方法)
|
||||
type avatarProvider interface {
|
||||
ProcessAvatar(userID uint, file io.Reader, contentType string, cropX, cropY, cropSize int) (string, error)
|
||||
ProcessImage(userID uint, file io.Reader, contentType string, cropX, cropY, cropSize int) (string, error)
|
||||
}
|
||||
|
||||
// auditSubmittable 个人设置对审核服务的依赖(ISP:3 个方法)
|
||||
type auditSubmittable interface {
|
||||
ShouldAudit(userID uint) (bool, error)
|
||||
SubmitProfileChanges(userID uint, currentUser *model.User, newUsername, newBio string) error
|
||||
Submit(userID uint, auditType, newValue string) error
|
||||
GetPendingTypes(userID uint) ([]string, error)
|
||||
}
|
||||
|
||||
// SettingsController 个人设置控制器
|
||||
type SettingsController struct {
|
||||
authService profileProvider
|
||||
avatarService avatarProvider
|
||||
auditService auditSubmittable
|
||||
}
|
||||
|
||||
// NewSettingsController 构造函数
|
||||
func NewSettingsController(authService profileProvider, avatarService avatarProvider) *SettingsController {
|
||||
return &SettingsController{authService: authService, avatarService: avatarService}
|
||||
func NewSettingsController(authService profileProvider, avatarService avatarProvider, auditService auditSubmittable) *SettingsController {
|
||||
return &SettingsController{authService: authService, avatarService: avatarService, auditService: auditService}
|
||||
}
|
||||
|
||||
// SettingsPage 个人设置页面(需登录)
|
||||
@ -55,17 +67,23 @@ func (sc *SettingsController) SettingsPage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 查询待审核类型(用于前端显示审核提示)
|
||||
pendingTypes, _ := sc.auditService.GetPendingTypes(uid)
|
||||
|
||||
c.HTML(http.StatusOK, "settings/index.html", common.BuildPageData(c, gin.H{
|
||||
"Title": "个人设置",
|
||||
"ExtraCSS": "/static/css/settings.css",
|
||||
"ActiveTab": tab,
|
||||
"User": user,
|
||||
"RoleName": model.RoleDisplayNames[user.Role],
|
||||
"StatusName": model.StatusDisplayNames[user.Status],
|
||||
"Title": "个人设置",
|
||||
"ExtraCSS": "/static/css/settings.css",
|
||||
"ActiveTab": tab,
|
||||
"User": user,
|
||||
"RoleName": model.RoleDisplayNames[user.Role],
|
||||
"StatusName": model.StatusDisplayNames[user.Status],
|
||||
"PendingTypes": pendingTypes,
|
||||
"AuditTypes": model.AuditTypeNames,
|
||||
}))
|
||||
}
|
||||
|
||||
// UpdateProfile 修改个人资料(用户名 + 个性签名,需登录)
|
||||
// 普通用户走审核流程,管理员及以上直接落库
|
||||
func (sc *SettingsController) UpdateProfile(c *gin.Context) {
|
||||
uid, exists := c.Get("uid")
|
||||
if !exists {
|
||||
@ -82,7 +100,31 @@ func (sc *SettingsController) UpdateProfile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := sc.authService.UpdateProfile(uid.(uint), req.Username, req.Bio); err != nil {
|
||||
userID := uid.(uint)
|
||||
|
||||
// 判断是否需要审核
|
||||
shouldAudit, err := sc.auditService.ShouldAudit(userID)
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusInternalServerError, "操作失败")
|
||||
return
|
||||
}
|
||||
|
||||
if shouldAudit {
|
||||
user, err := sc.authService.GetProfile(userID)
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusInternalServerError, "操作失败")
|
||||
return
|
||||
}
|
||||
if err := sc.auditService.SubmitProfileChanges(userID, user, req.Username, req.Bio); err != nil {
|
||||
handleAuditSubmitError(c, err)
|
||||
return
|
||||
}
|
||||
common.OkMessage(c, "修改已提交审核,通过前当前信息保持不变")
|
||||
return
|
||||
}
|
||||
|
||||
// 管理员及以上直接更新
|
||||
if err := sc.authService.UpdateProfile(userID, req.Username, req.Bio); err != nil {
|
||||
handleSettingsError(c, err)
|
||||
return
|
||||
}
|
||||
@ -91,6 +133,7 @@ func (sc *SettingsController) UpdateProfile(c *gin.Context) {
|
||||
}
|
||||
|
||||
// UploadAvatar 上传头像(需登录,multipart/form-data)
|
||||
// 普通用户走审核流程,管理员及以上直接更新
|
||||
func (sc *SettingsController) UploadAvatar(c *gin.Context) {
|
||||
uid, exists := c.Get("uid")
|
||||
if !exists {
|
||||
@ -110,7 +153,32 @@ func (sc *SettingsController) UploadAvatar(c *gin.Context) {
|
||||
cropY, _ := strconv.Atoi(c.PostForm("crop_y"))
|
||||
cropSize, _ := strconv.Atoi(c.PostForm("crop_size"))
|
||||
|
||||
url, err := sc.avatarService.ProcessAvatar(uid.(uint), file, header.Header.Get("Content-Type"), cropX, cropY, cropSize)
|
||||
userID := uid.(uint)
|
||||
|
||||
// 判断是否需要审核
|
||||
shouldAudit, err := sc.auditService.ShouldAudit(userID)
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusInternalServerError, "操作失败")
|
||||
return
|
||||
}
|
||||
|
||||
if shouldAudit {
|
||||
// 仅处理图片,不更新用户
|
||||
avatarURL, err := sc.avatarService.ProcessImage(userID, file, header.Header.Get("Content-Type"), cropX, cropY, cropSize)
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if err := sc.auditService.Submit(userID, model.AuditTypeAvatar, avatarURL); err != nil {
|
||||
handleAuditSubmitError(c, err)
|
||||
return
|
||||
}
|
||||
common.OkMessage(c, "头像已提交审核,通过前当前头像保持不变")
|
||||
return
|
||||
}
|
||||
|
||||
// 管理员及以上直接更新
|
||||
url, err := sc.avatarService.ProcessAvatar(userID, file, header.Header.Get("Content-Type"), cropX, cropY, cropSize)
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
@ -119,6 +187,56 @@ func (sc *SettingsController) UploadAvatar(c *gin.Context) {
|
||||
common.Ok(c, gin.H{"url": url})
|
||||
}
|
||||
|
||||
// ChangePassword 修改密码(需登录)
|
||||
func (sc *SettingsController) ChangePassword(c *gin.Context) {
|
||||
uid, exists := c.Get("uid")
|
||||
if !exists {
|
||||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
CurrentPassword string `json:"current_password" binding:"required"`
|
||||
NewPassword string `json:"new_password" binding:"required,min=8"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.Error(c, http.StatusBadRequest, "请检查输入")
|
||||
return
|
||||
}
|
||||
|
||||
if err := sc.authService.ChangePassword(uid.(uint), req.CurrentPassword, req.NewPassword); err != nil {
|
||||
handleSettingsError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
common.OkMessage(c, "密码已修改,请重新登录")
|
||||
}
|
||||
|
||||
// DeleteAccount 自助注销账号(需登录,验证密码 + 注销原因)
|
||||
func (sc *SettingsController) DeleteAccount(c *gin.Context) {
|
||||
uid, exists := c.Get("uid")
|
||||
if !exists {
|
||||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Password string `json:"password" binding:"required"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
common.Error(c, http.StatusBadRequest, "请检查输入")
|
||||
return
|
||||
}
|
||||
|
||||
if err := sc.authService.DeleteAccount(uid.(uint), req.Password, req.Reason); err != nil {
|
||||
handleSettingsError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
common.OkMessage(c, "账号已注销,7 天内重新登录即可恢复")
|
||||
}
|
||||
|
||||
// handleSettingsError 统一处理 settings 接口的 service 层错误
|
||||
func handleSettingsError(c *gin.Context, err error) {
|
||||
switch err {
|
||||
@ -128,7 +246,44 @@ func handleSettingsError(c *gin.Context, err error) {
|
||||
common.Error(c, http.StatusBadRequest, err.Error())
|
||||
case common.ErrBioTooLong:
|
||||
common.Error(c, http.StatusBadRequest, err.Error())
|
||||
case common.ErrIncorrectPassword:
|
||||
common.Error(c, http.StatusForbidden, err.Error())
|
||||
case common.ErrOwnerCannotDelete:
|
||||
common.Error(c, http.StatusForbidden, err.Error())
|
||||
default:
|
||||
common.Error(c, http.StatusInternalServerError, "操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
// handleAuditSubmitError 统一处理审核提交的错误
|
||||
func handleAuditSubmitError(c *gin.Context, err error) {
|
||||
switch err {
|
||||
case common.ErrAuditDisabled:
|
||||
common.Error(c, http.StatusForbidden, "审核功能未开启")
|
||||
case common.ErrAuditTypeOff:
|
||||
common.Error(c, http.StatusForbidden, "该类型审核未开启,请联系管理员")
|
||||
case common.ErrUsernameTaken:
|
||||
common.Error(c, http.StatusConflict, err.Error())
|
||||
default:
|
||||
common.Error(c, http.StatusInternalServerError, "提交审核失败")
|
||||
}
|
||||
}
|
||||
|
||||
// AuditStatus 查询当前用户的待审核类型(需登录)
|
||||
func (sc *SettingsController) AuditStatus(c *gin.Context) {
|
||||
uid, exists := c.Get("uid")
|
||||
if !exists {
|
||||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
types, err := sc.auditService.GetPendingTypes(uid.(uint))
|
||||
if err != nil {
|
||||
common.Ok(c, gin.H{"pending_types": []string{}})
|
||||
return
|
||||
}
|
||||
if types == nil {
|
||||
types = []string{}
|
||||
}
|
||||
common.Ok(c, gin.H{"pending_types": types})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user