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,102 @@
|
||||
using Khaemhat.Core.Instance;
|
||||
using Khaemhat.Data;
|
||||
using Khaemhat.Data.Querying;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Khaemhat.Application.Notifications;
|
||||
|
||||
/// <summary>A notification to deliver.</summary>
|
||||
/// <param name="Category">The area: <c>security</c>, <c>jobs</c>, <c>system</c>, <c>licenses</c>...</param>
|
||||
/// <param name="Severity">The weight.</param>
|
||||
/// <param name="Title">The title.</param>
|
||||
/// <param name="Body">The text.</param>
|
||||
/// <param name="Link">A link inside the application.</param>
|
||||
public sealed record NotificationMessage(string Category, NotificationSeverity Severity, string Title, string? Body = null, string? Link = null);
|
||||
|
||||
/// <summary>
|
||||
/// Delivers in-app notifications (SPEC §9; mockup 010): to instance accounts in <c>sys_notifications</c>, to company
|
||||
/// users in the company's <c>notifications</c>. A notification without recipient goes to everyone in the scope.
|
||||
/// Delivery never breaks the caller: a failure is logged.
|
||||
/// </summary>
|
||||
/// <param name="factory">Opens the databases.</param>
|
||||
/// <param name="logger">The log.</param>
|
||||
public sealed class NotificationDispatcher(IUnitOfWorkFactory factory, ILogger<NotificationDispatcher> logger)
|
||||
{
|
||||
/// <summary>Notifies instance accounts.</summary>
|
||||
/// <param name="message">The notification.</param>
|
||||
/// <param name="recipientUserId">One account, or <see langword="null"/> for all of them.</param>
|
||||
/// <param name="cancellationToken">Cancellation.</param>
|
||||
/// <returns>A task.</returns>
|
||||
public async Task NotifyInstanceAsync(NotificationMessage message, long? recipientUserId = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(message);
|
||||
try
|
||||
{
|
||||
await using var unitOfWork = await factory.CreateAsync(DatabaseScope.System, cancellationToken).ConfigureAwait(false);
|
||||
await unitOfWork.Repository<SystemNotification, long>().InsertAsync(Fill(new SystemNotification(), message, recipientUserId), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogError(ex, "Notification {Title} not delivered to instance accounts.", message.Title);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Notifies the users of a company.</summary>
|
||||
/// <param name="companyCode">The company.</param>
|
||||
/// <param name="message">The notification.</param>
|
||||
/// <param name="recipientUserId">One user, or <see langword="null"/> for all of them.</param>
|
||||
/// <param name="cancellationToken">Cancellation.</param>
|
||||
/// <returns>A task.</returns>
|
||||
public async Task NotifyCompanyAsync(string companyCode, NotificationMessage message, long? recipientUserId = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(message);
|
||||
try
|
||||
{
|
||||
await using var unitOfWork = await factory.CreateAsync(DatabaseScope.Company(companyCode), cancellationToken).ConfigureAwait(false);
|
||||
await unitOfWork.Repository<Notification, long>().InsertAsync(Fill(new Notification(), message, recipientUserId), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogError(ex, "Notification {Title} not delivered to company {Company}.", message.Title, companyCode);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The unread notifications of an instance account (its own and those for everyone).</summary>
|
||||
/// <param name="userId">The account.</param>
|
||||
/// <param name="cancellationToken">Cancellation.</param>
|
||||
/// <returns>The notifications, newest first.</returns>
|
||||
public async Task<IReadOnlyList<SystemNotification>> UnreadForInstanceAsync(long userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var unitOfWork = await factory.CreateAsync(DatabaseScope.System, cancellationToken).ConfigureAwait(false);
|
||||
return (await unitOfWork.Repository<SystemNotification, long>().ListAsync(
|
||||
Query.For<SystemNotification>().Where(n => n.ReadAt == null && (n.RecipientUserId == null || n.RecipientUserId == userId)).OrderByDescending(n => n.Id).Take(100),
|
||||
cancellationToken).ConfigureAwait(false)).Items;
|
||||
}
|
||||
|
||||
/// <summary>The unread notifications of a company user.</summary>
|
||||
/// <param name="companyCode">The company.</param>
|
||||
/// <param name="userId">The user.</param>
|
||||
/// <param name="cancellationToken">Cancellation.</param>
|
||||
/// <returns>The notifications, newest first.</returns>
|
||||
public async Task<IReadOnlyList<Notification>> UnreadForUserAsync(string companyCode, long userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var unitOfWork = await factory.CreateAsync(DatabaseScope.Company(companyCode), cancellationToken).ConfigureAwait(false);
|
||||
return (await unitOfWork.Repository<Notification, long>().ListAsync(
|
||||
Query.For<Notification>().Where(n => n.ReadAt == null && (n.RecipientUserId == null || n.RecipientUserId == userId)).OrderByDescending(n => n.Id).Take(100),
|
||||
cancellationToken).ConfigureAwait(false)).Items;
|
||||
}
|
||||
|
||||
private static T Fill<T>(T notification, NotificationMessage message, long? recipient)
|
||||
where T : NotificationRecord
|
||||
{
|
||||
notification.RecipientUserId = recipient;
|
||||
notification.Category = Clip(message.Category, 40);
|
||||
notification.Severity = message.Severity;
|
||||
notification.Title = Clip(message.Title, 200);
|
||||
notification.Body = message.Body;
|
||||
notification.Link = message.Link is null ? null : Clip(message.Link, 400);
|
||||
return notification;
|
||||
}
|
||||
|
||||
private static string Clip(string value, int length) => value.Length <= length ? value : value[..length];
|
||||
}
|
||||
Reference in new issue
Block a user