src/Controller/LoginController.php line 32

  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ChooseFormType;
  4. use App\Services\RecaptchaService;
  5. use App\Repository\UserRepository;
  6. use App\Services\Codes\SendCode;
  7. use App\Services\Mails\Mails;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. class LoginController extends AbstractController
  15. {
  16.     public function __construct(
  17.     private UserRepository $userRepository,
  18.     private SendCode $sendCode,
  19.     private RecaptchaService $recaptchaService
  20. )
  21. {
  22. }
  23.     #[Route(path: [
  24.         "fr" => "/login_site",
  25.         "en" => "/en/login_site",
  26.     ], name'app_login_site')]
  27.     public function index(Request $requestEntityManagerInterface $entityManagerMails $mailsUserPasswordHasherInterface $userPasswordHasher)
  28.     {
  29.         $form $this->createForm(ChooseFormType::class);
  30.         $handleRequest $form->handleRequest($request);
  31.         $error $request->query->get('error');
  32.         if (is_null($error)) {
  33.             $error false;
  34.         }
  35.         
  36.         $before $request->query->get('before') ?? $request->headers->get('referer');
  37.         $chemin parse_url($beforePHP_URL_PATH);
  38.         if($chemin == "/login_site")$before null;
  39.         if ($handleRequest->isSubmitted() && $handleRequest->isValid()) {
  40.             $captchaResponse $request->request->get('g-recaptcha-response');
  41.             if (!$this->recaptchaService->verify($captchaResponse)) {
  42.                 $this->addFlash('danger''Captcha invalide.');
  43.                 return $this->redirectToRoute('app_login_site', [
  44.                     'error' => true,
  45.                     'before' => $before
  46.                 ]);
  47.             };
  48.             $identifier $form->get('identifier')->getData();
  49.             $prefix $form->get('prefix')->getData();
  50.             $code = (string)random_int(100000999999);
  51.             
  52.             // check email
  53.             $userEmail $this->userRepository->findOneBy(['email' => $identifier]);
  54.             if ($userEmail) {
  55.                 if (!$userEmail->isIsActive()) {
  56.                     $this->addFlash('error''Votre compte est désactivé Pour toute information contacter contact@roomlers.com');
  57.                     return $this->redirectToRoute('app_login_site', ['error' => $error'before' => $before]);
  58.                 } else {
  59.                     
  60.                     $userEmail->getSendSMS() === null $diffTimeEmail 3600 $diffTimeEmail time() - $userEmail->getSendSMS()->getTimestamp();
  61.                     if($diffTimeEmail 60) {
  62.                         $this->addFlash('danger''Veuillez attendre avant l\'envoi du prochain code');
  63.                         return $this->redirectToRoute('app_login_site', ['error' => $error'before' => $before]);                        
  64.                     }
  65.                     $userEmail->setPassword($userPasswordHasher->hashPassword($userEmail$code));
  66.                     $entityManager->persist($userEmail);
  67.                     $entityManager->flush();
  68.                     $email $mails->createEmail('mails/security/AuthentificationCode.html.twig', ["code" => $code])->to($identifier)->subject('Votre code de connexion Roomlers');
  69.                     $mails->send($email);
  70.                     $userEmail->setSendSMS(new \DateTimeimmutable());
  71.                     return $this->redirectToRoute('app_code', ['identifier' => $identifier'before' => $before]);
  72.                 }
  73.             }
  74.     
  75.             // check phone
  76.             $userPhone $this->userRepository->findOneBy(['phone' => $identifier]);
  77.             if ($userPhone) {
  78.                 if (!$userPhone->isIsActive()) {
  79.                     $this->addFlash('danger''Votre compte est désactivé Pour toute information contacter contact@roomlers.com');
  80.                     return $this->redirectToRoute('app_login_site', ['error' => $error'before' => $before]);
  81.                 } else {
  82.                     $userPhone->getSendSMS() === null $diffTimeSMS 3600 $diffTimeSMS time() - $userPhone->getSendSMS()->getTimestamp();
  83.                     if($diffTimeSMS 60) {
  84.                         $this->addFlash('danger''Veuillez attendre avant l\'envoi du prochain code');
  85.                         return $this->redirectToRoute('app_login_site', ['error' => $error'before' => $before]);                        
  86.                     }
  87.                     $this->sendCode->send($identifier$prefix$code);
  88.                     $userPhone->setSendSMS(new \DateTimeimmutable());
  89.                     $userPhone->setPassword($userPasswordHasher->hashPassword($userPhone$code));
  90.                     $entityManager->persist($userPhone);
  91.                     $entityManager->flush();
  92.                     return $this->redirectToRoute('app_code', ['identifier' => $userPhone->getEmail(), 'before' => $before]);
  93.                 }
  94.             }
  95.     
  96.             $error true;
  97.             $this->addFlash('danger''Identifiant inconnu');
  98.             return $this->redirectToRoute('app_login_site', ['error' => $error'before' => $before]);
  99.         }
  100.     
  101.     
  102.         return $this->render('login/index.html.twig', [
  103.             'controller_name' => 'LoginController',
  104.             'form' => $form->createView(),
  105.             'error' => $error,
  106.             'before' => $before,
  107.         ]);
  108.     }
  109.     
  110.     #[Route(path: [
  111.         "fr" => "/code",
  112.         "en" => "/en/code",
  113.     ], name'app_code')]
  114.     public function code(Request $requestAuthenticationUtils $authenticationUtils)
  115.     {
  116.         $session $request->getSession();
  117.         $before $request->query->get('before') ?? $session->get('before');
  118.         if ($before) {
  119.             $session->set('before'$before);
  120.         }
  121.         
  122.         // get the login error if there is one
  123.         $error $authenticationUtils->getLastAuthenticationError();
  124.         // last username entered by the user
  125.         $lastUsername $authenticationUtils->getLastUsername();
  126.     
  127.         if ($request->query->get('error')) {
  128.             $error $request->query->get('error');
  129.         }
  130.     
  131.         if ($request->query->get('identifier')) {
  132.             $lastUsername $request->query->get('identifier');
  133.         } else {
  134.             if (!$error) {
  135.                 return $this->redirectToRoute('app_login_site', ['before' => $before]);
  136.             } else {
  137.                 return $this->redirectToRoute('app_code', ['identifier' => $lastUsername'error' => $error'before' => $before]);
  138.             }
  139.         }
  140.     
  141.         return $this->render('login/code.html.twig', [
  142.             'controller_name' => 'LoginController',
  143.             'last_username' => $lastUsername,
  144.             'error' => $error,
  145.             'before' => $before,
  146.         ]);
  147.     }    
  148. }