394 lines
14 KiB
Go
394 lines
14 KiB
Go
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"
|
|
)
|
|
|
|
type Dependencies struct {
|
|
DB *gorm.DB
|
|
Redis *redis.Client
|
|
}
|
|
|
|
// privateFileAuthorizer 仅允许对象所属用户、订单参与者或聊天成员读取私有文件。
|
|
// 未能关联到业务记录的对象默认拒绝,避免随机 key 成为访问凭证。
|
|
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
|
|
for _, suffix := range []string{".medium.jpg", ".thumb.jpg"} {
|
|
if strings.HasSuffix(basePrefix, suffix) {
|
|
basePrefix = strings.TrimSuffix(basePrefix, suffix)
|
|
break
|
|
}
|
|
}
|
|
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 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 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, 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 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 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 {
|
|
if db == nil || (userID == 0 && adminID == 0) || len(objectKeys) == 0 {
|
|
return nil
|
|
}
|
|
records := make([]model.FileUploadOwner, 0, len(objectKeys))
|
|
tenantID := tenancy.IDFromContext(ctx)
|
|
for _, key := range objectKeys {
|
|
if key == "" {
|
|
continue
|
|
}
|
|
records = append(records, model.FileUploadOwner{
|
|
TenantID: tenantID,
|
|
UserID: userID,
|
|
AdminID: adminID,
|
|
ObjectKey: key,
|
|
})
|
|
}
|
|
if len(records) == 0 {
|
|
return nil
|
|
}
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|