| | | 1 | | using Anichron.API.Security; |
| | | 2 | | using Anichron.API.Settings; |
| | | 3 | | using Anichron.Core.Data; |
| | | 4 | | using Anichron.Core.Data.Repository; |
| | | 5 | | using Anichron.Core.Domain; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | using Npgsql; |
| | | 8 | | |
| | | 9 | | namespace Anichron.API.Services; |
| | | 10 | | |
| | | 11 | | public interface IBootstrapSeeder |
| | | 12 | | { |
| | | 13 | | Task SeedAsync(CancellationToken ct); |
| | | 14 | | } |
| | | 15 | | |
| | 19 | 16 | | public sealed partial class BootstrapSeeder( |
| | 19 | 17 | | IUserRepository users, |
| | 19 | 18 | | IUnitOfWork unitOfWork, |
| | 19 | 19 | | IGuidFactory guidFactory, |
| | 19 | 20 | | IPasswordHasher passwordHasher, |
| | 19 | 21 | | IRegistrationValidator validator, |
| | 19 | 22 | | IConfiguration configuration, |
| | 19 | 23 | | ILogger<BootstrapSeeder> logger) : IBootstrapSeeder |
| | | 24 | | { |
| | | 25 | | private const string passwordSetting = "BOOTSTRAP_ADMIN_PASSWORD"; |
| | | 26 | | |
| | | 27 | | public async Task SeedAsync(CancellationToken ct) |
| | 18 | 28 | | { |
| | 18 | 29 | | if (await users.AnyAsync(ct)) |
| | 3 | 30 | | return; |
| | | 31 | | |
| | 15 | 32 | | var rawUsername = configuration["BOOTSTRAP_ADMIN_USERNAME"]; |
| | 15 | 33 | | var username = string.IsNullOrWhiteSpace(rawUsername) ? AppDefaults.Startup.AdminDefaultUsername : rawUsername; |
| | 15 | 34 | | var password = configuration[passwordSetting]; |
| | 15 | 35 | | if (string.IsNullOrWhiteSpace(password)) |
| | 3 | 36 | | throw new InvalidOperationException($"{passwordSetting} must be set to create the bootstrap admin."); |
| | 12 | 37 | | await validator.EnsureMeetsPolicyAsync(passwordSetting, password, ct); |
| | | 38 | | |
| | 9 | 39 | | Log.CreatingBootstrapAdmin(logger); |
| | | 40 | | |
| | 9 | 41 | | var user = new User |
| | 9 | 42 | | { |
| | 9 | 43 | | Id = guidFactory.NewGuid(), |
| | 9 | 44 | | Username = IdentityNormalizer.Normalize(username), |
| | 9 | 45 | | Email = AppDefaults.Startup.AdminDefaultMail, |
| | 9 | 46 | | PasswordHash = passwordHasher.Hash(password), |
| | 9 | 47 | | IsAdmin = true, |
| | 9 | 48 | | MustChangePassword = true, |
| | 9 | 49 | | }; |
| | | 50 | | |
| | 9 | 51 | | users.Add(user); |
| | | 52 | | try |
| | 9 | 53 | | { |
| | 9 | 54 | | await unitOfWork.SaveChangesAsync(ct); |
| | 7 | 55 | | } |
| | 2 | 56 | | catch (DbUpdateException ex) when (ex.InnerException is PostgresException { SqlState: "23505" }) |
| | 1 | 57 | | { |
| | 1 | 58 | | Log.BootstrapAdminAlreadyExists(logger, ex); |
| | 1 | 59 | | } |
| | 11 | 60 | | } |
| | | 61 | | |
| | | 62 | | private static partial class Log |
| | | 63 | | { |
| | | 64 | | [LoggerMessage(Level = LogLevel.Warning, Message = "No users exist. Creating bootstrap admin.")] |
| | | 65 | | public static partial void CreatingBootstrapAdmin(ILogger logger); |
| | | 66 | | |
| | | 67 | | [LoggerMessage(Level = LogLevel.Information, Message = "Bootstrap admin already exists (concurrent seed). Skippi |
| | | 68 | | public static partial void BootstrapAdminAlreadyExists(ILogger logger, Exception ex); |
| | | 69 | | } |
| | | 70 | | } |