#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.Common.Authorization;
///
/// FastEndpoints pre-processor that runs the application initialization check before the endpoint handler executes.
///
///
/// The check runs the RequireInitialization authorization policy, which queries the remote API and records the pending super admin setup state in the session.
/// Its result is deliberately ignored, because the pages that use this processor must be reachable both before and after the application is initialized.
///
/// The type of the request of the endpoint to which this pre-processor is applied.
public class InitializationCheckPreProcessor : IPreProcessor where TRequest : notnull
{
///
/// Runs the initialization check for the current request.
///
/// The pre-processor context for the current request.
/// Cancellation token that can be used to stop the execution.
public async Task PreProcessAsync(IPreProcessorContext preProcessorContext, CancellationToken cancellationToken)
{
HttpContext httpContext = preProcessorContext.HttpContext;
Microsoft.AspNetCore.Authorization.IAuthorizationService authorizationService = httpContext.RequestServices.GetRequiredService();
await authorizationService.AuthorizeAsync(httpContext.User, httpContext, AuthorizationPolicies.REQUIRE_INITIALIZATION).ConfigureAwait(false);
}
}