fix(tenant): 封堵审计文件与历史用户跨站访问

This commit is contained in:
yml
2026-11-15 22:46:58 +08:00
parent 0cb385d84f
commit 1108df195d
28 changed files with 531 additions and 60 deletions
@@ -6,6 +6,7 @@ import (
"hfb_sys/backend/internal/database"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
)
// TestPublicScopeAndSiteIsolation 覆盖公告中心的分站可见范围:
@@ -175,6 +176,47 @@ func TestAdminListFiltersBySite(t *testing.T) {
}
}
func TestTenantAdminAnnouncementsExcludeLegacyDistributionSite(t *testing.T) {
db := database.NewTestDB()
repo := NewRepository(db)
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
site := model.DistributionSite{
SiteCode: "ann_legacy_tenant", SiteName: "历史公告站",
Domain: "ann-legacy-tenant.example.com", Status: "active",
}
if err := db.Create(&site).Error; err != nil {
t.Fatal(err)
}
rows := []model.Announcement{
{TenantID: 101, Title: "本站公告", Content: "x", Category: "notice", Status: "draft"},
{TenantID: 101, DistributionSiteID: site.ID, Title: "历史分销公告", Content: "y", Category: "notice", Status: "draft"},
{TenantID: 202, Title: "其他站点公告", Content: "z", Category: "notice", Status: "draft"},
}
if err := db.Create(&rows).Error; err != nil {
t.Fatal(err)
}
result, err := repo.AdminList(ctx, AnnouncementListQuery{
AdminSiteID: &site.ID, Page: 1, PageSize: 20,
})
if err != nil {
t.Fatal(err)
}
items := result.Items.([]AnnouncementDTO)
if result.Total != 1 || len(items) != 1 || items[0].Title != "本站公告" {
t.Fatalf("tenant announcement list = %+v", result)
}
created, err := repo.Create(ctx, CreateAnnouncementRequest{
DistributionSiteID: site.ID, Title: "强制本站", Content: "content", Category: "notice",
}, 9)
if err != nil {
t.Fatal(err)
}
if created.TenantID != 101 || created.DistributionSiteID != 0 {
t.Fatalf("tenant announcement scope = %+v", created)
}
}
// TestPublicPopupSelection 覆盖弹窗公告的选取口径:
// 只有 published + is_popup 才参与,站点范围内按优先级取最高的一条,无候选时返回 nil。
func TestPublicPopupSelection(t *testing.T) {