src/Controller/Security/SecurityController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Entity\Auth\LoginAttempt;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use App\Service\Auth\LoginAttemptService;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. class SecurityController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/login", name="app_login")
  15.      */
  16.     public function login(AuthenticationUtils $authenticationUtilsRequest $requestLoginAttemptService $loginAttemptService,TranslatorInterface $translator): Response
  17.     {
  18.         date_default_timezone_set('Africa/Tunis');
  19.         $em $this->getDoctrine()->getManager();
  20.         $time_left_to_connect 0;
  21.         $manyLoginAttempts false;
  22.         $message $translator->trans('Trop de tentatives de connexion');
  23.         if ($loginAttemptService && !$loginAttemptService->canLogin($request)) {
  24.             $manyLoginAttempts true;
  25.            $query_last_connexion $em->getRepository(LoginAttempt::class)->lastAttempt($request->getSession()->get('_security.last_username'));
  26.             if($query_last_connexion){
  27.                 $current_date = new \DateTime();
  28.                 $last_try_connexion $query_last_connexion->getCreatedAt();
  29.                 $date_diff date_diff($current_date,$last_try_connexion);
  30.                 $time_left_to_connect $date_diff->format('%i');
  31.             }
  32.         }
  33.         // get the login error if there is one
  34.         $error $authenticationUtils->getLastAuthenticationError();
  35.         // last username entered by the user
  36.         $lastUsername $authenticationUtils->getLastUsername();
  37.         return $this->render('security/login.html.twig', [
  38.                 'last_username' => $lastUsername,
  39.                 'error' => $error,
  40.                 'manyLoginAttempts'=>$manyLoginAttempts,
  41.                 'message'=>$message,
  42.                 'time_left_to_connect'=>$time_left_to_connect
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/logout", name="app_logout")
  47.      */
  48.     public function logout()
  49.     {
  50.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  51.     }
  52. }