From 2addd2aa786fbcfb582d74376701085f58deaaee Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sat, 1 Jun 2024 08:20:27 +0300 Subject: [PATCH] feat: use Admin model --- .../Configuration/SetupController.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index adc1695..9bda318 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -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),