Add an Administrator #14

Merged
Wesser merged 3 commits from feat/add-user into release/v1.0.0 2024-06-01 08:27:02 +03:00
Showing only changes of commit 2addd2aa78 - Show all commits

View File

@ -32,7 +32,11 @@ namespace Mirea.Api.Endpoint.Controllers.Configuration;
[ApiController]
[MaintenanceModeIgnore]
[ApiExplorerSettings(IgnoreApi = true)]
public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigureService notConfigureService, IMemoryCache cache) : BaseController
public class SetupController(
ISetupToken setupToken,
IMaintenanceModeNotConfigureService notConfigureService,
IMemoryCache cache,
PasswordHashService passwordHashService) : BaseController
{
private const string CacheGeneralKey = "config_general";
private const string CacheAdminKey = "config_admin";
@ -208,6 +212,18 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur
if (!MailAddress.TryCreate(user.Email, out _))
throw new ControllerArgumentException("The email address is incorrect.");
var (salt, hash) = passwordHashService.HashPassword(user.Password);
var admin = new Admin
{
Username = user.Username,
Email = user.Email,
PasswordHash = hash,
Salt = salt
};
cache.Set(CacheAdminKey, admin);
return Ok(true);
}
@ -297,14 +313,13 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur
if (!new SettingsRequiredValidator(GeneralConfig).AreSettingsValid())
throw new ControllerArgumentException("The necessary data has not been configured.");
// todo: change CreateUserRequest to Domain entity
if (!cache.TryGetValue(CacheAdminKey, out CreateUserRequest? user) || user == null)
if (!cache.TryGetValue(CacheAdminKey, out Admin? admin) || admin == null)
throw new ControllerArgumentException("The administrator's data was not set.");
if (System.IO.File.Exists(PathBuilder.Combine(GeneralConfig.FilePath)))
System.IO.File.Delete(PathBuilder.Combine(GeneralConfig.FilePath));
System.IO.File.WriteAllText(PathBuilder.Combine("admin.json"), JsonSerializer.Serialize(user));
System.IO.File.WriteAllText(PathBuilder.Combine(Admin.PathToSave), JsonSerializer.Serialize(admin));
System.IO.File.WriteAllText(
PathBuilder.Combine(GeneralConfig.FilePath),