fix(listing): 保留平台超管全量商品查看
平台超管通过权限通配符获得全租户查询标记;普通租户管理员仍按所属租户及分销站隔离。
This commit is contained in:
@@ -111,6 +111,7 @@ func checkAnyPermission(c *gin.Context, rdb *redis.Client, permCodes ...string)
|
||||
|
||||
for _, code := range codes {
|
||||
if code == "*" {
|
||||
c.Request = c.Request.WithContext(tenancy.WithAdminAllTenantScope(c.Request.Context()))
|
||||
return true
|
||||
}
|
||||
for _, target := range permCodes {
|
||||
|
||||
@@ -119,7 +119,7 @@ func TestRequireAnyPermission(t *testing.T) {
|
||||
c.Next()
|
||||
})
|
||||
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
|
||||
}
|
||||
@@ -161,4 +161,11 @@ func TestRequireAnyPermission(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
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)
|
||||
}
|
||||
|
||||
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})
|
||||
if err != nil {
|
||||
t.Fatalf("ListAdmin() error = %v", err)
|
||||
|
||||
@@ -10,6 +10,19 @@ import (
|
||||
|
||||
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 {
|
||||
TenantID uint64
|
||||
TenantCode string
|
||||
|
||||
@@ -11,13 +11,13 @@ import (
|
||||
//
|
||||
// 主站商品按 source_tenant_id 归属租户;历史分销站商品按站点归属租户判定,
|
||||
// 因为分销用户的 tenant_id 历史数据存在 0/经营租户混用,不能作为唯一依据。
|
||||
// 平台超管(tenant_id=0)保持全量可见。
|
||||
// 平台超管(tenant_id=0 或权限通配符标记)保持全量可见。
|
||||
func ApplyListingTenantScope(ctx context.Context, query *gorm.DB, listingAlias string) *gorm.DB {
|
||||
if query == nil {
|
||||
return query
|
||||
}
|
||||
tenantID := IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
if tenantID == 0 || HasAdminAllTenantScope(ctx) {
|
||||
return query
|
||||
}
|
||||
sourceTenantColumn := qualifyScopeColumn(listingAlias, "source_tenant_id")
|
||||
|
||||
Reference in New Issue
Block a user