src/Controller/RegistrationController.php line 220

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\Customer;
  5. use App\Entity\CustomerProfil;
  6. use App\Repository\CustomerProfilRepository;
  7. use App\Form\RegistrationFormType;
  8. use App\Form\CustomerFormType;
  9. use App\Message\MailNotification;
  10. use App\Security\EmailVerifier;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Mime\Address;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. use App\Repository\UserRepository;
  23. use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
  24. use App\Services\UserManager;
  25. use App\Services\CustomerManager;
  26. use Symfony\Component\Mailer\MailerInterface;
  27. use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
  28. use Symfony\Component\Mime\Email;
  29. use App\Config\Config;
  30. use DateTime;
  31. class RegistrationController extends AbstractController
  32. {
  33.     private EmailVerifier $emailVerifier;
  34.     private $verifyEmail;
  35.     public function __construct(
  36.         CustomerManager $customerManager,
  37.         EmailVerifier $emailVerifier,
  38.         Security $security,
  39.         SessionInterface $session,
  40.         EntityManagerInterface $entityManager,
  41.         VerifyEmailHelperInterface $verifyEmail,
  42.         UserManager $um,
  43.         MailerInterface $mailer,
  44.         Config $config
  45.     ) {
  46.         $this->emailVerifier $emailVerifier;
  47.         $this->entityManager $entityManager;
  48.         $this->session $session;
  49.         $this->security $security;
  50.         $this->verifyEmail $verifyEmail;
  51.         $this->um $um;
  52.         $this->mailer $mailer;
  53.         $this->customerManager $customerManager;
  54.         $this->config $config;
  55.     }
  56.     #[Route("/register"name"app_register")]
  57.     public function register(
  58.         Request $request,
  59.         UserPasswordHasherInterface $userPasswordHasher,
  60.         EntityManagerInterface $entityManager
  61.     ): Response {
  62.         $user = new User();
  63.         $form $this->createForm(RegistrationFormType::class, $user);
  64.         $form->handleRequest($request);
  65.         if ($form->isSubmitted()) {
  66.             if ($this->session->get("to_be_redirected")) {
  67.                 $this->um->updateUser(
  68.                     $_POST["registration_form"]["email"],
  69.                     password_hash(
  70.                         $_POST["registration_form"]["plainPassword"],
  71.                         PASSWORD_DEFAULT
  72.                     ),
  73.                     null,
  74.                     null,
  75.                     null,
  76.                     $_POST["registration_form"]["username"]
  77.                 );
  78.                 $user $this->um->getUserByEmail(
  79.                     $_POST["registration_form"]["email"]
  80.                 )[0];
  81.                 $this->addFlash(
  82.                     "success_register",
  83.                     "Un email de confirmation a été envoyé."
  84.                 );
  85.                 $this->emailVerifier->sendEmailConfirmation($user);
  86.                 // return $this->redirectToRoute("register_information");
  87.                 return $this->redirectToRoute("catalog_participant", [
  88.                     "origin" => "?=0",
  89.                     "new" => 1,
  90.                     "f" => $this->session->get("to_be_redirected")["formation"],
  91.                     "sd" => $this->session->get("to_be_redirected")["startDate"],
  92.                 ]);
  93.             }
  94.             if ($form->get("captcha")->getData()) {
  95.                 if ( $form->isValid() ||
  96.                     in_array(
  97.                         strtolower($form->get("captcha")->getData()),
  98.                         $this->capcha_keys()
  99.                     )
  100.                 ) {
  101.                 ///***********************************
  102.                     if($q=$this->um->getUserByEmail($_POST["registration_form"]["email"]))
  103.                     {  
  104.                         if ( $_POST["registration_form"]["plainPassword"] != $_POST["registration_form"]["plainPasswordConfirm"]) {
  105.                             $this->addFlash(
  106.                                 "invalid_confirm_password",
  107.                                 "Vos mots de passe doivent être identiques."
  108.                             );
  109.                             return $this->redirectToRoute("login");
  110.                         }
  111.                         if($q[0]->getIdUserProfil()==2)
  112.                         { 
  113.                             $q[0]->setIdUserProfil(1);
  114.                             $q[0]->setRoles(["ROLE_CUSTOMER"]);
  115.                             $q[0]->setIsProfessional(1);
  116.                             $q[0]->setPassword(
  117.                             $userPasswordHasher->hashPassword(
  118.                                 $q[0],
  119.                                     $_POST["registration_form"]["plainPassword"]
  120.                                 )
  121.                             );
  122.                             
  123.                             $entityManager->flush();
  124.                             if(!$q[0]->isVerified())
  125.                             {
  126.                                 $this->emailVerifier->sendEmailConfirmation($q[0]);
  127.                                 $this->addFlash(
  128.                                     "success_register",
  129.                                     "Un email de confirmation a été envoyé."
  130.                                 );
  131.                                 return $this->redirectToRoute("register_information");
  132.                             }
  133.                             $this->addFlash(
  134.                                     "success_register",
  135.                                     "Le compte a bien été mise à jour."
  136.                             );
  137.                             return $this->redirectToRoute("register_information");
  138.                         }
  139.                         else{
  140.                            $this->addFlash(
  141.                             "verify_email_error",
  142.                             "Adresse email déjà enregistrée ou Captcha incorrect"
  143.                             );
  144.                             return $this->redirectToRoute("login");
  145.                         }
  146.                     }
  147.                 ///***********************************
  148.                     // encode the plain password
  149.                     if (
  150.                         $form->get("plainPassword")->getData() ===
  151.                         $form->get("plainPasswordConfirm")->getData()
  152.                     ) {
  153.                         $user->setPassword(
  154.                             $userPasswordHasher->hashPassword(
  155.                                 $user,
  156.                                 $form->get("plainPassword")->getData()
  157.                             )
  158.                         );
  159.                         $user->setRoles(["ROLE_CUSTOMER"]);
  160.                         $user->setIsActive(1);
  161.                         $entityManager->persist($user);
  162.                         $entityManager->flush();
  163.                         $this->addFlash(
  164.                             "success_register",
  165.                             "Un email de confirmation a été envoyé."
  166.                         );
  167.                         $this->emailVerifier->sendEmailConfirmation($user);
  168.                         return $this->redirectToRoute("register_information");
  169.                     } else {
  170.                         $this->addFlash(
  171.                             "invalid_confirm_password",
  172.                             "Vos mots de passe doivent être identiques."
  173.                         );
  174.                         return $this->redirectToRoute("login");
  175.                     }
  176.                 } else {
  177.                     $this->addFlash(
  178.                         "verify_email_error",
  179.                         "Adresse email déjà enregistrée ou Captcha incorrect"
  180.                     );
  181.                     return $this->redirectToRoute("login");
  182.                 }
  183.             } else {
  184.                 $this->addFlash(
  185.                     "verify_recapcha_error",
  186.                     "Veuillez valider le Captcha"
  187.                 );
  188.                 return $this->redirectToRoute("login");
  189.             }
  190.         }
  191.         if (isset($_GET["em"])) {
  192.             $_email base64_decode($_GET["em"]);
  193.         }
  194.         if (isset($_GET["exist"])) {
  195.             $exist $_GET["exist"];
  196.         }
  197.         if (isset($_GET["f"]) && isset($_GET["sd"])) {
  198.             $this->session->set("to_be_redirected", [
  199.                 "formation" => $_GET["f"],
  200.                 "startDate" => $_GET["sd"],
  201.             ]);
  202.         }
  203.         return $this->render("acoa/registration/_register.html.twig", [
  204.             "registrationForm" => $form->createView(),
  205.             "_email" => isset($_email) ? $_email null,
  206.             "exist" => isset($exist) ? $exist 1,
  207.         ]);
  208.     }
  209.     #[Route("/verify/email/register"name"app_verify_email")]
  210.     public function verifyUserEmail(
  211.         Request $request,
  212.         UserRepository $userRepository
  213.     ): Response {
  214.         $id $request->get("id"); // retrieve the user id from the url
  215.         // Verify the user id exists and is not null
  216.         if (null === $id) {
  217.             return $this->redirectToRoute("login");
  218.         }
  219.         $user $userRepository->find($id);
  220.         // Ensure the user exists in persistence
  221.         if (null === $user) {
  222.             return $this->redirectToRoute("login");
  223.         }
  224.         // Do not get the User's Id or Email Address from the Request object
  225.         try {
  226.             $this->verifyEmail->validateEmailConfirmation(
  227.                 $request->getUri(),
  228.                 $user->getId(),
  229.                 $user->getEmail()
  230.             );
  231.         } catch (VerifyEmailExceptionInterface $e) {
  232.             $this->addFlash("verify_email_error"$e->getReason());
  233.             return $this->redirectToRoute("app_register");
  234.         }
  235.         // Mark your user as verified. e.g. switch a User::verified property to true
  236.         $user->setIsVerified(1);
  237.         $this->entityManager->persist($user);
  238.         $this->entityManager->flush();
  239.         $this->addFlash(
  240.             "success",
  241.             "Votre adresse est vérifiée, merci de vous connecter pour finaliser la réservation."
  242.         );
  243.         //return $this->redirectToRoute("register_information");
  244.         return $this->redirectToRoute("backend_my_account");
  245.     }
  246.     #[Route("/register/information"name"register_information")]
  247.     public function registerInformation(
  248.         Request $request,
  249.         CustomerProfilRepository $customerProfilRepository,
  250.         SessionInterface $session
  251.     ): Response {
  252.         $this->denyAccessUnlessGranted("IS_AUTHENTICATED_FULLY");
  253.         $currentUser $this->security->getUser();
  254.         $new $this->session->get("new");
  255.         $f $this->session->get("f");
  256.         $sd $this->session->get("sd");
  257.         $ics $this->session->get("ics");
  258.         if (
  259.             (int) $new == &&
  260.             $currentUser->getEmail() &&
  261.             $this->customerManager->hasCompanyNameByEmail(
  262.                 $currentUser->getEmail()
  263.             )
  264.         ) {
  265.             $this->session->set("new""0");
  266.             return $this->redirectToRoute("catalog_participant", [
  267.                 "origin" => "?=0",
  268.                 "f" => $f,
  269.                 "sd" => $sd,
  270.                 "ics" => $ics,
  271.             ]);
  272.         }
  273.         $customer = new Customer();
  274.         $customer->setFirstName($currentUser->getFirstName());
  275.         $customer->setLastName($currentUser->getLastName());
  276.         $customer->setEmail($currentUser->getEmail());
  277.         $customer->setPassword($currentUser->getPassword());
  278.         if (
  279.             $session->get("nbUser") == &&
  280.             $session->get("user0")->getFirstName() ==
  281.             $customer->getFirstName() &&
  282.             $session->get("user0")->getLastName() == $customer->getLastName()
  283.         ) {
  284.             $customer->setIdCustomerProfil(
  285.                 $customerProfilRepository->find(2)->getIdCustomerProfil()
  286.             );
  287.         } else {
  288.             $customer->setIdCustomerProfil(
  289.                 $customerProfilRepository->find(1)->getIdCustomerProfil()
  290.             );
  291.         }
  292.         $form $this->createForm(CustomerFormType::class, $customer);
  293.         $form->handleRequest($request);
  294.         if ($form->isSubmitted() && $form->isValid()) {
  295.             $this->entityManager->persist($customer);
  296.             $this->entityManager->flush();
  297.             // PL$ - updateUser with customer info
  298.             $this->um->updateUserByCustomer(
  299.                 $customer->getEmail(),
  300.                 $this->customerManager->getLastIdCustomer(),
  301.                 $customer->getFirstName(),
  302.                 $customer->getLastName()
  303.             );
  304.             // $this->um->updateUserIdCustomer($customer->getEmail(), $this->customerManager->getLastIdCustomer());
  305.             $this->session->set("idCustomer"$customer->getId());
  306.             $currentUser->setIdCustomer($customer->getId());
  307.             $currentUser->setFirstName($customer->getFirstName());
  308.             $currentUser->setLastName($customer->getLastName());
  309.             $currentUser->setEmail($customer->getEmail());
  310.             if (
  311.                 $this->session->get("idCourseSession") &&
  312.                 $this->session->get("redirect_to_cart") == 1
  313.             ) {
  314.                 return $this->redirectToRoute("catalog_panier", [
  315.                     "idCourseSession" => $this->session->get("idCourseSession"),
  316.                 ]);
  317.             }
  318.             if ($this->session->get("idCourseSession") !== null) {
  319.                 return $this->redirectToRoute("catalog_panier", [
  320.                     "idCourseSession" => $this->session->get("idCourseSession"),
  321.                 ]);
  322.             } else {
  323.                 return $this->redirectToRoute("backend_my_account");
  324.             }
  325.         }
  326.         return $this->render("acoa/registration/information.html.twig", [
  327.             "form" => $form->createView(),
  328.         ]);
  329.     }
  330.     #[Route("/register/learners"name"register_learners")]
  331.     public function registerLearners(Request $request)
  332.     {
  333.         $status false;
  334.         $value false;
  335.         $email "";
  336.         if (isset($_GET["ko"]) && $_GET["ko"] == "email") {
  337.             $status true;
  338.         }
  339.         if (isset($_GET["value"]) && $_GET["value"]) {
  340.             $value true;
  341.             $email $_GET["value"];
  342.         }
  343.         return $this->render("acoa/registration/register_learners.html.twig", [
  344.             "ko_email" => $status,
  345.             "email" => $value,
  346.             "value" => $email,
  347.         ]);
  348.     }
  349.     #[Route("/register/learners/confirm"name"register_learners_confirm")]
  350.     public function registerConfirmation()
  351.     {
  352.         foreach (array_chunk($_POST["participants"], 4) as $record) {
  353.             $email $record[2];
  354.             $formation $record[3];
  355.             if ($this->um->emailExist($email)) {
  356.                 return $this->redirectToRoute("register_learners", [
  357.                     "ko" => "email",
  358.                     "value" => $email,
  359.                 ]);
  360.             }
  361.             $user = new User();
  362.             $user->setFirstName($record[1]);
  363.             $user->setLastName($record[0]);
  364.             $user->setPassword(
  365.                 password_hash("iokljhko!$#pkY@787NK---LN47"PASSWORD_DEFAULT)
  366.             );
  367.             $user->setEmail($email);
  368.             $user->setRoles(["ROLE_STUDENT"]);
  369.             $user->setIsVerified(1);
  370.             $user->setIsActive(1);
  371.             $this->entityManager->persist($user);
  372.             $this->entityManager->flush();
  373.             $url =
  374.                 $_SERVER["SERVER_NAME"] .
  375.                 "/confirm/registration/" .
  376.                 base64_encode("iokljhko!$#pkY@787NK---LN47") .
  377.                 "?sun=" .
  378.                 base64_encode($email) .
  379.                 "&f=" .
  380.                 base64_encode($formation);
  381.             $em = (new Email())
  382.                 ->from($this->config->getMailSender())
  383.                 ->priority(Email::PRIORITY_HIGH)
  384.                 ->to($email)
  385.                 ->cc("kalidougattaba@gmail.com""pascal.liatard@abware.fr")
  386.                 ->subject("Registration")
  387.                 ->html($url);
  388.             $this->mailer->send($em);
  389.             // $this->dispatchMessage(new MailNotification($email, "Registration", "<p>Click on the link bellow to sign: </p><br><a href='" . $url . "'>" . "Click here" . "</a>"));
  390.         }
  391.         return $this->redirectToRoute("home");
  392.     }
  393.     #[Route("/confirm/registration/{params}"name"confirm_registration")]
  394.     public function confirmRegistration(Request $request)
  395.     {
  396.         return $this->render(
  397.             "acoa/registration/confirm_registration_student.html.twig",
  398.             [
  399.                 "email" => base64_decode($_GET["sun"]),
  400.                 "formation" => base64_decode($_GET["f"]),
  401.             ]
  402.         );
  403.     }
  404.     #[Route("/register/finalize"name"register_finalize")]
  405.     public function finalizeRegistration()
  406.     {
  407.         if (
  408.             !isset($_POST["firstName"]) ||
  409.             !isset($_POST["lastName"]) ||
  410.             !isset($_POST["userName"]) ||
  411.             !isset($_POST["password"]) ||
  412.             !isset($_POST["confirmPassword"])
  413.         ) {
  414.             return $this->redirectToRoute("confirm_registration");
  415.         }
  416.         $password strip_tags($_POST["password"]);
  417.         $confirmPassword strip_tags($_POST["confirmPassword"]);
  418.         $email strip_tags($_POST["email"]);
  419.         $firstName strip_tags($_POST["firstName"]);
  420.         $lastName strip_tags($_POST["lastName"]);
  421.         $userName strip_tags($_POST["userName"]);
  422.         $occupation strip_tags($_POST["occupation"]);
  423.         $formation strip_tags($_POST["formation"]);
  424.         if ($password != $confirmPassword) {
  425.             return $this->redirectToRoute("home");
  426.         }
  427.         $this->um->updateUser(
  428.             $email,
  429.             password_hash($passwordPASSWORD_DEFAULT),
  430.             $occupation,
  431.             $firstName,
  432.             $lastName,
  433.             $userName
  434.         );
  435.         $em = (new TemplatedEmail())
  436.             ->from($this->config->getMailSender())
  437.             ->priority(Email::PRIORITY_HIGH)
  438.             ->to($email)
  439.             ->cc("kalidougattaba@gmail.com""pascal.liatard@abware.fr")
  440.             ->subject(
  441.                 $firstName .
  442.                     " " .
  443.                     $lastName .
  444.                     ", confirmation de votre inscription"
  445.             )
  446.             ->htmlTemplate(
  447.                 "acoa/email/mailto_student_confirm_registration.html.twig"
  448.             )
  449.             ->context([
  450.                 "link" => $this->config->getDomain(),
  451.                 "firstName" => $firstName,
  452.                 "lastName" => $lastName,
  453.             ]);
  454.         $this->mailer->send($em);
  455.         /*$content =
  456.             "Votre inscription sur la plateforme Acoa  pour la formation " .
  457.             $formation .
  458.             " a bien été prise en compte !";*/
  459.         //  $this->dispatchMessage(new MailNotification($email, "Registration | Confirmation", "<p>Click on the link bellow to sign: </p><br><a href='" . $content . "'>" . "Click here" . "</a>"));
  460.         return $this->redirectToRoute("home");
  461.         // return $this->render('registration/register_finalize.html.twig', ['email'=>base64_decode( $_GET['sun'])]);
  462.     }
  463.     public function capcha_keys()
  464.     {
  465.         return  ["dsp6da""jkhj6J""juihk7""iuiu28""ukr1zb""h1rzeh""ky4tbg""kzr4te""beg0vj""yit3rb""ag1str""tt0yke""2vgjut""po7bid""tr6krg""az78ea""thd3fe""fv5fed""io8gre""kyj2yt""kz78ez""kth4rt""fr3ovd""hkd1u5""jlefk6""btdt7s""hty8bg""kgb7rg""gero7e""ng8hfh""hrt6tz""grj3ef""tukd0h""tr4mzt""try2cr""rg6jtr""reht1e""ndrer7""trzj2w""af45ye""grea2h""rth98h""pou4rt""hj0tbf""ndh6hg""rui296""kloeiog""ouieng""urtjrz""trjy6l""jdh8sh""por2ei""fezgr2""jdtr2h""jr32tr""fez1kl""ub23hf"];
  466.     }
  467. }