src/Security/AuthenticationEntryPoint.php line 20

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\Exception\AuthenticationException;
  7. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  8. use App\Repository\UserRepository;
  9. class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
  10. {
  11.     public function __construct(
  12.         private UrlGeneratorInterface $urlGenerator,
  13.         private UserRepository $userRepo 
  14.     ) {
  15.     }
  16.     public function start(Request $requestAuthenticationException $authException null): RedirectResponse
  17.     {
  18.         if (empty(!$this->userRepo->findAll())) {
  19.             // add a custom flash message and redirect to the login page
  20.             $request->getSession()->getFlashBag()->add('danger''Vous devez vous connecter pour accéder à cette page.');
  21.             return new RedirectResponse($this->urlGenerator->generate('app_login'));
  22.         } else {
  23.             // add a custom flash message and redirect to the login page
  24.             $request->getSession()->getFlashBag()->add(
  25.                 'success',
  26.                 'Bien venue Cher Admin !!!'
  27.             );
  28.             return new RedirectResponse($this->urlGenerator->generate('app_admin_register'));
  29.         }
  30.        
  31.     }
  32. }