Skip to content

Commit

Permalink
seed improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Mar 18, 2024
1 parent f11baf8 commit b99404b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 115 deletions.
4 changes: 0 additions & 4 deletions PandaVerticalSlices.sln.DotSettings

This file was deleted.

4 changes: 0 additions & 4 deletions PandaWebApi.sln.DotSettings

This file was deleted.

29 changes: 0 additions & 29 deletions qodana.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Pandatech.VerticalSlices.Infrastructure.Extensions;
using Pandatech.VerticalSlices.Infrastructure.Repositories;
using Pandatech.VerticalSlices.Infrastructure.Seed;
using Pandatech.VerticalSlices.Infrastructure.Seed.User;
using Pandatech.VerticalSlices.SharedKernel.Extensions;

namespace Pandatech.VerticalSlices.Infrastructure;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections;
using Pandatech.Crypto;
using Pandatech.VerticalSlices.Domain.Entities;
using Pandatech.VerticalSlices.Domain.Enums;
using Pandatech.VerticalSlices.Infrastructure.Contexts;

namespace Pandatech.VerticalSlices.Infrastructure.Seed.User;

public static class SystemUser
{
public static WebApplication SeedSystemUser(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
var context = services.GetRequiredService<PostgresContext>();
var configuration = services.GetRequiredService<IConfiguration>();
var argon2Id = services.GetRequiredService<Argon2Id>();

var username = configuration["Security:SuperUser:Username"];
ValidateConfiguration(username, "SuperUser:Username");

var normalizedUsername = username!.ToLowerInvariant();
var existingUsers = context.Users
.Where(u => u.Username == normalizedUsername || u.Role == UserRole.SuperAdmin)
.ToList();

ValidateSuperUserUniqueness(existingUsers);

if (existingUsers.Count == 1)
{
return app;
}

var userPassword = configuration["Security:SuperUser:Password"];
ValidateConfiguration(userPassword, "SuperUser:Password");

var passwordHash = argon2Id.HashPassword(userPassword!);

var newUser = CreateNewUser(normalizedUsername, passwordHash);
context.Users.Add(newUser);
context.SaveChanges();

return app;
}

private static void ValidateConfiguration(string? configValue, string configName)
{
if (string.IsNullOrWhiteSpace(configValue))
{
throw new ArgumentException($"{configName} is not set in appsettings.json");
}
}

private static void ValidateSuperUserUniqueness(ICollection users)
{
if (users.Count > 1)
{
throw new InvalidOperationException("There are multiple super users in the database.");
}
}

private static UserEntity CreateNewUser(string username, byte[] passwordHash)
{
return new UserEntity
{
FullName = "System",
PasswordHash = passwordHash,
Username = username,
Role = UserRole.SuperAdmin,
ForcePasswordChange = false,
Comment = "Seeded user, please do not delete"
};
}
}

0 comments on commit b99404b

Please sign in to comment.