src/Controller/ResetPasswordController.php line 71

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 App\Form\CodeObpFormType;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  19. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  20. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  21. use App\Service\Tools;
  22. use App\Service\Config;
  23. use App\Service\ObpAPI;
  24. /**
  25.  * @Route("/reset-password")
  26.  */
  27. class ResetPasswordController extends AbstractController
  28. {
  29.     use ResetPasswordControllerTrait;
  30.     private $resetPasswordHelper;
  31.     private $em;
  32.     private $configService;
  33.     private $url_bridge_shop;
  34.     private $obpAPIservice;
  35.     private $session;
  36.     private $requestStack;
  37.     public function __construct(
  38.         Config $configService
  39.         ResetPasswordHelperInterface $resetPasswordHelper
  40.         EntityManagerInterface $em
  41.         ObpAPI $obpAPIservice
  42.         RequestStack $requestStack
  43.     )
  44.     {
  45.         $this->resetPasswordHelper $resetPasswordHelper;
  46.         $this->em $em;
  47.         $this->obpAPIservice $obpAPIservice;
  48.         $this->configService $configService;
  49.         $this->url_bridge_shop $this->configService->getShopUrl().'/bridge-customers.php';
  50.         $this->requestStack $requestStack;
  51.         $this->session $this->requestStack->getSession();
  52.     }
  53.     /**
  54.      * Display & process form to request a password reset.
  55.      *
  56.      * @Route("", name="app_forgot_password_request")
  57.      */
  58.     public function request(Request $request): Response
  59.     {
  60.         $form $this->createForm(ResetPasswordRequestFormType::class);
  61.         $form->handleRequest($request);
  62.         $this->em $this->getDoctrine()->getManager('customer');
  63.         if ($form->isSubmitted() && $form->isValid()) {
  64.             return $this->processSendingPasswordResetEmail(
  65.                 $form->get('email')->getData(),
  66.                 $request->request->get('g-recaptcha-response')
  67.             );
  68.         }
  69.         return $this->render('reset_password/request.html.twig', [
  70.             'requestForm' => $form->createView(),
  71.         ]);
  72.     }
  73.     /**
  74.      * Confirmation page after a user has requested a password reset.
  75.      *
  76.      * @Route("/check-email", name="app_check_email")
  77.      */
  78.     public function checkEmail(): Response
  79.     {
  80.         // Generate a fake token if the user does not exist or someone hit this page directly.
  81.         // This prevents exposing whether or not a user was found with the given email address or not
  82.         if (null === ($resetToken $this->getTokenObjectFromSession())) {
  83.             $resetToken $this->resetPasswordHelper->generateFakeResetToken();
  84.         }
  85.         return $this->render('reset_password/check_email.html.twig', [
  86.             'resetToken' => $resetToken,
  87.         ]);
  88.     }
  89.     /**
  90.      * Affiche le formulaire pour le code envoyé par Obypay
  91.      *
  92.      * @Route("/check-code", name="app_check_code")
  93.      */
  94.     public function checkCodeObp(Request $request): Response
  95.     {
  96.         $form $this->createForm(CodeObpFormType::class);
  97.         $form->handleRequest($request);
  98.         $this->em $this->getDoctrine()->getManager('customer');
  99.         $user $this->em->getRepository(Customer::class)->findOneBy([
  100.             'id' => $request->query->get('ru'),
  101.         ]);
  102.         if ($form->isSubmitted() && $form->isValid()) {
  103.             $dataObpAPI = array(
  104.                 'email' => $request->request->get('mail'),
  105.                 'g-recaptcha-response' => $request->request->get('g-recaptcha-response'),
  106.             );
  107.             $urlApi $this->obpAPIservice->getContactApiUrl();
  108.             $customer_obp_infos json_decode
  109.                 $this->obpAPIservice->CallAPI('POST'$urlApi.'/cashless/users/code/'.$form->get('code')->getData(), json_encode($dataObpAPI)) 
  110.             );
  111.             if(is_object($customer_obp_infos->meta
  112.                 && isset($customer_obp_infos->meta
  113.                 && $customer_obp_infos->meta->success
  114.                 && isset($customer_obp_infos->meta->jwt
  115.                 && $customer_obp_infos->meta->jwt->token
  116.             ) {
  117.                 // $this->session->set('uBOth') =$customer_obp_infos->meta->jwt->token;
  118.                 return $this->redirectToRoute('app_reset_password', ['id' => $user->getId(), 'tmp' => $customer_obp_infos->meta->jwt->token ]);
  119.             }
  120.         }
  121.         return $this->render('reset_password/code.html.twig', [
  122.             'mailCustomer' => $user->getEmail(),
  123.             'codeObpForm' => $form->createView(),
  124.         ]);
  125.     }
  126.     /**
  127.      * Confirmation page after a user has requested a password reset.
  128.      *
  129.      * @Route("/fail-email", name="app_fail_email")
  130.      */
  131.     public function failEmail(): Response
  132.     {
  133.         return $this->render('reset_password/fail_email.html.twig');
  134.     }
  135.     /**
  136.      * Validates and process the reset URL that the user clicked in their email.
  137.      *
  138.      * @Route("/reset/{id}/{tmp}", name="app_reset_password")
  139.      */
  140.     public function reset(Request $requestUserPasswordHasherInterface $userPasswordHasherstring $id nullstring $tmp null): Response
  141.     {
  142.         if(!$id or !$tmp) {
  143.             return $this->redirectToRoute('app_forgot_password_request');
  144.         }
  145.         $this->em $this->getDoctrine()->getManager('customer');
  146.         $user $this->em->getRepository(Customer::class)->findOneBy([
  147.             'id' => $id,
  148.         ]);
  149.         // The token is valid; allow the user to change their password.
  150.         $form $this->createForm(ChangePasswordFormType::class);
  151.         $form->handleRequest($request);
  152.         if ($form->isSubmitted() && $form->isValid() && $this->session->get('uBOth') == $tmp) {
  153.             // Encode(hash) the plain password, and set it.
  154.             $encodedPassword $userPasswordHasher->hashPassword(
  155.                 $user,
  156.                 $form->get('plainPassword')->getData()
  157.             );
  158.             $dataObpAPI = array(
  159.                 'password' => $form->get('plainPassword')->getData(),
  160.                 'instance' => $this->obpAPIservice->getBaseInstance(),
  161.             );
  162.             $urlApi $this->obpAPIservice->getContactApiUrl();
  163.             $customer_obp_infos json_decode
  164.                 $this->obpAPIservice->CallAPI('PUT'$urlApi.'/cashless/users/'.$this->session->get('uObpDatas')->idjson_encode($dataObpAPI))
  165.             );
  166.             // ################################################
  167.             // ############# synchro pass compte boutique #############
  168.             $query = array( 'update_pass_customer' => true'mail' => $user->getEmail(), 'secret' => $form->get('plainPassword')->getData());
  169.             $customer_infos json_decodeTools::CallAPI('POST'$this->url_bridge_shop$query) );
  170.             // ################################################
  171.             // ################################################
  172.             $user->setPassword($encodedPassword);
  173.             $this->em $this->getDoctrine()->getManager('customer');
  174.             $this->em->flush();
  175.             // The session is cleaned up after the password has been changed.
  176.             $this->cleanSessionAfterReset();
  177.             return $this->redirectToRoute('account');
  178.         }
  179.         return $this->render('reset_password/reset.html.twig', [
  180.             'resetForm' => $form->createView(),
  181.         ]);
  182.     }
  183.     private function processSendingPasswordResetEmail(string $emailFormDatastring $recaptcha): RedirectResponse
  184.     {
  185.         $this->em $this->getDoctrine()->getManager('customer');
  186.         $user $this->em->getRepository(Customer::class)->findOneBy([
  187.             'email' => $emailFormData,
  188.         ]);
  189.         // Do not reveal whether a user account was found or not.
  190.         if (!$user) {
  191.             return $this->redirectToRoute('app_fail_email');
  192.         }
  193.         $dataObpAPI = array(
  194.             'email' => $emailFormData,
  195.             'g-recaptcha-response' => $recaptcha,
  196.             'instance' => $this->obpAPIservice->getBaseInstance(),
  197.         );
  198.         $urlApi $this->obpAPIservice->getContactApiUrl();
  199.         $customer_obp_infos json_decode
  200.             $this->obpAPIservice->CallAPI('POST'$urlApi.'/cashless/users/code'json_encode($dataObpAPI)) 
  201.         );
  202.         // dd($customer_obp_infos);
  203.         // Store the token object in session for retrieval in check-email route.
  204.         // $this->setTokenObjectInSession($emailFormData);
  205.         return $this->redirectToRoute('app_check_code',['ru' => $user->getId()]);
  206.     }
  207. }