feat: strengthen P0 security and operations

This commit is contained in:
2026-08-01 11:20:02 +08:00
parent f776056834
commit 84c2b0b21d
77 changed files with 24185 additions and 355 deletions

View File

@@ -271,13 +271,18 @@ internal sealed class BackgroundJobConfiguration : IEntityTypeConfiguration<Back
builder.ConfigureTenantEntity("background_jobs");
builder.ConfigureTimestamps();
builder.Property(entity => entity.JobType).HasMaxLength(100);
builder.Property(entity => entity.IdempotencyKey).HasMaxLength(200);
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.Payload).IsJson("{}");
builder.Property(entity => entity.LockedBy).HasMaxLength(200);
builder.Property(entity => entity.LastError).HasMaxLength(4000);
builder.Property(entity => entity.CancellationReason).HasMaxLength(1000);
builder.Property(entity => entity.Result).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.RunAfter, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.JobType, entity.Status, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.JobType, entity.IdempotencyKey })
.IsUnique()
.HasFilter("idempotency_key is not null");
builder.HasOne<ContentAsset>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.OutputAssetId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
@@ -285,6 +290,38 @@ internal sealed class BackgroundJobConfiguration : IEntityTypeConfiguration<Back
}
}
internal sealed class TenantLifecycleOperationConfiguration : IEntityTypeConfiguration<TenantLifecycleOperation>
{
public void Configure(EntityTypeBuilder<TenantLifecycleOperation> builder)
{
builder.ConfigureTenantEntity("tenant_lifecycle_operations");
builder.ConfigureTimestamps();
builder.Property(entity => entity.OperationType).HasSnakeCaseEnum();
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.Reason).HasMaxLength(1000);
builder.Property(entity => entity.LastError).HasMaxLength(4000);
builder.Property(entity => entity.Result).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.OperationType, entity.Status, entity.CreatedAt });
builder.HasOne<ContentAsset>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ExportAssetId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class WorkerHeartbeatConfiguration : IEntityTypeConfiguration<WorkerHeartbeat>
{
public void Configure(EntityTypeBuilder<WorkerHeartbeat> builder)
{
builder.ConfigureEntity("worker_heartbeats");
builder.Property(entity => entity.WorkerId).HasMaxLength(200);
builder.Property(entity => entity.Processor).HasMaxLength(100);
builder.Property(entity => entity.LastError).HasMaxLength(4000);
builder.HasIndex(entity => new { entity.WorkerId, entity.Processor }).IsUnique();
builder.HasIndex(entity => new { entity.Processor, entity.LastHeartbeatAt });
}
}
internal sealed class UserNotificationConfiguration : IEntityTypeConfiguration<UserNotification>
{
public void Configure(EntityTypeBuilder<UserNotification> builder)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,157 @@
using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Tiku.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class P0SystemStrengthening : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "cancellation_reason",
table: "background_jobs",
type: "character varying(1000)",
maxLength: 1000,
nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(
name: "cancellation_requested_at",
table: "background_jobs",
type: "timestamp with time zone",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "cancellation_requested_by",
table: "background_jobs",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "idempotency_key",
table: "background_jobs",
type: "character varying(200)",
maxLength: 200,
nullable: true);
migrationBuilder.CreateTable(
name: "tenant_lifecycle_operations",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
operation_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
requested_by = table.Column<Guid>(type: "uuid", nullable: false),
target_user_id = table.Column<Guid>(type: "uuid", nullable: true),
export_asset_id = table.Column<Guid>(type: "uuid", nullable: true),
reason = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
last_error = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
result = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
started_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
completed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_lifecycle_operations", x => x.id);
table.UniqueConstraint("ak_tenant_lifecycle_operations_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_tenant_lifecycle_operations_content_assets_tenant_id_export~",
columns: x => new { x.tenant_id, x.export_asset_id },
principalTable: "content_assets",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_tenant_lifecycle_operations_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "worker_heartbeats",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
worker_id = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
processor = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
started_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
last_heartbeat_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
last_iteration_started_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
last_iteration_completed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
last_succeeded_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
last_error = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
is_running = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_worker_heartbeats", x => x.id);
});
migrationBuilder.CreateIndex(
name: "ix_background_jobs_tenant_id_job_type_idempotency_key",
table: "background_jobs",
columns: new[] { "tenant_id", "job_type", "idempotency_key" },
unique: true,
filter: "idempotency_key is not null");
migrationBuilder.CreateIndex(
name: "ix_tenant_lifecycle_operations_tenant_id_export_asset_id",
table: "tenant_lifecycle_operations",
columns: new[] { "tenant_id", "export_asset_id" });
migrationBuilder.CreateIndex(
name: "ix_tenant_lifecycle_operations_tenant_id_operation_type_status~",
table: "tenant_lifecycle_operations",
columns: new[] { "tenant_id", "operation_type", "status", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_worker_heartbeats_processor_last_heartbeat_at",
table: "worker_heartbeats",
columns: new[] { "processor", "last_heartbeat_at" });
migrationBuilder.CreateIndex(
name: "ix_worker_heartbeats_worker_id_processor",
table: "worker_heartbeats",
columns: new[] { "worker_id", "processor" },
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "tenant_lifecycle_operations");
migrationBuilder.DropTable(
name: "worker_heartbeats");
migrationBuilder.DropIndex(
name: "ix_background_jobs_tenant_id_job_type_idempotency_key",
table: "background_jobs");
migrationBuilder.DropColumn(
name: "cancellation_reason",
table: "background_jobs");
migrationBuilder.DropColumn(
name: "cancellation_requested_at",
table: "background_jobs");
migrationBuilder.DropColumn(
name: "cancellation_requested_by",
table: "background_jobs");
migrationBuilder.DropColumn(
name: "idempotency_key",
table: "background_jobs");
}
}
}

