Ensures all back office controllers have the thread culture set, we do this with a new application model targeting all back office controllers.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Web.Common.Attributes;
|
||||
|
||||
namespace Umbraco.Web.Common.ApplicationModels
|
||||
{
|
||||
/// <summary>
|
||||
/// An application model provider for all Umbraco Back Office controllers
|
||||
/// </summary>
|
||||
public class BackOfficeApplicationModelProvider : IApplicationModelProvider
|
||||
{
|
||||
public BackOfficeApplicationModelProvider(IModelMetadataProvider modelMetadataProvider)
|
||||
{
|
||||
ActionModelConventions = new List<IActionModelConvention>()
|
||||
{
|
||||
new BackOfficeIdentityCultureConvention()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will execute after <see cref="DefaultApplicationModelProvider"/>
|
||||
/// </summary>
|
||||
public int Order => 0;
|
||||
|
||||
public List<IActionModelConvention> ActionModelConventions { get; }
|
||||
|
||||
public void OnProvidersExecuted(ApplicationModelProviderContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnProvidersExecuting(ApplicationModelProviderContext context)
|
||||
{
|
||||
foreach (var controller in context.Result.Controllers)
|
||||
{
|
||||
if (!IsBackOfficeController(controller))
|
||||
continue;
|
||||
|
||||
foreach (var action in controller.Actions)
|
||||
{
|
||||
foreach (var convention in ActionModelConventions)
|
||||
{
|
||||
convention.Apply(action);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsBackOfficeController(ControllerModel controller)
|
||||
{
|
||||
var pluginControllerAttribute = controller.Attributes.OfType<PluginControllerAttribute>().FirstOrDefault();
|
||||
return pluginControllerAttribute != null
|
||||
&& pluginControllerAttribute.AreaName == Core.Constants.Web.Mvc.BackOfficeArea;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Umbraco.Web.Common.Filters;
|
||||
|
||||
namespace Umbraco.Web.Common.ApplicationModels
|
||||
{
|
||||
public class BackOfficeIdentityCultureConvention : IActionModelConvention
|
||||
{
|
||||
public void Apply(ActionModel action)
|
||||
{
|
||||
action.Filters.Add(new BackOfficeCultureFilter());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ namespace Umbraco.Web.Common.ApplicationModels
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// A custom application model provider for Umbraco controllers
|
||||
/// An application model provider for Umbraco API controllers to behave like WebApi controllers
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
|
||||
@@ -156,24 +156,7 @@ namespace Umbraco.Extensions
|
||||
out factory);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddUmbracoRequestLocalization(this IServiceCollection services)
|
||||
{
|
||||
services.Configure<RequestLocalizationOptions>(options =>
|
||||
{
|
||||
var supportedCultures = CultureInfo
|
||||
.GetCultures(CultureTypes.AllCultures & ~ CultureTypes.NeutralCultures)
|
||||
.Where(cul => !String.IsNullOrEmpty(cul.Name))
|
||||
.ToArray();
|
||||
|
||||
options.SupportedCultures = supportedCultures;
|
||||
options.SupportedUICultures = supportedCultures;
|
||||
options.AddInitialRequestCultureProvider(new UmbracoBackOfficeIdentityCultureProvider());
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the Umbraco Back Core requirements
|
||||
@@ -201,9 +184,6 @@ namespace Umbraco.Extensions
|
||||
if (container is null) throw new ArgumentNullException(nameof(container));
|
||||
if (entryAssembly is null) throw new ArgumentNullException(nameof(entryAssembly));
|
||||
|
||||
// Set culture options
|
||||
services.AddUmbracoRequestLocalization();
|
||||
|
||||
// Add supported databases
|
||||
services.AddUmbracoSqlCeSupport();
|
||||
services.AddUmbracoSqlServerSupport();
|
||||
|
||||
@@ -36,6 +36,7 @@ namespace Umbraco.Extensions
|
||||
{
|
||||
services.ConfigureOptions<UmbracoMvcConfigureOptions>();
|
||||
services.TryAddEnumerable(ServiceDescriptor.Transient<IApplicationModelProvider, UmbracoApiBehaviorApplicationModelProvider>());
|
||||
services.TryAddEnumerable(ServiceDescriptor.Transient<IApplicationModelProvider, BackOfficeApplicationModelProvider>());
|
||||
|
||||
// TODO: We need to avoid this, surely there's a way? See ContainerTests.BuildServiceProvider_Before_Host_Is_Configured
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
34
src/Umbraco.Web.Common/Filters/BackOfficeCultureFilter.cs
Normal file
34
src/Umbraco.Web.Common/Filters/BackOfficeCultureFilter.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Umbraco.Core.Security;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Umbraco.Web.Common.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// Applied to all Umbraco controllers to ensure the thread culture is set to the culture assigned to the back office identity
|
||||
/// </summary>
|
||||
public class BackOfficeCultureFilter : IActionFilter
|
||||
{
|
||||
public void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
var culture = context.HttpContext.User.Identity.GetCulture();
|
||||
if (culture != null)
|
||||
{
|
||||
SetCurrentThreadCulture(culture);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetCurrentThreadCulture(CultureInfo culture)
|
||||
{
|
||||
CultureInfo.CurrentCulture = culture;
|
||||
CultureInfo.CurrentUICulture = culture;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user