Padlock account creation hook handled properly

This commit is contained in:
2026-03-07 02:30:38 +08:00
parent d531a0b37a
commit 639c4a4afb
3 changed files with 71 additions and 0 deletions
@@ -88,6 +88,7 @@ public class AccountService(
db.Accounts.Add(account);
await db.SaveChangesAsync();
await PublishAccountCreated(account);
await PublishIdentityUpserted(account);
return account;
}
@@ -307,6 +308,21 @@ public class AccountService(
});
}
private async Task PublishAccountCreated(SnAccount account)
{
await eventBus.PublishAsync(AccountCreatedEvent.Type, new AccountCreatedEvent
{
AccountId = account.Id,
Name = account.Name,
Nick = account.Nick,
Language = account.Language,
Region = account.Region,
ActivatedAt = account.ActivatedAt,
IsSuperuser = account.IsSuperuser,
CreatedAt = SystemClock.Instance.GetCurrentInstant()
});
}
private async Task<bool> VerifyCachedFactorCode(SnAccountAuthFactor factor, string code)
{
var cached = await cache.GetAsync<string>($"{AuthFactorCachePrefix}{factor.Id}:code");
@@ -238,6 +238,45 @@ public static class ServiceCollectionExtensions
logger.LogInformation("Handled status update for user {AccountId} on disconnect", evt.AccountId);
})
.AddListener<AccountCreatedEvent>(async (evt, ctx) =>
{
var db = ctx.ServiceProvider.GetRequiredService<AppDatabase>();
var logger = ctx.ServiceProvider.GetRequiredService<ILogger<EventBus>>();
var changed = false;
var account = await db.Accounts.FirstOrDefaultAsync(a => a.Id == evt.AccountId, ctx.CancellationToken);
if (account is null)
{
account = new SnAccount
{
Id = evt.AccountId,
Name = evt.Name,
Nick = evt.Nick,
Language = evt.Language,
Region = evt.Region,
ActivatedAt = evt.ActivatedAt,
IsSuperuser = evt.IsSuperuser
};
db.Accounts.Add(account);
changed = true;
}
var profileExists = await db.AccountProfiles
.AnyAsync(p => p.AccountId == evt.AccountId, ctx.CancellationToken);
if (!profileExists)
{
db.AccountProfiles.Add(new SnAccountProfile
{
AccountId = evt.AccountId
});
changed = true;
}
if (changed)
await db.SaveChangesAsync(ctx.CancellationToken);
logger.LogInformation("Handled account created event for {AccountId}", evt.AccountId);
})
.AddListener<AccountIdentityUpsertedEvent>(async (evt, ctx) =>
{
var db = ctx.ServiceProvider.GetRequiredService<AppDatabase>();
+16
View File
@@ -25,6 +25,22 @@ public class AccountStatusUpdatedEvent : EventBase
public Instant UpdatedAt { get; set; } = SystemClock.Instance.GetCurrentInstant();
}
public class AccountCreatedEvent : EventBase
{
public static string Type => "accounts.created";
public override string EventType => Type;
public override string StreamName => "account_events";
public Guid AccountId { get; set; }
public string Name { get; set; } = string.Empty;
public string Nick { get; set; } = string.Empty;
public string Language { get; set; } = "en-US";
public string Region { get; set; } = "en";
public Instant? ActivatedAt { get; set; }
public bool IsSuperuser { get; set; }
public Instant CreatedAt { get; set; } = SystemClock.Instance.GetCurrentInstant();
}
public class AccountIdentityUpsertedEvent : EventBase
{
public static string Type => "account_identity_upserted";