fix(listing): 保留平台超管全量商品查看

平台超管通过权限通配符获得全租户查询标记;普通租户管理员仍按所属租户及分销站隔离。
This commit is contained in:
2026-11-21 15:24:51 +08:00
parent c22d6b749c
commit b4d058ef63
5 changed files with 43 additions and 3 deletions
@@ -111,6 +111,7 @@ func checkAnyPermission(c *gin.Context, rdb *redis.Client, permCodes ...string)
for _, code := range codes { for _, code := range codes {
if code == "*" { if code == "*" {
c.Request = c.Request.WithContext(tenancy.WithAdminAllTenantScope(c.Request.Context()))
return true return true
} }
for _, target := range permCodes { for _, target := range permCodes {
@@ -119,7 +119,7 @@ func TestRequireAnyPermission(t *testing.T) {
c.Next() c.Next()
}) })
r.GET("/test-any", RequireAnyPermission(rdb, "distribution:site_view", "qrcode:view"), func(c *gin.Context) { r.GET("/test-any", RequireAnyPermission(rdb, "distribution:site_view", "qrcode:view"), func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"ok": true}) c.JSON(http.StatusOK, gin.H{"ok": true, "all_tenants": tenancy.HasAdminAllTenantScope(c.Request.Context())})
}) })
return r return r
} }
@@ -161,4 +161,11 @@ func TestRequireAnyPermission(t *testing.T) {
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code) t.Fatalf("expected 200, got %d", w.Code)
} }
var payload map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil {
t.Fatalf("decode wildcard response: %v", err)
}
if payload["all_tenants"] != true {
t.Fatalf("super admin all-tenant scope marker missing: %v", payload)
}
} }
@@ -659,6 +659,25 @@ func TestPendingReviewIncludesDistributionSitesOwnedByTenant(t *testing.T) {
t.Fatalf("pending items(B) leaked tenant A listings: %+v", itemsB) t.Fatalf("pending items(B) leaked tenant A listings: %+v", itemsB)
} }
superCtx := tenancy.WithAdminAllTenantScope(ctxA)
superItems, err := repo.ListPendingReview(superCtx)
if err != nil {
t.Fatalf("ListPendingReview(platform super admin) error = %v", err)
}
if len(superItems) != 4 {
t.Fatalf("platform super admin pending items = %+v, want all 4 active pending listings", superItems)
}
if _, err := repo.FindAdmin(superCtx, siteB.ID); err != nil {
t.Fatalf("platform super admin FindAdmin cross-tenant site: %v", err)
}
superAdminResult, err := repo.ListAdmin(superCtx, AdminListQuery{Page: 1, PageSize: 20})
if err != nil {
t.Fatalf("ListAdmin(platform super admin) error = %v", err)
}
if superAdminResult.Total != 5 || len(superAdminResult.Items) != 5 {
t.Fatalf("platform super admin listings total=%d items=%d, want all 5", superAdminResult.Total, len(superAdminResult.Items))
}
adminResult, err := repo.ListAdmin(ctxA, AdminListQuery{Page: 1, PageSize: 20}) adminResult, err := repo.ListAdmin(ctxA, AdminListQuery{Page: 1, PageSize: 20})
if err != nil { if err != nil {
t.Fatalf("ListAdmin() error = %v", err) t.Fatalf("ListAdmin() error = %v", err)
+13
View File
@@ -10,6 +10,19 @@ import (
type contextKey struct{} type contextKey struct{}
type adminAllTenantsKey struct{}
// WithAdminAllTenantScope 标记当前管理员拥有平台级全租户查看权限。
// 该标记只用于管理后台查询范围,不改变请求 Host 对应的经营租户上下文。
func WithAdminAllTenantScope(ctx context.Context) context.Context {
return context.WithValue(ctx, adminAllTenantsKey{}, true)
}
func HasAdminAllTenantScope(ctx context.Context) bool {
enabled, _ := ctx.Value(adminAllTenantsKey{}).(bool)
return enabled
}
type Scope struct { type Scope struct {
TenantID uint64 TenantID uint64
TenantCode string TenantCode string
+2 -2
View File
@@ -11,13 +11,13 @@ import (
// //
// 主站商品按 source_tenant_id 归属租户;历史分销站商品按站点归属租户判定, // 主站商品按 source_tenant_id 归属租户;历史分销站商品按站点归属租户判定,
// 因为分销用户的 tenant_id 历史数据存在 0/经营租户混用,不能作为唯一依据。 // 因为分销用户的 tenant_id 历史数据存在 0/经营租户混用,不能作为唯一依据。
// 平台超管(tenant_id=0)保持全量可见。 // 平台超管(tenant_id=0 或权限通配符标记)保持全量可见。
func ApplyListingTenantScope(ctx context.Context, query *gorm.DB, listingAlias string) *gorm.DB { func ApplyListingTenantScope(ctx context.Context, query *gorm.DB, listingAlias string) *gorm.DB {
if query == nil { if query == nil {
return query return query
} }
tenantID := IDFromContext(ctx) tenantID := IDFromContext(ctx)
if tenantID == 0 { if tenantID == 0 || HasAdminAllTenantScope(ctx) {
return query return query
} }
sourceTenantColumn := qualifyScopeColumn(listingAlias, "source_tenant_id") sourceTenantColumn := qualifyScopeColumn(listingAlias, "source_tenant_id")