#region ========================================================================= USING ===================================================================================== using Microsoft.AspNetCore.Authorization; using System.Linq; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.Presentation.Web.Common.Authorization; /// /// Handles the by checking the currently logged in user's role against the remote API. /// public class RoleAuthorizationHandler : AuthorizationHandler { private readonly IAuthorizationService _authorizationService; /// /// Initializes a new instance of the class. /// /// Injected service for authorization checks against the remote API. public RoleAuthorizationHandler(IAuthorizationService authorizationService) { _authorizationService = authorizationService; } /// /// Succeeds the requirement when the currently logged in user belongs to any of the required roles. /// /// The authorization context. /// The role requirement to evaluate. protected override async Task HandleRequirementAsync(AuthorizationHandlerContext authorizationHandlerContext, RoleRequirement roleRequirement) { foreach (string role in roleRequirement.Roles) { if (await _authorizationService.IsInRoleAsync(role, CancellationToken.None).ConfigureAwait(false)) { authorizationHandlerContext.Succeed(roleRequirement); return; } } } }