perf(files): 优化私有文件鉴权与高成本查询隔离
This commit is contained in:
@@ -3,11 +3,15 @@ package router
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/filekey"
|
||||
"hfb_sys/backend/internal/logging"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
@@ -19,11 +23,13 @@ type Dependencies struct {
|
||||
|
||||
// privateFileAuthorizer 仅允许对象所属用户、订单参与者或聊天成员读取私有文件。
|
||||
// 未能关联到业务记录的对象默认拒绝,避免随机 key 成为访问凭证。
|
||||
func privateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (bool, error) {
|
||||
func privateFileAuthorizer(db *gorm.DB, modes ...string) func(context.Context, uint64, string) (bool, error) {
|
||||
mode := fileBindingMode(modes...)
|
||||
return func(ctx context.Context, userID uint64, key string) (bool, error) {
|
||||
if db == nil || userID == 0 || key == "" {
|
||||
return false, nil
|
||||
}
|
||||
start := time.Now()
|
||||
|
||||
// 若请求的是变体图片(.medium.jpg 或 .thumb.jpg),提取原始前缀,便于在存有原图的业务表中匹配
|
||||
basePrefix := key
|
||||
@@ -35,50 +41,293 @@ func privateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (b
|
||||
}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
|
||||
canonical := filekey.Canonical(key)
|
||||
var ownerCount int64
|
||||
if err := db.WithContext(ctx).Raw(
|
||||
"SELECT COUNT(1) FROM file_upload_owners WHERE user_id = ? AND object_key = ? AND (? = 0 OR tenant_id = ?)",
|
||||
userID, key, tenantID, tenantID,
|
||||
"SELECT COUNT(1) FROM file_upload_owners WHERE user_id = ? AND object_key IN (?, ?) AND (? = 0 OR tenant_id = ?)",
|
||||
userID, key, canonical, tenantID, tenantID,
|
||||
).Scan(&ownerCount).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
if ownerCount > 0 {
|
||||
logFileAuthorization(ctx, "user", "upload_owner", canonical, start, nil)
|
||||
return true, nil
|
||||
}
|
||||
if knownQueries, known := knownUserFileQueries(userID, tenantID, fileReferenceValues(key, basePrefix)); known {
|
||||
return runFileAccessQueries(ctx, db, knownQueries)
|
||||
if mode != "legacy" {
|
||||
bindings, err := loadFileObjectBindings(ctx, db, canonical)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(bindings) > 0 {
|
||||
for _, binding := range bindings {
|
||||
allowed, err := userCanAccessFileBinding(ctx, db, userID, tenantID, binding)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if allowed {
|
||||
logFileAuthorization(ctx, "user", "binding", canonical, start, nil)
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
if mode == "strict" {
|
||||
logFileAuthorization(ctx, "user", "binding_denied", canonical, start, nil)
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if mode == "strict" {
|
||||
logFileAuthorization(ctx, "user", "binding_missing", canonical, start, nil)
|
||||
return false, nil
|
||||
}
|
||||
if knownQueries, known := knownUserFileQueries(userID, tenantID, fileReferenceValues(key, basePrefix)); known {
|
||||
allowed, err := runFileAccessQueries(ctx, db, knownQueries)
|
||||
if err == nil && allowed {
|
||||
logFileAuthorization(ctx, "user", "legacy_reference", canonical, start, nil)
|
||||
return true, nil
|
||||
}
|
||||
logFileAuthorization(ctx, "user", "legacy_denied", canonical, start, err)
|
||||
return allowed, err
|
||||
}
|
||||
logFileAuthorization(ctx, "user", "unmatched", canonical, start, nil)
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// adminPrivateFileAuthorizer 将经营站点后台的私有文件访问限制在当前租户。
|
||||
// 历史分销后台(tenant_id=0)保留原有平台级访问语义。
|
||||
func adminPrivateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (bool, error) {
|
||||
func adminPrivateFileAuthorizer(db *gorm.DB, modes ...string) func(context.Context, uint64, string) (bool, error) {
|
||||
mode := fileBindingMode(modes...)
|
||||
return func(ctx context.Context, adminID uint64, key string) (bool, error) {
|
||||
if db == nil || key == "" {
|
||||
return false, nil
|
||||
}
|
||||
start := time.Now()
|
||||
basePrefix := strings.TrimSuffix(strings.TrimSuffix(key, ".medium.jpg"), ".thumb.jpg")
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
|
||||
canonical := filekey.Canonical(key)
|
||||
var ownerCount int64
|
||||
if err := db.WithContext(ctx).Raw(
|
||||
"SELECT COUNT(1) FROM file_upload_owners WHERE admin_id = ? AND object_key = ? AND (? = 0 OR tenant_id = ?)",
|
||||
adminID, key, tenantID, tenantID,
|
||||
"SELECT COUNT(1) FROM file_upload_owners WHERE admin_id = ? AND object_key IN (?, ?) AND (? = 0 OR tenant_id = ?)",
|
||||
adminID, key, canonical, tenantID, tenantID,
|
||||
).Scan(&ownerCount).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
if ownerCount > 0 {
|
||||
logFileAuthorization(ctx, "admin", "upload_owner", canonical, start, nil)
|
||||
return true, nil
|
||||
}
|
||||
if knownQueries, known := knownAdminFileQueries(ctx, db, adminID, tenantID, fileReferenceValues(key, basePrefix)); known {
|
||||
return runFileAccessQueries(ctx, db, knownQueries)
|
||||
if mode != "legacy" {
|
||||
bindings, err := loadFileObjectBindings(ctx, db, canonical)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(bindings) > 0 {
|
||||
for _, binding := range bindings {
|
||||
allowed, err := adminCanAccessFileBinding(ctx, db, adminID, tenantID, binding)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if allowed {
|
||||
logFileAuthorization(ctx, "admin", "binding", canonical, start, nil)
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
if mode == "strict" {
|
||||
logFileAuthorization(ctx, "admin", "binding_denied", canonical, start, nil)
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if mode == "strict" {
|
||||
logFileAuthorization(ctx, "admin", "binding_missing", canonical, start, nil)
|
||||
return false, nil
|
||||
}
|
||||
if knownQueries, known := knownAdminFileQueries(ctx, db, adminID, tenantID, fileReferenceValues(key, basePrefix)); known {
|
||||
allowed, err := runFileAccessQueries(ctx, db, knownQueries)
|
||||
if err == nil && allowed {
|
||||
logFileAuthorization(ctx, "admin", "legacy_reference", canonical, start, nil)
|
||||
return true, nil
|
||||
}
|
||||
logFileAuthorization(ctx, "admin", "legacy_denied", canonical, start, err)
|
||||
return allowed, err
|
||||
}
|
||||
logFileAuthorization(ctx, "admin", "unmatched", canonical, start, nil)
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func fileBindingMode(modes ...string) string {
|
||||
if len(modes) == 0 {
|
||||
return "prefer"
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(modes[0])) {
|
||||
case "strict", "legacy":
|
||||
return strings.ToLower(strings.TrimSpace(modes[0]))
|
||||
default:
|
||||
return "prefer"
|
||||
}
|
||||
}
|
||||
|
||||
func loadFileObjectBindings(ctx context.Context, db *gorm.DB, canonical string) ([]model.FileObjectBinding, error) {
|
||||
if canonical == "" {
|
||||
return nil, nil
|
||||
}
|
||||
var bindings []model.FileObjectBinding
|
||||
err := db.WithContext(ctx).
|
||||
Where("canonical_key = ?", canonical).
|
||||
Order("id ASC").
|
||||
Find(&bindings).Error
|
||||
return bindings, err
|
||||
}
|
||||
|
||||
func userCanAccessFileBinding(ctx context.Context, db *gorm.DB, userID, tenantID uint64, binding model.FileObjectBinding) (bool, error) {
|
||||
if binding.OwnerUserID > 0 && binding.OwnerUserID == userID {
|
||||
return bindingTenantMatches(binding.TenantID, tenantID), nil
|
||||
}
|
||||
switch binding.ResourceType {
|
||||
case model.FileResourceGameAccount:
|
||||
return userCanAccessGameAccountFiles(ctx, db, userID, tenantID, binding.ResourceID)
|
||||
case model.FileResourceChatConversation:
|
||||
return userCanAccessConversationFiles(ctx, db, userID, tenantID, binding.ResourceID)
|
||||
case model.FileResourceOrder:
|
||||
return userCanAccessOrderFiles(ctx, db, userID, tenantID, binding.ResourceID)
|
||||
case model.FileResourcePaymentAccount:
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Table("user_payment_accounts").
|
||||
Where("id = ? AND user_id = ? AND (? = 0 OR tenant_id = ?)", binding.ResourceID, userID, tenantID, tenantID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourceWithdrawal:
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Table("withdrawal_requests").
|
||||
Where("id = ? AND user_id = ? AND (? = 0 OR tenant_id = ?)", binding.ResourceID, userID, tenantID, tenantID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourceChatQrCode:
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Table("chat_qrcode_pool AS q").
|
||||
Joins("JOIN chat_participants AS p ON p.conversation_id = q.conversation_id AND p.participant_type = 'user' AND p.participant_id = ?", userID).
|
||||
Where("q.id = ?", binding.ResourceID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func adminCanAccessFileBinding(ctx context.Context, db *gorm.DB, adminID, tenantID uint64, binding model.FileObjectBinding) (bool, error) {
|
||||
if tenantID == 0 {
|
||||
return true, nil
|
||||
}
|
||||
switch binding.ResourceType {
|
||||
case model.FileResourceGameAccount:
|
||||
var count int64
|
||||
if err := db.WithContext(ctx).Table("game_accounts").
|
||||
Where("id = ? AND source_tenant_id = ?", binding.ResourceID, tenantID).
|
||||
Count(&count).Error; err != nil || count > 0 {
|
||||
return count > 0, err
|
||||
}
|
||||
err := db.WithContext(ctx).Table("rental_listings").
|
||||
Where("account_id = ? AND source_tenant_id = ?", binding.ResourceID, tenantID).
|
||||
Where("distribution_site_id IS NULL OR distribution_site_id = 0").
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourceChatConversation:
|
||||
return adminCanAccessConversationFiles(ctx, db, tenantID, binding.ResourceID)
|
||||
case model.FileResourceOrder:
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Table("rental_orders").
|
||||
Where("id = ? AND (buyer_tenant_id = ? OR source_tenant_id = ?)", binding.ResourceID, tenantID, tenantID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourcePaymentAccount:
|
||||
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").
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourceWithdrawal:
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Table("withdrawal_requests").
|
||||
Where("id = ? AND tenant_id = ? AND distribution_site_id = 0", binding.ResourceID, tenantID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourceChatQrCode:
|
||||
var count int64
|
||||
query := db.WithContext(ctx).Table("chat_qrcode_pool").
|
||||
Where("id = ? AND tenant_id = ?", binding.ResourceID, tenantID)
|
||||
if !isPlatformSuperAdmin(ctx, db, adminID) {
|
||||
query = query.Where("distribution_site_id IS NULL OR distribution_site_id = 0")
|
||||
}
|
||||
err := query.Count(&count).Error
|
||||
return count > 0, err
|
||||
case model.FileResourceOperatingExpense, model.FileResourceManualDisbursement:
|
||||
return isPlatformSuperAdmin(ctx, db, adminID), nil
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func bindingTenantMatches(bindingTenantID, tenantID uint64) bool {
|
||||
return bindingTenantID == 0 || tenantID == 0 || bindingTenantID == tenantID
|
||||
}
|
||||
|
||||
func userCanAccessGameAccountFiles(ctx context.Context, db *gorm.DB, userID, tenantID, accountID uint64) (bool, error) {
|
||||
var count int64
|
||||
if err := db.WithContext(ctx).Table("game_accounts").
|
||||
Where("id = ? AND owner_id = ?", accountID, userID).
|
||||
Where("? = 0 OR source_tenant_id = ?", tenantID, tenantID).
|
||||
Count(&count).Error; err != nil || count > 0 {
|
||||
return count > 0, err
|
||||
}
|
||||
if err := db.WithContext(ctx).Table("rental_listings AS l").
|
||||
Where("l.account_id = ? AND l.status = ? AND l.review_status = ?", accountID, "published", "approved").
|
||||
Where("? = 0 OR l.source_tenant_id = ?", tenantID, tenantID).
|
||||
Count(&count).Error; err != nil || count > 0 {
|
||||
return count > 0, err
|
||||
}
|
||||
err := db.WithContext(ctx).Table("rental_orders").
|
||||
Where("account_id = ? AND (owner_id = ? OR renter_id = ?)", accountID, userID, userID).
|
||||
Where("? = 0 OR buyer_tenant_id = ? OR source_tenant_id = ?", tenantID, tenantID, tenantID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
func userCanAccessConversationFiles(ctx context.Context, db *gorm.DB, userID, tenantID, conversationID uint64) (bool, error) {
|
||||
var count int64
|
||||
condition := conversationTenantAccessClause("c")
|
||||
args := conversationTenantAccessArgs(tenantID)
|
||||
err := db.WithContext(ctx).Table("chat_participants AS p").
|
||||
Joins("JOIN chat_conversations AS c ON c.id = p.conversation_id").
|
||||
Where("p.conversation_id = ? AND p.participant_type = ? AND p.participant_id = ?", conversationID, "user", userID).
|
||||
Where(condition, args...).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
func userCanAccessOrderFiles(ctx context.Context, db *gorm.DB, userID, tenantID, orderID uint64) (bool, error) {
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Table("rental_orders").
|
||||
Where("id = ? AND (owner_id = ? OR renter_id = ?)", orderID, userID, userID).
|
||||
Where("? = 0 OR buyer_tenant_id = ? OR source_tenant_id = ?", tenantID, tenantID, tenantID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
func adminCanAccessConversationFiles(ctx context.Context, db *gorm.DB, tenantID, conversationID uint64) (bool, error) {
|
||||
var count int64
|
||||
condition := conversationTenantAccessClause("c")
|
||||
args := conversationTenantAccessArgs(tenantID)
|
||||
err := db.WithContext(ctx).Table("chat_conversations AS c").
|
||||
Where("c.id = ?", conversationID).
|
||||
Where(condition, args...).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// recordPrivateFileUpload 记录用户或管理员上传的原图和变体,保证业务记录保存前仅上传者可预览。
|
||||
func recordPrivateFileUpload(db *gorm.DB) func(context.Context, uint64, uint64, []string) error {
|
||||
return func(ctx context.Context, userID, adminID uint64, objectKeys []string) error {
|
||||
@@ -117,3 +366,28 @@ func isPlatformSuperAdmin(ctx context.Context, db *gorm.DB, adminID uint64) bool
|
||||
Count(&count).Error
|
||||
return err == nil && count > 0
|
||||
}
|
||||
|
||||
func logFileAuthorization(ctx context.Context, subject, outcome, canonical string, start time.Time, err error) {
|
||||
elapsed := time.Since(start)
|
||||
fields := []zap.Field{
|
||||
zap.String("subject", subject),
|
||||
zap.String("outcome", outcome),
|
||||
zap.String("object_prefix", fileObjectPrefix(canonical)),
|
||||
zap.Float64("auth_ms", float64(elapsed.Microseconds())/1000),
|
||||
}
|
||||
if err != nil {
|
||||
fields = append(fields, zap.Error(err))
|
||||
}
|
||||
logger := logging.FromContext(ctx)
|
||||
logger.Debug("私有文件鉴权", fields...)
|
||||
if elapsed >= 250*time.Millisecond {
|
||||
logger.Warn("私有文件鉴权慢查询", fields...)
|
||||
}
|
||||
}
|
||||
|
||||
func fileObjectPrefix(key string) string {
|
||||
if index := strings.IndexByte(key, '/'); index > 0 {
|
||||
return key[:index]
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -86,6 +87,50 @@ func TestPrivateFileAuthorizer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateFileAuthorizerSupportsBindingModes(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
if err := db.Exec("INSERT INTO game_accounts (id, owner_id, server_region, login_platform, title, screenshot_urls) "+
|
||||
"VALUES (101, 201, '国服', 'steam', '绑定账号', '[]'), "+
|
||||
"(102, 202, '国服', 'steam', '旧数据账号', ?)",
|
||||
`["/api/files/object?key=listing%2Fmode.jpg"]`).Error; err != nil {
|
||||
t.Fatalf("create game accounts: %v", err)
|
||||
}
|
||||
if err := db.Create(&model.FileObjectBinding{
|
||||
CanonicalKey: "listing/mode.jpg",
|
||||
ResourceType: model.FileResourceGameAccount,
|
||||
ResourceID: 101,
|
||||
AccessScope: model.FileScopeListingAsset,
|
||||
OwnerUserID: 201,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("create file binding: %v", err)
|
||||
}
|
||||
|
||||
prefer := privateFileAuthorizer(db, "prefer")
|
||||
strict := privateFileAuthorizer(db, "strict")
|
||||
legacy := privateFileAuthorizer(db, "legacy")
|
||||
key := "listing/mode.jpg"
|
||||
|
||||
if allowed, err := prefer(context.Background(), 201, key); err != nil || !allowed {
|
||||
t.Fatalf("prefer binding owner allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := prefer(context.Background(), 202, key); err != nil || !allowed {
|
||||
t.Fatalf("prefer legacy fallback allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := strict(context.Background(), 202, key); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatal("strict mode must not fall back to JSON references")
|
||||
}
|
||||
if allowed, err := legacy(context.Background(), 201, key); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatal("legacy mode must skip binding table")
|
||||
}
|
||||
if allowed, err := legacy(context.Background(), 202, key); err != nil || !allowed {
|
||||
t.Fatalf("legacy JSON owner allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordPrivateFileUpload(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
if err := database.AutoMigrateIfMissing(db, &model.FileUploadOwner{}); err != nil {
|
||||
@@ -142,6 +187,31 @@ func TestPrivateFileAuthorizerRejectsCrossTenantOwner(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateFileAuthorizerRejectsCrossTenantBindingOwner(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
if err := db.Create(&model.FileObjectBinding{
|
||||
TenantID: 101,
|
||||
CanonicalKey: "listing/binding-tenant-a.jpg",
|
||||
ResourceType: model.FileResourceGameAccount,
|
||||
ResourceID: 999,
|
||||
AccessScope: model.FileScopeListingAsset,
|
||||
OwnerUserID: 11,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("create binding: %v", err)
|
||||
}
|
||||
authorize := privateFileAuthorizer(db, "strict")
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
|
||||
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202})
|
||||
if allowed, err := authorize(ctxA, 11, "listing/binding-tenant-a.jpg"); err != nil || !allowed {
|
||||
t.Fatalf("same tenant allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := authorize(ctxB, 11, "listing/binding-tenant-a.jpg"); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatal("cross tenant binding owner must not be readable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminPrivateFileAuthorizerScopesOperatingTenant(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
if err := database.AutoMigrateIfMissing(db, &model.FileUploadOwner{}); err != nil {
|
||||
@@ -174,6 +244,54 @@ func TestAdminPrivateFileAuthorizerScopesOperatingTenant(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminBindingAuthorizerExcludesDistributionFinanceFiles(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
user := model.User{
|
||||
TenantID: 101,
|
||||
DistributionSiteID: 77,
|
||||
Phone: "file-distribution-user",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
account := model.UserPaymentAccount{
|
||||
TenantID: 101,
|
||||
UserID: user.ID,
|
||||
AccountType: "alipay",
|
||||
AccountName: "测试",
|
||||
AccountNo: "test@example.com",
|
||||
CertificateURLs: datatypes.JSON([]byte(`["/api/files/object?key=payment-cert%2Fdistribution.webp"]`)),
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
withdrawal := model.WithdrawalRequest{
|
||||
WithdrawNo: "W-FILE-DIST",
|
||||
TenantID: 101,
|
||||
UserID: user.ID,
|
||||
DistributionSiteID: 77,
|
||||
AccountType: "alipay",
|
||||
AccountName: "测试",
|
||||
AccountNo: "test@example.com",
|
||||
Status: "processing",
|
||||
PaymentProofURL: "/api/files/object?key=payment-proof%2Fdistribution.webp",
|
||||
}
|
||||
if err := db.Create(&withdrawal).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
authorize := adminPrivateFileAuthorizer(db, "strict")
|
||||
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
|
||||
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 must not read distribution finance file %s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateFileAuthorizerAllowsDeliveredQrCodeParticipants(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
tenants := []model.Tenant{
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/config"
|
||||
"hfb_sys/backend/internal/handler"
|
||||
@@ -70,6 +71,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
engine.Use(middleware.RequestID())
|
||||
engine.Use(middleware.RequestLogger(logger))
|
||||
engine.Use(middleware.Recovery(logger))
|
||||
engine.Use(middleware.QueryBudget())
|
||||
var tenantRepo *tenancy.Repository
|
||||
if deps.DB != nil {
|
||||
tenantRepo = tenancy.NewRepository(deps.DB)
|
||||
@@ -398,6 +400,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
}
|
||||
engine.Use(middleware.RequireKnownApplicationHost(cfg.AppEnv, cfg.DistributionAdminDomain))
|
||||
engine.Use(middleware.PlatformOpsReadOnlyGuard())
|
||||
engine.Use(middleware.TenantConcurrencyLimit(2, 200*time.Millisecond))
|
||||
distributionHandler := distribution.NewHandler(distributionService)
|
||||
if invitationHandler != nil && distributionService != nil {
|
||||
invitationHandler.SetSiteScoper(distributionService)
|
||||
@@ -453,10 +456,11 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
fileHandler := filemodule.NewHandler(
|
||||
fileService,
|
||||
fileStorage,
|
||||
privateFileAuthorizer(deps.DB),
|
||||
privateFileAuthorizer(deps.DB, cfg.FileBindingMode),
|
||||
recordPrivateFileUpload(deps.DB),
|
||||
adminPrivateFileAuthorizer(deps.DB),
|
||||
adminPrivateFileAuthorizer(deps.DB, cfg.FileBindingMode),
|
||||
)
|
||||
fileHandler.SetAccessTicketManager(filemodule.NewAccessTicketManager(cfg.JWTSecret))
|
||||
listingService := listing.NewService(listingRepo, systemConfigRepo)
|
||||
listingHandler := listing.NewHandler(listingService, fileStorage, listing.HandlerOptions{
|
||||
ExternalUploadSecret: cfg.ExternalUploadSecret,
|
||||
@@ -727,6 +731,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
fileRoutes := api.Group("/files", requireFileAuth)
|
||||
{
|
||||
fileRoutes.POST("/upload", fileHandler.Upload)
|
||||
fileRoutes.POST("/access/batch", fileHandler.BatchAccess)
|
||||
fileRoutes.GET("/object", fileHandler.Object)
|
||||
}
|
||||
|
||||
@@ -830,6 +835,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
adminRoutes.GET("/distribution/auth/me", distributionHandler.Me)
|
||||
adminRoutes.POST("/distribution/auth/logout", distributionHandler.Logout)
|
||||
adminRoutes.POST("/files/upload", fileHandler.Upload)
|
||||
adminRoutes.POST("/files/access/batch", fileHandler.BatchAccess)
|
||||
adminRoutes.GET("/dashboard", requirePerm("dashboard:view"), adminDashboardHandler.Summary)
|
||||
adminRoutes.GET("/manual-entries/today-stats", requirePerm("cs_manual_entry:view"), manualEntryHandler.TodayAgentStats)
|
||||
adminRoutes.GET("/manual-entries/options", requirePerm("cs_manual_entry:view"), manualEntryHandler.ListOptions)
|
||||
|
||||
Reference in New Issue
Block a user