using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
namespace Ladaer.BackEnd.Infrastructures.Authorization
{
///
/// Custom Authorization Handler cho phAcp Founder bypass tt c role checks
///
public class FounderBypassAuthorizationHandler : IAuthorizationHandler
{
private readonly ILogger _logger;
public FounderBypassAuthorizationHandler(ILogger logger)
{
_logger = logger;
}
public Task HandleAsync(AuthorizationHandlerContext context)
{
// Kim tra nu user `A authenticated
if (!context.User.Identity?.IsAuthenticated ?? true)
{
return Task.CompletedTask;
}
// Kim tra nu user cA3 role "Founder"
if (context.User.IsInRole("Founder"))
{
_logger.LogInformation("Founder access granted for user: {UserId} to resource: {Resource}",
context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
context.Resource?.ToString() ?? "Unknown");
// Founder `c phAcp truy c-p tt c - mark tt c requirements lA succeeded
foreach (var requirement in context.Requirements)
{
if (!context.HasSucceeded)
{
context.Succeed(requirement);
}
}
}
return Task.CompletedTask;
}
}
}