refactor: 移除 Session 架构下已无用的 TokenVersion 即时吊销机制
- 删除 UserRepo.IncrementTokenVersion/FindTokenVersion 方法 - 从 userAuthStore/userAdminStore 接口移除 IncrementTokenVersion - 删除 middleware.tokenVersionStore 接口(中间件不再查询 token_version) - auth_service: DeleteAccount/InvalidateSessions 仅调用 DestroyByUID - admin_service: UpdateUserStatus/ResetUserToken 仅调用 DestroyByUID - FindByIDForAuth/FindByIDUnscoped Select 移除 token_version 列 - 新架构下登出/改密/封禁统一走 sessionManager.DestroyByUID,无需 token_version
This commit is contained in:
@ -95,21 +95,15 @@ func (s *AdminService) UpdateUserStatus(operatorUID, targetUID uint, newStatus s
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 修改状态必须立刻让已有 session 失效 + 递增 token_version(向后兼容)
|
||||
if err := s.sessionManager.DestroyByUID(target.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.userRepo.IncrementTokenVersion(target.ID)
|
||||
// 修改状态必须立刻让已有 session 失效
|
||||
return s.sessionManager.DestroyByUID(target.ID)
|
||||
})
|
||||
}
|
||||
|
||||
// ResetUserToken 强制下线:销毁所有 session + 递增 token_version
|
||||
// ResetUserToken 强制下线:销毁所有 session
|
||||
func (s *AdminService) ResetUserToken(operatorUID, targetUID uint) error {
|
||||
return s.checkAndOperate(operatorUID, targetUID, func(_ *model.User, target *model.User) error {
|
||||
if err := s.sessionManager.DestroyByUID(target.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.userRepo.IncrementTokenVersion(target.ID)
|
||||
return s.sessionManager.DestroyByUID(target.ID)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -259,11 +259,7 @@ func (s *AuthService) DeleteAccount(userID uint, password, reason string) error
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除所有 session + 递增 token_version(向后兼容)
|
||||
if err := s.invalidateSessions(userID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.userRepo.IncrementTokenVersion(userID)
|
||||
return s.invalidateSessions(userID)
|
||||
}
|
||||
|
||||
// invalidateSessions 销毁某用户的所有 session(内部方法)
|
||||
@ -273,10 +269,7 @@ func (s *AuthService) invalidateSessions(userID uint) error {
|
||||
|
||||
// InvalidateSessions 公开方法:销毁用户所有 session(供 admin 等外部调用)
|
||||
func (s *AuthService) InvalidateSessions(userID uint) error {
|
||||
if err := s.invalidateSessions(userID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.userRepo.IncrementTokenVersion(userID)
|
||||
return s.sessionManager.DestroyByUID(userID)
|
||||
}
|
||||
|
||||
// UpdateProfile 修改个人资料
|
||||
|
||||
@ -2,14 +2,13 @@ package service
|
||||
|
||||
import "metazone.cc/metalab/internal/model"
|
||||
|
||||
// userAuthStore AuthService 所需的最小仓储接口(ISP:8 个方法)
|
||||
// userAuthStore AuthService 所需的最小仓储接口(ISP:7 个方法)
|
||||
type userAuthStore interface {
|
||||
ExistsByEmail(email string) (bool, error)
|
||||
Create(user *model.User) error
|
||||
FindByEmail(email string) (*model.User, error)
|
||||
FindByID(id uint) (*model.User, error)
|
||||
Update(user *model.User) error
|
||||
IncrementTokenVersion(userID uint) error
|
||||
ExistsByUsername(username string) (bool, error)
|
||||
}
|
||||
|
||||
@ -18,7 +17,7 @@ type userTokenStore interface {
|
||||
FindByIDForAuth(userID uint) (*model.User, error)
|
||||
}
|
||||
|
||||
// userAdminStore AdminService 所需的最小仓储接口
|
||||
// userAdminStore AdminService 所需的最小仓储接口(ISP:7 个方法)
|
||||
type userAdminStore interface {
|
||||
CountSearchUsers(keyword, role, status string) (int64, error)
|
||||
SearchUsers(keyword, role, status string, offset, limit int) ([]model.User, error)
|
||||
@ -27,7 +26,6 @@ type userAdminStore interface {
|
||||
SoftDelete(uid uint) error
|
||||
Restore(uid uint) error
|
||||
ExistsByEmailExclude(email string, excludeUID uint) (bool, error)
|
||||
IncrementTokenVersion(userID uint) error
|
||||
}
|
||||
|
||||
// postStore PostService 所需的最小仓储接口
|
||||
|
||||
Reference in New Issue
Block a user