<?php
/*
* Created on Fri Dec 02 2022
*
* DAVID-OLIVIER DESCOMBES
*
* @licence
* You may not sell, sub-license, rent or lease any portion of the Software or Documentation to anyone.
*
* Copyright (c) 2022 dodarchitecte.com (https://dodarchitecte.com)
*
* Developed by developpeur-informatique.ma (https://www.developpeur-informatique.ma)
*/
namespace App\EventSubscriber;
use Error;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
class JWTSubscriber implements EventSubscriberInterface
{
public function __construct(private Security $security)
{
}
public function onLexikJwtAuthenticationOnJwtCreated(JWTCreatedEvent $event)
{
$data = $event->getData();
$user = $event->getUser();
$isGestionnaire = $this->security->getToken()->getUser()->getRoles() > 1;
// dd($user->isActive());
if ($isGestionnaire && !$user->isActive()) {
throw new Error("Impossible de se connecter.");
}
if ($user->isDiscarded()) {
throw new Error("Impossible de se connecter.");
} else {
// $user = $event->getUser();
// dd($event->getUser()->isCollaborator(),$event->getUser()->getClients());
// $data['email'] = $user->getEmail();
$data['username'] = $event->getUser()->getUserIdentifier();
$data['id'] = $event->getUser()->getId();
// if (!$isGestionnaire)
$data['is_collaborator'] = $event->getUser()->isCollaborator();
//$data['can_signe'] = $event->getUser()->isCollaborator() ? $event->getUser()->isCanSigne() : true;
$profile = $event->getUser()->getProfile();
if ($profile) {
$data['fullName'] = $profile->getFirstName() . " " . $profile->getLastName();
$data['firstname'] = $profile->getFirstName();
}
$event->setData($data);
}
}
public static function getSubscribedEvents(): array
{
return [
'lexik_jwt_authentication.on_jwt_created' => 'onLexikJwtAuthenticationOnJwtCreated',
];
}
}