fix(files): 分销站商品图片按租户范围鉴权
商品图片的绑定校验与历史回退查询统一使用站点归属租户;平台超管保留全量读取,并同步修正审核页超管文案。
This commit is contained in:
@@ -222,15 +222,19 @@ func adminCanAccessFileBinding(ctx context.Context, db *gorm.DB, adminID, tenant
|
|||||||
}
|
}
|
||||||
switch binding.ResourceType {
|
switch binding.ResourceType {
|
||||||
case model.FileResourceGameAccount:
|
case model.FileResourceGameAccount:
|
||||||
|
if isPlatformSuperAdmin(ctx, db, adminID) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
var count int64
|
var count int64
|
||||||
if err := db.WithContext(ctx).Table("game_accounts").
|
if err := db.WithContext(ctx).Table("game_accounts").
|
||||||
Where("id = ? AND source_tenant_id = ?", binding.ResourceID, tenantID).
|
Where("id = ? AND source_tenant_id = ?", binding.ResourceID, tenantID).
|
||||||
Count(&count).Error; err != nil || count > 0 {
|
Count(&count).Error; err != nil || count > 0 {
|
||||||
return count > 0, err
|
return count > 0, err
|
||||||
}
|
}
|
||||||
err := db.WithContext(ctx).Table("rental_listings").
|
scopeSQL, scopeArgs := adminListingFileScopeSQL(tenantID, false)
|
||||||
Where("account_id = ? AND source_tenant_id = ?", binding.ResourceID, tenantID).
|
err := db.WithContext(ctx).Table("rental_listings AS l").
|
||||||
Where("distribution_site_id IS NULL OR distribution_site_id = 0").
|
Where("l.account_id = ?", binding.ResourceID).
|
||||||
|
Where(scopeSQL, scopeArgs...).
|
||||||
Count(&count).Error
|
Count(&count).Error
|
||||||
return count > 0, err
|
return count > 0, err
|
||||||
case model.FileResourceChatConversation:
|
case model.FileResourceChatConversation:
|
||||||
|
|||||||
@@ -90,6 +90,25 @@ func jsonReferencePayload(values []string) string {
|
|||||||
return string(payload)
|
return string(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// adminListingFileScopeSQL 统一管理员读取商品图片的租户范围。
|
||||||
|
// 主站商品按货源租户归属;分销站商品按站点归属租户判定;平台超管全量可见。
|
||||||
|
func adminListingFileScopeSQL(tenantID uint64, platformSuperAdmin bool) (string, []any) {
|
||||||
|
if tenantID == 0 || platformSuperAdmin {
|
||||||
|
return "(1 = 1)", nil
|
||||||
|
}
|
||||||
|
return `(
|
||||||
|
((l.distribution_site_id IS NULL OR l.distribution_site_id = 0) AND l.source_tenant_id = ?)
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM chat_site_tenant_links AS st
|
||||||
|
JOIN tenants AS st_tenant ON st_tenant.id = st.tenant_id AND st_tenant.status = 'active'
|
||||||
|
WHERE st.site_id = l.distribution_site_id
|
||||||
|
AND st.enabled = 1
|
||||||
|
AND st.tenant_id = ?
|
||||||
|
)
|
||||||
|
)`, []any{tenantID, tenantID}
|
||||||
|
}
|
||||||
|
|
||||||
func jsonOverlapsAny(column string) string {
|
func jsonOverlapsAny(column string) string {
|
||||||
return fmt.Sprintf("JSON_OVERLAPS(%s, CAST(? AS JSON))", column)
|
return fmt.Sprintf("JSON_OVERLAPS(%s, CAST(? AS JSON))", column)
|
||||||
}
|
}
|
||||||
@@ -252,12 +271,14 @@ func knownAdminFileQueries(ctx context.Context, db *gorm.DB, adminID, tenantID u
|
|||||||
prefix := strings.SplitN(values[0], "/", 2)[0]
|
prefix := strings.SplitN(values[0], "/", 2)[0]
|
||||||
switch prefix {
|
switch prefix {
|
||||||
case "listing", "misc":
|
case "listing", "misc":
|
||||||
|
platformSuperAdmin := tenantID > 0 && isPlatformSuperAdmin(ctx, db, adminID)
|
||||||
|
scopeSQL, scopeArgs := adminListingFileScopeSQL(tenantID, platformSuperAdmin)
|
||||||
return []fileAccessQuery{{
|
return []fileAccessQuery{{
|
||||||
sql: `SELECT COUNT(1) FROM game_accounts a
|
sql: `SELECT COUNT(1) FROM game_accounts a
|
||||||
JOIN rental_listings l ON l.account_id = a.id
|
JOIN rental_listings l ON l.account_id = a.id
|
||||||
WHERE (? = 0 OR (l.source_tenant_id = ? AND (l.distribution_site_id IS NULL OR l.distribution_site_id = 0)))
|
WHERE ` + scopeSQL + `
|
||||||
AND ` + listingMembers,
|
AND ` + listingMembers,
|
||||||
args: append([]any{tenantID, tenantID}, jsonArgs...),
|
args: append(append([]any{}, scopeArgs...), jsonArgs...),
|
||||||
}}, true
|
}}, true
|
||||||
case "chat":
|
case "chat":
|
||||||
if isPlatformSuperAdmin(ctx, db, adminID) {
|
if isPlatformSuperAdmin(ctx, db, adminID) {
|
||||||
|
|||||||
@@ -223,3 +223,122 @@ func TestAdminQrCodeFileAccessUsesConversationScope(t *testing.T) {
|
|||||||
t.Fatal("管理员不得访问其他租户未绑定会话的码池图片")
|
t.Fatal("管理员不得访问其他租户未绑定会话的码池图片")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAdminListingFileAccessUsesTenantDistributionScope(t *testing.T) {
|
||||||
|
db := database.NewTestDB()
|
||||||
|
tenants := []model.Tenant{
|
||||||
|
{ID: 101, Code: "file_scope_a", Name: "A站", Status: "active"},
|
||||||
|
{ID: 202, Code: "file_scope_b", Name: "B站", Status: "active"},
|
||||||
|
}
|
||||||
|
if err := db.Create(&tenants).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sites := []model.DistributionSite{
|
||||||
|
{SiteCode: "file_scope_site_a", SiteName: "A分销站", Domain: "file-scope-a.example.com", Status: "active", DisplayAccountEnabled: true},
|
||||||
|
{SiteCode: "file_scope_site_b", SiteName: "B分销站", Domain: "file-scope-b.example.com", Status: "active", DisplayAccountEnabled: true},
|
||||||
|
}
|
||||||
|
if err := db.Create(&sites).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&[]model.ChatSiteTenantLink{
|
||||||
|
{SiteID: sites[0].ID, TenantID: 101, Enabled: true},
|
||||||
|
{SiteID: sites[1].ID, TenantID: 202, Enabled: true},
|
||||||
|
}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
owner := model.User{TenantID: 0, DistributionSiteID: sites[0].ID, Phone: "13900006101", Status: "active"}
|
||||||
|
if err := db.Create(&owner).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
account := model.GameAccount{
|
||||||
|
SourceTenantID: 0,
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
GameName: "delta_force",
|
||||||
|
ServerRegion: "国服",
|
||||||
|
LoginPlatform: "steam",
|
||||||
|
Title: "分销站商品",
|
||||||
|
Status: "draft",
|
||||||
|
ScreenshotURLS: datatypes.JSON([]byte(`["/api/files/object?key=listing%2Fscope-tenant.jpg"]`)),
|
||||||
|
}
|
||||||
|
if err := db.Create(&account).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
listing := model.RentalListing{
|
||||||
|
SourceTenantID: 0,
|
||||||
|
ListingNo: "FILE-SCOPE-1",
|
||||||
|
AccountID: account.ID,
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
PriceCent: 1000,
|
||||||
|
Status: "draft",
|
||||||
|
ReviewStatus: "pending",
|
||||||
|
DistributionSiteID: &sites[0].ID,
|
||||||
|
}
|
||||||
|
if err := db.Create(&listing).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
admin := model.AdminUser{Username: "file-scope-admin", PasswordHash: "x", Status: "active", TokenVersion: 1}
|
||||||
|
if err := db.Create(&admin).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
superRole := model.Role{Code: "super_admin", Name: "超级管理员"}
|
||||||
|
if err := db.Create(&superRole).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
superAdmin := model.AdminUser{Username: "file-scope-super", PasswordHash: "x", Status: "active", TokenVersion: 1}
|
||||||
|
if err := db.Create(&superAdmin).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&model.AdminUserRole{AdminID: superAdmin.ID, RoleID: superRole.ID, TenantID: 0}).Error; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
values := fileReferenceValues("listing/scope-tenant.jpg", "listing/scope-tenant.jpg")
|
||||||
|
queries, known := knownAdminFileQueries(t.Context(), db, admin.ID, 101, values)
|
||||||
|
if !known {
|
||||||
|
t.Fatal("商品文件应进入管理员权限校验")
|
||||||
|
}
|
||||||
|
allowed, err := runFileAccessQueries(t.Context(), db, queries)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
t.Fatal("大锤管理员应可读取大锤分销站商品图片")
|
||||||
|
}
|
||||||
|
|
||||||
|
queries, _ = knownAdminFileQueries(t.Context(), db, admin.ID, 202, values)
|
||||||
|
allowed, err = runFileAccessQueries(t.Context(), db, queries)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if allowed {
|
||||||
|
t.Fatal("解忧管理员不得读取大锤分销站商品图片")
|
||||||
|
}
|
||||||
|
|
||||||
|
queries, _ = knownAdminFileQueries(t.Context(), db, superAdmin.ID, 101, values)
|
||||||
|
allowed, err = runFileAccessQueries(t.Context(), db, queries)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
t.Fatal("平台超管应可读取任意租户商品图片")
|
||||||
|
}
|
||||||
|
|
||||||
|
var binding model.FileObjectBinding
|
||||||
|
if err := db.Where("canonical_key = ? AND resource_type = ?", "listing/scope-tenant.jpg", model.FileResourceGameAccount).First(&binding).Error; err != nil {
|
||||||
|
t.Fatalf("load file binding: %v", err)
|
||||||
|
}
|
||||||
|
allowed, err = adminCanAccessFileBinding(t.Context(), db, admin.ID, 101, binding)
|
||||||
|
if err != nil || !allowed {
|
||||||
|
t.Fatalf("大锤管理员绑定鉴权 allowed=%v err=%v, want true", allowed, err)
|
||||||
|
}
|
||||||
|
allowed, err = adminCanAccessFileBinding(t.Context(), db, admin.ID, 202, binding)
|
||||||
|
if err != nil || allowed {
|
||||||
|
t.Fatalf("解忧管理员绑定鉴权 allowed=%v err=%v, want false", allowed, err)
|
||||||
|
}
|
||||||
|
allowed, err = adminCanAccessFileBinding(t.Context(), db, superAdmin.ID, 101, binding)
|
||||||
|
if err != nil || !allowed {
|
||||||
|
t.Fatalf("平台超管绑定鉴权 allowed=%v err=%v, want true", allowed, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -62,10 +62,21 @@ import {
|
|||||||
readAssetString,
|
readAssetString,
|
||||||
} from '@/shared/utils/listingDisplay'
|
} from '@/shared/utils/listingDisplay'
|
||||||
import { formatDateTime } from '@/shared/utils/time'
|
import { formatDateTime } from '@/shared/utils/time'
|
||||||
|
import { useAdminSessionStore } from '@/stores/adminSession'
|
||||||
import { useDistributionSiteStore } from '@/stores/distributionSite'
|
import { useDistributionSiteStore } from '@/stores/distributionSite'
|
||||||
|
|
||||||
const siteStore = useDistributionSiteStore()
|
const siteStore = useDistributionSiteStore()
|
||||||
|
const adminSession = useAdminSessionStore()
|
||||||
const currentSiteName = computed(() => siteStore.siteName || '大锤商行')
|
const currentSiteName = computed(() => siteStore.siteName || '大锤商行')
|
||||||
|
const reviewScopeDescription = computed(() =>
|
||||||
|
adminSession.isSuperAdmin
|
||||||
|
? '平台超级管理员视图:可查看并审核全部租户及其分销站商品。'
|
||||||
|
: '仅审核本站号主提交的站内商品,核对账号资产、上号凭证、渠道溯源与定价风控。'
|
||||||
|
)
|
||||||
|
const reviewPoolLabel = computed(() => (adminSession.isSuperAdmin ? '全租户待审池' : '本站待审池'))
|
||||||
|
const reviewPageTitle = computed(() =>
|
||||||
|
adminSession.isSuperAdmin ? '全平台商品审核' : currentSiteName.value + '商品审核'
|
||||||
|
)
|
||||||
|
|
||||||
type RiskLevel = 'danger' | 'warning' | 'info' | 'success'
|
type RiskLevel = 'danger' | 'warning' | 'info' | 'success'
|
||||||
|
|
||||||
@@ -953,8 +964,8 @@ async function handleScreenshotSaved(payload: { oldUrl: string; newUrl: string }
|
|||||||
<div class="page-header-row review-header">
|
<div class="page-header-row review-header">
|
||||||
<div class="page-header">
|
<div class="page-header">
|
||||||
<p class="eyebrow">Audit Workbench · 审核工作台</p>
|
<p class="eyebrow">Audit Workbench · 审核工作台</p>
|
||||||
<h1>{{ currentSiteName }}商品审核</h1>
|
<h1>{{ reviewPageTitle }}</h1>
|
||||||
<p>仅审核本站号主提交的站内商品,核对账号资产、上号凭证、渠道溯源与定价风控。</p>
|
<p>{{ reviewScopeDescription }}</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="toolbar-actions">
|
<div class="toolbar-actions">
|
||||||
<span v-if="lastRefreshedAt" class="refresh-stamp">
|
<span v-if="lastRefreshedAt" class="refresh-stamp">
|
||||||
@@ -985,7 +996,7 @@ async function handleScreenshotSaved(payload: { oldUrl: string; newUrl: string }
|
|||||||
<div class="summary-card-text">
|
<div class="summary-card-text">
|
||||||
<span>待审核总量</span>
|
<span>待审核总量</span>
|
||||||
<strong>{{ listings.length }}</strong>
|
<strong>{{ listings.length }}</strong>
|
||||||
<small>本站待审池</small>
|
<small>{{ reviewPoolLabel }}</small>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -451,7 +451,7 @@ const currentSiteHint = computed(() => {
|
|||||||
const adminScopeNote = computed(() => {
|
const adminScopeNote = computed(() => {
|
||||||
if (siteStore.isPlatformOps) return '平台超级管理员 · 全平台只读总览'
|
if (siteStore.isPlatformOps) return '平台超级管理员 · 全平台只读总览'
|
||||||
return adminSession.isSuperAdmin
|
return adminSession.isSuperAdmin
|
||||||
? '平台超级管理员 · 当前仅查看本站数据'
|
? '平台超级管理员 · 当前经营站点视图'
|
||||||
: '站点管理员 · 数据仅限当前站点'
|
: '站点管理员 · 数据仅限当前站点'
|
||||||
})
|
})
|
||||||
const adminRoleText = computed(() => {
|
const adminRoleText = computed(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user