* Run code cleanup * Run dotnet format * Start manual cleanup in Web.Common * Finish up manual cleanup * Fix tests * Fix up InMemoryModelFactory.cs * Inject proper macroRenderer * Update src/Umbraco.Web.Common/Filters/JsonDateTimeFormatAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Web.Common/Filters/ValidateUmbracoFormRouteStringAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix based on review Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Umbraco.Extensions;
|
|
|
|
public static class ControllerExtensions
|
|
{
|
|
/// <summary>
|
|
/// Runs the authentication process
|
|
/// </summary>
|
|
/// <param name="controller"></param>
|
|
/// <returns></returns>
|
|
public static async Task<AuthenticateResult> AuthenticateBackOfficeAsync(this ControllerBase controller) =>
|
|
await controller.HttpContext.AuthenticateBackOfficeAsync();
|
|
|
|
/// <summary>
|
|
/// Return the controller name from the controller type
|
|
/// </summary>
|
|
/// <param name="controllerType"></param>
|
|
/// <returns></returns>
|
|
public static string GetControllerName(Type controllerType)
|
|
{
|
|
if (!controllerType.Name.EndsWith("Controller") && !controllerType.Name.EndsWith("Controller`1"))
|
|
{
|
|
throw new InvalidOperationException("The controller type " + controllerType +
|
|
" does not follow conventions, MVC Controller class names must be suffixed with the term 'Controller'");
|
|
}
|
|
|
|
return controllerType.Name[..controllerType.Name.LastIndexOf("Controller", StringComparison.Ordinal)];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return the controller name from the controller instance
|
|
/// </summary>
|
|
/// <param name="controllerInstance"></param>
|
|
/// <returns></returns>
|
|
public static string GetControllerName(this Controller controllerInstance) =>
|
|
GetControllerName(controllerInstance.GetType());
|
|
|
|
/// <summary>
|
|
/// Return the controller name from the controller type
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
/// <remarks></remarks>
|
|
public static string GetControllerName<T>() => GetControllerName(typeof(T));
|
|
}
|