From 30e2dea57a1034d79ee418377c2f5b5a86746d66 Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> Date: Tue, 19 Mar 2024 14:42:08 +0100 Subject: [PATCH] v14: Remove mentions of UmbracoApiController (#15863) * Remove mentions of UmbracoApiController * Remove last mentions of UmbracoApi controller --- .../Routing/BackOfficeAreaRoutes.cs | 35 +-- .../UmbracoApiControllerTypeCollection.cs | 11 - .../UmbracoApiControllerAttribute.cs | 11 - .../Controllers/UmbracoApiController.cs | 16 -- .../Controllers/UmbracoApiControllerBase.cs | 26 -- ...bracoApiControllerTypeCollectionBuilder.cs | 11 - .../UmbracoBuilderExtensions.cs | 4 - .../Extensions/LinkGeneratorExtensions.cs | 79 ------ .../Extensions/TypeLoaderExtensions.cs | 13 - .../Extensions/UrlHelperExtensions.cs | 230 ------------------ .../Security/ConfigureMemberCookieOptions.cs | 12 +- .../Extensions/TypeLoaderExtensions.cs | 6 - .../Routing/FrontEndRoutes.cs | 30 +-- .../UmbracoTestServerTestBase.cs | 12 - .../Security/MemberAuthorizeTests.cs | 36 --- .../Customizations/UmbracoCustomizations.cs | 8 - .../Routing/BackOfficeAreaRoutesTests.cs | 89 ------- 17 files changed, 3 insertions(+), 626 deletions(-) delete mode 100644 src/Umbraco.Core/UmbracoApiControllerTypeCollection.cs delete mode 100644 src/Umbraco.Web.Common/Attributes/UmbracoApiControllerAttribute.cs delete mode 100644 src/Umbraco.Web.Common/Controllers/UmbracoApiController.cs delete mode 100644 src/Umbraco.Web.Common/Controllers/UmbracoApiControllerBase.cs delete mode 100644 src/Umbraco.Web.Common/Controllers/UmbracoApiControllerTypeCollectionBuilder.cs delete mode 100644 src/Umbraco.Web.Common/Extensions/TypeLoaderExtensions.cs delete mode 100644 tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Routing/BackOfficeAreaRoutesTests.cs diff --git a/src/Umbraco.Cms.Api.Management/Routing/BackOfficeAreaRoutes.cs b/src/Umbraco.Cms.Api.Management/Routing/BackOfficeAreaRoutes.cs index 6e725d4191..baf8068b45 100644 --- a/src/Umbraco.Cms.Api.Management/Routing/BackOfficeAreaRoutes.cs +++ b/src/Umbraco.Cms.Api.Management/Routing/BackOfficeAreaRoutes.cs @@ -19,7 +19,6 @@ namespace Umbraco.Cms.Api.Management.Routing; /// public sealed class BackOfficeAreaRoutes : IAreaRoutes { - private readonly UmbracoApiControllerTypeCollection _apiControllers; private readonly GlobalSettings _globalSettings; private readonly IHostingEnvironment _hostingEnvironment; private readonly IRuntimeState _runtimeState; @@ -31,13 +30,11 @@ public sealed class BackOfficeAreaRoutes : IAreaRoutes public BackOfficeAreaRoutes( IOptions globalSettings, IHostingEnvironment hostingEnvironment, - IRuntimeState runtimeState, - UmbracoApiControllerTypeCollection apiControllers) + IRuntimeState runtimeState) { _globalSettings = globalSettings.Value; _hostingEnvironment = hostingEnvironment; _runtimeState = runtimeState; - _apiControllers = apiControllers; _umbracoPathSegment = _globalSettings.GetUmbracoMvcArea(_hostingEnvironment); } @@ -53,7 +50,6 @@ public sealed class BackOfficeAreaRoutes : IAreaRoutes case RuntimeLevel.Run: MapMinimalBackOffice(endpoints); - AutoRouteBackOfficeApiControllers(endpoints); break; case RuntimeLevel.BootFailed: case RuntimeLevel.Unknown: @@ -87,33 +83,4 @@ public sealed class BackOfficeAreaRoutes : IAreaRoutes Action = nameof(BackOfficeDefaultController.Index) }); } - - /// - /// Auto-routes all back office api controllers - /// - private void AutoRouteBackOfficeApiControllers(IEndpointRouteBuilder endpoints) - { - // TODO: We could investigate dynamically routing plugin controllers so we don't have to eagerly type scan for them, - // it would probably work well, see https://www.strathweb.com/2019/08/dynamic-controller-routing-in-asp-net-core-3-0/ - // will probably be what we use for front-end routing too. BTW the orig article about migrating from IRouter to endpoint - // routing for things like a CMS is here https://github.com/dotnet/aspnetcore/issues/4221 - - foreach (Type controller in _apiControllers) - { - PluginControllerMetadata meta = PluginController.GetMetadata(controller); - - // exclude front-end api controllers - if (!meta.IsBackOffice) - { - continue; - } - - endpoints.MapUmbracoApiRoute( - meta.ControllerType, - _umbracoPathSegment, - meta.AreaName, - meta.IsBackOffice, - string.Empty); // no default action (this is what we had before) - } - } } diff --git a/src/Umbraco.Core/UmbracoApiControllerTypeCollection.cs b/src/Umbraco.Core/UmbracoApiControllerTypeCollection.cs deleted file mode 100644 index afd6183b54..0000000000 --- a/src/Umbraco.Core/UmbracoApiControllerTypeCollection.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Umbraco.Cms.Core.Composing; - -namespace Umbraco.Cms.Core; - -public class UmbracoApiControllerTypeCollection : BuilderCollectionBase -{ - public UmbracoApiControllerTypeCollection(Func> items) - : base(items) - { - } -} diff --git a/src/Umbraco.Web.Common/Attributes/UmbracoApiControllerAttribute.cs b/src/Umbraco.Web.Common/Attributes/UmbracoApiControllerAttribute.cs deleted file mode 100644 index 48d3f3404e..0000000000 --- a/src/Umbraco.Web.Common/Attributes/UmbracoApiControllerAttribute.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Umbraco.Cms.Web.Common.ApplicationModels; - -namespace Umbraco.Cms.Web.Common.Attributes; - -/// -/// When present on a controller then conventions will apply -/// -[AttributeUsage(AttributeTargets.Class)] -public sealed class UmbracoApiControllerAttribute : Attribute -{ -} diff --git a/src/Umbraco.Web.Common/Controllers/UmbracoApiController.cs b/src/Umbraco.Web.Common/Controllers/UmbracoApiController.cs deleted file mode 100644 index 05d7004e1f..0000000000 --- a/src/Umbraco.Web.Common/Controllers/UmbracoApiController.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Umbraco.Cms.Core.Composing; - -namespace Umbraco.Cms.Web.Common.Controllers; - -/// -/// Provides a base class for auto-routed Umbraco API controllers. -/// -public abstract class UmbracoApiController : UmbracoApiControllerBase, IDiscoverable -{ - /// - /// Initializes a new instance of the class. - /// - protected UmbracoApiController() - { - } -} diff --git a/src/Umbraco.Web.Common/Controllers/UmbracoApiControllerBase.cs b/src/Umbraco.Web.Common/Controllers/UmbracoApiControllerBase.cs deleted file mode 100644 index e5cbe66cf6..0000000000 --- a/src/Umbraco.Web.Common/Controllers/UmbracoApiControllerBase.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Umbraco.Cms.Core.Features; -using Umbraco.Cms.Web.Common.Attributes; -using Umbraco.Cms.Web.Common.Authorization; - -namespace Umbraco.Cms.Web.Common.Controllers; - -/// -/// Provides a base class for Umbraco API controllers. -/// -/// -/// These controllers are NOT auto-routed. -/// The base class is which are netcore API controllers without any view support -/// -[Authorize(Policy = AuthorizationPolicies.UmbracoFeatureEnabled)] // TODO: This could be part of our conventions -[UmbracoApiController] -public abstract class UmbracoApiControllerBase : ControllerBase, IUmbracoFeature -{ - /// - /// Initializes a new instance of the class. - /// - protected UmbracoApiControllerBase() - { - } -} diff --git a/src/Umbraco.Web.Common/Controllers/UmbracoApiControllerTypeCollectionBuilder.cs b/src/Umbraco.Web.Common/Controllers/UmbracoApiControllerTypeCollectionBuilder.cs deleted file mode 100644 index 87a8c8e56d..0000000000 --- a/src/Umbraco.Web.Common/Controllers/UmbracoApiControllerTypeCollectionBuilder.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Umbraco.Cms.Core; -using Umbraco.Cms.Core.Composing; - -namespace Umbraco.Cms.Web.Common.Controllers; - -public class UmbracoApiControllerTypeCollectionBuilder : TypeCollectionBuilderBase< - UmbracoApiControllerTypeCollectionBuilder, UmbracoApiControllerTypeCollection, UmbracoApiController> -{ - // TODO: Should this only exist in the back office project? These really are only ever used for the back office AFAIK - protected override UmbracoApiControllerTypeCollectionBuilder This => this; -} diff --git a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs index 668e3c79ca..90c9c14f56 100644 --- a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs +++ b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs @@ -290,10 +290,6 @@ public static partial class UmbracoBuilderExtensions builder.Services.AddUnique(); builder.Services.AddUnique(); - var umbracoApiControllerTypes = builder.TypeLoader.GetUmbracoApiControllers().ToList(); - builder.WithCollectionBuilder() - .Add(umbracoApiControllerTypes); - builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/src/Umbraco.Web.Common/Extensions/LinkGeneratorExtensions.cs b/src/Umbraco.Web.Common/Extensions/LinkGeneratorExtensions.cs index 1c589ebe2a..8bf96bb69f 100644 --- a/src/Umbraco.Web.Common/Extensions/LinkGeneratorExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/LinkGeneratorExtensions.cs @@ -12,64 +12,6 @@ namespace Umbraco.Extensions; public static class LinkGeneratorExtensions { - /// - /// Gets the Umbraco backoffice URL (if Umbraco is installed). - /// - /// The link generator. - /// - /// The Umbraco backoffice URL. - /// - public static string? GetUmbracoBackOfficeUrl(this LinkGenerator linkGenerator) - => linkGenerator.GetPathByAction("Default", "BackOffice", new { area = Constants.Web.Mvc.BackOfficeArea }); - - /// - /// Gets the Umbraco backoffice URL (if Umbraco is installed) or application virtual path (in most cases just "/"). - /// - /// The link generator. - /// The hosting environment. - /// - /// The Umbraco backoffice URL. - /// - public static string GetUmbracoBackOfficeUrl(this LinkGenerator linkGenerator, IHostingEnvironment hostingEnvironment) - => GetUmbracoBackOfficeUrl(linkGenerator) ?? hostingEnvironment.ApplicationVirtualPath; - - /// - /// Return the back office url if the back office is installed - /// - /// - /// This method contained a bug that would result in always returning "/". - /// - [Obsolete("Use the GetUmbracoBackOfficeUrl extension method instead. This method will be removed in Umbraco 13.")] - public static string? GetBackOfficeUrl(this LinkGenerator linkGenerator, IHostingEnvironment hostingEnvironment) - => "/"; - - /// - /// Return the Url for a Web Api service - /// - /// The - public static string? GetUmbracoApiService(this LinkGenerator linkGenerator, string actionName, object? id = null) - where T : UmbracoApiControllerBase => linkGenerator.GetUmbracoControllerUrl( - actionName, - typeof(T), - new Dictionary { ["id"] = id }); - - public static string? GetUmbracoApiService(this LinkGenerator linkGenerator, string actionName, IDictionary? values) - where T : UmbracoApiControllerBase => linkGenerator.GetUmbracoControllerUrl(actionName, typeof(T), values); - - public static string? GetUmbracoApiServiceBaseUrl( - this LinkGenerator linkGenerator, - Expression> methodSelector) - where T : UmbracoApiControllerBase - { - MethodInfo? method = ExpressionHelper.GetMethodInfo(methodSelector); - if (method == null) - { - throw new MissingMethodException("Could not find the method " + methodSelector + " on type " + typeof(T) + - " or the result "); - } - - return linkGenerator.GetUmbracoApiService(method.Name)?.TrimEnd(method.Name); - } /// /// Return the Url for an Umbraco controller @@ -159,25 +101,4 @@ public static class LinkGeneratorExtensions return linkGenerator.GetUmbracoControllerUrl(actionName, ControllerExtensions.GetControllerName(controllerType), area, values); } - - public static string? GetUmbracoApiService( - this LinkGenerator linkGenerator, - Expression> methodSelector) - where T : UmbracoApiController - { - MethodInfo? method = ExpressionHelper.GetMethodInfo(methodSelector); - IDictionary? methodParams = ExpressionHelper.GetMethodParams(methodSelector); - if (method == null) - { - throw new MissingMethodException( - $"Could not find the method {methodSelector} on type {typeof(T)} or the result "); - } - - if (methodParams?.Any() == false) - { - return linkGenerator.GetUmbracoApiService(method.Name); - } - - return linkGenerator.GetUmbracoApiService(method.Name, methodParams); - } } diff --git a/src/Umbraco.Web.Common/Extensions/TypeLoaderExtensions.cs b/src/Umbraco.Web.Common/Extensions/TypeLoaderExtensions.cs deleted file mode 100644 index 423ea52536..0000000000 --- a/src/Umbraco.Web.Common/Extensions/TypeLoaderExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Umbraco.Cms.Core.Composing; -using Umbraco.Cms.Web.Common.Controllers; - -namespace Umbraco.Extensions; - -public static class TypeLoaderExtensions -{ - /// - /// Gets all types implementing . - /// - public static IEnumerable GetUmbracoApiControllers(this TypeLoader typeLoader) - => typeLoader.GetTypes(); -} diff --git a/src/Umbraco.Web.Common/Extensions/UrlHelperExtensions.cs b/src/Umbraco.Web.Common/Extensions/UrlHelperExtensions.cs index aacf8582b2..7edb173b21 100644 --- a/src/Umbraco.Web.Common/Extensions/UrlHelperExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/UrlHelperExtensions.cs @@ -21,236 +21,6 @@ namespace Umbraco.Extensions; public static class UrlHelperExtensions { - /// - /// Gets the Umbraco backoffice URL (if Umbraco is installed). - /// - /// The URL helper. - /// - /// The Umbraco backoffice URL. - /// - public static string? GetUmbracoBackOfficeUrl(this IUrlHelper urlHelper) - => urlHelper.Action("Default", "BackOffice", new { area = Constants.Web.Mvc.BackOfficeArea }); - - /// - /// Return the back office url if the back office is installed - /// - /// - /// - /// - /// This method contained a bug that would result in always returning "/". - /// - [Obsolete("Use the GetUmbracoBackOfficeUrl extension method instead. This method will be removed in Umbraco 13.")] - public static string? GetBackOfficeUrl(this IUrlHelper url) - => "/"; - - /// - /// Return the Url for a Web Api service - /// - /// - /// - /// - /// - /// - /// - public static string? GetUmbracoApiService( - this IUrlHelper url, - UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, - string actionName, - object? id = null) - where T : UmbracoApiController => - url.GetUmbracoApiService(umbracoApiControllerTypeCollection, actionName, typeof(T), id); - - public static string? GetUmbracoApiService( - this IUrlHelper url, - UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, - Expression> methodSelector) - where T : UmbracoApiController - { - MethodInfo? method = ExpressionHelper.GetMethodInfo(methodSelector); - IDictionary? methodParams = ExpressionHelper.GetMethodParams(methodSelector); - if (method == null) - { - throw new MissingMethodException("Could not find the method " + methodSelector + " on type " + typeof(T) + - " or the result "); - } - - if (methodParams?.Any() == false) - { - return url.GetUmbracoApiService(umbracoApiControllerTypeCollection, method.Name); - } - - return url.GetUmbracoApiService(umbracoApiControllerTypeCollection, method.Name, methodParams?.Values.First()); - } - - /// - /// Return the Url for a Web Api service - /// - /// - /// - /// - /// - /// - /// - public static string? GetUmbracoApiService( - this IUrlHelper url, - UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, - string actionName, - Type apiControllerType, - object? id = null) - { - if (actionName == null) - { - throw new ArgumentNullException(nameof(actionName)); - } - - if (string.IsNullOrWhiteSpace(actionName)) - { - throw new ArgumentException( - "Value can't be empty or consist only of white-space characters.", - nameof(actionName)); - } - - if (apiControllerType == null) - { - throw new ArgumentNullException(nameof(apiControllerType)); - } - - var area = string.Empty; - - Type? apiController = umbracoApiControllerTypeCollection.SingleOrDefault(x => x == apiControllerType); - if (apiController == null) - { - throw new InvalidOperationException("Could not find the umbraco api controller of type " + - apiControllerType.FullName); - } - - PluginControllerMetadata metaData = PluginController.GetMetadata(apiController); - if (metaData.AreaName.IsNullOrWhiteSpace() == false) - { - // set the area to the plugin area - area = metaData.AreaName; - } - - return url.GetUmbracoApiService(actionName, ControllerExtensions.GetControllerName(apiControllerType), area!, id); - } - - /// - /// Return the Url for a Web Api service - /// - /// - /// - /// - /// - /// - public static string? GetUmbracoApiService(this IUrlHelper url, string actionName, string controllerName, object? id = null) => url.GetUmbracoApiService(actionName, controllerName, string.Empty, id); - - /// - /// Return the Url for a Web Api service - /// - /// - /// - /// - /// - /// - /// - public static string? GetUmbracoApiService( - this IUrlHelper url, - string actionName, - string controllerName, - string area, - object? id = null) - { - if (actionName == null) - { - throw new ArgumentNullException(nameof(actionName)); - } - - if (string.IsNullOrWhiteSpace(actionName)) - { - throw new ArgumentException( - "Value can't be empty or consist only of white-space characters.", - nameof(actionName)); - } - - if (controllerName == null) - { - throw new ArgumentNullException(nameof(controllerName)); - } - - if (string.IsNullOrWhiteSpace(controllerName)) - { - throw new ArgumentException( - "Value can't be empty or consist only of white-space characters.", - nameof(controllerName)); - } - - if (area.IsNullOrWhiteSpace()) - { - if (id == null) - { - return url.Action(actionName, controllerName); - } - - return url.Action(actionName, controllerName, new { id }); - } - - if (id == null) - { - return url.Action(actionName, controllerName, new { area }); - } - - return url.Action(actionName, controllerName, new { area, id }); - } - - /// - /// Return the Base Url (not including the action) for a Web Api service - /// - /// - /// - /// - /// - /// - public static string? GetUmbracoApiServiceBaseUrl( - this IUrlHelper url, - UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, - string actionName) - where T : UmbracoApiController => - url.GetUmbracoApiService(umbracoApiControllerTypeCollection, actionName)?.TrimEnd(actionName); - - public static string? GetUmbracoApiServiceBaseUrl( - this IUrlHelper url, - UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, - Expression> methodSelector) - where T : UmbracoApiController - { - MethodInfo? method = ExpressionHelper.GetMethodInfo(methodSelector); - if (method == null) - { - throw new MissingMethodException("Could not find the method " + methodSelector + " on type " + typeof(T) + - " or the result "); - } - - return url.GetUmbracoApiService(umbracoApiControllerTypeCollection, method.Name)?.TrimEnd(method.Name); - } - - /// - /// Return the Url for an action with a cache-busting hash appended - /// - /// - public static string GetUrlWithCacheBust( - this IUrlHelper url, - string actionName, - string controllerName, - RouteValueDictionary routeVals, - IHostingEnvironment hostingEnvironment, - IUmbracoVersion umbracoVersion) - { - var applicationJs = url.Action(actionName, controllerName, routeVals); - applicationJs = applicationJs + "?umb__rnd=" + - GetCacheBustHash(hostingEnvironment, umbracoVersion); - return applicationJs; - } - /// /// /// diff --git a/src/Umbraco.Web.Common/Security/ConfigureMemberCookieOptions.cs b/src/Umbraco.Web.Common/Security/ConfigureMemberCookieOptions.cs index 1ba9a52526..a8e2fc63dc 100644 --- a/src/Umbraco.Web.Common/Security/ConfigureMemberCookieOptions.cs +++ b/src/Umbraco.Web.Common/Security/ConfigureMemberCookieOptions.cs @@ -60,17 +60,7 @@ public sealed class ConfigureMemberCookieOptions : IConfigureNamedOptions { - // When the controller is an UmbracoAPIController, we want to return a StatusCode instead of a redirect. - // All other cases should use the default Redirect of the CookieAuthenticationEvent. - var controllerDescriptor = ctx.HttpContext.GetEndpoint()?.Metadata - .OfType() - .FirstOrDefault(); - - if (!controllerDescriptor?.ControllerTypeInfo.IsSubclassOf(typeof(UmbracoApiController)) ?? false) - { - new CookieAuthenticationEvents().OnRedirectToAccessDenied(ctx); - } - + new CookieAuthenticationEvents().OnRedirectToAccessDenied(ctx); return Task.CompletedTask; }, }; diff --git a/src/Umbraco.Web.Website/Extensions/TypeLoaderExtensions.cs b/src/Umbraco.Web.Website/Extensions/TypeLoaderExtensions.cs index f088e53a9a..f0709d99c2 100644 --- a/src/Umbraco.Web.Website/Extensions/TypeLoaderExtensions.cs +++ b/src/Umbraco.Web.Website/Extensions/TypeLoaderExtensions.cs @@ -15,10 +15,4 @@ public static class TypeLoaderExtensions /// internal static IEnumerable GetSurfaceControllers(this TypeLoader typeLoader) => typeLoader.GetTypes(); - - /// - /// Gets all types implementing . - /// - internal static IEnumerable GetUmbracoApiControllers(this TypeLoader typeLoader) - => typeLoader.GetTypes(); } diff --git a/src/Umbraco.Web.Website/Routing/FrontEndRoutes.cs b/src/Umbraco.Web.Website/Routing/FrontEndRoutes.cs index 8c0e36a40f..9b992b8276 100644 --- a/src/Umbraco.Web.Website/Routing/FrontEndRoutes.cs +++ b/src/Umbraco.Web.Website/Routing/FrontEndRoutes.cs @@ -17,7 +17,6 @@ namespace Umbraco.Cms.Web.Website.Routing; /// public sealed class FrontEndRoutes : IAreaRoutes { - private readonly UmbracoApiControllerTypeCollection _apiControllers; private readonly IRuntimeState _runtimeState; private readonly SurfaceControllerTypeCollection _surfaceControllerTypeCollection; private readonly string _umbracoPathSegment; @@ -29,12 +28,10 @@ public sealed class FrontEndRoutes : IAreaRoutes IOptions globalSettings, IHostingEnvironment hostingEnvironment, IRuntimeState runtimeState, - SurfaceControllerTypeCollection surfaceControllerTypeCollection, - UmbracoApiControllerTypeCollection apiControllers) + SurfaceControllerTypeCollection surfaceControllerTypeCollection) { _runtimeState = runtimeState; _surfaceControllerTypeCollection = surfaceControllerTypeCollection; - _apiControllers = apiControllers; _umbracoPathSegment = globalSettings.Value.GetUmbracoMvcArea(hostingEnvironment); } @@ -48,7 +45,6 @@ public sealed class FrontEndRoutes : IAreaRoutes case RuntimeLevel.Run: AutoRouteSurfaceControllers(endpoints); - AutoRouteFrontEndApiControllers(endpoints); break; case RuntimeLevel.BootFailed: case RuntimeLevel.Unknown: @@ -75,28 +71,4 @@ public sealed class FrontEndRoutes : IAreaRoutes meta.AreaName); } } - - /// - /// Auto-routes all front-end api controllers - /// - private void AutoRouteFrontEndApiControllers(IEndpointRouteBuilder endpoints) - { - foreach (Type controller in _apiControllers) - { - PluginControllerMetadata meta = PluginController.GetMetadata(controller); - - // exclude back-end api controllers - if (meta.IsBackOffice) - { - continue; - } - - endpoints.MapUmbracoApiRoute( - meta.ControllerType, - _umbracoPathSegment, - meta.AreaName, - meta.IsBackOffice, - string.Empty); // no default action (this is what we had before) - } - } } diff --git a/tests/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs b/tests/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs index eb91207293..3efbcc1be6 100644 --- a/tests/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs +++ b/tests/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs @@ -105,18 +105,6 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest }); } - /// - /// Prepare a url before using . - /// This returns the url but also sets the HttpContext.request into to use this url. - /// - /// The string URL of the controller action. - protected string PrepareApiControllerUrl(Expression> methodSelector) - where T : UmbracoApiController - { - var url = LinkGenerator.GetUmbracoApiService(methodSelector); - return PrepareUrl(url); - } - protected string GetManagementApiUrl(Expression> methodSelector) where T : ManagementApiControllerBase { diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Web.Website/Security/MemberAuthorizeTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Web.Website/Security/MemberAuthorizeTests.cs index 35bea8bc0e..d28cbe03f4 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Web.Website/Security/MemberAuthorizeTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Web.Website/Security/MemberAuthorizeTests.cs @@ -67,36 +67,6 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Web.Website.Security Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode); Assert.AreEqual(cookieAuthenticationOptions.Value.AccessDeniedPath.ToString(), response.Headers.Location?.AbsolutePath); } - - [Test] - [LongRunning] - public async Task Secure_ApiController_Should_Return_Unauthorized_WhenNotLoggedIn() - { - _memberManagerMock.Setup(x => x.IsLoggedIn()).Returns(false); - var url = PrepareApiControllerUrl(x => x.Secure()); - - var response = await Client.GetAsync(url); - - Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); - } - - [Test] - [LongRunning] - public async Task Secure_ApiController_Should_Return_Forbidden_WhenNotAuthorized() - { - _memberManagerMock.Setup(x => x.IsLoggedIn()).Returns(true); - _memberManagerMock.Setup(x => x.IsMemberAuthorizedAsync( - It.IsAny>(), - It.IsAny>(), - It.IsAny>())) - .ReturnsAsync(false); - - var url = PrepareApiControllerUrl(x => x.Secure()); - - var response = await Client.GetAsync(url); - - Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode); - } } public class TestSurfaceController : SurfaceController @@ -121,10 +91,4 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Web.Website.Security [UmbracoMemberAuthorize] public IActionResult Secure() => NoContent(); } - - public class TestApiController : UmbracoApiController - { - [UmbracoMemberAuthorize] - public IActionResult Secure() => NoContent(); - } } diff --git a/tests/Umbraco.Tests.UnitTests/AutoFixture/Customizations/UmbracoCustomizations.cs b/tests/Umbraco.Tests.UnitTests/AutoFixture/Customizations/UmbracoCustomizations.cs index 099bbfa0eb..9aeb17716b 100644 --- a/tests/Umbraco.Tests.UnitTests/AutoFixture/Customizations/UmbracoCustomizations.cs +++ b/tests/Umbraco.Tests.UnitTests/AutoFixture/Customizations/UmbracoCustomizations.cs @@ -46,14 +46,6 @@ internal class UmbracoCustomizations : ICustomization fixture.Customize(x => x.With(settings => settings.ApplicationVirtualPath, string.Empty)); - fixture.Customize(u => u.FromFactory( - () => new BackOfficeAreaRoutes( - Options.Create(new GlobalSettings()), - Mock.Of(x => - x.ToAbsolute(It.IsAny()) == "/umbraco" && x.ApplicationVirtualPath == string.Empty), - Mock.Of(x => x.Level == RuntimeLevel.Run), - new UmbracoApiControllerTypeCollection(Enumerable.Empty)))); - fixture.Customize(u => u.FromFactory( () => new PreviewRoutes( Options.Create(new GlobalSettings()), diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Routing/BackOfficeAreaRoutesTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Routing/BackOfficeAreaRoutesTests.cs deleted file mode 100644 index 71735ba500..0000000000 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Routing/BackOfficeAreaRoutesTests.cs +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) Umbraco. -// See LICENSE for more details. - -using System.Linq; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; -using Microsoft.Extensions.Options; -using Moq; -using NUnit.Framework; -using Umbraco.Cms.Api.Management.Controllers.Security; -using Umbraco.Cms.Api.Management.Routing; -using Umbraco.Cms.Core; -using Umbraco.Cms.Core.Configuration.Models; -using Umbraco.Cms.Core.Hosting; -using Umbraco.Cms.Core.Services; -using Umbraco.Cms.Web.Common.Attributes; -using Umbraco.Cms.Web.Common.Controllers; -using Umbraco.Extensions; -using static Umbraco.Cms.Core.Constants.Web.Routing; - -namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Routing; - -[TestFixture] -public class BackOfficeAreaRoutesTests -{ - [TestCase(RuntimeLevel.BootFailed)] - [TestCase(RuntimeLevel.Unknown)] - [TestCase(RuntimeLevel.Boot)] - public void RuntimeState_No_Routes(RuntimeLevel level) - { - var routes = GetBackOfficeAreaRoutes(level); - var endpoints = new TestRouteBuilder(); - routes.CreateRoutes(endpoints); - - Assert.AreEqual(0, endpoints.DataSources.Count); - } - - [Test] - [TestCase(RuntimeLevel.Run)] - [TestCase(RuntimeLevel.Upgrade)] - [TestCase(RuntimeLevel.Install)] - public void RuntimeState_All_Routes(RuntimeLevel level) - { - var routes = GetBackOfficeAreaRoutes(level); - var endpoints = new TestRouteBuilder(); - routes.CreateRoutes(endpoints); - - Assert.AreEqual(1, endpoints.DataSources.Count); - var route = endpoints.DataSources.First(); - Assert.AreEqual(3, route.Endpoints.Count); - - AssertMinimalBackOfficeRoutes(route); - - var endpoint4 = (RouteEndpoint)route.Endpoints[2]; - var apiControllerName = ControllerExtensions.GetControllerName(); - Assert.AreEqual( - $"umbraco/backoffice/api/{apiControllerName.ToLowerInvariant()}/{{action}}/{{id?}}", - endpoint4.RoutePattern.RawText); - Assert.IsFalse(endpoint4.RoutePattern.Defaults.ContainsKey(AreaToken)); - Assert.IsFalse(endpoint4.RoutePattern.Defaults.ContainsKey(ActionToken)); - Assert.AreEqual(apiControllerName, endpoint4.RoutePattern.Defaults[ControllerToken]); - } - - private void AssertMinimalBackOfficeRoutes(EndpointDataSource route) - { - var endpoint1 = (RouteEndpoint)route.Endpoints[0]; - Assert.AreEqual("umbraco/{action}/{id?}", endpoint1.RoutePattern.RawText); - Assert.AreEqual("Index", endpoint1.RoutePattern.Defaults[ActionToken]); - Assert.AreEqual(ControllerExtensions.GetControllerName(), endpoint1.RoutePattern.Defaults[ControllerToken]); - } - - private BackOfficeAreaRoutes GetBackOfficeAreaRoutes(RuntimeLevel level) - { - var globalSettings = new GlobalSettings(); - var routes = new BackOfficeAreaRoutes( - Options.Create(globalSettings), - Mock.Of(x => - x.ToAbsolute(It.IsAny()) == "/umbraco" && x.ApplicationVirtualPath == string.Empty), - Mock.Of(x => x.Level == level), - new UmbracoApiControllerTypeCollection(() => new[] { typeof(Testing1Controller) })); - - return routes; - } - - [IsBackOffice] - private class Testing1Controller : UmbracoApiController - { - } -}