src/Controller/LoginController.php line 32
<?phpnamespace App\Controller;use App\Form\ChooseFormType;use App\Services\RecaptchaService;use App\Repository\UserRepository;use App\Services\Codes\SendCode;use App\Services\Mails\Mails;use Doctrine\ORM\EntityManagerInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;class LoginController extends AbstractController{public function __construct(private UserRepository $userRepository,private SendCode $sendCode,private RecaptchaService $recaptchaService){}#[Route(path: ["fr" => "/login_site","en" => "/en/login_site",], name: 'app_login_site')]public function index(Request $request, EntityManagerInterface $entityManager, Mails $mails, UserPasswordHasherInterface $userPasswordHasher){$form = $this->createForm(ChooseFormType::class);$handleRequest = $form->handleRequest($request);$error = $request->query->get('error');if (is_null($error)) {$error = false;}$before = $request->query->get('before') ?? $request->headers->get('referer');$chemin = parse_url($before, PHP_URL_PATH);if($chemin == "/login_site")$before = null;if ($handleRequest->isSubmitted() && $handleRequest->isValid()) {$captchaResponse = $request->request->get('g-recaptcha-response');if (!$this->recaptchaService->verify($captchaResponse)) {$this->addFlash('danger', 'Captcha invalide.');return $this->redirectToRoute('app_login_site', ['error' => true,'before' => $before]);};$identifier = $form->get('identifier')->getData();$prefix = $form->get('prefix')->getData();$code = (string)random_int(100000, 999999);// check email$userEmail = $this->userRepository->findOneBy(['email' => $identifier]);if ($userEmail) {if (!$userEmail->isIsActive()) {$this->addFlash('error', 'Votre compte est désactivé Pour toute information contacter contact@roomlers.com');return $this->redirectToRoute('app_login_site', ['error' => $error, 'before' => $before]);} else {$userEmail->getSendSMS() === null ? $diffTimeEmail = 3600 : $diffTimeEmail = time() - $userEmail->getSendSMS()->getTimestamp();if($diffTimeEmail < 60) {$this->addFlash('danger', 'Veuillez attendre avant l\'envoi du prochain code');return $this->redirectToRoute('app_login_site', ['error' => $error, 'before' => $before]);}$userEmail->setPassword($userPasswordHasher->hashPassword($userEmail, $code));$entityManager->persist($userEmail);$entityManager->flush();$email = $mails->createEmail('mails/security/AuthentificationCode.html.twig', ["code" => $code])->to($identifier)->subject('Votre code de connexion Roomlers');$mails->send($email);$userEmail->setSendSMS(new \DateTimeimmutable());return $this->redirectToRoute('app_code', ['identifier' => $identifier, 'before' => $before]);}}// check phone$userPhone = $this->userRepository->findOneBy(['phone' => $identifier]);if ($userPhone) {if (!$userPhone->isIsActive()) {$this->addFlash('danger', 'Votre compte est désactivé Pour toute information contacter contact@roomlers.com');return $this->redirectToRoute('app_login_site', ['error' => $error, 'before' => $before]);} else {$userPhone->getSendSMS() === null ? $diffTimeSMS = 3600 : $diffTimeSMS = time() - $userPhone->getSendSMS()->getTimestamp();if($diffTimeSMS < 60) {$this->addFlash('danger', 'Veuillez attendre avant l\'envoi du prochain code');return $this->redirectToRoute('app_login_site', ['error' => $error, 'before' => $before]);}$this->sendCode->send($identifier, $prefix, $code);$userPhone->setSendSMS(new \DateTimeimmutable());$userPhone->setPassword($userPasswordHasher->hashPassword($userPhone, $code));$entityManager->persist($userPhone);$entityManager->flush();return $this->redirectToRoute('app_code', ['identifier' => $userPhone->getEmail(), 'before' => $before]);}}$error = true;$this->addFlash('danger', 'Identifiant inconnu');return $this->redirectToRoute('app_login_site', ['error' => $error, 'before' => $before]);}return $this->render('login/index.html.twig', ['controller_name' => 'LoginController','form' => $form->createView(),'error' => $error,'before' => $before,]);}#[Route(path: ["fr" => "/code","en" => "/en/code",], name: 'app_code')]public function code(Request $request, AuthenticationUtils $authenticationUtils){$session = $request->getSession();$before = $request->query->get('before') ?? $session->get('before');if ($before) {$session->set('before', $before);}// get the login error if there is one$error = $authenticationUtils->getLastAuthenticationError();// last username entered by the user$lastUsername = $authenticationUtils->getLastUsername();if ($request->query->get('error')) {$error = $request->query->get('error');}if ($request->query->get('identifier')) {$lastUsername = $request->query->get('identifier');} else {if (!$error) {return $this->redirectToRoute('app_login_site', ['before' => $before]);} else {return $this->redirectToRoute('app_code', ['identifier' => $lastUsername, 'error' => $error, 'before' => $before]);}}return $this->render('login/code.html.twig', ['controller_name' => 'LoginController','last_username' => $lastUsername,'error' => $error,'before' => $before,]);}}