From 2dc11f6fa1e9b4443df0f9eb02e1ed5596a2a027 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 9 Nov 2017 23:05:56 +1100 Subject: [PATCH] Fixes new installation user login --- src/Umbraco.Core/ApplicationContext.cs | 47 +++++++++++++++++ .../BackOfficeCookieAuthenticationProvider.cs | 8 ++- src/Umbraco.Web/Install/InstallHelper.cs | 4 +- .../Install/InstallSteps/UpgradeStep.cs | 51 +++---------------- 4 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/Umbraco.Core/ApplicationContext.cs b/src/Umbraco.Core/ApplicationContext.cs index 79180550f0..ca54afb7b1 100644 --- a/src/Umbraco.Core/ApplicationContext.cs +++ b/src/Umbraco.Core/ApplicationContext.cs @@ -1,6 +1,7 @@ using System; using System.Configuration; using System.Threading; +using Semver; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.ObjectResolution; @@ -341,6 +342,52 @@ namespace Umbraco.Core } } + /// + /// Gets the Current Version of the Umbraco Site before an upgrade + /// by using the last/most recent Umbraco Migration that has been run + /// + /// A SemVersion of the latest Umbraco DB Migration run + /// + /// NOTE: This existed in the InstallHelper previously but should really be here so it can be re-used if necessary + /// + internal SemVersion CurrentVersion() + { + //Set a default version of 0.0.0 + var version = new SemVersion(0); + + //If we have a db context available, if we don't then we are not installed anyways + if (DatabaseContext.IsDatabaseConfigured && DatabaseContext.CanConnect) + version = DatabaseContext.ValidateDatabaseSchema().DetermineInstalledVersionByMigrations(Services.MigrationEntryService); + + if (version != new SemVersion(0)) + return version; + + // If we aren't able to get a result from the umbracoMigrations table then use the version in web.config, if it's available + if (string.IsNullOrWhiteSpace(GlobalSettings.ConfigurationStatus)) + return version; + + var configuredVersion = GlobalSettings.ConfigurationStatus; + + string currentComment = null; + + var current = configuredVersion.Split('-'); + if (current.Length > 1) + currentComment = current[1]; + + Version currentVersion; + if (Version.TryParse(current[0], out currentVersion)) + { + version = new SemVersion( + currentVersion.Major, + currentVersion.Minor, + currentVersion.Build, + string.IsNullOrWhiteSpace(currentComment) ? null : currentComment, + currentVersion.Revision > 0 ? currentVersion.Revision.ToString() : null); + } + + return version; + } + private void AssertIsNotReady() { if (this.IsReady) diff --git a/src/Umbraco.Core/Security/BackOfficeCookieAuthenticationProvider.cs b/src/Umbraco.Core/Security/BackOfficeCookieAuthenticationProvider.cs index 0a51359bde..3ff1266c6b 100644 --- a/src/Umbraco.Core/Security/BackOfficeCookieAuthenticationProvider.cs +++ b/src/Umbraco.Core/Security/BackOfficeCookieAuthenticationProvider.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Microsoft.AspNet.Identity; using Microsoft.Owin; using Microsoft.Owin.Security.Cookies; +using Semver; using Umbraco.Core.Configuration; using Umbraco.Core.Models.Identity; @@ -31,6 +32,8 @@ namespace Umbraco.Core.Security _appCtx = appCtx; } + private static readonly SemVersion MinUmbracoVersionSupportingLoginSessions = new SemVersion(7, 8); + public override void ResponseSignIn(CookieResponseSignInContext context) { var backOfficeIdentity = context.Identity as UmbracoBackOfficeIdentity; @@ -39,7 +42,10 @@ namespace Umbraco.Core.Security //generate a session id and assign it //create a session token - if we are configured and not in an upgrade state then use the db, otherwise just generate one - var session = _appCtx.IsConfigured && _appCtx.IsUpgrading == false + //NOTE - special check because when we are upgrading to 7.8 we cannot create a session since the db isn't ready and we'll get exceptions + var canAcquireSession = _appCtx.IsUpgrading == false || _appCtx.CurrentVersion() >= MinUmbracoVersionSupportingLoginSessions; + + var session = canAcquireSession ? _appCtx.Services.UserService.CreateLoginSession((int)backOfficeIdentity.Id, context.OwinContext.GetCurrentRequestIpAddress()) : Guid.NewGuid(); diff --git a/src/Umbraco.Web/Install/InstallHelper.cs b/src/Umbraco.Web/Install/InstallHelper.cs index a3069342fc..b2c3d1213d 100644 --- a/src/Umbraco.Web/Install/InstallHelper.cs +++ b/src/Umbraco.Web/Install/InstallHelper.cs @@ -8,6 +8,7 @@ using System.Net.Http; using System.Web; using System.Web.Script.Serialization; using System.Web.UI; +using Semver; using umbraco.BusinessLogic; using Umbraco.Core; using Umbraco.Core.Configuration; @@ -30,7 +31,6 @@ namespace Umbraco.Web.Install _umbContext = umbContext; } - /// /// Get the installer steps /// @@ -43,7 +43,7 @@ namespace Umbraco.Web.Install return new List { new NewInstallStep(_umbContext.HttpContext, _umbContext.Application), - new UpgradeStep(), + new UpgradeStep(_umbContext.Application), new FilePermissionsStep(), new MajorVersion7UpgradeReport(_umbContext.Application), new Version73FileCleanup(_umbContext.HttpContext, _umbContext.Application.ProfilingLogger.Logger), diff --git a/src/Umbraco.Web/Install/InstallSteps/UpgradeStep.cs b/src/Umbraco.Web/Install/InstallSteps/UpgradeStep.cs index 5ef2577179..902cae8096 100644 --- a/src/Umbraco.Web/Install/InstallSteps/UpgradeStep.cs +++ b/src/Umbraco.Web/Install/InstallSteps/UpgradeStep.cs @@ -14,6 +14,13 @@ namespace Umbraco.Web.Install.InstallSteps "Upgrade", "upgrade", 1, "Upgrading Umbraco to the latest and greatest version.")] internal class UpgradeStep : InstallSetupStep { + private readonly ApplicationContext _appCtx; + + public UpgradeStep(ApplicationContext appCtx) + { + _appCtx = appCtx; + } + public override bool RequiresExecution(object model) { return true; @@ -28,7 +35,7 @@ namespace Umbraco.Web.Install.InstallSteps { get { - var currentVersion = CurrentVersion().GetVersion(3).ToString(); + var currentVersion = _appCtx.CurrentVersion().GetVersion(3).ToString(); var newVersion = UmbracoVersion.Current.ToString(); var reportUrl = string.Format("https://our.umbraco.org/contribute/releases/compare?from={0}&to={1}¬es=1", currentVersion, newVersion); @@ -42,47 +49,5 @@ namespace Umbraco.Web.Install.InstallSteps } } - /// - /// Gets the Current Version of the Umbraco Site before an upgrade - /// by using the last/most recent Umbraco Migration that has been run - /// - /// A SemVersion of the latest Umbraco DB Migration run - private SemVersion CurrentVersion() - { - //Set a default version of 0.0.0 - var version = new SemVersion(0); - - //If we have a db context available, if we don't then we are not installed anyways - if (ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured && ApplicationContext.Current.DatabaseContext.CanConnect) - version = ApplicationContext.Current.DatabaseContext.ValidateDatabaseSchema().DetermineInstalledVersionByMigrations(ApplicationContext.Current.Services.MigrationEntryService); - - if (version != new SemVersion(0)) - return version; - - // If we aren't able to get a result from the umbracoMigrations table then use the version in web.config, if it's available - if (string.IsNullOrWhiteSpace(GlobalSettings.ConfigurationStatus)) - return version; - - var configuredVersion = GlobalSettings.ConfigurationStatus; - - string currentComment = null; - - var current = configuredVersion.Split('-'); - if (current.Length > 1) - currentComment = current[1]; - - Version currentVersion; - if (Version.TryParse(current[0], out currentVersion)) - { - version = new SemVersion( - currentVersion.Major, - currentVersion.Minor, - currentVersion.Build, - string.IsNullOrWhiteSpace(currentComment) ? null : currentComment, - currentVersion.Revision > 0 ? currentVersion.Revision.ToString() : null); - } - - return version; - } } } \ No newline at end of file