src/Controller/LoginController.php line 95

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