From ca8e54ffc6471d86d79f968f483e832a7210cbdf Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 22 Oct 2020 13:37:47 +0200 Subject: [PATCH] https://github.com/umbraco/Umbraco-CMS/pull/9248> - Removed BackOfficeArea - Injected IEmailSender - Uses nameof instead of magic strings - Uses GetControllerName instead of magic strings Signed-off-by: Bjarke Berg --- .../PublishedCache/DefaultCultureAccessor.cs | 11 +++-- .../EmailNotificationMethod.cs | 11 ++--- .../Controllers/AuthenticationController.cs | 12 +++--- .../Controllers/BackOfficeController.cs | 16 +++---- src/Umbraco.Web/Mvc/BackOfficeArea.cs | 43 ------------------- .../Runtime/WebInitialComponent.cs | 2 +- src/Umbraco.Web/Umbraco.Web.csproj | 1 - 7 files changed, 26 insertions(+), 70 deletions(-) delete mode 100644 src/Umbraco.Web/Mvc/BackOfficeArea.cs diff --git a/src/Umbraco.Core/PublishedCache/DefaultCultureAccessor.cs b/src/Umbraco.Core/PublishedCache/DefaultCultureAccessor.cs index dffe2274aa..bd78358519 100644 --- a/src/Umbraco.Core/PublishedCache/DefaultCultureAccessor.cs +++ b/src/Umbraco.Core/PublishedCache/DefaultCultureAccessor.cs @@ -1,4 +1,6 @@ -using Umbraco.Core; +using Microsoft.Extensions.Options; +using Umbraco.Core; +using Umbraco.Core.Configuration.Models; using Umbraco.Core.Services; namespace Umbraco.Web.PublishedCache @@ -9,21 +11,22 @@ namespace Umbraco.Web.PublishedCache public class DefaultCultureAccessor : IDefaultCultureAccessor { private readonly ILocalizationService _localizationService; + private readonly IOptions _options; private readonly RuntimeLevel _runtimeLevel; /// /// Initializes a new instance of the class. /// - public DefaultCultureAccessor(ILocalizationService localizationService, IRuntimeState runtimeState) + public DefaultCultureAccessor(ILocalizationService localizationService, IRuntimeState runtimeState, IOptions options) { _localizationService = localizationService; + _options = options; _runtimeLevel = runtimeState.Level; } /// public string DefaultCulture => _runtimeLevel == RuntimeLevel.Run ? _localizationService.GetDefaultLanguageIsoCode() ?? "" // fast - // TODO: Shouldn't this come from GlobalSettings.DefaultUILanguage? - : "en-US"; // default for install and upgrade, when the service is n/a + : _options.Value.DefaultUILanguage; // default for install and upgrade, when the service is n/a } } diff --git a/src/Umbraco.Infrastructure/HealthCheck/NotificationMethods/EmailNotificationMethod.cs b/src/Umbraco.Infrastructure/HealthCheck/NotificationMethods/EmailNotificationMethod.cs index 8060e7c257..ba8dfd300f 100644 --- a/src/Umbraco.Infrastructure/HealthCheck/NotificationMethods/EmailNotificationMethod.cs +++ b/src/Umbraco.Infrastructure/HealthCheck/NotificationMethods/EmailNotificationMethod.cs @@ -4,7 +4,6 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Umbraco.Core; -using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.Models; using Umbraco.Core.HealthCheck; using Umbraco.Core.Services; @@ -17,14 +16,14 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods { private readonly ILocalizedTextService _textService; private readonly IRequestAccessor _requestAccessor; + private readonly IEmailSender _emailSender; - private readonly GlobalSettings _globalSettings; private readonly ContentSettings _contentSettings; public EmailNotificationMethod( ILocalizedTextService textService, IRequestAccessor requestAccessor, - IOptions globalSettings, + IEmailSender emailSender, IOptions healthChecksSettings, IOptions contentSettings) : base(healthChecksSettings) @@ -40,7 +39,7 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods _textService = textService ?? throw new ArgumentNullException(nameof(textService)); _requestAccessor = requestAccessor; - _globalSettings = globalSettings.Value; + _emailSender = emailSender; _contentSettings = contentSettings.Value ?? throw new ArgumentNullException(nameof(contentSettings)); } @@ -71,11 +70,9 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods var subject = _textService.Localize("healthcheck/scheduledHealthCheckEmailSubject", new[] { host.ToString() }); - // TODO: Why isn't this injected? - var mailSender = new EmailSender(Options.Create(_globalSettings)); using (var mailMessage = CreateMailMessage(subject, message)) { - await mailSender.SendAsync(mailMessage); + await _emailSender.SendAsync(mailMessage); } } diff --git a/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs b/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs index d96efb6005..43dfad102d 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/AuthenticationController.cs @@ -34,7 +34,7 @@ using Microsoft.AspNetCore.Identity; namespace Umbraco.Web.BackOffice.Controllers { - // See + // See // for a bigger example of this type of controller implementation in netcore: // https://github.com/dotnet/AspNetCore.Docs/blob/2efb4554f8f659be97ee7cd5dd6143b871b330a5/aspnetcore/migration/1x-to-2x/samples/AspNetCoreDotNetCore2App/AspNetCoreDotNetCore2App/Controllers/AccountController.cs // https://github.com/dotnet/AspNetCore.Docs/blob/ad16f5e1da6c04fa4996ee67b513f2a90fa0d712/aspnetcore/common/samples/WebApplication1/Controllers/AccountController.cs @@ -378,18 +378,18 @@ namespace Umbraco.Web.BackOffice.Controllers /// Used to retrieve the 2FA providers for code submission /// /// - [SetAngularAntiForgeryTokens] - public async Task> Get2FAProviders() + [SetAngularAntiForgeryTokens] + public async Task>> Get2FAProviders() { var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { _logger.LogWarning("Get2FAProviders :: No verified user found, returning 404"); - throw new HttpResponseException(HttpStatusCode.NotFound); + return NotFound(); } var userFactors = await _userManager.GetValidTwoFactorProvidersAsync(user); - return userFactors; + return new ObjectResult(userFactors); } [SetAngularAntiForgeryTokens] @@ -470,7 +470,7 @@ namespace Umbraco.Web.BackOffice.Controllers if (result.IsNotAllowed) { throw HttpResponseException.CreateValidationErrorResponse("User is not allowed"); - } + } throw HttpResponseException.CreateValidationErrorResponse("Invalid code"); } diff --git a/src/Umbraco.Web.BackOffice/Controllers/BackOfficeController.cs b/src/Umbraco.Web.BackOffice/Controllers/BackOfficeController.cs index d1e67375e5..46c5625ee1 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/BackOfficeController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/BackOfficeController.cs @@ -256,7 +256,7 @@ namespace Umbraco.Web.BackOffice.Controllers { if (redirectUrl == null) { - redirectUrl = Url.Action("Default", "BackOffice"); + redirectUrl = Url.Action(nameof(Default), this.GetControllerName()); } var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); @@ -273,7 +273,7 @@ namespace Umbraco.Web.BackOffice.Controllers public ActionResult LinkLogin(string provider) { // Request a redirect to the external login provider to link a login for the current user - var redirectUrl = Url.Action("ExternalLinkLoginCallback", "BackOffice"); + var redirectUrl = Url.Action(nameof(ExternalLinkLoginCallback), this.GetControllerName()); var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, User.Identity.GetUserId()); return Challenge(properties, provider); } @@ -289,13 +289,13 @@ namespace Umbraco.Web.BackOffice.Controllers { //Add a flag and redirect for it to be displayed TempData[ViewDataExtensions.TokenPasswordResetCode] = _jsonSerializer.Serialize(new ValidatePasswordResetCodeModel { UserId = userId, ResetCode = resetCode }); - return RedirectToLocal(Url.Action("Default", "BackOffice")); + return RedirectToLocal(Url.Action(nameof(Default), this.GetControllerName())); } } //Add error and redirect for it to be displayed TempData[ViewDataExtensions.TokenPasswordResetCode] = new[] { _textService.Localize("login/resetCodeExpired") }; - return RedirectToLocal(Url.Action("Default", "BackOffice")); + return RedirectToLocal(Url.Action(nameof(Default), this.GetControllerName())); } /// @@ -311,7 +311,7 @@ namespace Umbraco.Web.BackOffice.Controllers { //Add error and redirect for it to be displayed TempData[ViewDataExtensions.TokenExternalSignInError] = new[] { "An error occurred, could not get external login info" }; - return RedirectToLocal(Url.Action("Default", "BackOffice")); + return RedirectToLocal(Url.Action(nameof(Default), this.GetControllerName())); } var user = await _userManager.FindByIdAsync(User.Identity.GetUserId()); @@ -319,7 +319,7 @@ namespace Umbraco.Web.BackOffice.Controllers { // ... this should really not happen TempData[ViewDataExtensions.TokenExternalSignInError] = new[] { "Local user does not exist" }; - return RedirectToLocal(Url.Action("Default", "BackOffice")); + return RedirectToLocal(Url.Action(nameof(Default), this.GetControllerName())); } var result2 = await _userManager.AddLoginAsync(user, loginInfo); @@ -330,12 +330,12 @@ namespace Umbraco.Web.BackOffice.Controllers // what this is for but we'll need to peek under the code here to figure out exactly what goes on. //await _signInManager.UpdateExternalAuthenticationTokensAsync(loginInfo); - return RedirectToLocal(Url.Action("Default", "BackOffice")); + return RedirectToLocal(Url.Action(nameof(Default), this.GetControllerName())); } //Add errors and redirect for it to be displayed TempData[ViewDataExtensions.TokenExternalSignInError] = result2.Errors; - return RedirectToLocal(Url.Action("Default", "BackOffice")); + return RedirectToLocal(Url.Action(nameof(Default), this.GetControllerName())); } /// diff --git a/src/Umbraco.Web/Mvc/BackOfficeArea.cs b/src/Umbraco.Web/Mvc/BackOfficeArea.cs deleted file mode 100644 index 2c15f83f14..0000000000 --- a/src/Umbraco.Web/Mvc/BackOfficeArea.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Web.Mvc; -using Umbraco.Core.Configuration; -using Umbraco.Core.Configuration.Models; -using Umbraco.Core.Hosting; -using Umbraco.Web.Editors; - -namespace Umbraco.Web.Mvc -{ - // TODO: This has been ported to netcore, can be removed - // Has preview been migrated? - internal class BackOfficeArea : AreaRegistration - { - private readonly GlobalSettings _globalSettings; - private readonly IHostingEnvironment _hostingEnvironment; - - public BackOfficeArea(GlobalSettings globalSettings, IHostingEnvironment hostingEnvironment) - { - _globalSettings = globalSettings; - _hostingEnvironment = hostingEnvironment; - } - - /// - /// Create the routes for the area - /// - /// - /// - /// By using the context to register the routes it means that the area is already applied to them all - /// and that the namespaces searched for the controllers are ONLY the ones specified. - /// - public override void RegisterArea(AreaRegistrationContext context) - { - - context.MapRoute( - "Umbraco_preview", - AreaName + "/preview/{action}/{editor}", - new {controller = "Preview", action = "Index", editor = UrlParameter.Optional}, - new[] { "Umbraco.Web.Editors" }); - - } - - public override string AreaName => _globalSettings.GetUmbracoMvcArea(_hostingEnvironment); - } -} diff --git a/src/Umbraco.Web/Runtime/WebInitialComponent.cs b/src/Umbraco.Web/Runtime/WebInitialComponent.cs index a5c7db12c9..e64f03f358 100644 --- a/src/Umbraco.Web/Runtime/WebInitialComponent.cs +++ b/src/Umbraco.Web/Runtime/WebInitialComponent.cs @@ -132,7 +132,7 @@ namespace Umbraco.Web.Runtime // RouteTable.Routes.RegisterArea(); // register all back office routes - RouteTable.Routes.RegisterArea(new BackOfficeArea(globalSettings, hostingEnvironment)); + // RouteTable.Routes.RegisterArea(new BackOfficeArea(globalSettings, hostingEnvironment)); // plugin controllers must come first because the next route will catch many things RoutePluginControllers(globalSettings, surfaceControllerTypes, apiControllerTypes, hostingEnvironment); diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index ce0437b074..ea9a05b6d4 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -291,7 +291,6 @@ -