src/Controller/RegisterController.php line 132

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\RouterInterface;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\Security\Http\SecurityEvents;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  13. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. use Symfony\Component\Mime\Address;
  19. use Symfony\Component\Mime\Email;
  20. use League\HTMLToMarkdown\HtmlConverter;
  21. use App\Form\compteType;
  22. use App\Form\lostType;
  23. use App\Form\ResetPasswordType;
  24. use App\Services\iresaService;
  25. use App\Services\SendinblueService;
  26. use App\Entity\compte;
  27. use App\Entity\Language;
  28. /**
  29.  * @Route("/{_locale}", requirements={"_locale": "en|fr"})
  30.  */
  31. class RegisterController extends AbstractController
  32. {
  33.     private $tokenStorage;
  34.     private $eventDispatcher;
  35.     private $router;
  36.     private $translator;
  37.     private $passwordEncoder;
  38.     private $mailer;
  39.     private $em;
  40.     private $iresaService;
  41.     private $sendinblueService;
  42.     
  43.     public function __construct(RouterInterface $routerTokenStorageInterface $tokenStorageEventDispatcherInterface $eventDispatcherTranslatorInterface $translatorUserPasswordEncoderInterface $passwordEncoderMailerInterface $mailerEntityManagerInterface $emiresaService $iresaServiceSendinblueService $sendinblueService)
  44.     {
  45.         $this->tokenStorage $tokenStorage;
  46.         $this->eventDispatcher $eventDispatcher;
  47.         $this->router $router;
  48.         $this->translator $translator;
  49.         $this->passwordEncoder $passwordEncoder;
  50.         $this->mailer $mailer;
  51.         $this->em $em;
  52.         $this->iresaService $iresaService;
  53.         $this->sendinblueService $sendinblueService;
  54.     }
  55.     /**
  56.      *
  57.      * @Route("/compte", name="compte")
  58.      */
  59.     public function accountAction(Request $request)
  60.     { 
  61.         $session $request->getSession();
  62.         $locale $request->getLocale();
  63.         $lang  $this->em->getRepository(Language::class)->findOneByCode($locale);
  64.         $compte = new compte();
  65.         $compte->setLang($lang);
  66.         $form $this->createForm(compteType::class, $compte);
  67.         $resetPasswordFrom $this->createForm(lostType::class);
  68.         $form->handleRequest($request);
  69.         if ($form->isSubmitted() && $form->isValid()) { 
  70.             $em $this->getDoctrine()->getManager();
  71.             
  72.             $compte->setRoles(['ROLE_CUSTOMER']);
  73.             $password $this->passwordEncoder->encodePassword($compte$compte->getPassword());
  74.             $compte->setPassword($password);
  75.             $this->em->persist($compte);
  76.             $obj->debug = ['User-agent' => $request->headers->get('User-Agent'), "script" => "RegisterController:accountAction:38"];
  77.             $createUserIresa $this->iresaService->iresaXFT("Create""Customer"$compte);
  78.             
  79.             if($createUserIresa != null) {
  80.                 $key intval($createUserIresa->Customer->Codes->Code[0]["Value"]);
  81.                 $compte->setKeyiresa($key);
  82.             }
  83.             $this->em->persist($compte);
  84.             $this->em->flush();
  85.             
  86.             $session->getFlashBag()->add('success', ['type' => 'success''title' => ""'message' => $this->translator->trans('compte.register.inscription.success', array(), 'app')]);
  87.             //return $this->redirectToRoute('compte');
  88.             $token = new UsernamePasswordToken($compte$compte->getPassword(), 'main_front'$compte->getRoles());
  89.             $this->tokenStorage->setToken($token);
  90.             $event = new InteractiveLoginEvent($request$token);
  91.             $this->eventDispatcher->dispatch($eventSecurityEvents::INTERACTIVE_LOGIN);
  92.             if($session->has('ReservationId') ) {
  93.                 $path $session->get('loginRedirectPath');
  94.                 $session->remove('ReservationId');
  95.                 $session->remove('loginRedirectPath');
  96.                 $response = new RedirectResponse($this->router->generate($path));
  97.                 return $response;
  98.             }
  99.         }
  100.         $route $request->query->get('route_name');
  101.         $params $request->query->get('params');
  102.         
  103.         return $this->render('/front/Compte/register.html.twig',[
  104.             'form' => $form->createView(),
  105.             'lostform' => $resetPasswordFrom->createView(),
  106.             "route_name" => $route,
  107.             "params" => $params
  108.         ]);
  109.     }
  110.     /**
  111.      *
  112.      * @Route("/compte/reset_password", name="account_lost_password")
  113.      */
  114.     public function lostPasswordAction(Request $request
  115.     {
  116.         $session $request->getSession();
  117.         $resetPasswordFrom $this->createForm(lostType::class);
  118.         $resetPasswordFrom->handleRequest($request);
  119.         
  120.         if ($resetPasswordFrom->isSubmitted() && $resetPasswordFrom->isValid()) { 
  121.             $email $resetPasswordFrom->getData()['email'];
  122.             $user $this->getDoctrine()->getRepository(compte::class)->findOneBy(['email' => $email]);
  123.             if ($user) {
  124.                 $token md5(time() . rand());
  125.                 $em $this->getDoctrine()->getManager();
  126.                 $user->setResetPasswordToken($token);
  127.                 $this->em->persist($user);
  128.                 $this->em->flush();
  129.                 $params = [
  130.                     'name' => $user->getprenom(),
  131.                     'email' => $user->getEmail(), 
  132.                     'token' => $token
  133.                     'subject' => $this->translator->trans('compte.register.inscription.reinitialisation.email.sujet', array(), 'app'),
  134.                     'view' => 'reset_password'
  135.                 ];
  136.                 // template #933 Brevo - réinitialisation du mot de passe
  137.                 $ret $this->sendMail($params933);
  138.                
  139.                 $title="";
  140.                 $message $this->translator->trans('compte.register.inscription.reinitialisation.flash', array(), 'app');
  141.                 $session->getFlashBag()->add('success', ['type' => 'success''title' => $title'message' => $message]);
  142.                 return $this->redirectToRoute('compte');
  143.             } 
  144.             
  145.             $message $this->translator->trans('compte.register.inscription.erreur.utilisateur_inconnu', array(), 'app');
  146.             $session->getFlashBag()->add('error', ['type' => 'success''title' => ""'message' => $message]);
  147.         }
  148.         $message $this->translator->trans('compte.register.inscription.erreur.formulaire_non_valide', array(), 'app');
  149.         $session->getFlashBag()->add('error', ['type' => 'success''title' => ""'message' => $message]);
  150.         return $this->redirectToRoute('compte');
  151.     }
  152.     
  153.     /**
  154.      * @Route("/reinitialiserMotDePasse", name="reinitialiserMotDePasse")
  155.      * 
  156.      */
  157.     public function confirmPasswordAction(Request $request)
  158.     {   
  159.         $session $request->getSession();
  160.         $email $request->query->get('email');
  161.         $requestToken $request->query->get('token');
  162.         $user $this->em->getRepository(compte::class)->findOneBy(['email' => $email]);
  163.         $form $this->createForm(ResetPasswordType::class);
  164.         $form->handleRequest($request);
  165.         if (!$form->isSubmitted() ) {
  166.             $csrfToken $user->getResetPasswordToken();
  167.             if ($csrfToken != $requestToken) {
  168.                 $resetPasswordFrom $this->createForm(lostType::class);
  169.             
  170.                 return $this->render('security/change_error.html.twig', [
  171.                     'lostform' => $resetPasswordFrom->createView(),
  172.                 ]);
  173.             }
  174.         }
  175.         
  176.         if ($form->isSubmitted() && $form->isValid()) {
  177.             $password $this->passwordEncoder->encodePassword($user$form->getData()['plainPassword']);
  178.             $user->setPassword($password);
  179.             $user->setResetPasswordToken(null);
  180.             $this->em->persist($user);
  181.             $this->em->flush();
  182.             $token = new UsernamePasswordToken($user$user->getPassword(), 'main_front'$user->getRoles());
  183.             $this->tokenStorage->setToken($token);
  184.             $session->set('_security_main'serialize($token));
  185.             
  186.             $title="";
  187.             $message="Password changed!";
  188.             $session->getFlashBag()->add('success', ['type' => 'success''title' => $title'message' => $message]);
  189.             
  190.             $params = [
  191.                 'name' => $user->getprenom(),
  192.                 'email' => $user->getEmail(), 
  193.                 'token' => $requestToken
  194.                 'subject' => 'Mot de passe réinitialisé avec succès',
  195.                 'view' => 'reset_password_confirmation'
  196.             ];
  197.             $this->sendMail($params934);
  198.             return $this->redirectToRoute('ac_platform_homepage');
  199.         } 
  200.         
  201.         $array = ['form' => $form->createView(), 'email' => $email'token' => $csrfToken];
  202.         return $this->render('security/confirm_password.html.twig'$array);
  203.     }
  204.     private function sendMail($params$templateId null)
  205.     {
  206.         if($templateId == null) {
  207.             $content $this->renderView('/front/Compte/' .  $params['view'] . '.html.twig'$params);
  208.             
  209.             // Convertir le contenu HTML en texte brut
  210.             $converter = new HtmlConverter();
  211.             $texte $converter->convert($content);
  212.             
  213.             $message = (new Email())
  214.                 ->from(new Address('noreply@ateya.fr'"Ateya-vacances"))
  215.                 ->to(new Address($params['email'], $params['name']))
  216.                 ->subject($params['subject'])
  217.                 ->text($texte)
  218.                 ->html((string)$content)
  219.                 ;
  220.             try {
  221.                 $this->mailer->send($message);
  222.             } catch (TransportExceptionInterface $e) {
  223.                 throw new NotificationErrorSendingException(sprintf('error sending for %s'$notification->getRecipient()->getEmail()));
  224.             }
  225.         } else {
  226.             $vars = [
  227.                 "EMAIL" => $params['email'],
  228.                 "TOKEN" => $params['token']
  229.             ];
  230.             $this->sendinblueService->setTemplateId($templateId);
  231.             $this->sendinblueService->setRecipient($params['email'], $params['name']);
  232.             $this->sendinblueService->setParams($vars);
  233.             try {
  234.                 $ret $this->sendinblueService->sendEmail();
  235.             } catch (Exception $e) {
  236.                echo $e->getMessage();
  237.             }
  238.         }
  239.         return "EMAIL";
  240.        
  241.     }
  242. }