From ea58dce644bd6bc6136704ff510719320ff17afd Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 2 Jun 2020 13:47:58 +1000 Subject: [PATCH] implements logic to log the user in on install --- .../CompositionExtensions/Installer.cs | 2 +- .../Install/InstallHelper.cs | 5 +- .../Install/InstallStepCollection.cs | 2 +- .../InstallSteps/CompleteInstallStep.cs | 31 +++++++++ .../InstallSteps/SetUmbracoVersionStep.cs | 67 ------------------- .../Install/InstallApiController.cs | 17 ++++- .../Install/InstallController.cs | 2 +- 7 files changed, 49 insertions(+), 77 deletions(-) create mode 100644 src/Umbraco.Infrastructure/Install/InstallSteps/CompleteInstallStep.cs delete mode 100644 src/Umbraco.Infrastructure/Install/InstallSteps/SetUmbracoVersionStep.cs diff --git a/src/Umbraco.Infrastructure/Composing/CompositionExtensions/Installer.cs b/src/Umbraco.Infrastructure/Composing/CompositionExtensions/Installer.cs index 0b9fb65606..585d6badfb 100644 --- a/src/Umbraco.Infrastructure/Composing/CompositionExtensions/Installer.cs +++ b/src/Umbraco.Infrastructure/Composing/CompositionExtensions/Installer.cs @@ -24,7 +24,7 @@ namespace Umbraco.Web.Composing.CompositionExtensions // composition.Register(Lifetime.Scope); // composition.Register(Lifetime.Scope); - composition.Register(Lifetime.Scope); + composition.Register(Lifetime.Scope); composition.Register(); composition.Register(); diff --git a/src/Umbraco.Infrastructure/Install/InstallHelper.cs b/src/Umbraco.Infrastructure/Install/InstallHelper.cs index b178a1e710..9c3eddbf2a 100644 --- a/src/Umbraco.Infrastructure/Install/InstallHelper.cs +++ b/src/Umbraco.Infrastructure/Install/InstallHelper.cs @@ -21,7 +21,6 @@ namespace Umbraco.Web.Install private static HttpClient _httpClient; private readonly DatabaseBuilder _databaseBuilder; private readonly ILogger _logger; - private readonly IGlobalSettings _globalSettings; private readonly IUmbracoVersion _umbracoVersion; private readonly IConnectionStrings _connectionStrings; private readonly IInstallationService _installationService; @@ -33,7 +32,6 @@ namespace Umbraco.Web.Install public InstallHelper(DatabaseBuilder databaseBuilder, ILogger logger, - IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IConnectionStrings connectionStrings, IInstallationService installationService, @@ -43,7 +41,6 @@ namespace Umbraco.Web.Install IJsonSerializer jsonSerializer) { _logger = logger; - _globalSettings = globalSettings; _umbracoVersion = umbracoVersion; _databaseBuilder = databaseBuilder; _connectionStrings = connectionStrings ?? throw new ArgumentNullException(nameof(connectionStrings)); @@ -59,7 +56,7 @@ namespace Umbraco.Web.Install return _installationType ?? (_installationType = IsBrandNewInstall ? InstallationType.NewInstall : InstallationType.Upgrade).Value; } - public async Task InstallStatus(bool isCompleted, string errorMsg) + public async Task SetInstallStatusAsync(bool isCompleted, string errorMsg) { try { diff --git a/src/Umbraco.Infrastructure/Install/InstallStepCollection.cs b/src/Umbraco.Infrastructure/Install/InstallStepCollection.cs index f31f5f7dbb..523cd35a27 100644 --- a/src/Umbraco.Infrastructure/Install/InstallStepCollection.cs +++ b/src/Umbraco.Infrastructure/Install/InstallStepCollection.cs @@ -30,7 +30,7 @@ namespace Umbraco.Web.Install // a.OfType().First(), // a.OfType().First(), - a.OfType().First(), + a.OfType().First(), }; } diff --git a/src/Umbraco.Infrastructure/Install/InstallSteps/CompleteInstallStep.cs b/src/Umbraco.Infrastructure/Install/InstallSteps/CompleteInstallStep.cs new file mode 100644 index 0000000000..c95defe51a --- /dev/null +++ b/src/Umbraco.Infrastructure/Install/InstallSteps/CompleteInstallStep.cs @@ -0,0 +1,31 @@ +using System.Threading.Tasks; +using Umbraco.Web.Install.Models; + +namespace Umbraco.Web.Install.InstallSteps +{ + [InstallSetupStep(InstallationType.NewInstall | InstallationType.Upgrade, + "UmbracoVersion", 50, "Installation is complete! Get ready to be redirected to your new CMS.", + PerformsAppRestart = true)] + public class CompleteInstallStep : InstallSetupStep + { + private readonly InstallHelper _installHelper; + + public CompleteInstallStep(InstallHelper installHelper) + { + _installHelper = installHelper; + } + + public override async Task ExecuteAsync(object model) + { + //reports the ended install + await _installHelper.SetInstallStatusAsync(true, ""); + + return null; + } + + public override bool RequiresExecution(object model) + { + return true; + } + } +} diff --git a/src/Umbraco.Infrastructure/Install/InstallSteps/SetUmbracoVersionStep.cs b/src/Umbraco.Infrastructure/Install/InstallSteps/SetUmbracoVersionStep.cs deleted file mode 100644 index 190e24d64b..0000000000 --- a/src/Umbraco.Infrastructure/Install/InstallSteps/SetUmbracoVersionStep.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System.Threading.Tasks; -using Umbraco.Core; -using Umbraco.Core.Configuration; -using Umbraco.Net; -using Umbraco.Web.Install.Models; - -namespace Umbraco.Web.Install.InstallSteps -{ - [InstallSetupStep(InstallationType.NewInstall | InstallationType.Upgrade, - "UmbracoVersion", 50, "Installation is complete! Get ready to be redirected to your new CMS.", - PerformsAppRestart = true)] - public class SetUmbracoVersionStep : InstallSetupStep - { - private readonly IUmbracoContextAccessor _umbracoContextAccessor; - private readonly InstallHelper _installHelper; - private readonly IGlobalSettings _globalSettings; - private readonly IUmbracoVersion _umbracoVersion; - - public SetUmbracoVersionStep(IUmbracoContextAccessor umbracoContextAccessor, InstallHelper installHelper, - IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion) - { - _umbracoContextAccessor = umbracoContextAccessor; - _installHelper = installHelper; - _globalSettings = globalSettings; - _umbracoVersion = umbracoVersion; - } - - public override Task ExecuteAsync(object model) - { - //TODO: This needs to be reintroduced, when users are compatible with ASP.NET Core Identity. - // var security = _umbracoContextAccessor.GetRequiredUmbracoContext().Security; - // if (security.IsAuthenticated() == false && _globalSettings.ConfigurationStatus.IsNullOrWhiteSpace()) - // { - // security.PerformLogin(-1); - // } - // - // if (security.IsAuthenticated()) - // { - // // when a user is already logged in, we need to check whether it's user 'zero' - // // which is the legacy super user from v7 - and then we need to actually log the - // // true super user in - but before that we need to log off, else audit events - // // will try to reference user zero and fail - // var userIdAttempt = security.GetUserId(); - // if (userIdAttempt && userIdAttempt.Result == 0) - // { - // security.ClearCurrentLogin(); - // security.PerformLogin(Constants.Security.SuperUserId); - // } - // } - // else if (_globalSettings.ConfigurationStatus.IsNullOrWhiteSpace()) - // { - // // for installs, we need to log the super user in - // security.PerformLogin(Constants.Security.SuperUserId); - // } - - //reports the ended install - _installHelper.InstallStatus(true, ""); - - return Task.FromResult(null); - } - - public override bool RequiresExecution(object model) - { - return true; - } - } -} diff --git a/src/Umbraco.Web.Common/Install/InstallApiController.cs b/src/Umbraco.Web.Common/Install/InstallApiController.cs index c171b09fa8..c8f42e0d04 100644 --- a/src/Umbraco.Web.Common/Install/InstallApiController.cs +++ b/src/Umbraco.Web.Common/Install/InstallApiController.cs @@ -14,11 +14,13 @@ using Umbraco.Web.Common.Attributes; using Umbraco.Web.Common.Exceptions; using Umbraco.Web.Common.Filters; using Umbraco.Web.Common.ModelBinding; +using Umbraco.Web.Common.Security; using Umbraco.Web.Install; using Umbraco.Web.Install.Models; namespace Umbraco.Web.Common.Install { + using Constants = Umbraco.Core.Constants; [UmbracoApiController] [TypeFilter(typeof(HttpResponseExceptionFilter))] @@ -30,19 +32,21 @@ namespace Umbraco.Web.Common.Install private readonly DatabaseBuilder _databaseBuilder; private readonly InstallStatusTracker _installStatusTracker; private readonly IUmbracoApplicationLifetime _umbracoApplicationLifetime; + private readonly BackOfficeSignInManager _backOfficeSignInManager; private readonly InstallStepCollection _installSteps; private readonly ILogger _logger; private readonly IProfilingLogger _proflog; public InstallApiController(DatabaseBuilder databaseBuilder, IProfilingLogger proflog, InstallHelper installHelper, InstallStepCollection installSteps, InstallStatusTracker installStatusTracker, - IUmbracoApplicationLifetime umbracoApplicationLifetime) + IUmbracoApplicationLifetime umbracoApplicationLifetime, BackOfficeSignInManager backOfficeSignInManager) { _databaseBuilder = databaseBuilder ?? throw new ArgumentNullException(nameof(databaseBuilder)); _proflog = proflog ?? throw new ArgumentNullException(nameof(proflog)); _installSteps = installSteps; _installStatusTracker = installStatusTracker; _umbracoApplicationLifetime = umbracoApplicationLifetime; + _backOfficeSignInManager = backOfficeSignInManager; InstallHelper = installHelper; _logger = _proflog; } @@ -85,10 +89,17 @@ namespace Umbraco.Web.Common.Install return starterKits; } - [HttpPost] - public ActionResult CompleteInstall() + public async Task CompleteInstall() { + // log the super user in if it's a new install + var installType = InstallHelper.GetInstallationType(); + if (installType == InstallationType.NewInstall) + { + var user = await _backOfficeSignInManager.UserManager.FindByIdAsync(Constants.Security.SuperUserId.ToString()); + await _backOfficeSignInManager.SignInAsync(user, false); + } + _umbracoApplicationLifetime.Restart(); return NoContent(); } diff --git a/src/Umbraco.Web.Common/Install/InstallController.cs b/src/Umbraco.Web.Common/Install/InstallController.cs index a4f659379f..4ea3452d92 100644 --- a/src/Umbraco.Web.Common/Install/InstallController.cs +++ b/src/Umbraco.Web.Common/Install/InstallController.cs @@ -87,7 +87,7 @@ namespace Umbraco.Web.Common.Install ViewData.SetUmbracoVersion(_umbracoVersion.SemanticVersion); - await _installHelper.InstallStatus(false, ""); + await _installHelper.SetInstallStatusAsync(false, ""); return View(); }