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
@@ -7,6 +7,9 @@ import (
"hfb_sys/backend/internal/database"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
"gorm.io/gorm"
)
func TestRepositoryUnreadCountAndMarkRead(t *testing.T) {
@@ -103,3 +106,31 @@ func TestRepositoryMarkReadNotFound(t *testing.T) {
t.Fatalf("MarkRead() error = %v, want ErrNotificationNotFound", err)
}
}
func TestAppendUsesExplicitOrContextTenant(t *testing.T) {
db := database.NewTestDB()
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202})
appendNotification := func(entry Entry) {
t.Helper()
if err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
return Append(tx, entry)
}); err != nil {
t.Fatal(err)
}
}
appendNotification(Entry{AdminUserID: 7, Type: "system", Title: "上下文站点", Content: "x"})
appendNotification(Entry{TenantID: 101, AdminUserID: 7, Type: "system", Title: "显式站点", Content: "y"})
if err := db.Transaction(func(tx *gorm.DB) error {
return Append(tx, Entry{AdminUserID: 7, Type: "platform", Title: "平台站点", Content: "z"})
}); err != nil {
t.Fatal(err)
}
var rows []model.AdminNotification
if err := db.Order("id ASC").Find(&rows).Error; err != nil {
t.Fatal(err)
}
if len(rows) != 3 || rows[0].TenantID != 202 || rows[1].TenantID != 101 || rows[2].TenantID != 0 {
t.Fatalf("admin notification tenants = %+v", rows)
}
}