<?php
namespace App\Controller\Security;
use App\Entity\Auth\LoginAttempt;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Service\Auth\LoginAttemptService;
use Symfony\Contracts\Translation\TranslatorInterface;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, Request $request, LoginAttemptService $loginAttemptService,TranslatorInterface $translator): Response
{
date_default_timezone_set('Africa/Tunis');
$em = $this->getDoctrine()->getManager();
$time_left_to_connect = 0;
$manyLoginAttempts = false;
$message = $translator->trans('Trop de tentatives de connexion');
if ($loginAttemptService && !$loginAttemptService->canLogin($request)) {
$manyLoginAttempts = true;
$query_last_connexion = $em->getRepository(LoginAttempt::class)->lastAttempt($request->getSession()->get('_security.last_username'));
if($query_last_connexion){
$current_date = new \DateTime();
$last_try_connexion = $query_last_connexion->getCreatedAt();
$date_diff = date_diff($current_date,$last_try_connexion);
$time_left_to_connect = $date_diff->format('%i');
}
}
// 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', [
'last_username' => $lastUsername,
'error' => $error,
'manyLoginAttempts'=>$manyLoginAttempts,
'message'=>$message,
'time_left_to_connect'=>5 - $time_left_to_connect
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}