View File

@@ -10404,6 +10404,19 @@ namespace Tiku.Infrastructure.Persistence.Migrations
.HasColumnName("id")
.HasDefaultValueSql("gen_random_uuid()");
b.Property<string>("CancellationReason")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)")
.HasColumnName("cancellation_reason");
b.Property<DateTimeOffset?>("CancellationRequestedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("cancellation_requested_at");
b.Property<Guid?>("CancellationRequestedBy")
.HasColumnType("uuid")
.HasColumnName("cancellation_requested_by");
b.Property<DateTimeOffset?>("CompletedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("completed_at");
@@ -10414,6 +10427,11 @@ namespace Tiku.Infrastructure.Persistence.Migrations
.HasColumnName("created_at")
.HasDefaultValueSql("now()");
b.Property<string>("IdempotencyKey")
.HasMaxLength(200)
.HasColumnType("character varying(200)")
.HasColumnName("idempotency_key");
b.Property<string>("JobType")
.IsRequired()
.HasMaxLength(100)
@@ -10491,6 +10509,11 @@ namespace Tiku.Infrastructure.Persistence.Migrations
b.HasIndex("TenantId", "OutputAssetId")
.HasDatabaseName("ix_background_jobs_tenant_id_output_asset_id");
b.HasIndex("TenantId", "JobType", "IdempotencyKey")
.IsUnique()
.HasDatabaseName("ix_background_jobs_tenant_id_job_type_idempotency_key")
.HasFilter("idempotency_key is not null");
b.HasIndex("TenantId", "JobType", "Status", "CreatedAt")
.HasDatabaseName("ix_background_jobs_tenant_id_job_type_status_created_at");
@@ -11261,6 +11284,93 @@ namespace Tiku.Infrastructure.Persistence.Migrations
b.ToTable("tenant_content_notifications", (string)null);
});
modelBuilder.Entity("Tiku.Domain.Operations.TenantLifecycleOperation", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasColumnName("id")
.HasDefaultValueSql("gen_random_uuid()");
b.Property<DateTimeOffset?>("CompletedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("completed_at");
b.Property<DateTimeOffset>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("created_at")
.HasDefaultValueSql("now()");
b.Property<Guid?>("ExportAssetId")
.HasColumnType("uuid")
.HasColumnName("export_asset_id");
b.Property<string>("LastError")
.HasMaxLength(4000)
.HasColumnType("character varying(4000)")
.HasColumnName("last_error");
b.Property<string>("OperationType")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)")
.HasColumnName("operation_type");
b.Property<string>("Reason")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)")
.HasColumnName("reason");
b.Property<Guid>("RequestedBy")
.HasColumnType("uuid")
.HasColumnName("requested_by");
b.Property<JsonElement>("Result")
.ValueGeneratedOnAdd()
.HasColumnType("jsonb")
.HasColumnName("result")
.HasDefaultValueSql("'{}'::jsonb");
b.Property<DateTimeOffset?>("StartedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("started_at");
b.Property<string>("Status")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)")
.HasColumnName("status");
b.Property<Guid?>("TargetUserId")
.HasColumnType("uuid")
.HasColumnName("target_user_id");
b.Property<Guid>("TenantId")
.HasColumnType("uuid")
.HasColumnName("tenant_id");
b.Property<DateTimeOffset>("UpdatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("updated_at")
.HasDefaultValueSql("now()");
b.HasKey("Id")
.HasName("pk_tenant_lifecycle_operations");
b.HasAlternateKey("TenantId", "Id")
.HasName("ak_tenant_lifecycle_operations_tenant_id_id");
b.HasIndex("TenantId", "ExportAssetId")
.HasDatabaseName("ix_tenant_lifecycle_operations_tenant_id_export_asset_id");
b.HasIndex("TenantId", "OperationType", "Status", "CreatedAt")
.HasDatabaseName("ix_tenant_lifecycle_operations_tenant_id_operation_type_status~");
b.ToTable("tenant_lifecycle_operations", (string)null);
});
modelBuilder.Entity("Tiku.Domain.Operations.TenantThemeConfig", b =>
{
b.Property<Guid>("Id")
@@ -11637,6 +11747,68 @@ namespace Tiku.Infrastructure.Persistence.Migrations
b.ToTable("user_notifications", (string)null);
});
modelBuilder.Entity("Tiku.Domain.Operations.WorkerHeartbeat", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasColumnName("id")
.HasDefaultValueSql("gen_random_uuid()");
b.Property<bool>("IsRunning")
.HasColumnType("boolean")
.HasColumnName("is_running");
b.Property<string>("LastError")
.HasMaxLength(4000)
.HasColumnType("character varying(4000)")
.HasColumnName("last_error");
b.Property<DateTimeOffset>("LastHeartbeatAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("last_heartbeat_at");
b.Property<DateTimeOffset?>("LastIterationCompletedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("last_iteration_completed_at");
b.Property<DateTimeOffset?>("LastIterationStartedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("last_iteration_started_at");
b.Property<DateTimeOffset?>("LastSucceededAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("last_succeeded_at");
b.Property<string>("Processor")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)")
.HasColumnName("processor");
b.Property<DateTimeOffset>("StartedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("started_at");
b.Property<string>("WorkerId")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("character varying(200)")
.HasColumnName("worker_id");
b.HasKey("Id")
.HasName("pk_worker_heartbeats");
b.HasIndex("Processor", "LastHeartbeatAt")
.HasDatabaseName("ix_worker_heartbeats_processor_last_heartbeat_at");
b.HasIndex("WorkerId", "Processor")
.IsUnique()
.HasDatabaseName("ix_worker_heartbeats_worker_id_processor");
b.ToTable("worker_heartbeats", (string)null);
});
modelBuilder.Entity("Tiku.Domain.Platform.PermissionModule", b =>
{
b.Property<Guid>("Id")
@@ -18883,6 +19055,23 @@ namespace Tiku.Infrastructure.Persistence.Migrations
.HasConstraintName("fk_tenant_content_notifications_question_banks_tenant_id_sourc~");
});
modelBuilder.Entity("Tiku.Domain.Operations.TenantLifecycleOperation", b =>
{
b.HasOne("Tiku.Domain.Tenancy.Tenant", null)
.WithMany()
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
.HasConstraintName("fk_tenant_lifecycle_operations_tenants_tenant_id");
b.HasOne("Tiku.Domain.Content.ContentAsset", null)
.WithMany()
.HasForeignKey("TenantId", "ExportAssetId")
.HasPrincipalKey("TenantId", "Id")
.OnDelete(DeleteBehavior.SetNull)
.HasConstraintName("fk_tenant_lifecycle_operations_content_assets_tenant_id_export~");
});
modelBuilder.Entity("Tiku.Domain.Operations.TenantThemeConfig", b =>
{
b.HasOne("Tiku.Domain.Operations.TenantThemeTemplate", null)

View File

@@ -174,6 +174,8 @@ public sealed class TikuDbContext(
public DbSet<PlatformBackendRoleMenu> PlatformBackendRoleMenus => Set<PlatformBackendRoleMenu>();
public DbSet<PlatformBackendUserRole> PlatformBackendUserRoles => Set<PlatformBackendUserRole>();
public DbSet<BackgroundJob> BackgroundJobs => Set<BackgroundJob>();
public DbSet<TenantLifecycleOperation> TenantLifecycleOperations => Set<TenantLifecycleOperation>();
public DbSet<WorkerHeartbeat> WorkerHeartbeats => Set<WorkerHeartbeat>();
public DbSet<UserNotification> UserNotifications => Set<UserNotification>();
public DbSet<Badge> Badges => Set<Badge>();
public DbSet<UserBadge> UserBadges => Set<UserBadge>();