120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
package router
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type Dependencies struct {
|
|
DB *gorm.DB
|
|
Redis *redis.Client
|
|
}
|
|
|
|
// privateFileAuthorizer 仅允许对象所属用户、订单参与者或聊天成员读取私有文件。
|
|
// 未能关联到业务记录的对象默认拒绝,避免随机 key 成为访问凭证。
|
|
func privateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (bool, error) {
|
|
return func(ctx context.Context, userID uint64, key string) (bool, error) {
|
|
if db == nil || userID == 0 || key == "" {
|
|
return false, nil
|
|
}
|
|
|
|
// 若请求的是变体图片(.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)
|
|
|
|
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,
|
|
).Scan(&ownerCount).Error; err != nil {
|
|
return false, err
|
|
}
|
|
if ownerCount > 0 {
|
|
return true, nil
|
|
}
|
|
if knownQueries, known := knownUserFileQueries(userID, tenantID, fileReferenceValues(key, basePrefix)); known {
|
|
return runFileAccessQueries(ctx, db, knownQueries)
|
|
}
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
// adminPrivateFileAuthorizer 将经营站点后台的私有文件访问限制在当前租户。
|
|
// 历史分销后台(tenant_id=0)保留原有平台级访问语义。
|
|
func adminPrivateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (bool, error) {
|
|
return func(ctx context.Context, adminID uint64, key string) (bool, error) {
|
|
if db == nil || key == "" {
|
|
return false, nil
|
|
}
|
|
basePrefix := strings.TrimSuffix(strings.TrimSuffix(key, ".medium.jpg"), ".thumb.jpg")
|
|
tenantID := tenancy.IDFromContext(ctx)
|
|
|
|
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,
|
|
).Scan(&ownerCount).Error; err != nil {
|
|
return false, err
|
|
}
|
|
if ownerCount > 0 {
|
|
return true, nil
|
|
}
|
|
if knownQueries, known := knownAdminFileQueries(ctx, db, adminID, tenantID, fileReferenceValues(key, basePrefix)); known {
|
|
return runFileAccessQueries(ctx, db, knownQueries)
|
|
}
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|