Created a common library for the APIs (#13530)

* Created a common library for the APIs and moved stuff that is required for the new content API. Also moved the versioned backoffice API route handling to the management API where it belongs.

* Remove test auth attribute from Media
This commit is contained in:
Kenn Jacobsen
2022-12-09 08:52:17 +01:00
committed by GitHub
parent f3396a04fc
commit 801966f1ae
92 changed files with 119 additions and 96 deletions

View File

@@ -1,17 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core;
namespace Umbraco.New.Cms.Web.Common.Routing;
/// <summary>
/// Routes a controller within the backoffice area, I.E /umbraco
/// </summary>
public class BackOfficeRouteAttribute : RouteAttribute
{
// All this does is append [umbracoBackoffice]/ to the route,
// this is then replaced with whatever is configures as UmbracoPath by the UmbracoBackofficeToken convention
public BackOfficeRouteAttribute(string template)
: base($"[{Constants.Web.AttributeRouting.BackOfficeToken}]/" + template.TrimStart('/'))
{
}
}

View File

@@ -1,42 +0,0 @@
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
namespace Umbraco.New.Cms.Web.Common.Routing;
/// <summary>
/// Adds a custom template token for specifying backoffice route with attribute routing
/// </summary>
// Adapted from https://stackoverflow.com/questions/68911881/asp-net-core-api-add-custom-route-token-resolver
public class UmbracoBackofficeToken : IApplicationModelConvention
{
private readonly string _umbracoPath;
private readonly string _tokenRegex;
public UmbracoBackofficeToken(string tokenName, string umbracoPath)
{
_umbracoPath = umbracoPath;
_tokenRegex = $@"(\[{tokenName}])(?<!\[\1(?=]))";
}
public void Apply(ApplicationModel application)
{
foreach (ControllerModel controller in application.Controllers)
{
UpdateSelectors(controller.Selectors, _umbracoPath);
UpdateSelectors(controller.Actions.SelectMany(actionModel => actionModel.Selectors), _umbracoPath);
}
}
private void UpdateSelectors(IEnumerable<SelectorModel> selectors, string tokenValue)
{
foreach (SelectorModel selector in selectors.Where(s => s.AttributeRouteModel is not null))
{
// We just checked that AttributeRouteModel is not null, so silence the nullable warning
selector.AttributeRouteModel!.Template = InsertTokenValue(selector.AttributeRouteModel.Template, tokenValue);
selector.AttributeRouteModel.Name = InsertTokenValue(selector.AttributeRouteModel.Name, tokenValue);
}
}
private string? InsertTokenValue(string? template, string tokenValue)
=> template is null ? template : Regex.Replace(template, _tokenRegex, tokenValue);
}

View File

@@ -1,9 +0,0 @@
namespace Umbraco.New.Cms.Web.Common.Routing;
public class VersionedApiBackOfficeRouteAttribute : BackOfficeRouteAttribute
{
public VersionedApiBackOfficeRouteAttribute(string template)
: base($"management/api/v{{version:apiVersion}}/{template.TrimStart('/')}")
{
}
}