Bootstrap admin promotion never lifts a suspension and writes a receipt when it changes status (tests cover this and the demotion fix)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
030bd0eede
commit
27ca04f1d6
3 changed files with 233 additions and 25 deletions
|
|
@ -12,7 +12,9 @@ public sealed class MembershipSynchronizationService(
|
|||
BifrostDbContext dbContext,
|
||||
BifrostIdentityService bifrostIdentityService,
|
||||
IOptions<BootstrapOptions> bootstrapOptions,
|
||||
TimeProvider timeProvider)
|
||||
TimeProvider timeProvider,
|
||||
AuditTrailService auditTrailService,
|
||||
ILogger<MembershipSynchronizationService> logger)
|
||||
{
|
||||
public async Task SynchronizeAsync(ClaimsPrincipal principal, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
@ -109,9 +111,14 @@ public sealed class MembershipSynchronizationService(
|
|||
invitation.AcceptedAtUtc = now;
|
||||
}
|
||||
|
||||
MembershipStatus? promotedFrom = null;
|
||||
if (IsBootstrapAdmin(gitHubLogin, bootstrapOptions.Value))
|
||||
{
|
||||
PromoteToBootstrapAdmin(userAccount.Membership!, now);
|
||||
promotedFrom = userAccount.Membership!.Status;
|
||||
if (!ApplicationBootstrapper.TryPromoteBootstrapAdmin(userAccount, now, logger))
|
||||
{
|
||||
promotedFrom = null;
|
||||
}
|
||||
}
|
||||
else if (userAccount.Membership!.Status == MembershipStatus.Active)
|
||||
{
|
||||
|
|
@ -125,22 +132,16 @@ public sealed class MembershipSynchronizationService(
|
|||
|
||||
userAccount.MemberProfile!.UpdatedAtUtc = now;
|
||||
|
||||
if (promotedFrom is { } before && before != userAccount.Membership!.Status)
|
||||
{
|
||||
// One save: the promotion and its receipt stand or fall together.
|
||||
await ApplicationBootstrapper.RecordPromotionAsync(auditTrailService, userAccount, before, cancellationToken);
|
||||
return;
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private static void PromoteToBootstrapAdmin(Bifrost.Web.Domain.Membership membership, DateTimeOffset now)
|
||||
{
|
||||
membership.Status = MembershipStatus.Active;
|
||||
membership.IsPlatformAdmin = true;
|
||||
membership.CanManageProjects = true;
|
||||
membership.CanManageLedger = true;
|
||||
membership.CanModerateMotions = true;
|
||||
membership.ApprovedAtUtc ??= now;
|
||||
|
||||
ApplicationBootstrapper.EnsureRole(membership, MemberRole.PlatformAdmin, null, now, "Bootstrap admin");
|
||||
ApplicationBootstrapper.EnsureRole(membership, MemberRole.StandardMember, null, now, "Default active member role");
|
||||
}
|
||||
|
||||
private static bool TryReadGitHubIdentity(
|
||||
ClaimsPrincipal principal,
|
||||
out long gitHubUserId,
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ public sealed class ApplicationBootstrapper(
|
|||
BifrostDbContext dbContext,
|
||||
IOptions<BootstrapOptions> bootstrapOptions,
|
||||
TimeProvider timeProvider,
|
||||
AuditTrailService auditTrailService,
|
||||
ILogger<ApplicationBootstrapper> logger)
|
||||
{
|
||||
public async Task RunAsync(CancellationToken cancellationToken)
|
||||
|
|
@ -40,16 +41,11 @@ public sealed class ApplicationBootstrapper(
|
|||
|
||||
foreach (var adminUser in adminUsers.Where(x => x.Membership is not null))
|
||||
{
|
||||
var membership = adminUser.Membership!;
|
||||
membership.Status = MembershipStatus.Active;
|
||||
membership.IsPlatformAdmin = true;
|
||||
membership.CanManageProjects = true;
|
||||
membership.CanManageLedger = true;
|
||||
membership.CanModerateMotions = true;
|
||||
membership.ApprovedAtUtc ??= now;
|
||||
|
||||
EnsureRole(membership, MemberRole.PlatformAdmin, null, now, "Bootstrap admin");
|
||||
EnsureRole(membership, MemberRole.StandardMember, null, now, "Default active member role");
|
||||
var before = adminUser.Membership!.Status;
|
||||
if (TryPromoteBootstrapAdmin(adminUser, now, logger) && before != adminUser.Membership.Status)
|
||||
{
|
||||
await RecordPromotionAsync(auditTrailService, adminUser, before, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
if (dbContext.ChangeTracker.HasChanges())
|
||||
|
|
@ -58,6 +54,41 @@ public sealed class ApplicationBootstrapper(
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The one bootstrap-admin promotion, used at startup and at sign-in. A Suspended account is left exactly as it is (an admin's
|
||||
/// decision; only the suspension service lifts it) and the refusal is logged by account id. Returns whether it promoted.
|
||||
/// </summary>
|
||||
public static bool TryPromoteBootstrapAdmin(UserAccount account, DateTimeOffset now, ILogger logger)
|
||||
{
|
||||
var membership = account.Membership!;
|
||||
if (membership.Status == MembershipStatus.Suspended)
|
||||
{
|
||||
logger.LogWarning("Bootstrap admin promotion skipped for suspended account {UserAccountId}.", account.Id);
|
||||
return false;
|
||||
}
|
||||
|
||||
membership.Status = MembershipStatus.Active;
|
||||
membership.IsPlatformAdmin = true;
|
||||
membership.CanManageProjects = true;
|
||||
membership.CanManageLedger = true;
|
||||
membership.CanModerateMotions = true;
|
||||
membership.ApprovedAtUtc ??= now;
|
||||
|
||||
EnsureRole(membership, MemberRole.PlatformAdmin, null, now, "Bootstrap admin");
|
||||
EnsureRole(membership, MemberRole.StandardMember, null, now, "Default active member role");
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>The receipt for a bootstrap promotion that changed membership status; saves together with the promotion.</summary>
|
||||
public static Task RecordPromotionAsync(AuditTrailService auditTrailService, UserAccount account, MembershipStatus before, CancellationToken cancellationToken) =>
|
||||
auditTrailService.RecordAsync(
|
||||
null,
|
||||
nameof(Bifrost.Web.Domain.Membership),
|
||||
account.Membership!.Id,
|
||||
"membership.bootstrap-promoted",
|
||||
$"Bootstrap admin promotion of {account.DisplayName} ({before} -> {account.Membership.Status}).",
|
||||
cancellationToken);
|
||||
|
||||
public static void EnsureRole(
|
||||
Bifrost.Web.Domain.Membership membership,
|
||||
MemberRole role,
|
||||
|
|
|
|||
Loading…
Reference in a new issue