src/Controller/Security/LoginController.php line 19

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Controller/Security/LoginController.php
  4. //----------------------------------------------------------------------
  5. namespace App\Controller\Security;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use App\Services\Security\IpTools;
  12. class LoginController extends AbstractController
  13. {
  14.     public function __construct(IpTools $ipTools)
  15.     {
  16.         $this->ipTools $ipTools;
  17.     }
  18.     public function login(Request $requestAuthenticationUtils $authenticationUtils): Response
  19.     {
  20.         // If the user is already logged in, redirect
  21.         if ($this->isGranted('IS_AUTHENTICATED_FULLY'))
  22.         {
  23.             return $this->redirectToRoute('login_redirect');
  24.         }
  25.         $today = new \DateTime();
  26.         $ip $request->getClientIp();
  27.         if ($this->ipTools->isBanned($ip))
  28.         {
  29.             // Don't throw AccessDeniedException to avoid infinite loop (Default firewall behavior : Redirect to login)
  30.             throw new AccessDeniedHttpException('');
  31.         }
  32.         // get the login error if there is one
  33.         $error $authenticationUtils->getLastAuthenticationError();
  34.         // last username entered by the user
  35.         $lastUsername $authenticationUtils->getLastUsername();
  36.         return $this->render('security/login.html.twig', array(
  37.             'last_username'         =>     $lastUsername,
  38.             'error'                 =>     $error,
  39.             'today'                    =>    $today,
  40.         ));
  41.     }
  42. }