You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Digitalisation_EPA_Serveur/EPAServeur/Security/CollaborateurAuthorizationH...

43 lines
1.4 KiB

using EPAServeur.IServices;
using EPAServeur.Services;
using IO.Swagger.DTO;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace EPAServeur.Security
{
/// <summary>
/// Handler permettant de vérifier que l'utilisateur connecté valide bien le requirement
/// </summary>
public class CollaborateurAuthorizationHandler : AuthorizationHandler<SameMailCollaborateurRequirement, CollaborateurDTO>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, SameMailCollaborateurRequirement requirement, CollaborateurDTO collaborateur)
{
if (!context.User.HasClaim(c => c.Type == ClaimTypes.Email))
{
return Task.FromResult(0);
}
// Récupération du mail du collaborateur dans le claim
var mailClaim = context.User.FindFirst(c => c.Type == ClaimTypes.Email).Value;
// Vérifie si le mail du collaborateur connecté est égal au mail apside de l'objet collaborateurDTO
if (mailClaim.ToLower() == collaborateur.MailApside.ToLower())
{
context.Succeed(requirement);
}
else
{
context.Fail();
}
return Task.CompletedTask;
}
}
}