<?php
namespace App\Controller;
use App\Entity\Customer\Customer;
use App\Form\ChangePasswordFormType;
use App\Form\ResetPasswordRequestFormType;
use App\Form\CodeObpFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
use App\Service\Tools;
use App\Service\Config;
use App\Service\ObpAPI;
/**
* @Route("/reset-password")
*/
class ResetPasswordController extends AbstractController
{
use ResetPasswordControllerTrait;
private $resetPasswordHelper;
private $em;
private $configService;
private $url_bridge_shop;
private $obpAPIservice;
private $session;
private $requestStack;
public function __construct(
Config $configService,
ResetPasswordHelperInterface $resetPasswordHelper,
EntityManagerInterface $em,
ObpAPI $obpAPIservice,
RequestStack $requestStack
)
{
$this->resetPasswordHelper = $resetPasswordHelper;
$this->em = $em;
$this->obpAPIservice = $obpAPIservice;
$this->configService = $configService;
$this->url_bridge_shop = $this->configService->getShopUrl().'/bridge-customers.php';
$this->requestStack = $requestStack;
$this->session = $this->requestStack->getSession();
}
/**
* Display & process form to request a password reset.
*
* @Route("", name="app_forgot_password_request")
*/
public function request(Request $request): Response
{
$form = $this->createForm(ResetPasswordRequestFormType::class);
$form->handleRequest($request);
$this->em = $this->getDoctrine()->getManager('customer');
if ($form->isSubmitted() && $form->isValid()) {
return $this->processSendingPasswordResetEmail(
$form->get('email')->getData(),
$request->request->get('g-recaptcha-response')
);
}
return $this->render('reset_password/request.html.twig', [
'requestForm' => $form->createView(),
]);
}
/**
* Confirmation page after a user has requested a password reset.
*
* @Route("/check-email", name="app_check_email")
*/
public function checkEmail(): Response
{
// Generate a fake token if the user does not exist or someone hit this page directly.
// This prevents exposing whether or not a user was found with the given email address or not
if (null === ($resetToken = $this->getTokenObjectFromSession())) {
$resetToken = $this->resetPasswordHelper->generateFakeResetToken();
}
return $this->render('reset_password/check_email.html.twig', [
'resetToken' => $resetToken,
]);
}
/**
* Affiche le formulaire pour le code envoyé par Obypay
*
* @Route("/check-code", name="app_check_code")
*/
public function checkCodeObp(Request $request): Response
{
$form = $this->createForm(CodeObpFormType::class);
$form->handleRequest($request);
$this->em = $this->getDoctrine()->getManager('customer');
$user = $this->em->getRepository(Customer::class)->findOneBy([
'id' => $request->query->get('ru'),
]);
if ($form->isSubmitted() && $form->isValid()) {
$dataObpAPI = array(
'email' => $request->request->get('mail'),
'g-recaptcha-response' => $request->request->get('g-recaptcha-response'),
);
$urlApi = $this->obpAPIservice->getContactApiUrl();
$customer_obp_infos = json_decode(
$this->obpAPIservice->CallAPI('POST', $urlApi.'/cashless/users/code/'.$form->get('code')->getData(), json_encode($dataObpAPI))
);
if(is_object($customer_obp_infos->meta)
&& isset($customer_obp_infos->meta)
&& $customer_obp_infos->meta->success
&& isset($customer_obp_infos->meta->jwt)
&& $customer_obp_infos->meta->jwt->token
) {
// $this->session->set('uBOth') =$customer_obp_infos->meta->jwt->token;
return $this->redirectToRoute('app_reset_password', ['id' => $user->getId(), 'tmp' => $customer_obp_infos->meta->jwt->token ]);
}
}
return $this->render('reset_password/code.html.twig', [
'mailCustomer' => $user->getEmail(),
'codeObpForm' => $form->createView(),
]);
}
/**
* Confirmation page after a user has requested a password reset.
*
* @Route("/fail-email", name="app_fail_email")
*/
public function failEmail(): Response
{
return $this->render('reset_password/fail_email.html.twig');
}
/**
* Validates and process the reset URL that the user clicked in their email.
*
* @Route("/reset/{id}/{tmp}", name="app_reset_password")
*/
public function reset(Request $request, UserPasswordHasherInterface $userPasswordHasher, string $id = null, string $tmp = null): Response
{
if(!$id or !$tmp) {
return $this->redirectToRoute('app_forgot_password_request');
}
$this->em = $this->getDoctrine()->getManager('customer');
$user = $this->em->getRepository(Customer::class)->findOneBy([
'id' => $id,
]);
// The token is valid; allow the user to change their password.
$form = $this->createForm(ChangePasswordFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && $this->session->get('uBOth') == $tmp) {
// Encode(hash) the plain password, and set it.
$encodedPassword = $userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
);
$dataObpAPI = array(
'password' => $form->get('plainPassword')->getData(),
'instance' => $this->obpAPIservice->getBaseInstance(),
);
$urlApi = $this->obpAPIservice->getContactApiUrl();
$customer_obp_infos = json_decode(
$this->obpAPIservice->CallAPI('PUT', $urlApi.'/cashless/users/'.$this->session->get('uObpDatas')->id, json_encode($dataObpAPI))
);
// ################################################
// ############# synchro pass compte boutique #############
$query = array( 'update_pass_customer' => true, 'mail' => $user->getEmail(), 'secret' => $form->get('plainPassword')->getData());
$customer_infos = json_decode( Tools::CallAPI('POST', $this->url_bridge_shop, $query) );
// ################################################
// ################################################
$user->setPassword($encodedPassword);
$this->em = $this->getDoctrine()->getManager('customer');
$this->em->flush();
// The session is cleaned up after the password has been changed.
$this->cleanSessionAfterReset();
return $this->redirectToRoute('account');
}
return $this->render('reset_password/reset.html.twig', [
'resetForm' => $form->createView(),
]);
}
private function processSendingPasswordResetEmail(string $emailFormData, string $recaptcha): RedirectResponse
{
$this->em = $this->getDoctrine()->getManager('customer');
$user = $this->em->getRepository(Customer::class)->findOneBy([
'email' => $emailFormData,
]);
// Do not reveal whether a user account was found or not.
if (!$user) {
return $this->redirectToRoute('app_fail_email');
}
$dataObpAPI = array(
'email' => $emailFormData,
'g-recaptcha-response' => $recaptcha,
'instance' => $this->obpAPIservice->getBaseInstance(),
);
$urlApi = $this->obpAPIservice->getContactApiUrl();
$customer_obp_infos = json_decode(
$this->obpAPIservice->CallAPI('POST', $urlApi.'/cashless/users/code', json_encode($dataObpAPI))
);
// dd($customer_obp_infos);
// Store the token object in session for retrieval in check-email route.
// $this->setTokenObjectInSession($emailFormData);
return $this->redirectToRoute('app_check_code',['ru' => $user->getId()]);
}
}