From 4bca006e869799074a4844707eb55a0c24d0c8a8 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Wed, 8 Jan 2020 14:46:20 +0100 Subject: [PATCH] AB4385 - Moved Content Apps to Infrastructure - Introduced ICurrentUserAccessor in Abstractions --- .../ContentAppFactoryCollection.cs | 8 ++++--- .../ContentAppFactoryCollectionBuilder.cs | 9 ++++---- .../ContentEditorContentAppFactory.cs | 0 .../ContentInfoContentAppFactory.cs | 0 .../ContentApps/ListViewContentAppFactory.cs | 0 .../ContentEditing/ContentPropertyBasic.cs | 0 .../ContentEditing/ContentPropertyDisplay.cs | 0 .../ContentEditing/PropertyTypeValidation.cs | 0 .../Models/Identity/ICurrentUserAccessor.cs | 13 ++++++++++++ src/Umbraco.Tests/Testing/UmbracoTestBase.cs | 3 +++ src/Umbraco.Web/Runtime/WebInitialComposer.cs | 2 ++ .../Security/CurrentUserAccessor.cs | 21 +++++++++++++++++++ src/Umbraco.Web/Umbraco.Web.csproj | 9 +------- 13 files changed, 50 insertions(+), 15 deletions(-) rename src/{Umbraco.Web => Umbraco.Abstractions}/ContentApps/ContentAppFactoryCollection.cs (85%) rename src/{Umbraco.Web => Umbraco.Abstractions}/ContentApps/ContentAppFactoryCollectionBuilder.cs (86%) rename src/{Umbraco.Web => Umbraco.Abstractions}/ContentApps/ContentEditorContentAppFactory.cs (100%) rename src/{Umbraco.Web => Umbraco.Abstractions}/ContentApps/ContentInfoContentAppFactory.cs (100%) rename src/{Umbraco.Web => Umbraco.Abstractions}/ContentApps/ListViewContentAppFactory.cs (100%) rename src/{Umbraco.Web => Umbraco.Abstractions}/Models/ContentEditing/ContentPropertyBasic.cs (100%) rename src/{Umbraco.Web => Umbraco.Abstractions}/Models/ContentEditing/ContentPropertyDisplay.cs (100%) rename src/{Umbraco.Web => Umbraco.Abstractions}/Models/ContentEditing/PropertyTypeValidation.cs (100%) create mode 100644 src/Umbraco.Abstractions/Models/Identity/ICurrentUserAccessor.cs create mode 100644 src/Umbraco.Web/Security/CurrentUserAccessor.cs diff --git a/src/Umbraco.Web/ContentApps/ContentAppFactoryCollection.cs b/src/Umbraco.Abstractions/ContentApps/ContentAppFactoryCollection.cs similarity index 85% rename from src/Umbraco.Web/ContentApps/ContentAppFactoryCollection.cs rename to src/Umbraco.Abstractions/ContentApps/ContentAppFactoryCollection.cs index 07987aea3e..f8f57ce4fd 100644 --- a/src/Umbraco.Web/ContentApps/ContentAppFactoryCollection.cs +++ b/src/Umbraco.Abstractions/ContentApps/ContentAppFactoryCollection.cs @@ -4,6 +4,7 @@ using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Core.Models.ContentEditing; using Umbraco.Core.Logging; +using Umbraco.Core.Models.Identity; using Umbraco.Core.Models.Membership; namespace Umbraco.Web.ContentApps @@ -11,17 +12,18 @@ namespace Umbraco.Web.ContentApps public class ContentAppFactoryCollection : BuilderCollectionBase { private readonly ILogger _logger; + private readonly ICurrentUserAccessor _currentUserAccessor; - public ContentAppFactoryCollection(IEnumerable items, ILogger logger) + public ContentAppFactoryCollection(IEnumerable items, ILogger logger, ICurrentUserAccessor currentUserAccessor) : base(items) { _logger = logger; + _currentUserAccessor = currentUserAccessor; } private IEnumerable GetCurrentUserGroups() { - var umbracoContext = Composing.Current.UmbracoContext; - var currentUser = umbracoContext?.Security?.CurrentUser; + var currentUser = _currentUserAccessor.TryGetCurrentUser(); return currentUser == null ? Enumerable.Empty() : currentUser.Groups; diff --git a/src/Umbraco.Web/ContentApps/ContentAppFactoryCollectionBuilder.cs b/src/Umbraco.Abstractions/ContentApps/ContentAppFactoryCollectionBuilder.cs similarity index 86% rename from src/Umbraco.Web/ContentApps/ContentAppFactoryCollectionBuilder.cs rename to src/Umbraco.Abstractions/ContentApps/ContentAppFactoryCollectionBuilder.cs index 0561cc06f0..0e21823b90 100644 --- a/src/Umbraco.Web/ContentApps/ContentAppFactoryCollectionBuilder.cs +++ b/src/Umbraco.Abstractions/ContentApps/ContentAppFactoryCollectionBuilder.cs @@ -6,6 +6,7 @@ using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Manifest; using Umbraco.Core.Models.ContentEditing; +using Umbraco.Core.Models.Identity; namespace Umbraco.Web.ContentApps { @@ -18,8 +19,8 @@ namespace Umbraco.Web.ContentApps { // get the logger just-in-time - see note below for manifest parser var logger = factory.GetInstance(); - - return new ContentAppFactoryCollection(CreateItems(factory), logger); + var currentUserAccessor = factory.GetInstance(); + return new ContentAppFactoryCollection(CreateItems(factory), logger, currentUserAccessor); } protected override IEnumerable CreateItems(IFactory factory) @@ -28,8 +29,8 @@ namespace Umbraco.Web.ContentApps // simply getting the builder in order to configure the collection, would require // its dependencies too, and that can create cycles or other oddities var manifestParser = factory.GetInstance(); - - return base.CreateItems(factory).Concat(manifestParser.Manifest.ContentApps.Select(x => new ManifestContentAppFactory(x, Current.IOHelper))); + var ioHelper = factory.GetInstance(); + return base.CreateItems(factory).Concat(manifestParser.Manifest.ContentApps.Select(x => new ManifestContentAppFactory(x, ioHelper))); } } } diff --git a/src/Umbraco.Web/ContentApps/ContentEditorContentAppFactory.cs b/src/Umbraco.Abstractions/ContentApps/ContentEditorContentAppFactory.cs similarity index 100% rename from src/Umbraco.Web/ContentApps/ContentEditorContentAppFactory.cs rename to src/Umbraco.Abstractions/ContentApps/ContentEditorContentAppFactory.cs diff --git a/src/Umbraco.Web/ContentApps/ContentInfoContentAppFactory.cs b/src/Umbraco.Abstractions/ContentApps/ContentInfoContentAppFactory.cs similarity index 100% rename from src/Umbraco.Web/ContentApps/ContentInfoContentAppFactory.cs rename to src/Umbraco.Abstractions/ContentApps/ContentInfoContentAppFactory.cs diff --git a/src/Umbraco.Web/ContentApps/ListViewContentAppFactory.cs b/src/Umbraco.Abstractions/ContentApps/ListViewContentAppFactory.cs similarity index 100% rename from src/Umbraco.Web/ContentApps/ListViewContentAppFactory.cs rename to src/Umbraco.Abstractions/ContentApps/ListViewContentAppFactory.cs diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentPropertyBasic.cs b/src/Umbraco.Abstractions/Models/ContentEditing/ContentPropertyBasic.cs similarity index 100% rename from src/Umbraco.Web/Models/ContentEditing/ContentPropertyBasic.cs rename to src/Umbraco.Abstractions/Models/ContentEditing/ContentPropertyBasic.cs diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentPropertyDisplay.cs b/src/Umbraco.Abstractions/Models/ContentEditing/ContentPropertyDisplay.cs similarity index 100% rename from src/Umbraco.Web/Models/ContentEditing/ContentPropertyDisplay.cs rename to src/Umbraco.Abstractions/Models/ContentEditing/ContentPropertyDisplay.cs diff --git a/src/Umbraco.Web/Models/ContentEditing/PropertyTypeValidation.cs b/src/Umbraco.Abstractions/Models/ContentEditing/PropertyTypeValidation.cs similarity index 100% rename from src/Umbraco.Web/Models/ContentEditing/PropertyTypeValidation.cs rename to src/Umbraco.Abstractions/Models/ContentEditing/PropertyTypeValidation.cs diff --git a/src/Umbraco.Abstractions/Models/Identity/ICurrentUserAccessor.cs b/src/Umbraco.Abstractions/Models/Identity/ICurrentUserAccessor.cs new file mode 100644 index 0000000000..49b2c6d9f9 --- /dev/null +++ b/src/Umbraco.Abstractions/Models/Identity/ICurrentUserAccessor.cs @@ -0,0 +1,13 @@ +using Umbraco.Core.Models.Membership; + +namespace Umbraco.Core.Models.Identity +{ + public interface ICurrentUserAccessor + { + /// + /// Returns the current user or null if no user is currently authenticated. + /// + /// The current user or null + IUser TryGetCurrentUser(); + } +} diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index 0ea43742da..c7d2ea9ca3 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -51,10 +51,12 @@ using FileSystems = Umbraco.Core.IO.FileSystems; using Umbraco.Web.Templates; using Umbraco.Web.PropertyEditors; using Umbraco.Core.Dictionary; +using Umbraco.Core.Models.Identity; using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Security; using Umbraco.Core.Services; using Umbraco.Net; +using Umbraco.Web.Security; namespace Umbraco.Tests.Testing { @@ -195,6 +197,7 @@ namespace Umbraco.Tests.Testing Composition.RegisterUnique(backOfficeInfo); Composition.RegisterUnique(ipResolver); Composition.RegisterUnique(); + Composition.RegisterUnique(); Composition.RegisterUnique(TestHelper.ShortStringHelper); diff --git a/src/Umbraco.Web/Runtime/WebInitialComposer.cs b/src/Umbraco.Web/Runtime/WebInitialComposer.cs index 9b36012ca2..0a5d4489ec 100644 --- a/src/Umbraco.Web/Runtime/WebInitialComposer.cs +++ b/src/Umbraco.Web/Runtime/WebInitialComposer.cs @@ -10,6 +10,7 @@ using Umbraco.Core.Dictionary; using Umbraco.Core.Events; using Umbraco.Core.Hosting; using Umbraco.Core.Migrations.PostMigrations; +using Umbraco.Core.Models.Identity; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.PropertyEditors.ValueConverters; using Umbraco.Core.Runtime; @@ -59,6 +60,7 @@ namespace Umbraco.Web.Runtime composition.Register(); composition.Register(); composition.Register(); + composition.Register(); composition.RegisterUnique(); // required for hybrid accessors diff --git a/src/Umbraco.Web/Security/CurrentUserAccessor.cs b/src/Umbraco.Web/Security/CurrentUserAccessor.cs new file mode 100644 index 0000000000..3d914be77f --- /dev/null +++ b/src/Umbraco.Web/Security/CurrentUserAccessor.cs @@ -0,0 +1,21 @@ +using Umbraco.Core.Models.Identity; +using Umbraco.Core.Models.Membership; + +namespace Umbraco.Web.Security +{ + internal class CurrentUserAccessor : ICurrentUserAccessor + { + private readonly IUmbracoContextAccessor _umbracoContextAccessor; + + public CurrentUserAccessor(IUmbracoContextAccessor umbracoContextAccessor) + { + _umbracoContextAccessor = umbracoContextAccessor; + } + + /// + public IUser TryGetCurrentUser() + { + return _umbracoContextAccessor.UmbracoContext?.Security?.CurrentUser; + } + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 2039383aa1..f30d8f3a30 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -145,11 +145,6 @@ - - - - - @@ -221,6 +216,7 @@ + @@ -747,7 +743,6 @@ - @@ -963,8 +958,6 @@ - -