-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Haik
committed
Mar 18, 2024
1 parent
f11baf8
commit b99404b
Showing
6 changed files
with
75 additions
and
115 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
src/Pandatech.VerticalSlices/Infrastructure/DependencyInjection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
src/Pandatech.VerticalSlices/Infrastructure/Seed/SeederExtension.cs
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
src/Pandatech.VerticalSlices/Infrastructure/Seed/User/SystemUser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}; | ||
} | ||
} |