fix(auth): 修复历史分销用户登录后立即退出
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/sitecode"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -32,12 +33,23 @@ func NewUserRepository(db *gorm.DB) *UserRepository {
|
||||
return &UserRepository{db: db}
|
||||
}
|
||||
|
||||
func scopeUserQueryToBoundSite(ctx context.Context, query *gorm.DB) *gorm.DB {
|
||||
code := sitecode.BoundSiteCodeFromContext(ctx)
|
||||
if code == "" {
|
||||
return query
|
||||
}
|
||||
return query.
|
||||
Joins("JOIN distribution_sites AS auth_site ON auth_site.id = users.distribution_site_id").
|
||||
Where("auth_site.site_code = ? AND auth_site.status = ?", code, "active")
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindByID(ctx context.Context, id uint64) (*model.User, error) {
|
||||
var user model.User
|
||||
query := r.db.WithContext(ctx)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
query = scopeUserQueryToBoundSite(ctx, query)
|
||||
if err := query.First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -54,8 +66,9 @@ func (r *UserRepository) FindActiveForToken(ctx context.Context, id uint64, toke
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
query = scopeUserQueryToBoundSite(ctx, query)
|
||||
if err := query.
|
||||
Select("id, phone, status, token_version").
|
||||
Select("users.id, users.phone, users.status, users.token_version").
|
||||
First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -73,6 +86,7 @@ func (r *UserRepository) UpdateProfile(ctx context.Context, id uint64, nickname
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
query = scopeUserQueryToBoundSite(ctx, query)
|
||||
if err := query.Updates(map[string]any{
|
||||
"nickname": nickname,
|
||||
"avatar_url": avatarURL,
|
||||
@@ -187,6 +201,7 @@ func (r *UserRepository) SetPassword(ctx context.Context, userID uint64, hash st
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
query = scopeUserQueryToBoundSite(ctx, query)
|
||||
return query.Updates(map[string]any{
|
||||
"password_hash": hash,
|
||||
"token_version": gorm.Expr("token_version + 1"),
|
||||
@@ -199,6 +214,7 @@ func (r *UserRepository) RevokeTokens(ctx context.Context, userID uint64) error
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
query = scopeUserQueryToBoundSite(ctx, query)
|
||||
return query.UpdateColumn("token_version", gorm.Expr("token_version + 1")).Error
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user