fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -47,6 +47,16 @@ func privateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (b
|
||||
sql: "SELECT COUNT(1) FROM file_upload_owners WHERE user_id = ? AND object_key = ? AND (? = 0 OR tenant_id = ?)",
|
||||
args: []any{userID, key, tenantID, tenantID},
|
||||
},
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q
|
||||
JOIN chat_conversations c ON c.id = q.conversation_id
|
||||
JOIN chat_participants p ON p.conversation_id = c.id AND p.participant_type = 'user' AND p.participant_id = ?
|
||||
WHERE (? = 0 OR c.tenant_id = ? OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro WHERE ro.id = c.order_id AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
))
|
||||
AND (INSTR(q.image_url, ?) > 0 OR INSTR(q.image_url, ?) > 0 OR INSTR(q.image_url, ?) > 0 OR INSTR(q.image_url, ?) > 0)`,
|
||||
args: []any{userID, tenantID, tenantID, tenantID, tenantID, key, encodedKey, basePrefix, encodedBasePrefix},
|
||||
},
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM game_accounts a
|
||||
JOIN rental_listings l ON l.account_id = a.id
|
||||
@@ -138,7 +148,7 @@ WHERE w.user_id = ?
|
||||
// adminPrivateFileAuthorizer 将经营站点后台的私有文件访问限制在当前租户。
|
||||
// 历史分销后台(tenant_id=0)保留原有平台级访问语义。
|
||||
func adminPrivateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (bool, error) {
|
||||
return func(ctx context.Context, _ uint64, key string) (bool, error) {
|
||||
return func(ctx context.Context, adminID uint64, key string) (bool, error) {
|
||||
if db == nil || key == "" {
|
||||
return false, nil
|
||||
}
|
||||
@@ -152,8 +162,8 @@ func adminPrivateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, strin
|
||||
args []any
|
||||
}{
|
||||
{
|
||||
sql: "SELECT COUNT(1) FROM file_upload_owners WHERE object_key = ? AND (? = 0 OR tenant_id = ?)",
|
||||
args: []any{key, tenantID, tenantID},
|
||||
sql: "SELECT COUNT(1) FROM file_upload_owners WHERE admin_id = ? AND object_key = ? AND (? = 0 OR tenant_id = ?)",
|
||||
args: []any{adminID, key, tenantID, tenantID},
|
||||
},
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM game_accounts a
|
||||
@@ -204,6 +214,33 @@ WHERE (? = 0 OR (w.tenant_id = ? AND w.distribution_site_id = 0))
|
||||
AND (INSTR(w.payment_proof_url, ?) > 0 OR INSTR(w.payment_proof_url, ?) > 0 OR INSTR(w.payment_proof_url, ?) > 0 OR INSTR(w.payment_proof_url, ?) > 0)`,
|
||||
args: []any{tenantID, tenantID, key, encodedKey, basePrefix, encodedBasePrefix},
|
||||
},
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q
|
||||
WHERE (? = 0 OR (q.tenant_id = ? AND (q.distribution_site_id IS NULL OR q.distribution_site_id = 0)))
|
||||
AND (INSTR(q.image_url, ?) > 0 OR INSTR(q.image_url, ?) > 0 OR INSTR(q.image_url, ?) > 0 OR INSTR(q.image_url, ?) > 0)`,
|
||||
args: []any{tenantID, tenantID, key, encodedKey, basePrefix, encodedBasePrefix},
|
||||
},
|
||||
}
|
||||
if (strings.HasPrefix(key, "operating-expense/") || strings.HasPrefix(key, "manual-disbursement/")) &&
|
||||
isPlatformSuperAdmin(ctx, db, adminID) {
|
||||
queries = append(queries,
|
||||
struct {
|
||||
sql string
|
||||
args []any
|
||||
}{
|
||||
sql: `SELECT COUNT(1) FROM operating_expenses oe
|
||||
WHERE INSTR(oe.voucher_url, ?) > 0 OR INSTR(oe.voucher_url, ?) > 0 OR INSTR(oe.voucher_url, ?) > 0 OR INSTR(oe.voucher_url, ?) > 0`,
|
||||
args: []any{key, encodedKey, basePrefix, encodedBasePrefix},
|
||||
},
|
||||
struct {
|
||||
sql string
|
||||
args []any
|
||||
}{
|
||||
sql: `SELECT COUNT(1) FROM manual_disbursements md
|
||||
WHERE INSTR(md.voucher_url, ?) > 0 OR INSTR(md.voucher_url, ?) > 0 OR INSTR(md.voucher_url, ?) > 0 OR INSTR(md.voucher_url, ?) > 0`,
|
||||
args: []any{key, encodedKey, basePrefix, encodedBasePrefix},
|
||||
},
|
||||
)
|
||||
}
|
||||
for _, query := range queries {
|
||||
var count int64
|
||||
@@ -221,10 +258,10 @@ WHERE (? = 0 OR (w.tenant_id = ? AND w.distribution_site_id = 0))
|
||||
}
|
||||
}
|
||||
|
||||
// recordPrivateFileUpload 记录用户上传的原图和变体,保证草稿状态下也仅上传者可预览。
|
||||
func recordPrivateFileUpload(db *gorm.DB) func(context.Context, uint64, []string) error {
|
||||
return func(ctx context.Context, userID uint64, objectKeys []string) error {
|
||||
if db == nil || userID == 0 || len(objectKeys) == 0 {
|
||||
// recordPrivateFileUpload 记录用户或管理员上传的原图和变体,保证业务记录保存前仅上传者可预览。
|
||||
func recordPrivateFileUpload(db *gorm.DB) func(context.Context, uint64, uint64, []string) error {
|
||||
return func(ctx context.Context, userID, adminID uint64, objectKeys []string) error {
|
||||
if db == nil || (userID == 0 && adminID == 0) || len(objectKeys) == 0 {
|
||||
return nil
|
||||
}
|
||||
records := make([]model.FileUploadOwner, 0, len(objectKeys))
|
||||
@@ -233,7 +270,12 @@ func recordPrivateFileUpload(db *gorm.DB) func(context.Context, uint64, []string
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
records = append(records, model.FileUploadOwner{TenantID: tenantID, UserID: userID, ObjectKey: key})
|
||||
records = append(records, model.FileUploadOwner{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
AdminID: adminID,
|
||||
ObjectKey: key,
|
||||
})
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return nil
|
||||
@@ -241,3 +283,16 @@ func recordPrivateFileUpload(db *gorm.DB) func(context.Context, uint64, []string
|
||||
return db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(&records).Error
|
||||
}
|
||||
}
|
||||
|
||||
func isPlatformSuperAdmin(ctx context.Context, db *gorm.DB, adminID uint64) bool {
|
||||
if db == nil || adminID == 0 {
|
||||
return false
|
||||
}
|
||||
var count int64
|
||||
err := db.WithContext(ctx).
|
||||
Table("admin_user_roles AS aur").
|
||||
Joins("JOIN roles AS r ON r.id = aur.role_id").
|
||||
Where("aur.admin_user_id = ? AND r.code = ?", adminID, "super_admin").
|
||||
Count(&count).Error
|
||||
return err == nil && count > 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user