src/Security/AuthenticationEntryPoint.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  10. class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
  11. {
  12.     private $urlGenerator;
  13.     private $security;
  14.     private $tokenStorage;
  15.     public function __construct(UrlGeneratorInterface $urlGeneratorTokenStorageInterface $tokenStorageSecurity $security)
  16.     {
  17.         $this->urlGenerator $urlGenerator;
  18.         $this->security $security;
  19.         $this->tokenStorage $tokenStorage;
  20.     }
  21.     public function start(Request $requestAuthenticationException $authException null): RedirectResponse
  22.     {
  23.         // add a custom flash message and redirect to the login page
  24.         $request->getSession()->getFlashBag()->add('unauthorized_notice''La modification de vos informations est requise lors de votre première connexion.');
  25.         $user $this->security->getUser();
  26.         if ($user === null || !in_array('ROLE_EMPLOYEE'$user->getRoles())) {
  27.             $this->tokenStorage->setToken();
  28.             return new RedirectResponse($this->urlGenerator->generate('app_login'));
  29.         }
  30.     }
  31. }