From 1557169628b94a60b4b668c4cf271f093862671b Mon Sep 17 00:00:00 2001 From: KrzysztofPajak Date: Tue, 26 Nov 2024 19:42:25 +0100 Subject: [PATCH 1/2] Refactor InstallController and add DatabaseConfigured attribute Added DatabaseConfiguredAttribute.cs: - Defined DatabaseConfiguredAttribute implementing IAsyncActionFilter to check database installation and handle action accordingly. --- .../Controllers/InstallController.cs | 29 +++++++------------ .../Filters/DatabaseConfiguredAttribute.cs | 20 +++++++++++++ 2 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 src/Modules/Grand.Module.Installer/Filters/DatabaseConfiguredAttribute.cs diff --git a/src/Modules/Grand.Module.Installer/Controllers/InstallController.cs b/src/Modules/Grand.Module.Installer/Controllers/InstallController.cs index 966d86e82..9c6ccc2ad 100644 --- a/src/Modules/Grand.Module.Installer/Controllers/InstallController.cs +++ b/src/Modules/Grand.Module.Installer/Controllers/InstallController.cs @@ -1,8 +1,8 @@ using Grand.Data; using Grand.Infrastructure.Caching; using Grand.Infrastructure.Configuration; -using Grand.Infrastructure.Migrations; using Grand.Infrastructure.Plugins; +using Grand.Module.Installer.Filters; using Grand.Module.Installer.Interfaces; using Grand.Module.Installer.Models; using Grand.SharedKernel.Extensions; @@ -16,6 +16,7 @@ namespace Grand.Module.Installer.Controllers; +[DatabaseConfigured] public class InstallController : Controller { @@ -24,8 +25,6 @@ public class InstallController : Controller private readonly ICacheBase _cacheBase; private readonly IHostApplicationLifetime _applicationLifetime; private readonly DatabaseConfig _dbConfig; - private readonly IDatabaseFactoryContext _databaseFactory; - private readonly IInstallationLocalizedService _installationLocalizedService; private readonly ILogger _logger; /// @@ -40,16 +39,12 @@ public class InstallController : Controller public InstallController( ICacheBase cacheBase, IHostApplicationLifetime applicationLifetime, - IDatabaseFactoryContext databaseFactory, - IInstallationLocalizedService installationLocalizedService, DatabaseConfig dbConfig, ILogger logger) { _cacheBase = cacheBase; _applicationLifetime = applicationLifetime; - _installationLocalizedService = installationLocalizedService; _dbConfig = dbConfig; - _databaseFactory = databaseFactory; _logger = logger; } @@ -70,6 +65,8 @@ public InstallController( private InstallModel PrepareModel(InstallModel? model) { + var installationLocalizedService = HttpContext.RequestServices.GetService(); + model ??= new InstallModel { AdminEmail = "admin@yourstore.com", InstallSampleData = false, @@ -85,10 +82,10 @@ private InstallModel PrepareModel(InstallModel? model) model.SelectedLanguage = GetLanguage(); - foreach (var lang in _installationLocalizedService.GetAvailableLanguages()) + foreach (var lang in installationLocalizedService.GetAvailableLanguages()) { var selected = false; - if (_installationLocalizedService.GetCurrentLanguage(model.SelectedLanguage).Code == lang.Code) + if (installationLocalizedService.GetCurrentLanguage(model.SelectedLanguage).Code == lang.Code) { selected = true; model.SelectedLanguage = lang.Code; @@ -102,21 +99,18 @@ private InstallModel PrepareModel(InstallModel? model) } //prepare collation list - foreach (var col in _installationLocalizedService.GetAvailableCollations()) + foreach (var col in installationLocalizedService.GetAvailableCollations()) model.AvailableCollation.Add(new SelectListItem { Value = col.Value, Text = col.Name, - Selected = _installationLocalizedService.GetCurrentLanguage().Code == col.Value + Selected = installationLocalizedService.GetCurrentLanguage().Code == col.Value }); return model; } public virtual async Task Index() - { - if (DataSettingsManager.DatabaseIsInstalled()) - return RedirectToRoute("HomePage"); - + { var installed = await _cacheBase.GetAsync("Installed", async () => await Task.FromResult(false)); return View(installed ? new InstallModel { Installed = true, AdminEmail = "", AdminPassword = "", ConfirmPassword = "" } : PrepareModel(null)); } @@ -185,7 +179,7 @@ protected async Task CheckConnectionString(IInstallationLocalizedService locServ { if (model.DataProvider != DbProvider.LiteDB) { - var mdb = _databaseFactory.GetDatabaseContext(connectionString, DbProvider.MongoDB); + var mdb = HttpContext.RequestServices.GetRequiredService().GetDatabaseContext(connectionString, DbProvider.MongoDB); if (await mdb.DatabaseExist()) ModelState.AddModelError("", locService.GetResource(model.SelectedLanguage, "AlreadyInstalled")); @@ -202,9 +196,6 @@ protected async Task CheckConnectionString(IInstallationLocalizedService locServ [HttpPost] public virtual async Task Index(InstallModel model) { - if (DataSettingsManager.DatabaseIsInstalled()) - return RedirectToRoute("HomePage"); - var installed = await _cacheBase.GetAsync("Installed", async () => await Task.FromResult(false)); if (installed) return View(new InstallModel { Installed = true, AdminEmail = "", AdminPassword = "", ConfirmPassword = "" }); diff --git a/src/Modules/Grand.Module.Installer/Filters/DatabaseConfiguredAttribute.cs b/src/Modules/Grand.Module.Installer/Filters/DatabaseConfiguredAttribute.cs new file mode 100644 index 000000000..d4319211c --- /dev/null +++ b/src/Modules/Grand.Module.Installer/Filters/DatabaseConfiguredAttribute.cs @@ -0,0 +1,20 @@ +using Grand.Data; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace Grand.Module.Installer.Filters; + +public class DatabaseConfiguredAttribute : Attribute, IAsyncActionFilter +{ + public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) + { + if (!DataSettingsManager.DatabaseIsInstalled()) + { + await next(); + } + else + { + context.Result = new NotFoundResult(); + } + } +} From 5f5c310c11558d23b792de81d934201f4dec52f1 Mon Sep 17 00:00:00 2001 From: KrzysztofPajak Date: Wed, 27 Nov 2024 17:47:52 +0100 Subject: [PATCH 2/2] Minor changes - ensure IInstallationLocalizedService is always available --- .../Grand.Module.Installer/Controllers/InstallController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Modules/Grand.Module.Installer/Controllers/InstallController.cs b/src/Modules/Grand.Module.Installer/Controllers/InstallController.cs index 9c6ccc2ad..5ab7da3b6 100644 --- a/src/Modules/Grand.Module.Installer/Controllers/InstallController.cs +++ b/src/Modules/Grand.Module.Installer/Controllers/InstallController.cs @@ -65,7 +65,7 @@ public InstallController( private InstallModel PrepareModel(InstallModel? model) { - var installationLocalizedService = HttpContext.RequestServices.GetService(); + var installationLocalizedService = HttpContext.RequestServices.GetRequiredService(); model ??= new InstallModel { AdminEmail = "admin@yourstore.com",