mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 16:11:10 +00:00
feat: adds FastEndpoints core infrastructure and contracts to the web project
This commit is contained in:
1 parent
394b33efd2
commit
733e174cac
137 files changed
+3139
-158
No files matched your search
@@ -0,0 +1,42 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Handles the <see cref="RoleRequirement"/> by checking the currently logged in user's role against the remote API.
|
||||
/// </summary>
|
||||
public class RoleAuthorizationHandler : AuthorizationHandler<RoleRequirement>
|
||||
{
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RoleAuthorizationHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="authorizationService">Injected service for authorization checks against the remote API.</param>
|
||||
public RoleAuthorizationHandler(IAuthorizationService authorizationService)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Succeeds the requirement when the currently logged in user belongs to any of the required roles.
|
||||
/// </summary>
|
||||
/// <param name="authorizationHandlerContext">The authorization context.</param>
|
||||
/// <param name="roleRequirement">The role requirement to evaluate.</param>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user