src/Controller/ResetPasswordController.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Customer\Customer;
  4. use App\Form\ChangePasswordFormType;
  5. use App\Form\ResetPasswordRequestFormType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  17. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  18. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  19. use App\Service\Tools;
  20. /**
  21.  * @Route("/reset-password")
  22.  */
  23. class ResetPasswordController extends AbstractController
  24. {
  25.     use ResetPasswordControllerTrait;
  26.     private $resetPasswordHelper;
  27.     private $em;
  28.     private $url_bridge_shop 'https://boutique.1055.fr/bridge-customers.php';
  29.     public function __construct(ResetPasswordHelperInterface $resetPasswordHelperEntityManagerInterface $em)
  30.     {
  31.         $this->resetPasswordHelper $resetPasswordHelper;
  32.         $this->em $em;
  33.     }
  34.     /**
  35.      * Display & process form to request a password reset.
  36.      *
  37.      * @Route("", name="app_forgot_password_request")
  38.      */
  39.     public function request(Request $requestMailerInterface $mailer): Response
  40.     {
  41.         $form $this->createForm(ResetPasswordRequestFormType::class);
  42.         $form->handleRequest($request);
  43.         $this->em $this->getDoctrine()->getManager('customer');
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.             return $this->processSendingPasswordResetEmail(
  46.                 $form->get('email')->getData(),
  47.                 $mailer
  48.             );
  49.         }
  50.         return $this->render('reset_password/request.html.twig', [
  51.             'requestForm' => $form->createView(),
  52.         ]);
  53.     }
  54.     /**
  55.      * Confirmation page after a user has requested a password reset.
  56.      *
  57.      * @Route("/check-email", name="app_check_email")
  58.      */
  59.     public function checkEmail(): Response
  60.     {
  61.         // Generate a fake token if the user does not exist or someone hit this page directly.
  62.         // This prevents exposing whether or not a user was found with the given email address or not
  63.         if (null === ($resetToken $this->getTokenObjectFromSession())) {
  64.             $resetToken $this->resetPasswordHelper->generateFakeResetToken();
  65.         }
  66.         return $this->render('reset_password/check_email.html.twig', [
  67.             'resetToken' => $resetToken,
  68.         ]);
  69.     }
  70.     /**
  71.      * Confirmation page after a user has requested a password reset.
  72.      *
  73.      * @Route("/fail-email", name="app_fail_email")
  74.      */
  75.     public function failEmail(): Response
  76.     {
  77.         return $this->render('reset_password/fail_email.html.twig');
  78.     }
  79.     /**
  80.      * Validates and process the reset URL that the user clicked in their email.
  81.      *
  82.      * @Route("/reset/{token}", name="app_reset_password")
  83.      */
  84.     public function reset(Request $requestUserPasswordHasherInterface $userPasswordHasherstring $token null): Response
  85.     {
  86.         if ($token) {
  87.             // We store the token in session and remove it from the URL, to avoid the URL being
  88.             // loaded in a browser and potentially leaking the token to 3rd party JavaScript.
  89.             $this->storeTokenInSession($token);
  90.             return $this->redirectToRoute('app_reset_password');
  91.         }
  92.         $token $this->getTokenFromSession();
  93.         if (null === $token) {
  94.             throw $this->createNotFoundException('No reset password token found in the URL or in the session.');
  95.         }
  96.         try {
  97.             $user $this->resetPasswordHelper->validateTokenAndFetchUser($token);
  98.         } catch (ResetPasswordExceptionInterface $e) {
  99.             $this->addFlash('reset_password_error'sprintf(
  100.                 'There was a problem validating your reset request - %s',
  101.                 $e->getReason()
  102.             ));
  103.             return $this->redirectToRoute('app_forgot_password_request');
  104.         }
  105.         // The token is valid; allow the user to change their password.
  106.         $form $this->createForm(ChangePasswordFormType::class);
  107.         $form->handleRequest($request);
  108.         if ($form->isSubmitted() && $form->isValid()) {
  109.             // A password reset token should be used only once, remove it.
  110.             $this->resetPasswordHelper->removeResetRequest($token);
  111.             // Encode(hash) the plain password, and set it.
  112.             $encodedPassword $userPasswordHasher->hashPassword(
  113.                 $user,
  114.                 $form->get('plainPassword')->getData()
  115.             );
  116.             // ################################################
  117.             // ############# synchro pass compte boutique #############
  118.             $query = array( 'update_pass_customer' => true'mail' => $user->getEmail(), 'secret' => $form->get('plainPassword')->getData());
  119.             $customer_infos json_decodeTools::CallAPI('POST'$this->url_bridge_shop$query) );
  120.             // ################################################
  121.             // ################################################
  122.             $user->setPassword($encodedPassword);
  123.             $this->em $this->getDoctrine()->getManager('customer');
  124.             $this->em->flush();
  125.             // The session is cleaned up after the password has been changed.
  126.             $this->cleanSessionAfterReset();
  127.             return $this->redirectToRoute('account');
  128.         }
  129.         return $this->render('reset_password/reset.html.twig', [
  130.             'resetForm' => $form->createView(),
  131.         ]);
  132.     }
  133.     private function processSendingPasswordResetEmail(string $emailFormDataMailerInterface $mailer): RedirectResponse
  134.     {
  135.         $this->em $this->getDoctrine()->getManager('customer');
  136.         $user $this->em->getRepository(Customer::class)->findOneBy([
  137.             'email' => $emailFormData,
  138.         ]);
  139.         // Do not reveal whether a user account was found or not.
  140.         // dd($user);
  141.         if (!$user) {
  142.             // ##########################
  143.             // ############# test si client existe sur boutique #############
  144.             $query = array( 'check_customer' => true'mail' => $emailFormData'secret' => '''centre' => '' );
  145.             $check_shop_customer json_decodeTools::CallAPI('POST'$this->url_bridge_shop$query) );
  146.             // ##########################
  147.             
  148.             if($check_shop_customer->exist && !$check_shop_customer->update_require) {
  149.                 // ################################################
  150.                 // ############# client existe sur boutique mais pas dans résa : synchro compte #############
  151.                 unset($query['check_customer']);
  152.                 $query['create_customer_resa'] = true;
  153.                 $customer_infos json_decodeTools::CallAPI('POST'$this->url_bridge_shop$query) );
  154.                 $user $this->em->getRepository(Customer::class)->findOneBy(['email' => $emailFormData]);
  155.                 // ################################################
  156.                 // ################################################
  157.                 
  158.             } elseif($check_shop_customer->update_require) {
  159.                 // ################################################
  160.                 // ############# client existe sur boutique mais sans coordonnées complète #############
  161.                 return $this->redirectToRoute('app_fail_email');
  162.             } else {
  163.                 return $this->redirectToRoute('app_check_email');
  164.             }
  165.         }
  166.         try {
  167.             $resetToken $this->resetPasswordHelper->generateResetToken($user);
  168.         } catch (ResetPasswordExceptionInterface $e) {
  169.             // If you want to tell the user why a reset email was not sent, uncomment
  170.             // the lines below and change the redirect to 'app_forgot_password_request'.
  171.             // Caution: This may reveal if a user is registered or not.
  172.             //
  173.             // $this->addFlash('reset_password_error', sprintf(
  174.             //     'There was a problem handling your password reset request - %s',
  175.             //     $e->getReason()
  176.             // ));
  177.             return $this->redirectToRoute('app_check_email');
  178.         }
  179.         $email = (new TemplatedEmail())
  180.             ->from(new Address('no-reply@1055.fr''10·55 Réservation'))
  181.             ->to($user->getEmail())
  182.             ->subject('Demande de réinitialisation de mot de passe')
  183.             ->htmlTemplate('reset_password/email.html.twig')
  184.             ->context([
  185.                 'resetToken' => $resetToken,
  186.             ])
  187.         ;
  188.         $mailer->send($email);
  189.         // Store the token object in session for retrieval in check-email route.
  190.         $this->setTokenObjectInSession($resetToken);
  191.         return $this->redirectToRoute('app_check_email');
  192.     }
  193. }