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:
@ -74,28 +74,11 @@ func (r *UserRepo) ExistsByEmailExclude(email string, excludeUID uint) (bool, er
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// IncrementTokenVersion 递增用户令牌版本,使所有已签发的 JWT 即时失效
|
||||
func (r *UserRepo) IncrementTokenVersion(userID uint) error {
|
||||
return r.db.Unscoped().Model(&model.User{}).Where("uid = ?", userID).
|
||||
UpdateColumn("token_version", gorm.Expr("token_version + 1")).Error
|
||||
}
|
||||
|
||||
// FindTokenVersion 按 UID 查询当前令牌版本(轻量查询,仅 SELECT token_version)
|
||||
func (r *UserRepo) FindTokenVersion(userID uint) (int, error) {
|
||||
var version int
|
||||
err := r.db.Model(&model.User{}).
|
||||
Select("token_version").
|
||||
Where("uid = ?", userID).
|
||||
Scan(&version).Error
|
||||
return version, err
|
||||
}
|
||||
|
||||
// FindByIDForAuth 认证专用查询:返回 uid/email/username/role/status/token_version
|
||||
// FindByIDForAuth 认证专用查询:返回 uid/email/username/role/status
|
||||
// 比 FindByID 轻量(不查 avatar、bio 等无用字段),避免全量 SELECT *
|
||||
// 使用默认 scope(排除软删除),locked 用户不可持有有效 JWT
|
||||
func (r *UserRepo) FindByIDForAuth(userID uint) (*model.User, error) {
|
||||
var user model.User
|
||||
err := r.db.Select("uid", "email", "username", "role", "status", "token_version").
|
||||
err := r.db.Select("uid", "email", "username", "role", "status").
|
||||
First(&user, userID).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -106,7 +89,7 @@ func (r *UserRepo) FindByIDForAuth(userID uint) (*model.User, error) {
|
||||
// FindByIDUnscoped 管理后台专用:含软删除用户,用于解锁/封禁等操作
|
||||
func (r *UserRepo) FindByIDUnscoped(userID uint) (*model.User, error) {
|
||||
var user model.User
|
||||
err := r.db.Unscoped().Select("uid", "email", "username", "role", "status", "token_version", "deleted_at").
|
||||
err := r.db.Unscoped().Select("uid", "email", "username", "role", "status", "deleted_at").
|
||||
First(&user, userID).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user