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:
@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
// Setup 注册所有路由
|
||||
func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
|
||||
func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config, siteSettings *config.SiteSettings) {
|
||||
// --- 全局中间件 ---
|
||||
r.Use(middleware.SecurityHeaders())
|
||||
|
||||
@ -28,14 +28,35 @@ func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
|
||||
rateLimiter := middleware.NewRateLimiter()
|
||||
authCtrl := controller.NewAuthController(authService, tokenSvc, rateLimiter, cfg)
|
||||
avatarSvc := service.NewAvatarService(userRepo)
|
||||
settingsCtrl := controller.NewSettingsController(authService, avatarSvc)
|
||||
|
||||
authMdw := middleware.NewAuthMiddleware(cfg, userRepo)
|
||||
|
||||
// 管理后台
|
||||
adminService := service.NewAdminService(userRepo)
|
||||
adminController := adminCtrl.NewAdminController(adminService)
|
||||
|
||||
// 审核系统
|
||||
auditRepo := repository.NewAuditRepo(db)
|
||||
notificationRepo := repository.NewNotificationRepo(db)
|
||||
notificationService := service.NewNotificationService(notificationRepo)
|
||||
auditService := service.NewAuditService(auditRepo, userRepo, siteSettings, cfg.Audit, notificationService)
|
||||
auditController := adminCtrl.NewAuditController(auditService, userRepo)
|
||||
|
||||
// 消息中心
|
||||
messageController := controller.NewMessageController(notificationService)
|
||||
|
||||
// 站点设置
|
||||
siteSettingService := service.NewSiteSettingService(siteSettings)
|
||||
auditDefaults := &service.AuditDefaults{
|
||||
Enabled: cfg.Audit.Enabled,
|
||||
UsernameAudit: cfg.Audit.UsernameAudit,
|
||||
AvatarAudit: cfg.Audit.AvatarAudit,
|
||||
BioAudit: cfg.Audit.BioAudit,
|
||||
}
|
||||
siteSettingController := adminCtrl.NewSiteSettingController(siteSettingService, auditDefaults)
|
||||
|
||||
// 个人设置(需要审核服务)
|
||||
settingsCtrl := controller.NewSettingsController(authService, avatarSvc, auditService)
|
||||
|
||||
// --- 页面路由(CSRF 仅下发 token,不验证——页面 GET 被豁免) ---
|
||||
pages := r.Group("/")
|
||||
pages.Use(authMdw.Optional())
|
||||
@ -58,6 +79,9 @@ func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
|
||||
c.Redirect(http.StatusFound, "/settings/profile")
|
||||
})
|
||||
pages.GET("/settings/:tab", settingsCtrl.SettingsPage)
|
||||
|
||||
// 消息中心(需登录,noindex)
|
||||
pages.GET("/messages", messageController.MessagesPage)
|
||||
}
|
||||
|
||||
// 认证页面(已登录自动跳走)
|
||||
@ -80,6 +104,20 @@ func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
|
||||
{
|
||||
settingsAPI.PUT("/profile", settingsCtrl.UpdateProfile)
|
||||
settingsAPI.POST("/avatar", settingsCtrl.UploadAvatar)
|
||||
settingsAPI.PUT("/password", settingsCtrl.ChangePassword)
|
||||
settingsAPI.POST("/delete-account", settingsCtrl.DeleteAccount)
|
||||
// 查询当前用户的待审核状态
|
||||
settingsAPI.GET("/audit-status", settingsCtrl.AuditStatus)
|
||||
}
|
||||
|
||||
// --- 消息中心 API(需登录) ---
|
||||
messagesAPI := r.Group("/api/messages")
|
||||
messagesAPI.Use(authMdw.Required())
|
||||
messagesAPI.Use(middleware.CSRF(cfg))
|
||||
{
|
||||
messagesAPI.GET("/unread", messageController.UnreadCount)
|
||||
messagesAPI.POST("/:id/read", messageController.MarkRead)
|
||||
messagesAPI.POST("/read-all", messageController.MarkAllRead)
|
||||
}
|
||||
|
||||
// --- 管理后台 SSR 页面(认证失败 302 跳首页) ---
|
||||
@ -95,6 +133,8 @@ func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
|
||||
adminPages.GET("/", adminController.Dashboard)
|
||||
adminPages.GET("/users", adminController.UsersPage,
|
||||
middleware.RequirePageRole(model.RoleAdmin))
|
||||
adminPages.GET("/audits", auditController.AuditPage,
|
||||
middleware.RequirePageRole(model.RoleAdmin))
|
||||
}
|
||||
|
||||
// --- 管理后台 API(JSON 响应) ---
|
||||
@ -106,6 +146,20 @@ func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
|
||||
adminAPI.GET("/users", adminController.ListUsers)
|
||||
adminAPI.PUT("/users/:uid/status", adminController.UpdateUserStatus)
|
||||
adminAPI.POST("/users/:uid/reset-token", adminController.ResetToken)
|
||||
// 审核 API
|
||||
adminAPI.GET("/audits", auditController.ListAudits)
|
||||
adminAPI.POST("/audits/:id/approve", auditController.Approve)
|
||||
adminAPI.POST("/audits/:id/reject", auditController.Reject)
|
||||
}
|
||||
|
||||
// --- 站点设置 API(仅 owner 可访问) ---
|
||||
siteSettingsAPI := r.Group("/api/admin/site-settings")
|
||||
siteSettingsAPI.Use(authMdw.Required())
|
||||
siteSettingsAPI.Use(middleware.RequireMinRole(model.RoleOwner))
|
||||
siteSettingsAPI.Use(middleware.CSRF(cfg))
|
||||
{
|
||||
siteSettingsAPI.GET("", siteSettingController.GetSettings)
|
||||
siteSettingsAPI.PUT("", siteSettingController.UpdateSetting)
|
||||
}
|
||||
|
||||
// --- API 路由(CSRF 严格验证) ---
|
||||
@ -115,6 +169,7 @@ func Setup(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
|
||||
api.POST("/auth/check-email", authCtrl.CheckEmail)
|
||||
api.POST("/auth/register", authCtrl.Register)
|
||||
api.POST("/auth/login", authCtrl.Login)
|
||||
api.POST("/auth/confirm-restore", authCtrl.ConfirmRestore)
|
||||
api.POST("/auth/logout", authCtrl.Logout)
|
||||
api.POST("/auth/refresh", authCtrl.RefreshToken)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user