fix(tenant): 收紧双站后台边界并补齐货架管理
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"hfb_sys/backend/internal/modules/realname"
|
||||
"hfb_sys/backend/internal/modules/rentergrowth"
|
||||
"hfb_sys/backend/internal/modules/wallet"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/crypto"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -74,6 +75,7 @@ func (r *Repository) List(ctx context.Context, page, pageSize int, query ListQue
|
||||
Joins("LEFT JOIN (SELECT renter_id AS user_id, SUM(deposit_waived_amount_cent) AS deposit_free_used_cent FROM rental_orders WHERE status NOT IN ('completed', 'cancelled', 'closed') GROUP BY renter_id) AS df ON df.user_id = u.id").
|
||||
Joins("LEFT JOIN wallet_accounts AS w ON w.user_id = u.id AND w.distribution_site_id = 0")
|
||||
listTx = applyUserFilter(listTx, query, growthConfig)
|
||||
listTx = applyUserContextScope(ctx, listTx, "u")
|
||||
err = listTx.
|
||||
Order("u.id DESC").
|
||||
Offset(offset).Limit(pageSize).
|
||||
@@ -89,6 +91,7 @@ func (r *Repository) List(ctx context.Context, page, pageSize int, query ListQue
|
||||
} else {
|
||||
countTx := applyUserFilter(r.db.WithContext(ctx).Table("users AS u").
|
||||
Joins("LEFT JOIN tenants AS t ON t.id = u.tenant_id"), query, growthConfig)
|
||||
countTx = applyUserContextScope(ctx, countTx, "u")
|
||||
if err := countTx.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -101,6 +104,28 @@ func (r *Repository) List(ctx context.Context, page, pageSize int, query ListQue
|
||||
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
|
||||
}
|
||||
|
||||
// applyUserContextScope 将经营站点后台固定到当前租户及其本站用户。
|
||||
// 历史分销用户虽回填到大锤租户,但仍通过 distribution_site_id 保留渠道归属。
|
||||
func applyUserContextScope(ctx context.Context, db *gorm.DB, alias string) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return db
|
||||
}
|
||||
column := "tenant_id"
|
||||
siteColumn := "distribution_site_id"
|
||||
if alias != "" {
|
||||
column = alias + "." + column
|
||||
siteColumn = alias + "." + siteColumn
|
||||
}
|
||||
return db.Where(column+" = ?", tenantID).Where(siteColumn + " = 0")
|
||||
}
|
||||
|
||||
func lockScopedUser(ctx context.Context, tx *gorm.DB, userID uint64, user *model.User) error {
|
||||
return applyUserContextScope(ctx, tx, "").
|
||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
First(user, userID).Error
|
||||
}
|
||||
|
||||
// applyUserFilter 将列表筛选条件应用到查询(基于 users 表别名 u)。
|
||||
func applyUserFilter(tx *gorm.DB, query ListQuery, growthConfig rentergrowth.Config) *gorm.DB {
|
||||
if query.Status != "" {
|
||||
@@ -208,7 +233,7 @@ func (r *Repository) SetDepositFreeQuota(ctx context.Context, adminID uint64, us
|
||||
}
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
if err := lockScopedUser(ctx, tx, userID, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
beforeAmountCent := user.DepositFreeQuotaCent
|
||||
@@ -231,7 +256,7 @@ func (r *Repository) SetDepositFreeQuota(ctx context.Context, adminID uint64, us
|
||||
func (r *Repository) AdjustGrowthPoints(ctx context.Context, adminID uint64, userID uint64, req GrowthPointsAdjustRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
if err := lockScopedUser(ctx, tx, userID, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
if user.RenterGrowthPoints == req.TargetPoints {
|
||||
@@ -298,7 +323,7 @@ func (r *Repository) AdjustGrowthPoints(ctx context.Context, adminID uint64, use
|
||||
func (r *Repository) AdjustGrowthLevel(ctx context.Context, adminID uint64, userID uint64, req GrowthLevelAdjustRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
if err := lockScopedUser(ctx, tx, userID, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
cfg, err := rentergrowth.ConfigForTx(tx)
|
||||
@@ -349,7 +374,7 @@ func (r *Repository) updateStatus(ctx context.Context, adminID uint64, userID ui
|
||||
statusChanged := false
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
if err := lockScopedUser(ctx, tx, userID, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
beforeStatus := user.Status
|
||||
@@ -389,7 +414,7 @@ func (r *Repository) updateStatus(ctx context.Context, adminID uint64, userID ui
|
||||
func (r *Repository) RevokeRealname(ctx context.Context, adminID uint64, userID uint64, req RevokeRealnameRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
if err := lockScopedUser(ctx, tx, userID, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
var realnameCount int64
|
||||
@@ -457,7 +482,7 @@ func (r *Repository) ManualRealname(ctx context.Context, adminID uint64, userID
|
||||
|
||||
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
if err := lockScopedUser(ctx, tx, userID, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
if user.RealnameStatus == "verified" {
|
||||
@@ -533,7 +558,7 @@ func (r *Repository) Find(ctx context.Context, userID uint64) (*UserDTO, error)
|
||||
return nil, err
|
||||
}
|
||||
var row userRow
|
||||
err = r.db.WithContext(ctx).Table("users AS u").
|
||||
query := r.db.WithContext(ctx).Table("users AS u").
|
||||
Select(`u.*, COALESCE(t.code, '') AS tenant_code, COALESCE(t.name, '') AS tenant_name,
|
||||
COALESCE(o.order_count, 0) AS order_count,
|
||||
COALESCE(l.listing_count, 0) AS listing_count,
|
||||
@@ -547,8 +572,9 @@ func (r *Repository) Find(ctx context.Context, userID uint64) (*UserDTO, error)
|
||||
Joins("LEFT JOIN (SELECT user_id, COUNT(*) AS dispute_count FROM (SELECT initiator_id AS user_id FROM disputes UNION ALL SELECT target_user_id AS user_id FROM disputes) AS dispute_users GROUP BY user_id) AS d ON d.user_id = u.id").
|
||||
Joins("LEFT JOIN (SELECT renter_id AS user_id, SUM(deposit_waived_amount_cent) AS deposit_free_used_cent FROM rental_orders WHERE status NOT IN ('completed', 'cancelled', 'closed') GROUP BY renter_id) AS df ON df.user_id = u.id").
|
||||
Joins("LEFT JOIN wallet_accounts AS w ON w.user_id = u.id AND w.distribution_site_id = 0").
|
||||
Where("u.id = ?", userID).
|
||||
First(&row).Error
|
||||
Where("u.id = ?", userID)
|
||||
query = applyUserContextScope(ctx, query, "u")
|
||||
err = query.First(&row).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -581,7 +607,7 @@ func (r *Repository) AdjustWallet(ctx context.Context, adminID uint64, userID ui
|
||||
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
if err := lockScopedUser(ctx, tx, userID, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package adminuser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -8,10 +9,15 @@ import (
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/rentergrowth"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func adminUserTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func TestDepositFreeQuotaAmountCent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -578,6 +584,45 @@ func TestFreezeAndUnfreezeBumpUserTokenVersion(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserAdminIsScopedToCurrentTenantAndOperatingUsers(t *testing.T) {
|
||||
db := setupAdminUserTestDB(t)
|
||||
tenants := []model.Tenant{
|
||||
{ID: 101, Code: "tenant_a", Name: "A站", Status: "active"},
|
||||
{ID: 202, Code: "tenant_b", Name: "B站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := []model.User{
|
||||
{TenantID: 101, Phone: "13700000101", Status: "active"},
|
||||
{TenantID: 101, Phone: "13700000102", Status: "active", DistributionSiteID: 999},
|
||||
{TenantID: 202, Phone: "13700000201", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
ctx := adminUserTenantContext(101)
|
||||
|
||||
result, err := repo.List(ctx, 1, 20, ListQuery{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := result.Items.([]UserDTO)
|
||||
if result.Total != 1 || len(items) != 1 || items[0].ID != users[0].ID {
|
||||
t.Fatalf("tenant user list = %+v", result)
|
||||
}
|
||||
if _, err := repo.Find(ctx, users[2].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("cross-tenant user detail error = %v", err)
|
||||
}
|
||||
if _, err := repo.Freeze(ctx, 9, users[2].ID, FreezeRequest{Reason: "越权测试"}, AuditMeta{}); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("cross-tenant user freeze error = %v", err)
|
||||
}
|
||||
if _, err := repo.Find(ctx, users[1].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("legacy distribution user detail error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupAdminUserTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db := database.NewTestDB()
|
||||
|
||||
Reference in New Issue
Block a user