fix(withdrawal): 修复分站审核凭证与历史订单可见性
This commit is contained in:
@@ -217,7 +217,7 @@ func userCanAccessFileBinding(ctx context.Context, db *gorm.DB, userID, tenantID
|
||||
}
|
||||
|
||||
func adminCanAccessFileBinding(ctx context.Context, db *gorm.DB, adminID, tenantID uint64, binding model.FileObjectBinding) (bool, error) {
|
||||
if tenantID == 0 {
|
||||
if tenantID == 0 || tenancy.HasAdminAllTenantScope(ctx) {
|
||||
return true, nil
|
||||
}
|
||||
switch binding.ResourceType {
|
||||
@@ -246,17 +246,20 @@ func adminCanAccessFileBinding(ctx context.Context, db *gorm.DB, adminID, tenant
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourcePaymentAccount:
|
||||
scopeSQL, scopeArgs := adminFinanceAccountScope(ctx, tenantID, "p", "u")
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Table("user_payment_accounts AS p").
|
||||
Joins("JOIN users AS u ON u.id = p.user_id").
|
||||
Where("p.id = ? AND p.tenant_id = ? AND u.tenant_id = ?", binding.ResourceID, tenantID, tenantID).
|
||||
Where("u.distribution_site_id = 0").
|
||||
Where("p.id = ?", binding.ResourceID).
|
||||
Where(scopeSQL, scopeArgs...).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourceWithdrawal:
|
||||
scopeSQL, scopeArgs := adminFinanceWithdrawalScope(ctx, tenantID, "withdrawal_requests")
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Table("withdrawal_requests").
|
||||
Where("id = ? AND tenant_id = ? AND distribution_site_id = 0", binding.ResourceID, tenantID).
|
||||
Where("id = ?", binding.ResourceID).
|
||||
Where(scopeSQL, scopeArgs...).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourceChatQrCode:
|
||||
|
||||
@@ -244,11 +244,30 @@ func TestAdminPrivateFileAuthorizerScopesOperatingTenant(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminBindingAuthorizerExcludesDistributionFinanceFiles(t *testing.T) {
|
||||
func TestAdminBindingAuthorizerAllowsOwnDistributionFinanceFiles(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
tenant := model.Tenant{ID: 101, Code: "dachui", Name: "大锤商行", Status: "active"}
|
||||
if err := db.Create(&tenant).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
site := model.DistributionSite{
|
||||
ID: 77,
|
||||
SiteCode: "gz_dist",
|
||||
SiteName: "天域商行",
|
||||
Domain: "tysh.example.com",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&site).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.ChatSiteTenantLink{
|
||||
SiteID: site.ID, TenantID: tenant.ID, Enabled: true,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := model.User{
|
||||
TenantID: 101,
|
||||
DistributionSiteID: 77,
|
||||
DistributionSiteID: site.ID,
|
||||
Phone: "file-distribution-user",
|
||||
Status: "active",
|
||||
}
|
||||
@@ -270,7 +289,7 @@ func TestAdminBindingAuthorizerExcludesDistributionFinanceFiles(t *testing.T) {
|
||||
WithdrawNo: "W-FILE-DIST",
|
||||
TenantID: 101,
|
||||
UserID: user.ID,
|
||||
DistributionSiteID: 77,
|
||||
DistributionSiteID: site.ID,
|
||||
AccountType: "alipay",
|
||||
AccountName: "测试",
|
||||
AccountNo: "test@example.com",
|
||||
@@ -282,12 +301,25 @@ func TestAdminBindingAuthorizerExcludesDistributionFinanceFiles(t *testing.T) {
|
||||
}
|
||||
|
||||
authorize := adminPrivateFileAuthorizer(db, "strict")
|
||||
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
|
||||
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{
|
||||
TenantID: tenant.ID, TenantCode: tenant.Code,
|
||||
})
|
||||
for _, key := range []string{"payment-cert/distribution.webp", "payment-proof/distribution.webp"} {
|
||||
if allowed, err := authorize(ctx, 9, key); err != nil {
|
||||
t.Fatalf("%s authorize error: %v", key, err)
|
||||
} else if !allowed {
|
||||
t.Fatalf("tenant admin should read own distribution finance file %s", key)
|
||||
}
|
||||
}
|
||||
|
||||
otherCtx := tenancy.WithScope(context.Background(), tenancy.Scope{
|
||||
TenantID: 202, TenantCode: "jieyou",
|
||||
})
|
||||
for _, key := range []string{"payment-cert/distribution.webp", "payment-proof/distribution.webp"} {
|
||||
if allowed, err := authorize(otherCtx, 9, key); err != nil {
|
||||
t.Fatalf("%s cross-tenant authorize error: %v", key, err)
|
||||
} else if allowed {
|
||||
t.Fatalf("tenant admin must not read distribution finance file %s", key)
|
||||
t.Fatalf("other tenant must not read finance file %s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -125,6 +127,46 @@ func scalarInAny(column string, values []string) (string, []any) {
|
||||
return fmt.Sprintf("%s IN (%s)", column, placeholders), args
|
||||
}
|
||||
|
||||
func adminFinanceTenantPredicate(ctx context.Context, tenantID uint64, column string) (string, []any) {
|
||||
if tenantID == 0 || tenancy.HasAdminAllTenantScope(ctx) {
|
||||
return "(1 = 1)", nil
|
||||
}
|
||||
scope, ok := tenancy.FromContext(ctx)
|
||||
if ok && scope.TenantCode == "dachui" {
|
||||
return fmt.Sprintf("(%s IN (0, ?))", column), []any{tenantID}
|
||||
}
|
||||
return fmt.Sprintf("(%s = ?)", column), []any{tenantID}
|
||||
}
|
||||
|
||||
func adminFinanceSitePredicate(ctx context.Context, tenantID uint64, column string) (string, []any) {
|
||||
if tenantID == 0 || tenancy.HasAdminAllTenantScope(ctx) {
|
||||
return "(1 = 1)", nil
|
||||
}
|
||||
return fmt.Sprintf(`(
|
||||
%s IS NULL OR %s = 0 OR EXISTS (
|
||||
SELECT 1 FROM chat_site_tenant_links st
|
||||
WHERE st.site_id = %s
|
||||
AND st.enabled = 1
|
||||
AND st.tenant_id = ?
|
||||
)
|
||||
)`, column, column, column), []any{tenantID}
|
||||
}
|
||||
|
||||
func adminFinanceAccountScope(ctx context.Context, tenantID uint64, accountAlias, userAlias string) (string, []any) {
|
||||
accountTenant, accountArgs := adminFinanceTenantPredicate(ctx, tenantID, accountAlias+".tenant_id")
|
||||
userTenant, userArgs := adminFinanceTenantPredicate(ctx, tenantID, userAlias+".tenant_id")
|
||||
userSite, siteArgs := adminFinanceSitePredicate(ctx, tenantID, userAlias+".distribution_site_id")
|
||||
args := append(append(append([]any{}, accountArgs...), userArgs...), siteArgs...)
|
||||
return accountTenant + " AND " + userTenant + " AND " + userSite, args
|
||||
}
|
||||
|
||||
func adminFinanceWithdrawalScope(ctx context.Context, tenantID uint64, alias string) (string, []any) {
|
||||
tenantPredicate, tenantArgs := adminFinanceTenantPredicate(ctx, tenantID, alias+".tenant_id")
|
||||
sitePredicate, siteArgs := adminFinanceSitePredicate(ctx, tenantID, alias+".distribution_site_id")
|
||||
args := append(append([]any{}, tenantArgs...), siteArgs...)
|
||||
return tenantPredicate + " AND " + sitePredicate, args
|
||||
}
|
||||
|
||||
func runFileAccessQueries(ctx context.Context, db *gorm.DB, queries []fileAccessQuery) (bool, error) {
|
||||
for _, query := range queries {
|
||||
var count int64
|
||||
@@ -351,20 +393,22 @@ WHERE (? = 0 OR o.buyer_tenant_id = ? OR o.source_tenant_id = ?)
|
||||
},
|
||||
}, true
|
||||
case "payment-cert":
|
||||
scopeSQL, scopeArgs := adminFinanceAccountScope(ctx, tenantID, "p", "u")
|
||||
return []fileAccessQuery{
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM user_payment_accounts p
|
||||
JOIN users u ON u.id = p.user_id
|
||||
WHERE (? = 0 OR (p.tenant_id = ? AND u.tenant_id = ? AND u.distribution_site_id = 0))
|
||||
WHERE ` + scopeSQL + `
|
||||
AND ` + certificateMembers,
|
||||
args: append([]any{tenantID, tenantID, tenantID}, jsonArgs...),
|
||||
args: append(append([]any{}, scopeArgs...), jsonArgs...),
|
||||
},
|
||||
}, true
|
||||
case "payment-proof":
|
||||
scopeSQL, scopeArgs := adminFinanceWithdrawalScope(ctx, tenantID, "w")
|
||||
return []fileAccessQuery{{
|
||||
sql: `SELECT COUNT(1) FROM withdrawal_requests w
|
||||
WHERE (? = 0 OR (w.tenant_id = ? AND w.distribution_site_id = 0)) AND ` + withdrawalClause,
|
||||
args: append([]any{tenantID, tenantID}, withdrawalArgs...),
|
||||
WHERE ` + scopeSQL + ` AND ` + withdrawalClause,
|
||||
args: append(append([]any{}, scopeArgs...), withdrawalArgs...),
|
||||
}}, true
|
||||
case "operating-expense", "manual-disbursement":
|
||||
if !isPlatformSuperAdmin(ctx, db, adminID) {
|
||||
|
||||
@@ -7,10 +7,77 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func TestAdminPaymentCertificateFallbackAllowsOwnDistributionSite(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
tenant := model.Tenant{ID: 101, Code: "dachui", Name: "大锤商行", Status: "active"}
|
||||
if err := db.Create(&tenant).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
site := model.DistributionSite{
|
||||
ID: 77,
|
||||
SiteCode: "gz_dist",
|
||||
SiteName: "天域商行",
|
||||
Domain: "tysh.example.com",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&site).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.ChatSiteTenantLink{
|
||||
SiteID: site.ID, TenantID: tenant.ID, Enabled: true,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := model.User{
|
||||
TenantID: tenant.ID, DistributionSiteID: site.ID,
|
||||
Phone: "13900004501", Status: "active",
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
certificateURL := "/api/files/object?key=payment-cert%2Ftianyu-qr.webp"
|
||||
if err := db.Create(&model.UserPaymentAccount{
|
||||
TenantID: tenant.ID,
|
||||
UserID: user.ID,
|
||||
AccountType: "alipay",
|
||||
AccountName: "天域号主",
|
||||
AccountNo: "encrypted",
|
||||
CertificateURLs: datatypes.JSON([]byte(`["` + certificateURL + `"]`)),
|
||||
Status: "active",
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
values := fileReferenceValues("payment-cert/tianyu-qr.webp", "payment-cert/tianyu-qr.webp")
|
||||
dachuiCtx := tenancy.WithScope(context.Background(), tenancy.Scope{
|
||||
TenantID: tenant.ID, TenantCode: tenant.Code,
|
||||
})
|
||||
queries, known := knownAdminFileQueries(dachuiCtx, db, 9, tenant.ID, values)
|
||||
if !known {
|
||||
t.Fatal("收款凭证应进入管理员文件权限校验")
|
||||
}
|
||||
if allowed, err := runFileAccessQueries(dachuiCtx, db, queries); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !allowed {
|
||||
t.Fatal("大锤财务应可查看天域站用户的收款二维码")
|
||||
}
|
||||
|
||||
jieyouCtx := tenancy.WithScope(context.Background(), tenancy.Scope{
|
||||
TenantID: 202, TenantCode: "jieyou",
|
||||
})
|
||||
queries, _ = knownAdminFileQueries(jieyouCtx, db, 9, 202, values)
|
||||
if allowed, err := runFileAccessQueries(jieyouCtx, db, queries); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatal("其他经营租户不得查看天域站用户收款二维码")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateFileAccessMatchesCrossTenantConversation(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
seller := model.User{TenantID: 202, Phone: "13900004001", Nickname: "解忧号主", Status: "active"}
|
||||
|
||||
Reference in New Issue
Block a user