src/Controller/RegistrationController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\LandingPage;
  4. use App\Entity\User;
  5. use App\Form\RegistrationFormType;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\Form\FormError;
  15. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  16. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  17. use Symfony\Component\Validator\Constraints\Length;
  18. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  19. use Symfony\Component\Validator\Constraints\NotBlank;
  20. use App\Helper\Utils;
  21. /**
  22.  *
  23.  * @Route("{_locale}", requirements={"_locale": "en|fr"})
  24.  */
  25. class RegistrationController extends AbstractController
  26. {
  27.     private function authenticateUser(User $user)
  28.     {
  29.         $providerKey 'secured_area'// your firewall name
  30.         $token = new UsernamePasswordToken($usernull$providerKey$user->getRoles());
  31.         $this->container->get('security.token_storage')->setToken($token);
  32.     }
  33.     
  34.     /**
  35.      * @Route({
  36.      *     "fr": "/inscription",
  37.      *     "en": "/register"
  38.      * }, name="register")
  39.      */
  40.     public function register(EntityManagerInterface $emTranslatorInterface $translatorRequest $requestUserPasswordEncoderInterface $passwordEncoder): Response
  41.     {
  42.         if (!$platform $request->getSession()->get('platform')) {
  43.             throw $this->createNotFoundException('this domain is not allowed');
  44.         }
  45.         $user $this->getUser();
  46.         if ($user) {
  47.             return $this->redirectToRoute('login');
  48.         }
  49.         $utils = new Utils;
  50.         $user = new User();
  51.         $user->setPlatform($platform);
  52.         $form $this->createForm(RegistrationFormType::class, $user);
  53.         $data $request->request->get('registration_form');
  54.         $form->handleRequest($request);
  55.         $googleResponse false;
  56.         if ($form->isSubmitted()) {
  57.             $googleResponse $utils->validCaptcha($form$translator);
  58.             if ($this->getDoctrine()->getRepository(User::class)->findOneBy(['deleted' => false'pseudo' => $data['pseudo'], 'platform' => $platform])) {
  59.                 $form['pseudo']->addError(new FormError($translator->trans('this_username_address_is_already_in_use')));
  60.             }
  61.             if ($this->getDoctrine()->getRepository(User::class)->findOneBy(['deleted' => false'email' => $data['email'], 'platform' => $platform])) {
  62.                 $form['email']->addError(new FormError($translator->trans('this_email_address_is_already_in_use')));
  63.             }
  64.             $user->setUsername($platform->getId().'_'.$user->getEmail());
  65.         }
  66.         if ($form->isSubmitted() && $form->isValid()) {
  67.             $user->setNewResetToken();
  68.             $em->persist($user);
  69.             $em->flush();
  70.             $transport = (new \Swift_SmtpTransport($platform->getSmtpServer(), $platform->getSmtpPort(), 'tls'))
  71.                 ->setUsername($platform->getEmail())
  72.                 ->setPassword($platform->getEmailPassword())
  73.             ;
  74.             $mailer = new \Swift_Mailer($transport);
  75.             $message = (new \Swift_Message($translator->trans('confirm_your_account')))
  76.                 ->setFrom($platform->getEmail(), $platform->getName())
  77.                 ->setTo($data['email'])
  78.                 ->setBody(
  79.                     $this->renderView(
  80.                         'emails/confirmYourAccount.html.twig', [
  81.                             'user' => $user,
  82.                         ]
  83.                     ),
  84.                     'text/html'
  85.                 )
  86.             ;
  87.             $mailer->send($message);
  88.             return $this->redirectToRoute('register_confirmation');
  89.         }
  90.         return $this->render('registration/register.html.twig', [
  91.             'registrationForm' => $form->createView(),
  92.             'showCaptchaError' => $form->isSubmitted()&&!$googleResponse?true:false,
  93.         ]);
  94.     }
  95.     
  96.     /**
  97.      * @Route({
  98.      *     "fr": "/confirmation-inscription",
  99.      *     "en": "/register-confirmation"
  100.      * }, name="register_confirmation")
  101.      */
  102.     public function registerConfirmation(Request $requestEntityManagerInterface $em): Response
  103.     {
  104.         if (!$platform $request->getSession()->get('platform')) {
  105.             throw $this->createNotFoundException('this domain is not allowed');
  106.         }
  107.         $utils = new Utils;
  108.         $user $this->getUser();
  109.         if ($user) {
  110.             return $this->redirectToRoute('login');
  111.         }
  112.         return $this->render('registration/registerConfirmation.html.twig');
  113.     }
  114.     
  115.     /**
  116.      * @Route({
  117.      *     "fr": "/reinitialiser-mon-mot-de-passe",
  118.      *     "en": "/reset-my-password"
  119.      * }, name="forgot_password")
  120.      */
  121.     public function forgotPassword(TranslatorInterface $translatorEntityManagerInterface $emRequest $request): Response
  122.     {
  123.         if (!$platform $request->getSession()->get('platform')) {
  124.             throw $this->createNotFoundException('this domain is not allowed');
  125.         }
  126.         $utils = new Utils;
  127.         $user $this->getUser();
  128.         if ($user) {
  129.             return $this->redirectToRoute('login');
  130.         }
  131.         $data $request->request->get('form');
  132.         $form $this->createFormBuilder()
  133.             ->add('email'EmailType::class, [
  134.                 'mapped' => false,
  135.                 'required' => true,
  136.                 'label' => 'email',
  137.                 'constraints' => [new NotBlank()],
  138.                 'attr' => [
  139.                     'class' => 'text-center form-control-lg',
  140.                     'placeholder' => 'email',
  141.                 ],
  142.             ])
  143.         ->getForm();
  144.         $form->handleRequest($request);
  145.         $googleResponse false;
  146.         if ($form->isSubmitted()) {
  147.             $googleResponse $utils->validCaptcha($form$translator);
  148.             $user $this->getDoctrine()->getRepository(User::class)->findOneBy(['deleted' => false'enabled' => true'email' => $data['email']]);
  149.             if (!$user) {
  150.                 $form['email']->addError(new FormError($translator->trans('this_email_address_cannot_be_found')));
  151.             }
  152.         }
  153.         if ($form->isSubmitted() && $form->isValid()) {
  154.             $user->setNewResetToken();
  155.             $em->persist($user);
  156.             $em->flush();
  157.             $transport = (new \Swift_SmtpTransport($platform->getSmtpServer(), $platform->getSmtpPort(), 'tls'))
  158.                 ->setUsername($platform->getEmail())
  159.                 ->setPassword($platform->getEmailPassword())
  160.             ;
  161.             $mailer = new \Swift_Mailer($transport);
  162.             $message = (new \Swift_Message($translator->trans('reset_my_password')))
  163.                 ->setFrom($platform->getEmail(), $platform->getName())
  164.                 ->setTo($data['email'])
  165.                 ->setBody(
  166.                     $this->renderView(
  167.                         'emails/forgotPassword.html.twig', [
  168.                             'user' => $user,
  169.                         ]
  170.                     ),
  171.                     'text/html'
  172.                 )
  173.             ;
  174.             $mailer->send($message);
  175.             return $this->redirectToRoute('forgot_password_confirmation');
  176.         }
  177.         // $masseurs = $this->getDoctrine()->getRepository(User::class)->getMasseurs();
  178.         return $this->render('registration/forgotPassword.html.twig', [
  179.             'form' => $form->createView(),
  180.             'showCaptchaError' => $form->isSubmitted()&&!$googleResponse?true:false,
  181.         ]);
  182.     }
  183.     
  184.     /**
  185.      * @Route({
  186.      *     "fr": "/mot-de-passe-oublie-confirmation",
  187.      *     "en": "/forgot-password-confirmation"
  188.      * }, name="forgot_password_confirmation")
  189.      */
  190.     public function forgotPasswordConfirmation(Request $requestEntityManagerInterface $em): Response
  191.     {
  192.         if (!$platform $request->getSession()->get('platform')) {
  193.             throw $this->createNotFoundException('this domain is not allowed');
  194.         }
  195.         $utils = new Utils;
  196.         $user $this->getUser();
  197.         if ($user) {
  198.             return $this->redirectToRoute('home');
  199.         }
  200.         return $this->render('registration/forgotPasswordConfirmation.html.twig');
  201.     }
  202.     
  203.     /**
  204.      * @Route({
  205.      *     "fr": "/reinitialiser-mon-mot-de-passe/{email}/{resetToken}",
  206.      *     "en": "/reset-my-password/{email}/{resetToken}"
  207.      * }, name="reset_password")
  208.      */
  209.     public function resetPassword(TranslatorInterface $translatorEntityManagerInterface $emRequest $requestUserPasswordEncoderInterface $passwordEncoderUser $newUser): Response
  210.     {
  211.         if (!$platform $request->getSession()->get('platform')) {
  212.             throw $this->createNotFoundException('this domain is not allowed');
  213.         }
  214.         $utils = new Utils;
  215.         $user $this->getUser();
  216.         if ($user) {
  217.             return $this->redirectToRoute('home');
  218.         }
  219.         $user $newUser;
  220.         $data $request->request->get('form');
  221.         $form $this->createFormBuilder()
  222.             ->add('plainPassword'RepeatedType::class, [
  223.                 'type' => PasswordType::class,
  224.                 'invalid_message' => $translator->trans('the_password_fields_must_match'),
  225.                 'options' => ['attr' => ['class' => 'password-field']],
  226.                 'required' => true,
  227.                 'label' => 'password',
  228.                 'mapped' => false,
  229.                 'attr' => [
  230.                     'class' => 'text-center',
  231.                 ],
  232.                 'first_options'  => [
  233.                     'label' => 'new_password',
  234.                     'attr' => [
  235.                         'class' => 'text-center form-control-lg',
  236.                         'placeholder' => 'new_password'
  237.                     ],
  238.                 ],
  239.                 'second_options' => [
  240.                     'label' => 'new_repeat_password',
  241.                     'attr' => [
  242.                         'class' => 'text-center form-control-lg',
  243.                         'placeholder' => 'new_repeat_password'
  244.                     ],
  245.                 ],
  246.                 'constraints' => [
  247.                     new NotBlank([
  248.                         'message' => $translator->trans('please_enter_your_password'),
  249.                     ]),
  250.                     new Length([
  251.                         'min' => 6,
  252.                         'minMessage' => $translator->trans('password_min_characters_message', [ '%limit%' => ]),
  253.                         // max length allowed by Symfony for security reasons
  254.                         'max' => 4096,
  255.                     ]),
  256.                 ],
  257.             ])
  258.         ->getForm();
  259.         $form->handleRequest($request);
  260.         if ($form->isSubmitted() && $form->isValid()) {
  261.             $user->setPassword(
  262.                 $passwordEncoder->encodePassword(
  263.                     $user,
  264.                     $form->get('plainPassword')->getData()
  265.                 )
  266.             );
  267.             $user->setResetToken(null);
  268.             $user->setEnabled(true);
  269.             $em->persist($user);
  270.             $em->flush();
  271.             $this->authenticateUser($user);
  272.             return $this->redirectToRoute('login');
  273.         }
  274.         // $masseurs = $this->getDoctrine()->getRepository(User::class)->getMasseurs();
  275.         return $this->render('registration/resetPassword.html.twig', [
  276.             'form' => $form->createView(),
  277.         ]);
  278.     }
  279.     
  280.     /**
  281.      * @Route({
  282.      *     "fr": "/validez-votre-compte/{email}/{resetToken}",
  283.      *     "en": "/confirm-your-account/{email}/{resetToken}"
  284.      * }, name="confirm_account")
  285.      */
  286.     public function confirmAccount(TranslatorInterface $translatorEntityManagerInterface $emRequest $requestUserPasswordEncoderInterface $passwordEncoderUser $newUser): Response
  287.     {
  288.         if (!$platform $request->getSession()->get('platform')) {
  289.             throw $this->createNotFoundException('this domain is not allowed');
  290.         }
  291.         $utils = new Utils;
  292.         $user $this->getUser();
  293.         if ($user) {
  294.             return $this->redirectToRoute('offers');
  295.         }
  296.         $user $newUser;
  297.         $data $request->request->get('form');
  298.         $form $this->createFormBuilder()
  299.             ->add('plainPassword'RepeatedType::class, [
  300.                 'type' => PasswordType::class,
  301.                 'invalid_message' => $translator->trans('the_password_fields_must_match'),
  302.                 'options' => ['attr' => ['class' => 'password-field']],
  303.                 'required' => true,
  304.                 'label' => 'password',
  305.                 'mapped' => false,
  306.                 'attr' => [
  307.                     'class' => 'text-center',
  308.                 ],
  309.                 'first_options'  => [
  310.                     'label' => 'new_password',
  311.                     'attr' => [
  312.                         'class' => 'text-center form-control-lg',
  313.                         'placeholder' => 'new_password'
  314.                     ],
  315.                 ],
  316.                 'second_options' => [
  317.                     'label' => 'new_repeat_password',
  318.                     'attr' => [
  319.                         'class' => 'text-center form-control-lg',
  320.                         'placeholder' => 'new_repeat_password'
  321.                     ],
  322.                 ],
  323.                 'constraints' => [
  324.                     new NotBlank([
  325.                         'message' => $translator->trans('please_enter_your_password'),
  326.                     ]),
  327.                     new Length([
  328.                         'min' => 6,
  329.                         'minMessage' => $translator->trans('password_min_characters_message', [ '%limit%' => ]),
  330.                         // max length allowed by Symfony for security reasons
  331.                         'max' => 4096,
  332.                     ]),
  333.                 ],
  334.             ])
  335.         ->getForm();
  336.         $form->handleRequest($request);
  337.         if ($form->isSubmitted() && $form->isValid()) {
  338.             $user->setPassword(
  339.                 $passwordEncoder->encodePassword(
  340.                     $user,
  341.                     $form->get('plainPassword')->getData()
  342.                 )
  343.             );
  344.             $user->setResetToken(null);
  345.             $user->setEnabled(true);
  346.             $em->persist($user);
  347.             $em->flush();
  348.             $this->authenticateUser($user);
  349.             return $this->redirectToRoute('login');
  350.         }
  351.         // $masseurs = $this->getDoctrine()->getRepository(User::class)->getMasseurs();
  352.         return $this->render('registration/confirmAccount.html.twig', [
  353.             'form' => $form->createView(),
  354.         ]);
  355.     }
  356. }