From 31130be85ae1352c5d9e9e8dea00dc8a59d20df8 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 13 Feb 2020 12:29:08 +0100 Subject: [PATCH] Move files --- .../HybridVariationContextAccessor.cs | 2 +- .../PublishedValueFallback.cs | 12 ++- .../Cache/DistributedCacheBinderTests.cs | 5 +- .../PublishedContentCacheTests.cs | 6 +- .../PublishedContentSnapshotTestBase.cs | 4 +- .../Scoping/ScopedNuCacheTests.cs | 4 +- .../Security/BackOfficeCookieManagerTests.cs | 6 +- .../TestHelpers/TestObjects-Mocks.cs | 5 +- .../TestHelpers/TestWithDatabaseBase.cs | 4 +- .../Objects/TestUmbracoContextFactory.cs | 5 +- src/Umbraco.Tests/Views/web.config | 74 ------------------- ...RenderIndexActionSelectorAttributeTests.cs | 17 +++-- .../Web/Mvc/SurfaceControllerTests.cs | 16 +++- .../Web/Mvc/UmbracoViewPageTests.cs | 3 +- .../Web/WebExtensionMethodTests.cs | 12 ++- .../InstallSteps/SetUmbracoVersionStep.cs | 2 +- src/Umbraco.Web/Security/WebSecurity.cs | 31 ++++---- src/Umbraco.Web/UmbracoContextFactory.cs | 6 +- 18 files changed, 89 insertions(+), 125 deletions(-) rename src/{Umbraco.Web => Umbraco.Abstractions}/Models/PublishedContent/HybridVariationContextAccessor.cs (86%) delete mode 100644 src/Umbraco.Tests/Views/web.config diff --git a/src/Umbraco.Web/Models/PublishedContent/HybridVariationContextAccessor.cs b/src/Umbraco.Abstractions/Models/PublishedContent/HybridVariationContextAccessor.cs similarity index 86% rename from src/Umbraco.Web/Models/PublishedContent/HybridVariationContextAccessor.cs rename to src/Umbraco.Abstractions/Models/PublishedContent/HybridVariationContextAccessor.cs index 897ff17db4..6f97c1dc5c 100644 --- a/src/Umbraco.Web/Models/PublishedContent/HybridVariationContextAccessor.cs +++ b/src/Umbraco.Abstractions/Models/PublishedContent/HybridVariationContextAccessor.cs @@ -6,7 +6,7 @@ namespace Umbraco.Web.Models.PublishedContent /// /// Implements a hybrid . /// - internal class HybridVariationContextAccessor : HybridAccessorBase, IVariationContextAccessor + public class HybridVariationContextAccessor : HybridAccessorBase, IVariationContextAccessor { public HybridVariationContextAccessor(IRequestCache requestCache) : base(requestCache) diff --git a/src/Umbraco.Abstractions/Models/PublishedContent/PublishedValueFallback.cs b/src/Umbraco.Abstractions/Models/PublishedContent/PublishedValueFallback.cs index 6a3cae9120..7c207c23c0 100644 --- a/src/Umbraco.Abstractions/Models/PublishedContent/PublishedValueFallback.cs +++ b/src/Umbraco.Abstractions/Models/PublishedContent/PublishedValueFallback.cs @@ -14,16 +14,14 @@ namespace Umbraco.Web.Models.PublishedContent { private readonly ILocalizationService _localizationService; private readonly IVariationContextAccessor _variationContextAccessor; - private readonly IPublishedValueFallback _publishedValueFallback; /// /// Initializes a new instance of the class. /// - public PublishedValueFallback(ServiceContext serviceContext, IVariationContextAccessor variationContextAccessor, IPublishedValueFallback publishedValueFallback) + public PublishedValueFallback(ServiceContext serviceContext, IVariationContextAccessor variationContextAccessor) { _localizationService = serviceContext.LocalizationService; _variationContextAccessor = variationContextAccessor; - _publishedValueFallback = publishedValueFallback; } /// @@ -184,7 +182,7 @@ namespace Umbraco.Web.Models.PublishedContent // if we found a content with the property having a value, return that property value if (property != null && property.HasValue(culture, segment)) { - value = property.Value(_publishedValueFallback, culture, segment); + value = property.Value(this, culture, segment); return true; } @@ -218,7 +216,7 @@ namespace Umbraco.Web.Models.PublishedContent if (property.HasValue(culture2, segment)) { - value = property.Value(_publishedValueFallback, culture2, segment); + value = property.Value(this, culture2, segment); return true; } @@ -252,7 +250,7 @@ namespace Umbraco.Web.Models.PublishedContent if (content.HasValue(alias, culture2, segment)) { - value = content.Value(_publishedValueFallback, alias, culture2, segment); + value = content.Value(this, alias, culture2, segment); return true; } @@ -289,7 +287,7 @@ namespace Umbraco.Web.Models.PublishedContent if (content.HasValue(alias, culture2, segment)) { - value = content.Value(_publishedValueFallback, alias, culture2, segment); + value = content.Value(this, alias, culture2, segment); return true; } diff --git a/src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs b/src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs index 5fe13f01aa..fda3683064 100644 --- a/src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs +++ b/src/Umbraco.Tests/Cache/DistributedCacheBinderTests.cs @@ -153,6 +153,8 @@ namespace Umbraco.Tests.Cache }; + var httpContextAccessor = TestObjects.GetHttpContextAccessor(); + var umbracoContextFactory = new UmbracoContextFactory( new TestUmbracoContextAccessor(), Mock.Of(), @@ -163,7 +165,8 @@ namespace Umbraco.Tests.Cache new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - IOHelper); + IOHelper, + httpContextAccessor); // just assert it does not throw var refreshers = new DistributedCacheBinder(null, umbracoContextFactory, null); diff --git a/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs b/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs index 36d78adf10..48bf22d96e 100644 --- a/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs +++ b/src/Umbraco.Tests/Cache/PublishedCache/PublishedContentCacheTests.cs @@ -74,10 +74,12 @@ namespace Umbraco.Tests.Cache.PublishedCache var publishedSnapshotService = new Mock(); publishedSnapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny())).Returns(publishedShapshot); + var httpContext = _httpContextFactory.HttpContext; + var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext); _umbracoContext = new UmbracoContext( - _httpContextFactory.HttpContext, + httpContext, publishedSnapshotService.Object, - new WebSecurity(_httpContextFactory.HttpContext, Mock.Of(), globalSettings, IOHelper), + new WebSecurity(httpContextAccessor, Mock.Of(), globalSettings, IOHelper), umbracoSettings, Enumerable.Empty(), Enumerable.Empty(), diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentSnapshotTestBase.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentSnapshotTestBase.cs index a1592a57d0..614cbfa393 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentSnapshotTestBase.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentSnapshotTestBase.cs @@ -68,10 +68,12 @@ namespace Umbraco.Tests.PublishedContent var globalSettings = TestObjects.GetGlobalSettings(); var httpContext = GetHttpContextFactory("http://umbraco.local/", routeData).HttpContext; + + var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext); var umbracoContext = new UmbracoContext( httpContext, publishedSnapshotService.Object, - new WebSecurity(httpContext, Current.Services.UserService, globalSettings, IOHelper), + new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), TestObjects.GetUmbracoSettings(), Enumerable.Empty(), Enumerable.Empty(), diff --git a/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs b/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs index ed25764201..8ec190a2eb 100644 --- a/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs @@ -117,12 +117,12 @@ namespace Umbraco.Tests.Scoping var service = PublishedSnapshotService as PublishedSnapshotService; var httpContext = GetHttpContextFactory(url, routeData).HttpContext; - + var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext); var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContext = new UmbracoContext( httpContext, service, - new WebSecurity(httpContext, Current.Services.UserService, globalSettings, IOHelper), + new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), umbracoSettings ?? SettingsForTests.GetDefaultUmbracoSettings(), urlProviders ?? Enumerable.Empty(), Enumerable.Empty(), diff --git a/src/Umbraco.Tests/Security/BackOfficeCookieManagerTests.cs b/src/Umbraco.Tests/Security/BackOfficeCookieManagerTests.cs index b16c739553..3dabcc556f 100644 --- a/src/Umbraco.Tests/Security/BackOfficeCookieManagerTests.cs +++ b/src/Umbraco.Tests/Security/BackOfficeCookieManagerTests.cs @@ -28,11 +28,12 @@ namespace Umbraco.Tests.Security //should force app ctx to show not-configured ConfigurationManager.AppSettings.Set(Constants.AppSettings.ConfigurationStatus, ""); + var httpContextAccessor = TestObjects.GetHttpContextAccessor(); var globalSettings = TestObjects.GetGlobalSettings(); var umbracoContext = new UmbracoContext( Mock.Of(), Mock.Of(), - new WebSecurity(Mock.Of(), Current.Services.UserService, globalSettings, IOHelper), + new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), TestObjects.GetUmbracoSettings(), new List(), Enumerable.Empty(), globalSettings, new TestVariationContextAccessor(), IOHelper); @@ -48,11 +49,12 @@ namespace Umbraco.Tests.Security [Test] public void ShouldAuthenticateRequest_When_Configured() { + var httpContextAccessor = TestObjects.GetHttpContextAccessor(); var globalSettings = TestObjects.GetGlobalSettings(); var umbCtx = new UmbracoContext( Mock.Of(), Mock.Of(), - new WebSecurity(Mock.Of(), Current.Services.UserService, globalSettings, IOHelper), + new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), TestObjects.GetUmbracoSettings(), new List(), Enumerable.Empty(), globalSettings, new TestVariationContextAccessor(), IOHelper); diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs index 12282d1603..d25b07722f 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs @@ -127,6 +127,8 @@ namespace Umbraco.Tests.TestHelpers if (accessor == null) accessor = new TestUmbracoContextAccessor(); + var httpContextAccessor = Mock.Of(); + var umbracoContextFactory = new UmbracoContextFactory( accessor, publishedSnapshotService, @@ -137,7 +139,8 @@ namespace Umbraco.Tests.TestHelpers urlProviders, mediaUrlProviders, Mock.Of(), - TestHelper.IOHelper); + TestHelper.IOHelper, + httpContextAccessor); return umbracoContextFactory.EnsureUmbracoContext(httpContext).UmbracoContext; } diff --git a/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs b/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs index 5f6467abc4..f85a1e6332 100644 --- a/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs +++ b/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs @@ -380,11 +380,11 @@ namespace Umbraco.Tests.TestHelpers } var httpContext = GetHttpContextFactory(url, routeData).HttpContext; - + var httpContextAccessor = TestObjects.GetHttpContextAccessor(httpContext); var umbracoContext = new UmbracoContext( httpContext, service, - new WebSecurity(httpContext, Factory.GetInstance(), + new WebSecurity(httpContextAccessor, Factory.GetInstance(), Factory.GetInstance(), IOHelper), umbracoSettings ?? Factory.GetInstance(), urlProviders ?? Enumerable.Empty(), diff --git a/src/Umbraco.Tests/Testing/Objects/TestUmbracoContextFactory.cs b/src/Umbraco.Tests/Testing/Objects/TestUmbracoContextFactory.cs index f28613735d..8ea1f4fbcf 100644 --- a/src/Umbraco.Tests/Testing/Objects/TestUmbracoContextFactory.cs +++ b/src/Umbraco.Tests/Testing/Objects/TestUmbracoContextFactory.cs @@ -33,6 +33,8 @@ namespace Umbraco.Tests.Testing.Objects var snapshotService = new Mock(); snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny())).Returns(snapshot.Object); + var httpContextAccessor = Mock.Of(); + var umbracoContextFactory = new UmbracoContextFactory( umbracoContextAccessor, snapshotService.Object, @@ -43,7 +45,8 @@ namespace Umbraco.Tests.Testing.Objects new UrlProviderCollection(new[] { urlProvider }), new MediaUrlProviderCollection(new[] { mediaUrlProvider }), Mock.Of(), - TestHelper.IOHelper); + TestHelper.IOHelper, + httpContextAccessor); return umbracoContextFactory; } diff --git a/src/Umbraco.Tests/Views/web.config b/src/Umbraco.Tests/Views/web.config deleted file mode 100644 index efd80424e5..0000000000 --- a/src/Umbraco.Tests/Views/web.config +++ /dev/null @@ -1,74 +0,0 @@ - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs b/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs index 2daa8e9a98..54e45b5532 100644 --- a/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs @@ -63,7 +63,7 @@ namespace Umbraco.Tests.Web.Mvc var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); - + var httpContextAccessor = Mock.Of(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, Mock.Of(), @@ -74,7 +74,8 @@ namespace Umbraco.Tests.Web.Mvc new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - TestHelper.IOHelper); + TestHelper.IOHelper, + httpContextAccessor); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbCtx = umbracoContextReference.UmbracoContext; @@ -94,6 +95,7 @@ namespace Umbraco.Tests.Web.Mvc var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); + var httpContextAccessor = Mock.Of(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -105,7 +107,8 @@ namespace Umbraco.Tests.Web.Mvc new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - TestHelper.IOHelper); + TestHelper.IOHelper, + httpContextAccessor); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbCtx = umbracoContextReference.UmbracoContext; @@ -125,6 +128,7 @@ namespace Umbraco.Tests.Web.Mvc var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); + var httpContextAccessor = Mock.Of(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -136,7 +140,8 @@ namespace Umbraco.Tests.Web.Mvc new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - TestHelper.IOHelper); + TestHelper.IOHelper, + httpContextAccessor); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbCtx = umbracoContextReference.UmbracoContext; @@ -156,6 +161,7 @@ namespace Umbraco.Tests.Web.Mvc var globalSettings = TestObjects.GetGlobalSettings(); var attr = new RenderIndexActionSelectorAttribute(); var req = new RequestContext(); + var httpContextAccessor = Mock.Of(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -167,7 +173,8 @@ namespace Umbraco.Tests.Web.Mvc new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - TestHelper.IOHelper); + TestHelper.IOHelper, + httpContextAccessor); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbCtx = umbracoContextReference.UmbracoContext; diff --git a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs index 6a75fe5457..c4972b16db 100644 --- a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs @@ -39,6 +39,7 @@ namespace Umbraco.Tests.Web.Mvc public void Can_Construct_And_Get_Result() { var globalSettings = TestObjects.GetGlobalSettings(); + var httpContextAccessor = TestObjects.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -50,7 +51,8 @@ namespace Umbraco.Tests.Web.Mvc new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - IOHelper); + IOHelper, + httpContextAccessor); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbracoContext = umbracoContextReference.UmbracoContext; @@ -68,6 +70,7 @@ namespace Umbraco.Tests.Web.Mvc public void Umbraco_Context_Not_Null() { var globalSettings = TestObjects.GetGlobalSettings(); + var httpContextAccessor = TestObjects.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -79,7 +82,8 @@ namespace Umbraco.Tests.Web.Mvc new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - IOHelper); + IOHelper, + httpContextAccessor); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbCtx = umbracoContextReference.UmbracoContext; @@ -100,6 +104,7 @@ namespace Umbraco.Tests.Web.Mvc content.Setup(x => x.Id).Returns(2); var publishedSnapshotService = new Mock(); var globalSettings = TestObjects.GetGlobalSettings(); + var httpContextAccessor = TestObjects.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -111,7 +116,8 @@ namespace Umbraco.Tests.Web.Mvc new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - IOHelper); + IOHelper, + httpContextAccessor); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbracoContext = umbracoContextReference.UmbracoContext; @@ -139,6 +145,7 @@ namespace Umbraco.Tests.Web.Mvc { var webRoutingSettings = Mock.Of(section => section.UrlProviderMode == "Auto"); var globalSettings = TestObjects.GetGlobalSettings(); + var httpContextAccessor = TestObjects.GetHttpContextAccessor(); var umbracoContextFactory = new UmbracoContextFactory( Current.UmbracoContextAccessor, @@ -150,7 +157,8 @@ namespace Umbraco.Tests.Web.Mvc new UrlProviderCollection(Enumerable.Empty()), new MediaUrlProviderCollection(Enumerable.Empty()), Mock.Of(), - IOHelper); + IOHelper, + httpContextAccessor); var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of()); var umbracoContext = umbracoContextReference.UmbracoContext; diff --git a/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs b/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs index 6fa54e34cb..780ad63156 100644 --- a/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/UmbracoViewPageTests.cs @@ -435,12 +435,13 @@ namespace Umbraco.Tests.Web.Mvc var http = GetHttpContextFactory(url, routeData).HttpContext; + var httpContextAccessor = TestObjects.GetHttpContextAccessor(http); var globalSettings = TestObjects.GetGlobalSettings(); var ctx = new UmbracoContext( http, _service, - new WebSecurity(http, Current.Services.UserService, globalSettings, IOHelper), + new WebSecurity(httpContextAccessor, Current.Services.UserService, globalSettings, IOHelper), TestObjects.GetUmbracoSettings(), Enumerable.Empty(), Enumerable.Empty(), diff --git a/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs b/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs index cb125fa851..db4dc4b705 100644 --- a/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs +++ b/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs @@ -26,10 +26,12 @@ namespace Umbraco.Tests.Web [Test] public void RouteDataExtensions_GetUmbracoContext() { + var httpContextAccessor = Mock.Of(); + var umbCtx = new UmbracoContext( Mock.Of(), Mock.Of(), - new WebSecurity(Mock.Of(), Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), + new WebSecurity(httpContextAccessor, Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), TestObjects.GetUmbracoSettings(), new List(), Enumerable.Empty(), @@ -46,10 +48,12 @@ namespace Umbraco.Tests.Web [Test] public void ControllerContextExtensions_GetUmbracoContext_From_RouteValues() { + var httpContextAccessor = Mock.Of(); + var umbCtx = new UmbracoContext( Mock.Of(), Mock.Of(), - new WebSecurity(Mock.Of(), Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), + new WebSecurity(httpContextAccessor, Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), TestObjects.GetUmbracoSettings(), new List(), Enumerable.Empty(), @@ -76,10 +80,12 @@ namespace Umbraco.Tests.Web [Test] public void ControllerContextExtensions_GetUmbracoContext_From_Current() { + var httpContextAccessor = Mock.Of(); + var umbCtx = new UmbracoContext( Mock.Of(), Mock.Of(), - new WebSecurity(Mock.Of(), Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), + new WebSecurity(httpContextAccessor, Current.Services.UserService, TestObjects.GetGlobalSettings(), IOHelper), TestObjects.GetUmbracoSettings(), new List(), Enumerable.Empty(), diff --git a/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs b/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs index 6b9e2f392e..dcf115082f 100644 --- a/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs +++ b/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs @@ -37,7 +37,7 @@ namespace Umbraco.Web.Install.InstallSteps public override Task ExecuteAsync(object model) { - var security = new WebSecurity(_httpContextAccessor.HttpContext, _userService, _globalSettings, _ioHelper); + var security = new WebSecurity(_httpContextAccessor, _userService, _globalSettings, _ioHelper); if (security.IsAuthenticated() == false && _globalSettings.ConfigurationStatus.IsNullOrWhiteSpace()) { diff --git a/src/Umbraco.Web/Security/WebSecurity.cs b/src/Umbraco.Web/Security/WebSecurity.cs index 9a5bfb2437..842ec47962 100644 --- a/src/Umbraco.Web/Security/WebSecurity.cs +++ b/src/Umbraco.Web/Security/WebSecurity.cs @@ -22,14 +22,14 @@ namespace Umbraco.Web.Security /// public class WebSecurity : IWebSecurity { - private readonly HttpContextBase _httpContext; + private readonly IHttpContextAccessor _httpContextAccessor; private readonly IUserService _userService; private readonly IGlobalSettings _globalSettings; private readonly IIOHelper _ioHelper; - public WebSecurity(HttpContextBase httpContext, IUserService userService, IGlobalSettings globalSettings, IIOHelper ioHelper) + public WebSecurity(IHttpContextAccessor httpContextAccessor, IUserService userService, IGlobalSettings globalSettings, IIOHelper ioHelper) { - _httpContext = httpContext; + _httpContextAccessor = httpContextAccessor; _userService = userService; _globalSettings = globalSettings; _ioHelper = ioHelper; @@ -63,7 +63,7 @@ namespace Umbraco.Web.Security { if (_signInManager == null) { - var mgr = _httpContext.GetOwinContext().Get(); + var mgr = _httpContextAccessor.HttpContext.GetOwinContext().Get(); if (mgr == null) { throw new NullReferenceException("Could not resolve an instance of " + typeof(BackOfficeSignInManager) + " from the " + typeof(IOwinContext)); @@ -76,7 +76,7 @@ namespace Umbraco.Web.Security private BackOfficeUserManager _userManager; protected BackOfficeUserManager UserManager - => _userManager ?? (_userManager = _httpContext.GetOwinContext().GetBackOfficeUserManager()); + => _userManager ?? (_userManager = _httpContextAccessor.HttpContext.GetOwinContext().GetBackOfficeUserManager()); /// /// Logs a user in. @@ -85,7 +85,7 @@ namespace Umbraco.Web.Security /// returns the number of seconds until their session times out public virtual double PerformLogin(int userId) { - var owinCtx = _httpContext.GetOwinContext(); + var owinCtx = _httpContextAccessor.HttpContext.GetOwinContext(); //ensure it's done for owin too owinCtx.Authentication.SignOut(Constants.Security.BackOfficeExternalAuthenticationType); @@ -93,7 +93,7 @@ namespace Umbraco.Web.Security SignInManager.SignInAsync(user, isPersistent: true, rememberBrowser: false).Wait(); - _httpContext.SetPrincipalForRequest(owinCtx.Request.User); + _httpContextAccessor.HttpContext.SetPrincipalForRequest(owinCtx.Request.User); return TimeSpan.FromMinutes(_globalSettings.TimeOutInMinutes).TotalSeconds; } @@ -103,8 +103,8 @@ namespace Umbraco.Web.Security /// public virtual void ClearCurrentLogin() { - _httpContext.UmbracoLogout(); - _httpContext.GetOwinContext().Authentication.SignOut( + _httpContextAccessor.HttpContext.UmbracoLogout(); + _httpContextAccessor.HttpContext.GetOwinContext().Authentication.SignOut( Core.Constants.Security.BackOfficeAuthenticationType, Core.Constants.Security.BackOfficeExternalAuthenticationType); } @@ -114,7 +114,7 @@ namespace Umbraco.Web.Security /// public virtual void RenewLoginTimeout() { - _httpContext.RenewUmbracoAuthTicket(); + _httpContextAccessor.HttpContext.RenewUmbracoAuthTicket(); } /// @@ -154,7 +154,7 @@ namespace Umbraco.Web.Security /// public virtual Attempt GetUserId() { - var identity = _httpContext.GetCurrentIdentity(false); + var identity = _httpContextAccessor.HttpContext.GetCurrentIdentity(false); return identity == null ? Attempt.Fail() : Attempt.Succeed(Convert.ToInt32(identity.Id)); } @@ -164,7 +164,7 @@ namespace Umbraco.Web.Security /// public virtual string GetSessionId() { - var identity = _httpContext.GetCurrentIdentity(false); + var identity = _httpContextAccessor.HttpContext.GetCurrentIdentity(false); return identity?.SessionId; } @@ -199,7 +199,7 @@ namespace Umbraco.Web.Security var user = CurrentUser; // Check for console access - if (user == null || (requiresApproval && user.IsApproved == false) || (user.IsLockedOut && RequestIsInUmbracoApplication(_httpContext, _globalSettings, _ioHelper))) + if (user == null || (requiresApproval && user.IsApproved == false) || (user.IsLockedOut && RequestIsInUmbracoApplication(_httpContextAccessor.HttpContext, _globalSettings, _ioHelper))) { if (throwExceptions) throw new ArgumentException("You have no privileges to the umbraco console. Please contact your administrator"); return ValidateRequestAttempt.FailedNoPrivileges; @@ -221,7 +221,7 @@ namespace Umbraco.Web.Security public ValidateRequestAttempt AuthorizeRequest(bool throwExceptions = false) { // check for secure connection - if (_globalSettings.UseHttps && _httpContext.Request.IsSecureConnection == false) + if (_globalSettings.UseHttps && _httpContextAccessor.HttpContext.Request.IsSecureConnection == false) { if (throwExceptions) throw new SecurityException("This installation requires a secure connection (via SSL). Please update the URL to include https://"); return ValidateRequestAttempt.FailedNoSsl; @@ -262,7 +262,8 @@ namespace Umbraco.Web.Security /// public bool IsAuthenticated() { - return _httpContext.User != null && _httpContext.User.Identity.IsAuthenticated && _httpContext.GetCurrentIdentity(false) != null; + var httpContext = _httpContextAccessor.HttpContext; + return httpContext.User != null && httpContext.User.Identity.IsAuthenticated && httpContext.GetCurrentIdentity(false) != null; } } diff --git a/src/Umbraco.Web/UmbracoContextFactory.cs b/src/Umbraco.Web/UmbracoContextFactory.cs index 268048edd3..38c7d03835 100644 --- a/src/Umbraco.Web/UmbracoContextFactory.cs +++ b/src/Umbraco.Web/UmbracoContextFactory.cs @@ -34,11 +34,12 @@ namespace Umbraco.Web private readonly MediaUrlProviderCollection _mediaUrlProviders; private readonly IUserService _userService; private readonly IIOHelper _ioHelper; + private readonly IHttpContextAccessor _httpContextAccessor; /// /// Initializes a new instance of the class. /// - public UmbracoContextFactory(IUmbracoContextAccessor umbracoContextAccessor, IPublishedSnapshotService publishedSnapshotService, IVariationContextAccessor variationContextAccessor, IDefaultCultureAccessor defaultCultureAccessor, IUmbracoSettingsSection umbracoSettings, IGlobalSettings globalSettings, UrlProviderCollection urlProviders, MediaUrlProviderCollection mediaUrlProviders, IUserService userService, IIOHelper ioHelper) + public UmbracoContextFactory(IUmbracoContextAccessor umbracoContextAccessor, IPublishedSnapshotService publishedSnapshotService, IVariationContextAccessor variationContextAccessor, IDefaultCultureAccessor defaultCultureAccessor, IUmbracoSettingsSection umbracoSettings, IGlobalSettings globalSettings, UrlProviderCollection urlProviders, MediaUrlProviderCollection mediaUrlProviders, IUserService userService, IIOHelper ioHelper, IHttpContextAccessor httpContextAccessor) { _umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor)); _publishedSnapshotService = publishedSnapshotService ?? throw new ArgumentNullException(nameof(publishedSnapshotService)); @@ -51,6 +52,7 @@ namespace Umbraco.Web _mediaUrlProviders = mediaUrlProviders ?? throw new ArgumentNullException(nameof(mediaUrlProviders)); _userService = userService ?? throw new ArgumentNullException(nameof(userService)); _ioHelper = ioHelper; + _httpContextAccessor = httpContextAccessor; } private IUmbracoContext CreateUmbracoContext(HttpContextBase httpContext) @@ -67,7 +69,7 @@ namespace Umbraco.Web } - var webSecurity = new WebSecurity(httpContext, _userService, _globalSettings, _ioHelper); + var webSecurity = new WebSecurity(_httpContextAccessor, _userService, _globalSettings, _ioHelper); return new UmbracoContext(httpContext, _publishedSnapshotService, webSecurity, _umbracoSettings, _urlProviders, _mediaUrlProviders, _globalSettings, _variationContextAccessor, _ioHelper); }