1 using Microsoft.AspNetCore.Http;
2 using Microsoft.AspNetCore.Mvc;
3 using Microsoft.AspNetCore.Mvc.Filters;
4 using Microsoft.Extensions.Logging;
10 using System.Threading.Tasks;
43 protected ILogger Logger {
get; }
70 DatabaseContext = databaseContext ??
throw new ArgumentNullException(nameof(databaseContext));
71 if (authenticationContextFactory == null)
72 throw new ArgumentNullException(nameof(authenticationContextFactory));
73 Logger = logger ??
throw new ArgumentNullException(nameof(logger));
76 this.requireInstance = requireInstance;
77 this.requireHeaders = requireHeaders;
81 #pragma warning disable CA1506 // TODO: Decomplexify 89 await Unauthorized().ExecuteResultAsync(context).ConfigureAwait(
false);
101 (
int)HttpStatusCode.UpgradeRequired,
103 .ExecuteResultAsync(context)
104 .ConfigureAwait(
false);
112 await BadRequest(
new ErrorMessage(
ErrorCode.InstanceHeaderRequired)).ExecuteResultAsync(context).ConfigureAwait(
false);
119 await Forbid().ExecuteResultAsync(context).ConfigureAwait(
false);
124 catch (InvalidOperationException e)
131 AdditionalData = e.Message
133 .ExecuteResultAsync(context)
134 .ConfigureAwait(
false);
139 if (ModelState?.IsValid ==
false)
141 var errorMessages = ModelState
142 .SelectMany(x => x.Value.Errors)
143 .Select(x => x.ErrorMessage)
149 .Where(x => !x.EndsWith(
" field is required.", StringComparison.Ordinal));
151 if (errorMessages.Any())
156 AdditionalData = String.Join(Environment.NewLine, errorMessages)
158 .ExecuteResultAsync(context).ConfigureAwait(
false);
171 using (LogContext.PushProperty(
"Request", $
"{Request.Method} {Request.Path}"))
175 "Starting API Request: Version: {1}. User-Agent: {2}",
183 await base.OnActionExecutionAsync(context, next).ConfigureAwait(
false);
186 #pragma warning restore CA1506 For creating and accessing authentication contexts
Manages Api.Models.Users for a scope
ApiController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, ILogger logger, bool requireInstance, bool requireHeaders)
Construct an ApiController
ErrorCode
Types of ErrorMessages that the API may return.
override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
Use server authentication
Represents the currently authenticated Api.Models.User
Backend abstract implementation of IDatabaseContext
InstanceUser InstanceUser
A Controller for API functions
readonly bool requireHeaders
If ApiHeaders are required
long Id
The ID of the User
readonly bool requireInstance
If IAuthenticationContext.InstanceUser permissions are required to access the ApiController ...
Metadata about a server instance
IAuthenticationContext CurrentAuthenticationContext
The IAuthenticationContext the IAuthenticationContextFactory created
Represents an error message returned by the server