fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -223,7 +223,8 @@ func (h *Handler) PasswordLogin(c *gin.Context) {
|
||||
response.BadRequest(c, "手机号和密码不能为空,密码长度 8-20 位")
|
||||
return
|
||||
}
|
||||
result, err := h.service.LoginWithPassword(c.Request.Context(), strings.TrimSpace(req.Phone), req.Password, c.ClientIP())
|
||||
siteCode := sitecode.FromRequest(c)
|
||||
result, err := h.service.LoginWithPassword(c.Request.Context(), strings.TrimSpace(req.Phone), req.Password, c.ClientIP(), siteCode)
|
||||
if err != nil {
|
||||
writeAuthError(c, err)
|
||||
return
|
||||
@@ -275,7 +276,8 @@ func (h *Handler) ResetPassword(c *gin.Context) {
|
||||
response.BadRequest(c, "手机号、验证码和新密码不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.ResetPassword(c.Request.Context(), strings.TrimSpace(req.Phone), strings.TrimSpace(req.Code), req.NewPassword); err != nil {
|
||||
siteCode := sitecode.FromRequest(c)
|
||||
if err := h.service.ResetPassword(c.Request.Context(), strings.TrimSpace(req.Phone), strings.TrimSpace(req.Code), req.NewPassword, siteCode); err != nil {
|
||||
writeAuthError(c, err)
|
||||
return
|
||||
}
|
||||
@@ -311,8 +313,6 @@ func writeAuthError(c *gin.Context, err error) {
|
||||
response.Error(c, http.StatusTooManyRequests, "login_locked", "登录失败次数过多,请 15 分钟后再试")
|
||||
case errors.Is(err, ErrUserAlreadyExists):
|
||||
response.BadRequest(c, "该手机号已注册,请直接登录或使用短信验证码登录")
|
||||
case errors.Is(err, ErrLegacyDistributionUser):
|
||||
response.Error(c, http.StatusForbidden, "legacy_distribution_user", "该手机号属于历史分销渠道,请从原分销站点登录")
|
||||
default:
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "服务暂时不可用")
|
||||
}
|
||||
|
||||
@@ -98,33 +98,29 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
now := time.Now()
|
||||
code := sanitizeTrackCode(trackCode)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID > 0 {
|
||||
var existing model.User
|
||||
query := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND phone = ? AND distribution_site_id = 0", tenantID, phone)
|
||||
if err := query.First(&existing).Error; err == nil {
|
||||
_ = r.db.WithContext(ctx).Model(&existing).Updates(map[string]any{
|
||||
"last_login_at": now,
|
||||
"updated_at": now,
|
||||
}).Error
|
||||
return &existing, nil
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
var legacyCount int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).
|
||||
Where("tenant_id = ? AND phone = ? AND distribution_site_id > 0", tenantID, phone).
|
||||
Count(&legacyCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if legacyCount > 0 {
|
||||
return nil, ErrLegacyDistributionUser
|
||||
}
|
||||
}
|
||||
var siteID uint64
|
||||
if len(siteCode) > 0 {
|
||||
siteID = r.resolveSiteID(ctx, siteCode[0])
|
||||
}
|
||||
if tenantID > 0 {
|
||||
siteID = 0
|
||||
}
|
||||
var existing model.User
|
||||
query := r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if siteID > 0 {
|
||||
query = query.Where("distribution_site_id = ?", siteID)
|
||||
} else {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
if err := query.First(&existing).Error; err == nil {
|
||||
_ = r.db.WithContext(ctx).Model(&existing).Updates(map[string]any{
|
||||
"last_login_at": now,
|
||||
"updated_at": now,
|
||||
}).Error
|
||||
return &existing, nil
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
user := model.User{
|
||||
TenantID: tenantID,
|
||||
Phone: phone,
|
||||
@@ -140,11 +136,11 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
|
||||
onConflict := clause.OnConflict{
|
||||
DoUpdates: clause.AssignmentColumns([]string{"last_login_at", "updated_at"}),
|
||||
}
|
||||
if tenantID > 0 {
|
||||
onConflict.Columns = []clause.Column{{Name: "tenant_id"}, {Name: "phone"}}
|
||||
} else {
|
||||
onConflict.Columns = []clause.Column{{Name: "phone"}}
|
||||
Columns: []clause.Column{
|
||||
{Name: "tenant_id"},
|
||||
{Name: "distribution_site_id"},
|
||||
{Name: "phone"},
|
||||
},
|
||||
}
|
||||
err := r.db.WithContext(ctx).Clauses(onConflict).Create(&user).Error
|
||||
if err != nil {
|
||||
@@ -152,8 +148,10 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
}
|
||||
|
||||
var found model.User
|
||||
query := r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if tenantID > 0 {
|
||||
query = r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if siteID > 0 {
|
||||
query = query.Where("distribution_site_id = ?", siteID)
|
||||
} else {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
if err := query.First(&found).Error; err != nil {
|
||||
@@ -162,24 +160,23 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
return &found, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindByPhone(ctx context.Context, phone string) (*model.User, error) {
|
||||
func (r *UserRepository) FindByPhone(ctx context.Context, phone string, siteCode ...string) (*model.User, error) {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var siteID uint64
|
||||
if len(siteCode) > 0 {
|
||||
siteID = r.resolveSiteID(ctx, siteCode[0])
|
||||
}
|
||||
if tenantID > 0 {
|
||||
siteID = 0
|
||||
}
|
||||
var user model.User
|
||||
query := r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
if siteID > 0 {
|
||||
query = query.Where("distribution_site_id = ?", siteID)
|
||||
} else {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
if err := query.First(&user).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) && tenancy.IDFromContext(ctx) > 0 {
|
||||
var legacyCount int64
|
||||
if countErr := r.db.WithContext(ctx).Model(&model.User{}).
|
||||
Where("tenant_id = ? AND phone = ? AND distribution_site_id > 0", tenancy.IDFromContext(ctx), phone).
|
||||
Count(&legacyCount).Error; countErr != nil {
|
||||
return nil, countErr
|
||||
}
|
||||
if legacyCount > 0 {
|
||||
return nil, ErrLegacyDistributionUser
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
@@ -209,21 +206,13 @@ func (r *UserRepository) RegisterWithPassword(ctx context.Context, phone string,
|
||||
now := time.Now()
|
||||
code := sanitizeTrackCode(trackCode)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID > 0 {
|
||||
var legacyCount int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).
|
||||
Where("tenant_id = ? AND phone = ? AND distribution_site_id > 0", tenantID, phone).
|
||||
Count(&legacyCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if legacyCount > 0 {
|
||||
return nil, ErrLegacyDistributionUser
|
||||
}
|
||||
}
|
||||
var siteID uint64
|
||||
if len(siteCode) > 0 {
|
||||
siteID = r.resolveSiteID(ctx, siteCode[0])
|
||||
}
|
||||
if tenantID > 0 {
|
||||
siteID = 0
|
||||
}
|
||||
user := model.User{
|
||||
TenantID: tenantID,
|
||||
Phone: phone,
|
||||
|
||||
@@ -22,21 +22,20 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
ErrInvalidPhone = errors.New("invalid phone")
|
||||
ErrCaptchaInvalid = errors.New("captcha invalid")
|
||||
ErrCodeRateLimited = errors.New("sms code rate limited")
|
||||
ErrCodeInvalid = errors.New("sms code invalid")
|
||||
ErrSMSSendFailed = errors.New("sms send failed")
|
||||
ErrUserDisabled = errors.New("user disabled")
|
||||
ErrInvalidPassword = errors.New("invalid password")
|
||||
ErrInvalidOldPassword = errors.New("invalid old password")
|
||||
ErrPasswordNotSet = errors.New("password not set")
|
||||
ErrPasswordTooWeak = errors.New("password too weak")
|
||||
ErrLoginLocked = errors.New("login locked")
|
||||
ErrUserAlreadyExists = errors.New("user already exists")
|
||||
ErrLegacyDistributionUser = errors.New("user belongs to legacy distribution channel")
|
||||
ErrTokenVersionMismatch = errors.New("user token version mismatch")
|
||||
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
ErrInvalidPhone = errors.New("invalid phone")
|
||||
ErrCaptchaInvalid = errors.New("captcha invalid")
|
||||
ErrCodeRateLimited = errors.New("sms code rate limited")
|
||||
ErrCodeInvalid = errors.New("sms code invalid")
|
||||
ErrSMSSendFailed = errors.New("sms send failed")
|
||||
ErrUserDisabled = errors.New("user disabled")
|
||||
ErrInvalidPassword = errors.New("invalid password")
|
||||
ErrInvalidOldPassword = errors.New("invalid old password")
|
||||
ErrPasswordNotSet = errors.New("password not set")
|
||||
ErrPasswordTooWeak = errors.New("password too weak")
|
||||
ErrLoginLocked = errors.New("login locked")
|
||||
ErrUserAlreadyExists = errors.New("user already exists")
|
||||
ErrTokenVersionMismatch = errors.New("user token version mismatch")
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -309,7 +308,7 @@ func PublicPhone(phone string) string {
|
||||
return fmt.Sprintf("%s****%s", phone[:3], phone[len(phone)-4:])
|
||||
}
|
||||
|
||||
func (s *Service) LoginWithPassword(ctx context.Context, phone, password, clientIP string) (LoginResult, error) {
|
||||
func (s *Service) LoginWithPassword(ctx context.Context, phone, password, clientIP string, siteCode ...string) (LoginResult, error) {
|
||||
if s.redis == nil || s.users == nil {
|
||||
return LoginResult{}, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -326,7 +325,7 @@ func (s *Service) LoginWithPassword(ctx context.Context, phone, password, client
|
||||
return LoginResult{}, ErrLoginLocked
|
||||
}
|
||||
|
||||
user, err := s.users.FindByPhone(ctx, phone)
|
||||
user, err := s.users.FindByPhone(ctx, phone, siteCode...)
|
||||
if IsNotFound(err) {
|
||||
// 用户不存在也计失败次数,避免该接口被用于探测手机号是否已注册
|
||||
_ = recordLoginFailure(ctx, s.redis, tenantID, phone, clientIP)
|
||||
@@ -378,7 +377,7 @@ func (s *Service) SetPassword(ctx context.Context, userID uint64, password, oldP
|
||||
return s.users.SetPassword(ctx, userID, string(hash))
|
||||
}
|
||||
|
||||
func (s *Service) ResetPassword(ctx context.Context, phone, code, newPassword string) error {
|
||||
func (s *Service) ResetPassword(ctx context.Context, phone, code, newPassword string, siteCode ...string) error {
|
||||
if s.redis == nil || s.users == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
@@ -398,7 +397,7 @@ func (s *Service) ResetPassword(ctx context.Context, phone, code, newPassword st
|
||||
return ErrPasswordTooWeak
|
||||
}
|
||||
|
||||
user, err := s.users.FindByPhone(ctx, phone)
|
||||
user, err := s.users.FindByPhone(ctx, phone, siteCode...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -435,7 +434,7 @@ func (s *Service) RegisterWithPassword(ctx context.Context, phone, code, passwor
|
||||
}
|
||||
|
||||
// 已注册的号不允许通过注册流程覆盖密码,避免"注册即改密"接管账号
|
||||
existing, err := s.users.FindByPhone(ctx, phone)
|
||||
existing, err := s.users.FindByPhone(ctx, phone, siteCode...)
|
||||
if err == nil && existing != nil {
|
||||
return LoginResult{}, ErrUserAlreadyExists
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
@@ -109,16 +108,17 @@ func TestLegacyDistributionUserCannotEnterTenantSite(t *testing.T) {
|
||||
if _, err := repo.FindByID(ctx, legacy.ID); err == nil {
|
||||
t.Fatal("tenant site must not load legacy distribution user")
|
||||
}
|
||||
if _, err := repo.FindByPhone(ctx, legacy.Phone); !errors.Is(err, ErrLegacyDistributionUser) {
|
||||
t.Fatalf("legacy phone login error = %v", err)
|
||||
if _, err := repo.FindByPhone(ctx, legacy.Phone); !IsNotFound(err) {
|
||||
t.Fatalf("tenant phone login should not resolve legacy user, got %v", err)
|
||||
}
|
||||
if _, err := repo.FindOrCreateByPhone(ctx, legacy.Phone, ""); !errors.Is(err, ErrLegacyDistributionUser) {
|
||||
t.Fatalf("legacy SMS login error = %v", err)
|
||||
mainUser, err := repo.FindOrCreateByPhone(ctx, legacy.Phone, "")
|
||||
if err != nil {
|
||||
t.Fatalf("create tenant user with legacy phone: %v", err)
|
||||
}
|
||||
if _, err := repo.RegisterWithPassword(ctx, legacy.Phone, "hash", ""); !errors.Is(err, ErrLegacyDistributionUser) {
|
||||
t.Fatalf("legacy registration error = %v", err)
|
||||
if mainUser.ID == legacy.ID || mainUser.TenantID != legacy.TenantID || mainUser.DistributionSiteID != 0 {
|
||||
t.Fatalf("main user = %+v, legacy = %+v", mainUser, legacy)
|
||||
}
|
||||
if _, err := repo.FindByPhone(context.Background(), legacy.Phone); err != nil {
|
||||
if _, err := repo.FindByPhone(context.Background(), legacy.Phone, site.SiteCode); err != nil {
|
||||
t.Fatalf("legacy distribution domain login failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user