fix(tenant): 封堵审计文件与历史用户跨站访问
This commit is contained in:
@@ -104,17 +104,8 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
continue
|
||||
}
|
||||
tenantID := entry.TenantID
|
||||
if tenantID == 0 {
|
||||
var row struct {
|
||||
TenantID uint64
|
||||
}
|
||||
if err := tx.Table("admin_user_roles").
|
||||
Select("tenant_id").
|
||||
Where("admin_user_id = ? AND tenant_id > 0", entry.AdminUserID).
|
||||
Order("tenant_id ASC").
|
||||
Take(&row).Error; err == nil {
|
||||
tenantID = row.TenantID
|
||||
}
|
||||
if tenantID == 0 && tx.Statement != nil && tx.Statement.Context != nil {
|
||||
tenantID = tenancy.IDFromContext(tx.Statement.Context)
|
||||
}
|
||||
row := model.AdminNotification{
|
||||
TenantID: tenantID,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user