mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 09:54:30 +01:00
d5d0be5824
* Editorconfig: Add space_before_self_closing setting This was a difference between the way dotnet-format and Rider format code. See https://www.jetbrains.com/help/rider/EditorConfig_Index.html * Editorconfig: Keep 4 spaces indentation for Swagger JSON files They are all formatted that way, let's keep it like that. * Apply dotnet-format, mostly white-space related changes
77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace BTCPayServer.Hosting
|
|
{
|
|
public class ValidateControllerNameTransformer : IOutboundParameterTransformer
|
|
{
|
|
public static void Register(IServiceCollection services)
|
|
{
|
|
services.AddRouting(opts =>
|
|
{
|
|
opts.ConstraintMap["validate"] = typeof(ValidateControllerNameTransformer);
|
|
});
|
|
services.AddTransient<IApplicationModelProvider, ApplicitionModelProvider>();
|
|
services.AddSingleton<ControllerNameList>();
|
|
}
|
|
public class ControllerNotFoundException : Exception
|
|
{
|
|
public ControllerNotFoundException(string controllerName) : base($"The controller {controllerName} has not been found")
|
|
{
|
|
|
|
}
|
|
}
|
|
public class ControllerNameList : HashSet<string>
|
|
{
|
|
|
|
}
|
|
public class ApplicitionModelProvider : IApplicationModelProvider
|
|
{
|
|
public ApplicitionModelProvider(ControllerNameList list)
|
|
{
|
|
List = list;
|
|
}
|
|
public int Order => 0;
|
|
|
|
public ControllerNameList List { get; }
|
|
|
|
public void OnProvidersExecuted(ApplicationModelProviderContext context)
|
|
{
|
|
if (List.Count != 0)
|
|
return;
|
|
lock (List)
|
|
{
|
|
foreach (var controller in context.Result.Controllers)
|
|
{
|
|
List.Add(controller.ControllerName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnProvidersExecuting(ApplicationModelProviderContext context)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
private readonly ControllerNameList list;
|
|
public ValidateControllerNameTransformer(ControllerNameList list)
|
|
{
|
|
this.list = list;
|
|
}
|
|
public string TransformOutbound(object value)
|
|
{
|
|
if (value is not string str)
|
|
return null;
|
|
if (!list.Contains(str))
|
|
throw new ControllerNotFoundException(str);
|
|
return str;
|
|
}
|
|
}
|
|
|
|
|
|
}
|