- Removed BackOfficeArea
- Injected IEmailSender
- Uses nameof instead of magic strings
- Uses GetControllerName instead of magic strings

Signed-off-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
Bjarke Berg
2020-10-22 13:37:47 +02:00
parent 785c3a34e9
commit ca8e54ffc6
7 changed files with 26 additions and 70 deletions

View File

@@ -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<GlobalSettings> _options;
private readonly RuntimeLevel _runtimeLevel;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultCultureAccessor"/> class.
/// </summary>
public DefaultCultureAccessor(ILocalizationService localizationService, IRuntimeState runtimeState)
public DefaultCultureAccessor(ILocalizationService localizationService, IRuntimeState runtimeState, IOptions<GlobalSettings> options)
{
_localizationService = localizationService;
_options = options;
_runtimeLevel = runtimeState.Level;
}
/// <inheritdoc />
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
}
}

View File

@@ -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> globalSettings,
IEmailSender emailSender,
IOptions<HealthChecksSettings> healthChecksSettings,
IOptions<ContentSettings> 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);
}
}

View File

@@ -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
/// </summary>
/// <returns></returns>
[SetAngularAntiForgeryTokens]
public async Task<IEnumerable<string>> Get2FAProviders()
[SetAngularAntiForgeryTokens]
public async Task<ActionResult<IEnumerable<string>>> 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");
}

View File

@@ -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()));
}
/// <summary>
@@ -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()));
}
/// <summary>

View File

@@ -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;
}
/// <summary>
/// Create the routes for the area
/// </summary>
/// <param name="context"></param>
/// <remarks>
/// 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.
/// </remarks>
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);
}
}

View File

@@ -132,7 +132,7 @@ namespace Umbraco.Web.Runtime
// RouteTable.Routes.RegisterArea<UmbracoInstallArea>();
// 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);

View File

@@ -291,7 +291,6 @@
<Compile Include="Mvc\AreaRegistrationExtensions.cs" />
<Compile Include="Mvc\QueryStringFilterAttribute.cs" />
<Compile Include="Mvc\MemberAuthorizeAttribute.cs" />
<Compile Include="Mvc\BackOfficeArea.cs" />
<Compile Include="Mvc\ControllerFactoryExtensions.cs" />
<Compile Include="Mvc\SurfaceRouteHandler.cs" />
<Compile Include="Controllers\UmbLoginController.cs" />