<?php
namespace App\EventListener;
use App\Entity\Admin;
use App\Entity\ControleActivity;
use App\Entity\User;
use App\Repository\ControleActivityRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class AuthenticationSuccessListener
{
private $customer;
public function __construct(
Security $security,
private RequestStack $requestStack,
private EntityManagerInterface $entityManagerInterface
) {
$this->customer = $security->getUser();
}
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$data = $event->getData();
$user = $this->customer;
if (!$user instanceof UserInterface) {
return;
}
if (true) {
$data['id'] = $user->getId();
$data['firstName'] = $user->getFirstName();
$data['lastName'] = $user->getLastName();
$data['phone'] = $user->getPhone();
$data['email'] = $user->getEmail();
$data['roles'] = $user->getRoles();
if (in_array('ROLE_ADMIN', $data['roles'])) {
$data['roles'] = $user->getRole()->toArray()[0]->getLabel();
}
$event->setData($data);
} else {
$erroMessage = "You are not authoticated";
throw new UnauthorizedHttpException($erroMessage, $erroMessage);
}
}
}