From d0f91495853d7583705e765ca677e178cc3bb218 Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Thu, 19 Jun 2025 15:24:28 -0600 Subject: [PATCH] add backend to write server config. --- Controllers/HomeController.cs | 6 ++++ Helper/ConfigHelper.cs | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 917c6d0..a4ba1ea 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -577,6 +577,12 @@ namespace CarCareTracker.Controllers return PartialView("_ServerConfig", viewModel); } [Authorize(Roles = nameof(UserData.IsRootUser))] + public IActionResult WriteServerConfiguration(ServerConfig serverConfig) + { + var result = _config.SaveServerConfig(serverConfig); + return Json(result); + } + [Authorize(Roles = nameof(UserData.IsRootUser))] public IActionResult SendTestEmail(string emailAddress) { var result = _mailHelper.SendTestEmail(emailAddress); diff --git a/Helper/ConfigHelper.cs b/Helper/ConfigHelper.cs index ef956be..7c66542 100644 --- a/Helper/ConfigHelper.cs +++ b/Helper/ConfigHelper.cs @@ -13,6 +13,7 @@ namespace CarCareTracker.Helper MailConfig GetMailConfig(); UserConfig GetUserConfig(ClaimsPrincipal user); bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData); + bool SaveServerConfig(ServerConfig serverConfig); bool AuthenticateRootUser(string username, string password); bool AuthenticateRootUserOIDC(string email); string GetWebHookUrl(); @@ -142,6 +143,59 @@ namespace CarCareTracker.Helper { return CheckBool(CheckString(nameof(UserConfig.EnableShopSupplies))); } + public bool SaveServerConfig(ServerConfig serverConfig) + { + //nullify default values + if (string.IsNullOrWhiteSpace(serverConfig.PostgresConnection)) + { + serverConfig.PostgresConnection = null; + } + if (serverConfig.AllowedFileExtensions == StaticHelper.DefaultAllowedFileExtensions || string.IsNullOrWhiteSpace(serverConfig.AllowedFileExtensions)) + { + serverConfig.AllowedFileExtensions = null; + } + if (serverConfig.CustomLogoURL == StaticHelper.DefaultLogoPath || string.IsNullOrWhiteSpace(serverConfig.CustomLogoURL)) + { + serverConfig.CustomLogoURL = null; + } + if (serverConfig.CustomSmallLogoURL == StaticHelper.DefaultSmallLogoPath || string.IsNullOrWhiteSpace(serverConfig.CustomSmallLogoURL)) + { + serverConfig.CustomSmallLogoURL = null; + } + if (string.IsNullOrWhiteSpace(serverConfig.MessageOfTheDay)) + { + serverConfig.MessageOfTheDay = null; + } + if (string.IsNullOrWhiteSpace(serverConfig.WebHookURL)) + { + serverConfig.WebHookURL = null; + } + if (!serverConfig.CustomWidgetsEnabled.Value) + { + serverConfig.CustomWidgetsEnabled = null; + } + if (!serverConfig.InvariantAPIEnabled.Value) + { + serverConfig.InvariantAPIEnabled = null; + } + if (string.IsNullOrWhiteSpace(serverConfig.SMTPConfig.EmailServer)) + { + serverConfig.SMTPConfig = null; + } + if (string.IsNullOrWhiteSpace(serverConfig.OIDCConfig.Name)) + { + serverConfig.OIDCConfig = null; + } + try + { + File.WriteAllText(StaticHelper.ServerConfigPath, JsonSerializer.Serialize(serverConfig)); + return true; + } catch (Exception ex) + { + _logger.LogWarning(ex.Message); + return false; + } + } public bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData) { var storedUserId = user.FindFirstValue(ClaimTypes.NameIdentifier);