src/Controller/RegistrationController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\CurrencyRepository;
  6. use App\Repository\ThemeRepository;
  7. use App\Repository\UserRepository;
  8. use App\Security\EmailVerifier;
  9. use App\Service\MailerService;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  20. class RegistrationController extends AbstractController
  21. {
  22.     public const PREVENTED_PASSWORDS = ['1234''Qwert''qwert''2345''3456''4567''5678''6789''7890''0987''9876''8765''7654''6543''5432''4321''3210''1111''2222''3333''4444''5555''6666',
  23.         '7777''8888''9999''0000''password''Password''123123''98765''uiop''mynoob''123321''18atcskd2v''1g2w3e4r''3ris1la7ge''google''Google''1g2w3e''g2w3e4''2w3e4r''w3e4r5''123qwe''zxcvbnm''abc123''loveyou''Loveyou''LoveYou''Monkey''monkey''Dragon''dragon''master''Master'];
  24.     public function __construct(private readonly EmailVerifier $emailVerifier, private readonly MailerService $mailerService)
  25.     {
  26.     }
  27.     #[Route('/user/register'name'app_register')]
  28.     public function clientRegistration(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManagerThemeRepository $themeRepositoryCurrencyRepository $currencyRepository): Response
  29.     {
  30.         $preventedPasswords array_merge(self::PREVENTED_PASSWORDSrange(19002050));
  31.         if ($this->getUser()) {
  32.             return $this->redirectToRoute('home');
  33.         }
  34.         $logoUrl $request->getScheme().'://'.$request->getHttpHost().$request->getBasePath().'/assets/img/Consort/Consort1-email.png';
  35.         $user = new User();
  36.         $form $this->createForm(RegistrationFormType::class, $user);
  37.         $form->handleRequest($request);
  38.         // Prevent a list of passwords patterns
  39.         if ($form->isSubmitted() && $form->isValid()) {
  40.             foreach ($preventedPasswords as $pattern) {
  41.                 if (str_contains($form->get('password')->getData(), $pattern)) {
  42.                     $this->addFlash(
  43.                         'password_error''Your password contains unsecure characteristics (dates, patterns, or popular phrases), please choose another password.'
  44.                     );
  45.                     return $this->render('registration/register.html.twig', [
  46.                         'registrationForm' => $form->createView(),
  47.                     ]);
  48.                 }
  49.             }
  50.             // encode the plain password
  51.             $user->setPassword(
  52.                 $userPasswordHasher->hashPassword(
  53.                     $user,
  54.                     $form->get('password')->getData()
  55.                 )
  56.             );
  57.             $user->setRoles(['ROLE_TRADER']);
  58.             $defaultTheme $themeRepository->findOneBy(['name' => 'Night Trader']);
  59.             $defaultCurrency $currencyRepository->findOneBy(['value' => 'GBP']);
  60.             $user->setAssociatedTheme($defaultTheme);
  61.             $user->setSelectedCurrency($defaultCurrency);
  62.             $entityManager->persist($user);
  63.             $entityManager->flush();
  64.             // Send email notification to admin
  65.             $this->mailerService->sendRegistrationMailToAdmin('justin.clapham@consort1.com''New User Registration');
  66.             // generate a signed url and email it to the user
  67.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  68.                 (new TemplatedEmail())
  69.                     ->from(new Address('trading@sdsrepo.io''Consort1 - SDSrepo.io'))
  70.                     ->to($user->getEmail())
  71.                     ->subject('Please Confirm your Email')
  72.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  73.                     ->context([
  74.                         'imgUrl' => $logoUrl,
  75.                     ])
  76.             );
  77.             // do anything else you need here, like send an email
  78.             return $this->redirectToRoute('app_verify_email_request', [
  79.                 'userId' => $user->getId(),
  80.             ]);
  81.         }
  82.         return $this->render('registration/register.html.twig', [
  83.             'registrationForm' => $form->createView(),
  84.         ]);
  85.     }
  86.     #[Route('/counterparty/register'name'counterparty_register')]
  87.     public function counterpartyRegistration(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManagerThemeRepository $themeRepositoryCurrencyRepository $currencyRepository): Response
  88.     {
  89.         $preventedPasswords array_merge(self::PREVENTED_PASSWORDSrange(19002050));
  90.         if ($this->getUser()) {
  91.             return $this->redirectToRoute('counterparty_login');
  92.         }
  93.         $logoUrl $request->getScheme().'://'.$request->getHttpHost().$request->getBasePath().'/assets/img/Consort/Consort1-email.png';
  94.         $user = new User();
  95.         $form $this->createForm(RegistrationFormType::class, $user);
  96.         $form->handleRequest($request);
  97.         // Prevent a list of passwords patterns
  98.         if ($form->isSubmitted() && $form->isValid()) {
  99.             foreach ($preventedPasswords as $pattern) {
  100.                 if (str_contains($form->get('password')->getData(), $pattern)) {
  101.                     $this->addFlash(
  102.                         'password_error''Your password contains unsecure characteristics (dates, patterns, or popular phrases), please choose another password.'
  103.                     );
  104.                     return $this->render('registration/register.html.twig', [
  105.                         'registrationForm' => $form->createView(),
  106.                     ]);
  107.                 }
  108.             }
  109.             // encode the plain password
  110.             $user->setPassword(
  111.                 $userPasswordHasher->hashPassword(
  112.                     $user,
  113.                     $form->get('password')->getData()
  114.                 )
  115.             );
  116.             $user->setRoles(['ROLE_COUNTERPARTY']);
  117.             $defaultTheme $themeRepository->findOneBy(['name' => 'Night Trader']);
  118.             $defaultCurrency $currencyRepository->findOneBy(['value' => 'GBP']);
  119.             $user->setAssociatedTheme($defaultTheme);
  120.             $user->setSelectedCurrency($defaultCurrency);
  121.             $entityManager->persist($user);
  122.             $entityManager->flush();
  123.             // Send email notification to admin
  124.             $this->mailerService->sendRegistrationMailToAdmin('justin.clapham@consort1.com''New Counterparty Registration');
  125.             // generate a signed url and email it to the user
  126.             $this->emailVerifier->sendEmailConfirmation('counterparty_verify_email'$user,
  127.                 (new TemplatedEmail())
  128.                     ->from(new Address('trading@sdsrepo.io''Consort1 - SDSrepo.io'))
  129.                     ->to($user->getEmail())
  130.                     ->subject('Please Confirm your Email')
  131.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  132.                     ->context([
  133.                         'imgUrl' => $logoUrl,
  134.                     ])
  135.             );
  136.             // do anything else you need here, like send an email
  137.             return $this->redirectToRoute('counterparty_verify_email_request', [
  138.                 'userId' => $user->getId(),
  139.             ]);
  140.         }
  141.         return $this->render('registration/register.html.twig', [
  142.             'registrationForm' => $form->createView(),
  143.         ]);
  144.     }
  145.     #[Route('/user/verify/email'name'app_verify_email')]
  146.     public function verifyUserEmail(Request $requestTranslatorInterface $translatorUserRepository $userRepository): Response
  147.     {
  148.         $id $request->get('id');
  149.         if (null === $id) {
  150.             return $this->redirectToRoute('app_register');
  151.         }
  152.         $user $userRepository->find($id);
  153.         if (null === $user) {
  154.             return $this->redirectToRoute('app_register');
  155.         }
  156.         // validate email confirmation link, sets User::isVerified=true and persists
  157.         try {
  158.             $this->emailVerifier->handleEmailConfirmation($request$user);
  159.         } catch (VerifyEmailExceptionInterface $exception) {
  160.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  161.             return $this->redirectToRoute('app_register');
  162.         }
  163.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  164.         $this->addFlash('success''Congratulations! Your email address has just been verified.');
  165.         return $this->redirectToRoute('app_login');
  166.     }
  167.     #[Route('/counterparty/verify/email'name'counterparty_verify_email')]
  168.     public function verifyCounterpartyEmail(Request $requestTranslatorInterface $translatorUserRepository $userRepository): Response
  169.     {
  170.         $id $request->get('id');
  171.         if (null === $id) {
  172.             return $this->redirectToRoute('counterparty_register');
  173.         }
  174.         $user $userRepository->find($id);
  175.         if (null === $user) {
  176.             return $this->redirectToRoute('counterparty_register');
  177.         }
  178.         // validate email confirmation link, sets User::isVerified=true and persists
  179.         try {
  180.             $this->emailVerifier->handleEmailConfirmation($request$user);
  181.         } catch (VerifyEmailExceptionInterface $exception) {
  182.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  183.             return $this->redirectToRoute('counterparty_register');
  184.         }
  185.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  186.         $this->addFlash('success''Congratulations! Your email address has just been verified.');
  187.         return $this->redirectToRoute('app_login');
  188.     }
  189.     #[Route('/user/verify/email/{userId}/request'name'app_verify_email_request')]
  190.     public function verifyUserEmailRequest(int $userIdUserRepository $userRepository): Response
  191.     {
  192.         return $this->render('email_verification/check_email.html.twig', [
  193.             'user_email' => $userRepository->find($userId)->getEmail(),
  194.         ]);
  195.     }
  196.     #[Route('/counterparty/verify/email/{userId}/request'name'counterparty_verify_email_request')]
  197.     public function verifyCounterpartyEmailRequest(int $userIdUserRepository $userRepository): Response
  198.     {
  199.         return $this->render('email_verification/check_email.html.twig', [
  200.             'user_email' => $userRepository->find($userId)->getEmail(),
  201.         ]);
  202.     }
  203. }