<?php
//----------------------------------------------------------------------
// src/Controller/Security/LoginController.php
//----------------------------------------------------------------------
namespace App\Controller\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Services\Security\IpTools;
class LoginController extends AbstractController
{
public function __construct(IpTools $ipTools)
{
$this->ipTools = $ipTools;
}
public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
{
// If the user is already logged in, redirect
if ($this->isGranted('IS_AUTHENTICATED_FULLY'))
{
return $this->redirectToRoute('login_redirect');
}
$today = new \DateTime();
$ip = $request->getClientIp();
if ($this->ipTools->isBanned($ip))
{
// Don't throw AccessDeniedException to avoid infinite loop (Default firewall behavior : Redirect to login)
throw new AccessDeniedHttpException('');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
'today' => $today,
));
}
}