From 08ace8b08d826dbaa32504e670a8eadc195b8fec Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Thu, 8 Feb 2024 16:54:01 -0700 Subject: [PATCH] consolidated settings into confighelper, fixed shop supplies access issue for non root user. --- Controllers/VehicleController.cs | 5 ++++- Filter/CollaboratorFilter.cs | 9 ++++++--- Helper/ConfigHelper.cs | 5 +++++ Views/Home/Index.cshtml | 5 ++--- Views/Vehicle/_SupplyUsage.cshtml | 6 ++++-- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index d3b1f1e..809c8f3 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -1518,7 +1518,10 @@ namespace CarCareTracker.Controllers public IActionResult GetSupplyRecordsForRecordsByVehicleId(int vehicleId) { var result = _supplyRecordDataAccess.GetSupplyRecordsByVehicleId(vehicleId); - result.AddRange(_supplyRecordDataAccess.GetSupplyRecordsByVehicleId(0)); // add shop supplies + if (_config.GetServerEnableShopSupplies()) + { + result.AddRange(_supplyRecordDataAccess.GetSupplyRecordsByVehicleId(0)); // add shop supplies + } result.RemoveAll(x => x.Quantity <= 0); bool _useDescending = _config.GetUserConfig(User).UseDescending; if (_useDescending) diff --git a/Filter/CollaboratorFilter.cs b/Filter/CollaboratorFilter.cs index 550c458..7940d3c 100644 --- a/Filter/CollaboratorFilter.cs +++ b/Filter/CollaboratorFilter.cs @@ -17,10 +17,13 @@ namespace CarCareTracker.Filter if (!filterContext.HttpContext.User.IsInRole(nameof(UserData.IsRootUser))) { var vehicleId = int.Parse(filterContext.ActionArguments["vehicleId"].ToString()); - var userId = int.Parse(filterContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)); - if (!_userLogic.UserCanEditVehicle(userId, vehicleId)) + if (vehicleId != default) { - filterContext.Result = new RedirectResult("/Error/Unauthorized"); + var userId = int.Parse(filterContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)); + if (!_userLogic.UserCanEditVehicle(userId, vehicleId)) + { + filterContext.Result = new RedirectResult("/Error/Unauthorized"); + } } } } diff --git a/Helper/ConfigHelper.cs b/Helper/ConfigHelper.cs index 41dffe2..e00345f 100644 --- a/Helper/ConfigHelper.cs +++ b/Helper/ConfigHelper.cs @@ -11,6 +11,7 @@ namespace CarCareTracker.Helper bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData); string GetLogoUrl(); string GetServerLanguage(); + bool GetServerEnableShopSupplies(); public bool DeleteUserConfig(int userId); } public class ConfigHelper : IConfigHelper @@ -40,6 +41,10 @@ namespace CarCareTracker.Helper var serverLanguage = _config[nameof(UserConfig.UserLanguage)] ?? "en_US"; return serverLanguage; } + public bool GetServerEnableShopSupplies() + { + return bool.Parse(_config[nameof(UserConfig.EnableShopSupplies)] ?? "false"); + } public bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData) { var storedUserId = user.FindFirstValue(ClaimTypes.NameIdentifier); diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index 5a9e669..eed9813 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -1,6 +1,5 @@ @using CarCareTracker.Helper @inject IConfigHelper config -@inject IConfiguration serverConfig @inject ITranslationHelper translator @{ var userConfig = config.GetUserConfig(User); @@ -22,7 +21,7 @@ - @if(bool.Parse(serverConfig[nameof(UserConfig.EnableShopSupplies)] ?? "false")) + @if(config.GetServerEnableShopSupplies()) { - @if (bool.Parse(serverConfig[nameof(UserConfig.EnableShopSupplies)] ?? "false")) + @if (config.GetServerEnableShopSupplies()) {