mirror of
https://github.com/hargata/lubelog.git
synced 2025-12-10 00:46:08 -06:00
replaced IConfiguration injection with IConfigHelper
This commit is contained in:
parent
8c6920afab
commit
8d74799099
@ -1,4 +1,5 @@
|
||||
using CarCareTracker.Logic;
|
||||
using CarCareTracker.Helper;
|
||||
using CarCareTracker.Logic;
|
||||
using CarCareTracker.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -12,10 +13,12 @@ namespace CarCareTracker.Controllers
|
||||
{
|
||||
private ILoginLogic _loginLogic;
|
||||
private IUserLogic _userLogic;
|
||||
public AdminController(ILoginLogic loginLogic, IUserLogic userLogic)
|
||||
private IConfigHelper _configHelper;
|
||||
public AdminController(ILoginLogic loginLogic, IUserLogic userLogic, IConfigHelper configHelper)
|
||||
{
|
||||
_loginLogic = loginLogic;
|
||||
_userLogic = userLogic;
|
||||
_configHelper = configHelper;
|
||||
}
|
||||
public IActionResult Index()
|
||||
{
|
||||
@ -38,7 +41,7 @@ namespace CarCareTracker.Controllers
|
||||
}
|
||||
public IActionResult DeleteUser(int userId)
|
||||
{
|
||||
var result =_userLogic.DeleteAllAccessToUser(userId) && _loginLogic.DeleteUser(userId);
|
||||
var result =_userLogic.DeleteAllAccessToUser(userId) && _configHelper.DeleteUserConfig(userId) && _loginLogic.DeleteUser(userId);
|
||||
return Json(result);
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ namespace CarCareTracker.Controllers
|
||||
[HttpPost]
|
||||
public IActionResult WriteToSettings(UserConfig userConfig)
|
||||
{
|
||||
var result = _config.SaveUserConfig(User.IsInRole(nameof(UserData.IsRootUser)), GetUserID(), userConfig);
|
||||
var result = _config.SaveUserConfig(User, userConfig);
|
||||
return Json(result);
|
||||
}
|
||||
public IActionResult Privacy()
|
||||
|
||||
@ -27,7 +27,7 @@ namespace CarCareTracker.Controllers
|
||||
private readonly IUpgradeRecordDataAccess _upgradeRecordDataAccess;
|
||||
private readonly IWebHostEnvironment _webEnv;
|
||||
private readonly bool _useDescending;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly IConfigHelper _config;
|
||||
private readonly IFileHelper _fileHelper;
|
||||
private readonly IGasHelper _gasHelper;
|
||||
private readonly IReminderHelper _reminderHelper;
|
||||
@ -49,7 +49,7 @@ namespace CarCareTracker.Controllers
|
||||
IUpgradeRecordDataAccess upgradeRecordDataAccess,
|
||||
IUserLogic userLogic,
|
||||
IWebHostEnvironment webEnv,
|
||||
IConfiguration config)
|
||||
IConfigHelper config)
|
||||
{
|
||||
_logger = logger;
|
||||
_dataAccess = dataAccess;
|
||||
@ -67,7 +67,7 @@ namespace CarCareTracker.Controllers
|
||||
_userLogic = userLogic;
|
||||
_webEnv = webEnv;
|
||||
_config = config;
|
||||
_useDescending = bool.Parse(config[nameof(UserConfig.UseDescending)]);
|
||||
_useDescending = config.GetUserConfig(User).UseDescending;
|
||||
}
|
||||
private int GetUserID()
|
||||
{
|
||||
@ -231,8 +231,8 @@ namespace CarCareTracker.Controllers
|
||||
var fileNameToExport = $"temp/{Guid.NewGuid()}.csv";
|
||||
var fullExportFilePath = _fileHelper.GetFullFilePath(fileNameToExport, false);
|
||||
var vehicleRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId);
|
||||
bool useMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]);
|
||||
bool useUKMPG = bool.Parse(_config[nameof(UserConfig.UseUKMPG)]);
|
||||
bool useMPG = _config.GetUserConfig(User).UseMPG;
|
||||
bool useUKMPG = _config.GetUserConfig(User).UseUKMPG;
|
||||
vehicleRecords = vehicleRecords.OrderBy(x => x.Date).ThenBy(x => x.Mileage).ToList();
|
||||
var convertedRecords = _gasHelper.GetGasRecordViewModels(vehicleRecords, useMPG, useUKMPG);
|
||||
var exportData = convertedRecords.Select(x => new GasRecordExportModel { Date = x.Date.ToString(), Cost = x.Cost.ToString(), FuelConsumed = x.Gallons.ToString(), FuelEconomy = x.MilesPerGallon.ToString(), Odometer = x.Mileage.ToString() });
|
||||
@ -389,8 +389,8 @@ namespace CarCareTracker.Controllers
|
||||
//need it in ascending order to perform computation.
|
||||
result = result.OrderBy(x => x.Date).ThenBy(x => x.Mileage).ToList();
|
||||
//check if the user uses MPG or Liters per 100km.
|
||||
bool useMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]);
|
||||
bool useUKMPG = bool.Parse(_config[nameof(UserConfig.UseUKMPG)]);
|
||||
bool useMPG = _config.GetUserConfig(User).UseMPG;
|
||||
bool useUKMPG = _config.GetUserConfig(User).UseUKMPG;
|
||||
var computedResults = _gasHelper.GetGasRecordViewModels(result, useMPG, useUKMPG);
|
||||
if (_useDescending)
|
||||
{
|
||||
@ -753,8 +753,8 @@ namespace CarCareTracker.Controllers
|
||||
var upgradeRecords = _upgradeRecordDataAccess.GetUpgradeRecordsByVehicleId(vehicleId);
|
||||
var taxRecords = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId);
|
||||
var gasRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId);
|
||||
bool useMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]);
|
||||
bool useUKMPG = bool.Parse(_config[nameof(UserConfig.UseUKMPG)]);
|
||||
bool useMPG = _config.GetUserConfig(User).UseMPG;
|
||||
bool useUKMPG = _config.GetUserConfig(User).UseUKMPG;
|
||||
vehicleHistory.TotalGasCost = gasRecords.Sum(x => x.Cost);
|
||||
vehicleHistory.TotalCost = serviceRecords.Sum(x => x.Cost) + repairRecords.Sum(x => x.Cost) + upgradeRecords.Sum(x => x.Cost) + taxRecords.Sum(x => x.Cost);
|
||||
var averageMPG = 0.00M;
|
||||
|
||||
@ -23,7 +23,8 @@ namespace CarCareTracker.External.Implementations
|
||||
using (var db = new LiteDatabase(dbName))
|
||||
{
|
||||
var table = db.GetCollection<UserConfigData>(tableName);
|
||||
return table.Upsert(userConfigData);
|
||||
table.Upsert(userConfigData);
|
||||
return true;
|
||||
};
|
||||
}
|
||||
public bool DeleteUserConfig(int userId)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using CarCareTracker.External.Interfaces;
|
||||
using CarCareTracker.Models;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace CarCareTracker.Helper
|
||||
@ -7,20 +8,31 @@ namespace CarCareTracker.Helper
|
||||
public interface IConfigHelper
|
||||
{
|
||||
UserConfig GetUserConfig(ClaimsPrincipal user);
|
||||
bool SaveUserConfig(bool isRootUser, int userId, UserConfig configData);
|
||||
bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData);
|
||||
public bool DeleteUserConfig(int userId);
|
||||
}
|
||||
public class ConfigHelper : IConfigHelper
|
||||
{
|
||||
private readonly IConfiguration _config;
|
||||
private readonly IUserConfigDataAccess _userConfig;
|
||||
public ConfigHelper(IConfiguration serverConfig, IUserConfigDataAccess userConfig)
|
||||
private IMemoryCache _cache;
|
||||
public ConfigHelper(IConfiguration serverConfig,
|
||||
IUserConfigDataAccess userConfig,
|
||||
IMemoryCache memoryCache)
|
||||
{
|
||||
_config = serverConfig;
|
||||
_userConfig = userConfig;
|
||||
_cache = memoryCache;
|
||||
}
|
||||
public bool SaveUserConfig(bool isRootUser, int userId, UserConfig configData)
|
||||
public bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData)
|
||||
{
|
||||
var storedUserId = user.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
int userId = 0;
|
||||
if (storedUserId != null)
|
||||
{
|
||||
userId = int.Parse(storedUserId);
|
||||
}
|
||||
bool isRootUser = user.IsInRole(nameof(UserData.IsRootUser));
|
||||
if (isRootUser)
|
||||
{
|
||||
try
|
||||
@ -46,6 +58,7 @@ namespace CarCareTracker.Helper
|
||||
configData.UserPasswordHash = string.Empty;
|
||||
}
|
||||
File.WriteAllText(StaticHelper.UserConfigPath, System.Text.Json.JsonSerializer.Serialize(configData));
|
||||
_cache.Set<UserConfig>($"userConfig_{userId}", configData);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -60,46 +73,65 @@ namespace CarCareTracker.Helper
|
||||
UserConfig = configData
|
||||
};
|
||||
var result = _userConfig.SaveUserConfig(userConfig);
|
||||
_cache.Set<UserConfig>($"userConfig_{userId}", configData);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public bool DeleteUserConfig(int userId)
|
||||
{
|
||||
_cache.Remove($"userConfig_{userId}");
|
||||
var result = _userConfig.DeleteUserConfig(userId);
|
||||
return result;
|
||||
}
|
||||
public UserConfig GetUserConfig(ClaimsPrincipal user)
|
||||
{
|
||||
var serverConfig = new UserConfig
|
||||
int userId = 0;
|
||||
if (user != null)
|
||||
{
|
||||
EnableCsvImports = bool.Parse(_config[nameof(UserConfig.EnableCsvImports)]),
|
||||
UseDarkMode = bool.Parse(_config[nameof(UserConfig.UseDarkMode)]),
|
||||
UseMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]),
|
||||
UseDescending = bool.Parse(_config[nameof(UserConfig.UseDescending)]),
|
||||
EnableAuth = bool.Parse(_config[nameof(UserConfig.EnableAuth)]),
|
||||
HideZero = bool.Parse(_config[nameof(UserConfig.HideZero)]),
|
||||
UseUKMPG = bool.Parse(_config[nameof(UserConfig.UseUKMPG)])
|
||||
};
|
||||
if (!user.Identity.IsAuthenticated)
|
||||
{
|
||||
return serverConfig;
|
||||
}
|
||||
bool isRootUser = user.IsInRole(nameof(UserData.IsRootUser));
|
||||
int userId = int.Parse(user.FindFirstValue(ClaimTypes.NameIdentifier));
|
||||
if (isRootUser)
|
||||
{
|
||||
return serverConfig;
|
||||
var storedUserId = user.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (storedUserId != null)
|
||||
{
|
||||
userId = int.Parse(storedUserId);
|
||||
}
|
||||
} else
|
||||
{
|
||||
var result = _userConfig.GetUserConfig(userId);
|
||||
if (result == null)
|
||||
return new UserConfig();
|
||||
}
|
||||
return _cache.GetOrCreate<UserConfig>($"userConfig_{userId}", entry =>
|
||||
{
|
||||
entry.SlidingExpiration = TimeSpan.FromHours(1);
|
||||
var serverConfig = new UserConfig
|
||||
{
|
||||
EnableCsvImports = bool.Parse(_config[nameof(UserConfig.EnableCsvImports)]),
|
||||
UseDarkMode = bool.Parse(_config[nameof(UserConfig.UseDarkMode)]),
|
||||
UseMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]),
|
||||
UseDescending = bool.Parse(_config[nameof(UserConfig.UseDescending)]),
|
||||
EnableAuth = bool.Parse(_config[nameof(UserConfig.EnableAuth)]),
|
||||
HideZero = bool.Parse(_config[nameof(UserConfig.HideZero)]),
|
||||
UseUKMPG = bool.Parse(_config[nameof(UserConfig.UseUKMPG)])
|
||||
};
|
||||
if (!user.Identity.IsAuthenticated)
|
||||
{
|
||||
return serverConfig;
|
||||
} else
|
||||
{
|
||||
return result.UserConfig;
|
||||
}
|
||||
}
|
||||
bool isRootUser = user.IsInRole(nameof(UserData.IsRootUser));
|
||||
if (isRootUser)
|
||||
{
|
||||
return serverConfig;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = _userConfig.GetUserConfig(userId);
|
||||
if (result == null)
|
||||
{
|
||||
return serverConfig;
|
||||
}
|
||||
else
|
||||
{
|
||||
return result.UserConfig;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ namespace CarCareTracker.Logic
|
||||
OperationResponse ResetPasswordByUser(LoginModel credentials);
|
||||
OperationResponse ResetUserPassword(LoginModel credentials);
|
||||
UserData ValidateUserCredentials(LoginModel credentials);
|
||||
bool CheckIfUserIsValid(int userId);
|
||||
bool CreateRootUserCredentials(LoginModel credentials);
|
||||
bool DeleteRootUserCredentials();
|
||||
List<UserData> GetAllUsers();
|
||||
@ -36,6 +37,21 @@ namespace CarCareTracker.Logic
|
||||
_tokenData = tokenData;
|
||||
_mailHelper = mailHelper;
|
||||
}
|
||||
public bool CheckIfUserIsValid(int userId)
|
||||
{
|
||||
if (userId == -1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
var result = _userData.GetUserRecordById(userId);
|
||||
if (result == null)
|
||||
{
|
||||
return false;
|
||||
} else
|
||||
{
|
||||
return result.Id != 0;
|
||||
}
|
||||
}
|
||||
//handles user registration
|
||||
public OperationResponse RegisterNewUser(LoginModel credentials)
|
||||
{
|
||||
|
||||
@ -113,11 +113,17 @@ namespace CarCareTracker.Middleware
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_loginLogic.CheckIfUserIsValid(authCookie.UserData.Id))
|
||||
{
|
||||
return AuthenticateResult.Fail("Cookie points to non-existant user.");
|
||||
}
|
||||
//validate if user is still valid
|
||||
var appIdentity = new ClaimsIdentity("Custom");
|
||||
var userIdentity = new List<Claim>
|
||||
{
|
||||
new(ClaimTypes.Name, authCookie.UserData.UserName),
|
||||
new(ClaimTypes.NameIdentifier, authCookie.UserData.Id.ToString())
|
||||
new(ClaimTypes.NameIdentifier, authCookie.UserData.Id.ToString()),
|
||||
new(ClaimTypes.Role, "CookieAuth")
|
||||
};
|
||||
if (authCookie.UserData.IsAdmin)
|
||||
{
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
@inject IConfiguration Configuration
|
||||
@using CarCareTracker.Helper
|
||||
@inject IConfigHelper config
|
||||
@{
|
||||
var enableAuth = bool.Parse(Configuration[nameof(UserConfig.EnableAuth)]);
|
||||
var enableAuth = config.GetUserConfig(User).EnableAuth;
|
||||
}
|
||||
@model string
|
||||
@{
|
||||
@ -17,7 +18,7 @@
|
||||
<li class="nav-item" role="presentation">
|
||||
<button class="nav-link" id="settings-tab" data-bs-toggle="tab" data-bs-target="#settings-tab-pane" type="button" role="tab"><span class="ms-2 display-3"><i class="bi bi-gear me-2"></i>Settings</span></button>
|
||||
</li>
|
||||
@if (enableAuth)
|
||||
@if (User.IsInRole("CookieAuth"))
|
||||
{
|
||||
<li class="nav-item" role="presentation">
|
||||
<button class="nav-link" onclick="performLogOut()"><span class="display-3 ms-2"><i class="bi bi-box-arrow-right me-2"></i>Logout</span></button>
|
||||
@ -42,7 +43,7 @@
|
||||
<li class="nav-item ms-auto" role="presentation">
|
||||
<button class="nav-link @(Model == "settings" ? "active" : "")" id="settings-tab" data-bs-toggle="tab" data-bs-target="#settings-tab-pane" type="button" role="tab"><i class="bi bi-gear me-2"></i>Settings</button>
|
||||
</li>
|
||||
@if (enableAuth)
|
||||
@if (User.IsInRole("CookieAuth"))
|
||||
{
|
||||
<li class="nav-item">
|
||||
<button class="nav-link" onclick="performLogOut()"><i class="bi bi-box-arrow-right me-2"></i>Logout</button>
|
||||
@ -69,6 +70,5 @@
|
||||
</div>
|
||||
<script>
|
||||
loadGarage();
|
||||
loadSettings();
|
||||
bindWindowResize();
|
||||
</script>
|
||||
@ -1,7 +1,8 @@
|
||||
@inject IConfiguration Configuration
|
||||
@using CarCareTracker.Helper
|
||||
@inject IConfigHelper config
|
||||
@{
|
||||
var enableCsvImports = bool.Parse(Configuration[nameof(UserConfig.EnableCsvImports)]);
|
||||
var hideZero = bool.Parse(Configuration[nameof(UserConfig.HideZero)]);
|
||||
var enableCsvImports = config.GetUserConfig(User).EnableCsvImports;
|
||||
var hideZero = config.GetUserConfig(User).HideZero;
|
||||
}
|
||||
@model List<CollisionRecord>
|
||||
<div class="row">
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
@inject IConfiguration Configuration
|
||||
@using CarCareTracker.Helper
|
||||
@inject IConfigHelper config
|
||||
@model GasRecordViewModelContainer
|
||||
@{
|
||||
var enableCsvImports = bool.Parse(Configuration[nameof(UserConfig.EnableCsvImports)]);
|
||||
var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]);
|
||||
var useUKMPG = bool.Parse(Configuration[nameof(UserConfig.UseUKMPG)]);
|
||||
var hideZero = bool.Parse(Configuration[nameof(UserConfig.HideZero)]);
|
||||
var enableCsvImports = config.GetUserConfig(User).EnableCsvImports;
|
||||
var useMPG = config.GetUserConfig(User).UseMPG;
|
||||
var useUKMPG = config.GetUserConfig(User).UseUKMPG;
|
||||
var hideZero = config.GetUserConfig(User).HideZero;
|
||||
var useKwh = Model.UseKwh;
|
||||
string consumptionUnit;
|
||||
string fuelEconomyUnit;
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
@inject IConfiguration Configuration
|
||||
@using CarCareTracker.Helper
|
||||
@inject IConfigHelper config
|
||||
@model GasRecordInputContainer
|
||||
@{
|
||||
var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]);
|
||||
var useUKMPG = bool.Parse(Configuration[nameof(UserConfig.UseUKMPG)]);
|
||||
var useMPG = config.GetUserConfig(User).UseMPG;
|
||||
var useUKMPG = config.GetUserConfig(User).UseUKMPG;
|
||||
var useKwh = Model.UseKwh;
|
||||
var isNew = Model.GasRecord.Id == 0;
|
||||
string consumptionUnit;
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
@inject IConfiguration Configuration
|
||||
@using CarCareTracker.Helper
|
||||
@inject IConfigHelper config
|
||||
@{
|
||||
var enableCsvImports = bool.Parse(Configuration[nameof(UserConfig.EnableCsvImports)]);
|
||||
var hideZero = bool.Parse(Configuration[nameof(UserConfig.HideZero)]);
|
||||
var enableCsvImports = config.GetUserConfig(User).EnableCsvImports;
|
||||
var hideZero = config.GetUserConfig(User).HideZero;
|
||||
}
|
||||
@model List<ServiceRecord>
|
||||
<div class="row">
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
@inject IConfiguration Configuration
|
||||
@using CarCareTracker.Helper
|
||||
@inject IConfigHelper config
|
||||
@{
|
||||
var enableCsvImports = bool.Parse(Configuration[nameof(UserConfig.EnableCsvImports)]);
|
||||
var hideZero = bool.Parse(Configuration[nameof(UserConfig.HideZero)]);
|
||||
var enableCsvImports = config.GetUserConfig(User).EnableCsvImports;
|
||||
var hideZero = config.GetUserConfig(User).HideZero;
|
||||
}
|
||||
@model List<TaxRecord>
|
||||
<div class="row">
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
@inject IConfiguration Configuration
|
||||
@using CarCareTracker.Helper
|
||||
@inject IConfigHelper config
|
||||
@{
|
||||
var enableCsvImports = bool.Parse(Configuration[nameof(UserConfig.EnableCsvImports)]);
|
||||
var hideZero = bool.Parse(Configuration[nameof(UserConfig.HideZero)]);
|
||||
var enableCsvImports = config.GetUserConfig(User).EnableCsvImports;
|
||||
var hideZero = config.GetUserConfig(User).HideZero;
|
||||
}
|
||||
@model List<UpgradeRecord>
|
||||
<div class="row">
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
@inject IConfiguration Configuration
|
||||
@using CarCareTracker.Helper
|
||||
@inject IConfigHelper config
|
||||
@{
|
||||
var hideZero = bool.Parse(Configuration[nameof(UserConfig.HideZero)]);
|
||||
var useMPG = bool.Parse(Configuration[nameof(UserConfig.UseMPG)]);
|
||||
var useUKMPG = bool.Parse(Configuration[nameof(UserConfig.UseUKMPG)]);
|
||||
var hideZero = config.GetUserConfig(User).HideZero;
|
||||
var useMPG = config.GetUserConfig(User).UseMPG;
|
||||
var useUKMPG = config.GetUserConfig(User).UseUKMPG;
|
||||
var useKwh = Model.VehicleData.IsElectric;
|
||||
string fuelEconomyUnit;
|
||||
if (useKwh)
|
||||
|
||||
@ -14,6 +14,7 @@ function hideAddVehicleModal() {
|
||||
function loadGarage() {
|
||||
$.get('/Home/Garage', function (data) {
|
||||
$("#garageContainer").html(data);
|
||||
loadSettings();
|
||||
});
|
||||
}
|
||||
function loadSettings() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user