feat: system and security - start-up, accounts, sign-in, licences, log, jobs, companies (M2)
CI / Build and test (SQLite) (pull_request) Successful in 55s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 5s
CI / Docker image (pull_request) Failing after 2m4s
CI / Build and test (SQLite) (pull_request) Successful in 55s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 5s
CI / Docker image (pull_request) Failing after 2m4s
The instance now starts on its own from KHAEMHAT_* variables: it migrates the system and company databases, creates the super user and the first company, and only then reports ready. Users sign in with password and TOTP through revocable database sessions, and licences decide which modules and how many companies are available. - Configuration: every wrong variable reported at once (exit code 2), _FILE variants - Accounts, password policy and lockout, TOTP with recovery codes, session states with token rotation, security audit with a hash chain - Sign-in pages (mockups 001-003), authorization attributes, security headers and a CSP without inline scripts, login rate limit - Signed licences, activation codes, test mode, licence tool - DataProtection keys in the system database, encrypted with the instance key - Memory or Redis cache (in-house RESP client) that falls back to memory - Database log by section with trace id; cron scheduler with locks and system jobs; record history (audit_log) - Company creation, states, company context and X-Company, 503 in maintenance - In-memory SQLite databases stay alive for the process (tests, demo) - Tests for all of the above, ADR 0019, wiki, SECURITY, CHANGELOG, STATUS, DATA-MODEL Refs #15 #16 #17 #18 #19 #20 #21 #22 #23 #24 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4582837b70
commit
032c602b49
107 files changed
+12202
-116
No files matched your search
@@ -0,0 +1,97 @@
|
||||
using Khaemhat.Core.Instance;
|
||||
using Khaemhat.Core.Security;
|
||||
using Khaemhat.Data.Mapping;
|
||||
using Khaemhat.Data.SystemDatabase;
|
||||
|
||||
namespace Khaemhat.Data.CompanyDatabase;
|
||||
|
||||
/// <summary>Map of <see cref="CompanyUser"/> to <c>users</c>.</summary>
|
||||
public sealed class CompanyUserMap : EntityMap<CompanyUser>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public CompanyUserMap()
|
||||
{
|
||||
ToTable("users");
|
||||
AccountColumns.Configure(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="UserSession"/> to <c>user_sessions</c>.</summary>
|
||||
public sealed class UserSessionMap : EntityMap<UserSession>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public UserSessionMap()
|
||||
{
|
||||
ToTable("user_sessions");
|
||||
SessionColumns.Configure(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="Role"/> to <c>roles</c>.</summary>
|
||||
public sealed class RoleMap : EntityMap<Role>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public RoleMap()
|
||||
{
|
||||
ToTable("roles");
|
||||
Map(x => x.Code).MaxLength(50);
|
||||
Map(x => x.Name).MaxLength(100);
|
||||
Map(x => x.TemplateCode).MaxLength(50);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="RoleGrant"/> to <c>role_permissions</c>.</summary>
|
||||
public sealed class RoleGrantMap : EntityMap<RoleGrant>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public RoleGrantMap()
|
||||
{
|
||||
ToTable("role_permissions");
|
||||
Map(x => x.Permission).MaxLength(100);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="UserRole"/> to <c>user_roles</c>.</summary>
|
||||
public sealed class UserRoleMap : EntityMap<UserRole>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public UserRoleMap() => ToTable("user_roles");
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="UserPermissionOverride"/> to <c>user_permission_overrides</c>.</summary>
|
||||
public sealed class UserPermissionOverrideMap : EntityMap<UserPermissionOverride>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public UserPermissionOverrideMap()
|
||||
{
|
||||
ToTable("user_permission_overrides");
|
||||
Map(x => x.Permission).MaxLength(100);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="AuditEntry"/> to <c>audit_log</c>.</summary>
|
||||
public sealed class AuditEntryMap : EntityMap<AuditEntry>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public AuditEntryMap()
|
||||
{
|
||||
ToTable("audit_log");
|
||||
Map(x => x.Entity).MaxLength(100);
|
||||
Map(x => x.EntityId).MaxLength(100);
|
||||
Map(x => x.Action).MaxLength(20);
|
||||
Map(x => x.UserName).MaxLength(100);
|
||||
Map(x => x.Diff).Json();
|
||||
Map(x => x.TraceId).MaxLength(64);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="Notification"/> to <c>notifications</c>.</summary>
|
||||
public sealed class NotificationMap : EntityMap<Notification>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public NotificationMap()
|
||||
{
|
||||
ToTable("notifications");
|
||||
NotificationColumns.Configure(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.CompanyDatabase.Migrations;
|
||||
|
||||
/// <summary>The company users (SPEC §7.1).</summary>
|
||||
[Migration(20261111_001, "create_users")]
|
||||
public sealed class M20261111_001_CreateUsers : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("users", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("user_name", 100).NotNull();
|
||||
t.String("display_name", 200).NotNull();
|
||||
t.String("email", 254);
|
||||
t.String("kind", 40).NotNull();
|
||||
t.String("password_hash", 200);
|
||||
t.Json("password_history");
|
||||
t.DateTimeUtc("password_changed_at");
|
||||
t.Bool("must_change_password").NotNull().Default(false);
|
||||
t.DateTimeUtc("temporary_password_expires_at");
|
||||
t.Int32("failed_login_count").NotNull().Default(0);
|
||||
t.DateTimeUtc("locked_until");
|
||||
t.Text("totp_secret");
|
||||
t.Bool("totp_enabled").NotNull().Default(false);
|
||||
t.Bool("totp_required").NotNull().Default(false);
|
||||
t.Int64("totp_last_step");
|
||||
t.Json("totp_recovery_codes");
|
||||
t.String("recovery_token_hash", 64);
|
||||
t.DateTimeUtc("recovery_token_expires_at");
|
||||
t.Bool("is_active").NotNull().Default(true);
|
||||
t.DateTimeUtc("last_login_at");
|
||||
t.Bool("own_customers_only").NotNull().Default(false);
|
||||
t.Bool("can_see_costs").NotNull().Default(false);
|
||||
t.Bool("can_export").NotNull().Default(false);
|
||||
t.Bool("can_use_api").NotNull().Default(false);
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_users_user_name", "user_name");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.CompanyDatabase.Migrations;
|
||||
|
||||
/// <summary>The sessions of the company users (SPEC §7.3).</summary>
|
||||
[Migration(20261111_002, "create_user_sessions")]
|
||||
public sealed class M20261111_002_CreateUserSessions : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("user_sessions", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("token_hash", 64).NotNull();
|
||||
t.Int64("user_id").NotNull();
|
||||
t.String("state", 40).NotNull();
|
||||
t.String("company_code", 20);
|
||||
t.String("ip_address", 64);
|
||||
t.String("user_agent", 400);
|
||||
t.Bool("persistent").NotNull().Default(false);
|
||||
t.DateTimeUtc("last_seen_at").NotNull();
|
||||
t.DateTimeUtc("expires_at").NotNull();
|
||||
t.DateTimeUtc("revoked_at");
|
||||
t.String("revoked_reason", 100);
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_user_sessions_token_hash", "token_hash");
|
||||
t.Index("ix_user_sessions_user_id", "user_id");
|
||||
t.ForeignKey("fk_user_sessions_user", "user_id", "users", ReferentialAction.Cascade);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.CompanyDatabase.Migrations;
|
||||
|
||||
/// <summary>Roles, their permissions, the roles of each user and the per-user overrides (SPEC §7.2).</summary>
|
||||
[Migration(20261111_003, "create_roles")]
|
||||
public sealed class M20261111_003_CreateRoles : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("roles", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("code", 50).NotNull();
|
||||
t.String("name", 100).NotNull();
|
||||
t.Text("description");
|
||||
t.Bool("is_predefined").NotNull().Default(false);
|
||||
t.String("template_code", 50);
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_roles_code", "code");
|
||||
});
|
||||
|
||||
schema.CreateTable("role_permissions", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.Int64("role_id").NotNull();
|
||||
t.String("permission", 100).NotNull();
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_role_permissions_role_id_permission", "role_id", "permission");
|
||||
t.ForeignKey("fk_role_permissions_role", "role_id", "roles", ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
schema.CreateTable("user_roles", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.Int64("user_id").NotNull();
|
||||
t.Int64("role_id").NotNull();
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_user_roles_user_id_role_id", "user_id", "role_id");
|
||||
t.ForeignKey("fk_user_roles_user", "user_id", "users", ReferentialAction.Cascade);
|
||||
t.ForeignKey("fk_user_roles_role", "role_id", "roles", ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
schema.CreateTable("user_permission_overrides", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.Int64("user_id").NotNull();
|
||||
t.String("permission", 100).NotNull();
|
||||
t.Bool("allowed").NotNull();
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_user_permission_overrides_user_id_permission", "user_id", "permission");
|
||||
t.ForeignKey("fk_user_permission_overrides_user", "user_id", "users", ReferentialAction.Cascade);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.CompanyDatabase.Migrations;
|
||||
|
||||
/// <summary>The history of changes to company data (SPEC §7.3).</summary>
|
||||
[Migration(20261111_004, "create_audit_log")]
|
||||
public sealed class M20261111_004_CreateAuditLog : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("audit_log", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.DateTimeUtc("at").NotNull();
|
||||
t.String("entity", 100).NotNull();
|
||||
t.String("entity_id", 100).NotNull();
|
||||
t.String("action", 20).NotNull();
|
||||
t.String("user_name", 100);
|
||||
t.Json("diff");
|
||||
t.String("trace_id", 64);
|
||||
t.Index("ix_audit_log_entity_entity_id", "entity", "entity_id");
|
||||
t.Index("ix_audit_log_at", "at");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.CompanyDatabase.Migrations;
|
||||
|
||||
/// <summary>The notifications of the company users (SPEC §9).</summary>
|
||||
[Migration(20261111_005, "create_notifications")]
|
||||
public sealed class M20261111_005_CreateNotifications : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("notifications", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.Int64("recipient_user_id");
|
||||
t.String("category", 40).NotNull();
|
||||
t.String("severity", 40).NotNull();
|
||||
t.String("title", 200).NotNull();
|
||||
t.Text("body");
|
||||
t.String("link", 400);
|
||||
t.DateTimeUtc("read_at");
|
||||
t.AuditColumns();
|
||||
t.Index("ix_notifications_recipient_user_id", "recipient_user_id");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,15 @@ public sealed class DataAccessContext
|
||||
/// <summary>The current user name, or <see langword="null"/>.</summary>
|
||||
public string? CurrentUser => _currentUser();
|
||||
|
||||
/// <summary>Statements slower than this are reported to <see cref="SlowQueryObserver"/> (SPEC §9.5: 500 ms).</summary>
|
||||
public TimeSpan SlowQueryThreshold { get; init; } = TimeSpan.FromMilliseconds(500);
|
||||
|
||||
/// <summary>
|
||||
/// Receives the statements slower than <see cref="SlowQueryThreshold"/>: the host writes them to the log section
|
||||
/// <i>Prestazioni</i>. The SQL text carries no values: they are always parameters.
|
||||
/// </summary>
|
||||
public Action<SlowQuery>? SlowQueryObserver { get; init; }
|
||||
|
||||
/// <summary>The current UTC time, truncated to microseconds (the precision of PostgreSQL and of SQLite storage).</summary>
|
||||
public DateTime UtcNow => Truncate(Clock.GetUtcNow().UtcDateTime);
|
||||
|
||||
@@ -41,3 +50,9 @@ public sealed class DataAccessContext
|
||||
/// <returns>The truncated timestamp.</returns>
|
||||
public static DateTime Truncate(DateTime value) => new(value.Ticks - (value.Ticks % 10), value.Kind);
|
||||
}
|
||||
|
||||
/// <summary>A statement that took longer than <see cref="DataAccessContext.SlowQueryThreshold"/>.</summary>
|
||||
/// <param name="Database">The database name.</param>
|
||||
/// <param name="Sql">The SQL text (parameters, never values).</param>
|
||||
/// <param name="Duration">How long it took.</param>
|
||||
public sealed record SlowQuery(string Database, string Sql, TimeSpan Duration);
|
||||
@@ -218,6 +218,20 @@ public sealed class UnitOfWork : IUnitOfWork
|
||||
string text, IEnumerable<SqlParameterValue> parameters, Func<DbCommand, CancellationToken, Task<T>> run, CancellationToken cancellationToken)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
var started = System.Diagnostics.Stopwatch.GetTimestamp();
|
||||
try
|
||||
{
|
||||
return await WriteCoreAsync(text, parameters, run, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Observe(text, started);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<T> WriteCoreAsync<T>(
|
||||
string text, IEnumerable<SqlParameterValue> parameters, Func<DbCommand, CancellationToken, Task<T>> run, CancellationToken cancellationToken)
|
||||
{
|
||||
if (Transaction is not null)
|
||||
{
|
||||
await using var command = Executor.CreateCommand(Connection, Transaction, text, parameters);
|
||||
@@ -260,9 +274,34 @@ public sealed class UnitOfWork : IUnitOfWork
|
||||
return rows;
|
||||
}
|
||||
|
||||
return Transaction is not null
|
||||
? await Run(cancellationToken).ConfigureAwait(false)
|
||||
: await Context.RetryPolicy.ExecuteAsync(Run, Provider.IsTransient, cancellationToken).ConfigureAwait(false);
|
||||
var started = System.Diagnostics.Stopwatch.GetTimestamp();
|
||||
try
|
||||
{
|
||||
return Transaction is not null
|
||||
? await Run(cancellationToken).ConfigureAwait(false)
|
||||
: await Context.RetryPolicy.ExecuteAsync(Run, Provider.IsTransient, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (Context.SlowQueryObserver is not null)
|
||||
{
|
||||
Observe(sql.Render(Dialect).Text, started);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Observe(string text, long started)
|
||||
{
|
||||
if (Context.SlowQueryObserver is not { } observer)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var elapsed = System.Diagnostics.Stopwatch.GetElapsedTime(started);
|
||||
if (elapsed >= Context.SlowQueryThreshold)
|
||||
{
|
||||
observer(new SlowQuery(Target.DatabaseName, text, elapsed));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task EndTransactionAsync()
|
||||
|
||||
@@ -156,7 +156,7 @@ public abstract class EntityMap<T> : IEntityMap
|
||||
/// <typeparam name="TProperty">The property type.</typeparam>
|
||||
/// <param name="property">The property, for example <c>x => x.Code</c>.</param>
|
||||
/// <returns>The column map to configure.</returns>
|
||||
protected ColumnMap Map<TProperty>(Expression<Func<T, TProperty>> property)
|
||||
protected internal ColumnMap Map<TProperty>(Expression<Func<T, TProperty>> property)
|
||||
{
|
||||
if (_columns is not null)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>The instance accounts (SPEC §7.1): the super user and the administrators.</summary>
|
||||
[Migration(20261111_003, "create_sys_users", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_003_CreateSysUsers : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_users", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("user_name", 100).NotNull();
|
||||
t.String("display_name", 200).NotNull();
|
||||
t.String("email", 254);
|
||||
t.String("level", 40).NotNull();
|
||||
t.String("password_hash", 200);
|
||||
t.Json("password_history");
|
||||
t.DateTimeUtc("password_changed_at");
|
||||
t.Bool("must_change_password").NotNull().Default(false);
|
||||
t.DateTimeUtc("temporary_password_expires_at");
|
||||
t.Int32("failed_login_count").NotNull().Default(0);
|
||||
t.DateTimeUtc("locked_until");
|
||||
t.Text("totp_secret");
|
||||
t.Bool("totp_enabled").NotNull().Default(false);
|
||||
t.Int64("totp_last_step");
|
||||
t.Json("totp_recovery_codes");
|
||||
t.String("recovery_token_hash", 64);
|
||||
t.DateTimeUtc("recovery_token_expires_at");
|
||||
t.Bool("is_active").NotNull().Default(true);
|
||||
t.DateTimeUtc("last_login_at");
|
||||
t.String("linked_company_code", 20);
|
||||
t.Int64("linked_user_id");
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_sys_users_user_name", "user_name");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>The sessions of the instance accounts (SPEC §7.3).</summary>
|
||||
[Migration(20261111_004, "create_sys_sessions", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_004_CreateSysSessions : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_sessions", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("token_hash", 64).NotNull();
|
||||
t.Int64("user_id").NotNull();
|
||||
t.String("state", 40).NotNull();
|
||||
t.String("company_code", 20);
|
||||
t.String("ip_address", 64);
|
||||
t.String("user_agent", 400);
|
||||
t.Bool("persistent").NotNull().Default(false);
|
||||
t.DateTimeUtc("last_seen_at").NotNull();
|
||||
t.DateTimeUtc("expires_at").NotNull();
|
||||
t.DateTimeUtc("revoked_at");
|
||||
t.String("revoked_reason", 100);
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_sys_sessions_token_hash", "token_hash");
|
||||
t.Index("ix_sys_sessions_user_id", "user_id");
|
||||
t.ForeignKey("fk_sys_sessions_user", "user_id", "sys_users", ReferentialAction.Cascade);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>The predefined roles copied into each company (SPEC §7.2).</summary>
|
||||
[Migration(20261111_005, "create_sys_roles_templates", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_005_CreateSysRolesTemplates : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_roles_templates", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("code", 50).NotNull();
|
||||
t.String("name", 100).NotNull();
|
||||
t.Text("description");
|
||||
t.Json("permissions").NotNull();
|
||||
t.Int32("sort_order").NotNull().Default(0);
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_sys_roles_templates_code", "code");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>The installed licences and their history (SPEC §8.2).</summary>
|
||||
[Migration(20261111_006, "create_sys_licenses", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_006_CreateSysLicenses : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_licenses", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("source", 40).NotNull();
|
||||
t.Text("payload").NotNull();
|
||||
t.String("licensee", 200);
|
||||
t.String("modules", 400).NotNull();
|
||||
t.Int32("max_concurrent_users").NotNull().Default(0);
|
||||
t.Int32("max_companies").NotNull().Default(0);
|
||||
t.DateTimeUtc("issued_at");
|
||||
t.DateTimeUtc("expires_at");
|
||||
t.Int32("grace_period_days").NotNull().Default(0);
|
||||
t.Bool("is_active").NotNull().Default(true);
|
||||
t.AuditColumns();
|
||||
});
|
||||
|
||||
schema.CreateTable("sys_license_activations", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("action", 40).NotNull();
|
||||
t.String("source", 40).NotNull();
|
||||
t.String("modules", 400);
|
||||
t.String("outcome", 60).NotNull();
|
||||
t.Text("message");
|
||||
t.AuditColumns();
|
||||
});
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>The DataProtection keys of the cookies and antiforgery tokens (SPEC §5.4).</summary>
|
||||
[Migration(20261111_007, "create_sys_data_protection_keys", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_007_CreateSysDataProtectionKeys : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_data_protection_keys", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("friendly_name", 200).NotNull();
|
||||
t.Text("xml").NotNull();
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_sys_data_protection_keys_friendly_name", "friendly_name");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>The application log by section (SPEC §9.5).</summary>
|
||||
[Migration(20261111_008, "create_sys_log", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_008_CreateSysLog : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_log", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.DateTimeUtc("at").NotNull();
|
||||
t.String("level", 20).NotNull();
|
||||
t.String("section", 40).NotNull();
|
||||
t.String("source", 200).NotNull();
|
||||
t.Text("message").NotNull();
|
||||
t.Text("exception");
|
||||
t.String("user_name", 100);
|
||||
t.String("company_code", 20);
|
||||
t.String("trace_id", 64);
|
||||
t.String("request", 400);
|
||||
t.String("entity", 100);
|
||||
t.String("entity_id", 100);
|
||||
t.Json("data");
|
||||
t.Index("ix_sys_log_at", "at");
|
||||
t.Index("ix_sys_log_section_at", "section", "at");
|
||||
t.Index("ix_sys_log_trace_id", "trace_id");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>The immutable, hash-chained audit of security and instance actions (SPEC §7.3).</summary>
|
||||
[Migration(20261111_009, "create_sys_security_audit", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_009_CreateSysSecurityAudit : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_security_audit", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.DateTimeUtc("at").NotNull();
|
||||
t.String("action", 100).NotNull();
|
||||
t.String("user_name", 100);
|
||||
t.String("company_code", 20);
|
||||
t.String("ip_address", 64);
|
||||
t.String("target", 200);
|
||||
t.Json("details");
|
||||
t.String("previous_hash", 64).NotNull();
|
||||
t.String("hash", 64).NotNull();
|
||||
t.Index("ix_sys_security_audit_at", "at");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>Scheduled tasks, their runs and the locks that keep a task from running twice (SPEC §9).</summary>
|
||||
[Migration(20261111_010, "create_sys_scheduler", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_010_CreateSysScheduler : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_scheduled_tasks", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("code", 100).NotNull();
|
||||
t.String("name", 200).NotNull();
|
||||
t.String("job_type", 100).NotNull();
|
||||
t.String("company_code", 20);
|
||||
t.String("cron", 100).NotNull();
|
||||
t.String("time_zone", 64).NotNull();
|
||||
t.Int32("timeout_seconds").NotNull().Default(600);
|
||||
t.Int32("max_retries").NotNull().Default(0);
|
||||
t.Bool("is_enabled").NotNull().Default(true);
|
||||
t.Bool("is_system").NotNull().Default(false);
|
||||
t.Json("parameters");
|
||||
t.DateTimeUtc("last_run_at");
|
||||
t.DateTimeUtc("next_run_at");
|
||||
t.String("last_outcome", 40);
|
||||
t.AuditColumns();
|
||||
t.Unique("ux_sys_scheduled_tasks_code", "code");
|
||||
});
|
||||
|
||||
schema.CreateTable("sys_job_runs", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.String("task_code", 100).NotNull();
|
||||
t.DateTimeUtc("started_at").NotNull();
|
||||
t.DateTimeUtc("finished_at");
|
||||
t.String("outcome", 40).NotNull();
|
||||
t.Int32("attempt").NotNull().Default(1);
|
||||
t.Text("message");
|
||||
t.String("trace_id", 64);
|
||||
t.Index("ix_sys_job_runs_task_code_started_at", "task_code", "started_at");
|
||||
});
|
||||
|
||||
schema.CreateTable("sys_job_locks", t =>
|
||||
{
|
||||
t.String("id", 100).NotNull();
|
||||
t.String("owner", 100).NotNull();
|
||||
t.DateTimeUtc("acquired_at").NotNull();
|
||||
t.DateTimeUtc("expires_at").NotNull();
|
||||
t.PrimaryKey("id");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Khaemhat.Data.Migrations;
|
||||
using Khaemhat.Data.Schema;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase.Migrations;
|
||||
|
||||
/// <summary>The notifications of the instance accounts (SPEC §9).</summary>
|
||||
[Migration(20261111_011, "create_sys_notifications", Target = MigrationTarget.System)]
|
||||
public sealed class M20261111_011_CreateSysNotifications : IMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Up(ISchemaBuilder schema)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(schema);
|
||||
schema.CreateTable("sys_notifications", t =>
|
||||
{
|
||||
t.Id();
|
||||
t.Int64("recipient_user_id");
|
||||
t.String("category", 40).NotNull();
|
||||
t.String("severity", 40).NotNull();
|
||||
t.String("title", 200).NotNull();
|
||||
t.Text("body");
|
||||
t.String("link", 400);
|
||||
t.DateTimeUtc("read_at");
|
||||
t.AuditColumns();
|
||||
t.Index("ix_sys_notifications_recipient_user_id", "recipient_user_id");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
using Khaemhat.Core.Instance;
|
||||
using Khaemhat.Core.Security;
|
||||
using Khaemhat.Data.Mapping;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase;
|
||||
|
||||
/// <summary>The account columns shared by <c>sys_users</c> and <c>users</c>.</summary>
|
||||
internal static class AccountColumns
|
||||
{
|
||||
public static void Configure<T>(EntityMap<T> map)
|
||||
where T : class, IAccount, new()
|
||||
{
|
||||
map.Map(x => x.UserName).MaxLength(100);
|
||||
map.Map(x => x.DisplayName).MaxLength(200);
|
||||
map.Map(x => x.Email).MaxLength(254);
|
||||
map.Map(x => x.PasswordHash).MaxLength(200);
|
||||
map.Map(x => x.PasswordHistory).Json();
|
||||
map.Map(x => x.TotpRecoveryCodes).Json();
|
||||
map.Map(x => x.RecoveryTokenHash).MaxLength(64);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="SystemUser"/> to <c>sys_users</c>.</summary>
|
||||
public sealed class SystemUserMap : EntityMap<SystemUser>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public SystemUserMap()
|
||||
{
|
||||
ToTable("sys_users");
|
||||
AccountColumns.Configure(this);
|
||||
Map(x => x.LinkedCompanyCode).MaxLength(20);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="SystemSession"/> to <c>sys_sessions</c>.</summary>
|
||||
public sealed class SystemSessionMap : EntityMap<SystemSession>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public SystemSessionMap()
|
||||
{
|
||||
ToTable("sys_sessions");
|
||||
SessionColumns.Configure(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The session columns shared by <c>sys_sessions</c> and <c>user_sessions</c>.</summary>
|
||||
internal static class SessionColumns
|
||||
{
|
||||
public static void Configure<T>(EntityMap<T> map)
|
||||
where T : SessionRecord, new()
|
||||
{
|
||||
map.Map(x => x.TokenHash).MaxLength(64);
|
||||
map.Map(x => x.CompanyCode).MaxLength(20);
|
||||
map.Map(x => x.IpAddress).MaxLength(64);
|
||||
map.Map(x => x.UserAgent).MaxLength(400);
|
||||
map.Map(x => x.RevokedReason).MaxLength(100);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="RoleTemplate"/> to <c>sys_roles_templates</c>.</summary>
|
||||
public sealed class RoleTemplateMap : EntityMap<RoleTemplate>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public RoleTemplateMap()
|
||||
{
|
||||
ToTable("sys_roles_templates");
|
||||
Map(x => x.Code).MaxLength(50);
|
||||
Map(x => x.Name).MaxLength(100);
|
||||
Map(x => x.Permissions).Json();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="LicenseRecord"/> to <c>sys_licenses</c>.</summary>
|
||||
public sealed class LicenseRecordMap : EntityMap<LicenseRecord>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public LicenseRecordMap()
|
||||
{
|
||||
ToTable("sys_licenses");
|
||||
Map(x => x.Licensee).MaxLength(200);
|
||||
Map(x => x.Modules).MaxLength(400);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="LicenseActivation"/> to <c>sys_license_activations</c>.</summary>
|
||||
public sealed class LicenseActivationMap : EntityMap<LicenseActivation>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public LicenseActivationMap()
|
||||
{
|
||||
ToTable("sys_license_activations");
|
||||
Map(x => x.Action).MaxLength(40);
|
||||
Map(x => x.Modules).MaxLength(400);
|
||||
Map(x => x.Outcome).MaxLength(60);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="DataProtectionKey"/> to <c>sys_data_protection_keys</c>.</summary>
|
||||
public sealed class DataProtectionKeyMap : EntityMap<DataProtectionKey>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public DataProtectionKeyMap()
|
||||
{
|
||||
ToTable("sys_data_protection_keys");
|
||||
Map(x => x.FriendlyName).MaxLength(200);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="LogEntry"/> to <c>sys_log</c>.</summary>
|
||||
public sealed class LogEntryMap : EntityMap<LogEntry>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public LogEntryMap()
|
||||
{
|
||||
ToTable("sys_log");
|
||||
Map(x => x.Level).MaxLength(20);
|
||||
Map(x => x.Section).MaxLength(40);
|
||||
Map(x => x.Source).MaxLength(200);
|
||||
Map(x => x.UserName).MaxLength(100);
|
||||
Map(x => x.CompanyCode).MaxLength(20);
|
||||
Map(x => x.TraceId).MaxLength(64);
|
||||
Map(x => x.Request).MaxLength(400);
|
||||
Map(x => x.Entity).MaxLength(100);
|
||||
Map(x => x.EntityId).MaxLength(100);
|
||||
Map(x => x.Data).Json();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="SecurityAuditEntry"/> to <c>sys_security_audit</c>.</summary>
|
||||
public sealed class SecurityAuditEntryMap : EntityMap<SecurityAuditEntry>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public SecurityAuditEntryMap()
|
||||
{
|
||||
ToTable("sys_security_audit");
|
||||
Map(x => x.Action).MaxLength(100);
|
||||
Map(x => x.UserName).MaxLength(100);
|
||||
Map(x => x.CompanyCode).MaxLength(20);
|
||||
Map(x => x.IpAddress).MaxLength(64);
|
||||
Map(x => x.Target).MaxLength(200);
|
||||
Map(x => x.Details).Json();
|
||||
Map(x => x.PreviousHash).MaxLength(64);
|
||||
Map(x => x.Hash).MaxLength(64);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="ScheduledTask"/> to <c>sys_scheduled_tasks</c>.</summary>
|
||||
public sealed class ScheduledTaskMap : EntityMap<ScheduledTask>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public ScheduledTaskMap()
|
||||
{
|
||||
ToTable("sys_scheduled_tasks");
|
||||
Map(x => x.Code).MaxLength(100);
|
||||
Map(x => x.Name).MaxLength(200);
|
||||
Map(x => x.JobType).MaxLength(100);
|
||||
Map(x => x.CompanyCode).MaxLength(20);
|
||||
Map(x => x.Cron).MaxLength(100);
|
||||
Map(x => x.TimeZone).MaxLength(64);
|
||||
Map(x => x.Parameters).Json();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="JobRun"/> to <c>sys_job_runs</c>.</summary>
|
||||
public sealed class JobRunMap : EntityMap<JobRun>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public JobRunMap()
|
||||
{
|
||||
ToTable("sys_job_runs");
|
||||
Map(x => x.TaskCode).MaxLength(100);
|
||||
Map(x => x.TraceId).MaxLength(64);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="JobLock"/> to <c>sys_job_locks</c>.</summary>
|
||||
public sealed class JobLockMap : EntityMap<JobLock>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public JobLockMap()
|
||||
{
|
||||
ToTable("sys_job_locks");
|
||||
HasKey(x => x.Id, identity: false);
|
||||
Map(x => x.Id).MaxLength(100);
|
||||
Map(x => x.Owner).MaxLength(100);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="SystemNotification"/> to <c>sys_notifications</c>.</summary>
|
||||
public sealed class SystemNotificationMap : EntityMap<SystemNotification>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public SystemNotificationMap()
|
||||
{
|
||||
ToTable("sys_notifications");
|
||||
NotificationColumns.Configure(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The notification columns shared by <c>sys_notifications</c> and <c>notifications</c>.</summary>
|
||||
internal static class NotificationColumns
|
||||
{
|
||||
public static void Configure<T>(EntityMap<T> map)
|
||||
where T : NotificationRecord, new()
|
||||
{
|
||||
map.Map(x => x.Category).MaxLength(40);
|
||||
map.Map(x => x.Title).MaxLength(200);
|
||||
map.Map(x => x.Link).MaxLength(400);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user