vendor/kobizo/core-bundle/src/Controller/FeRegistrationController.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kobizo\Bundle\CoreBundle\Controller;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Kobizo\Bundle\CoreBundle\Helper\SiteParamsHelper;
  6. use Kobizo\Component\Attributes\DefaultRolesAttribute;
  7. use Kobizo\Component\Configuration\Backend\AdminUrlConfig;
  8. use Kobizo\Component\Configuration\MailTemplate\RegistrationEmailConfirmMailTemplateConfig;
  9. use Kobizo\Component\Entity\User;
  10. use Kobizo\Bundle\CoreBundle\Exception\NoRoleExistException;
  11. use Kobizo\Bundle\CoreBundle\Form\RegistrationFormType;
  12. use Kobizo\Component\Helper\TokenGeneratorInterface;
  13. use Kobizo\Bundle\CoreBundle\Repository\RoleRepository;
  14. use Kobizo\Bundle\CoreBundle\Repository\UserRepository;
  15. use Kobizo\Bundle\CoreBundle\Security\LoginFormAuthenticator;
  16. use Kobizo\Component\Provider\MailTemplateProvider;
  17. use Kobizo\Component\Resources\AccessControl\DashboardResource;
  18. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Mailer\MailerInterface;
  22. use Symfony\Component\Mime\Address;
  23. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  26. use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
  27. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. use Kobizo\Component\Configuration\Google\GoogleCaptchaConfig;
  30. use Kobizo\Component\Configuration\Google\GoogleCaptchaPublicKeyConfig;
  31. use Kobizo\Bundle\CoreBundle\Exception\LoginCaptchaValidationException;
  32. use Kobizo\Component\Helper\GoogleReCaptchaV3Helper;
  33. /**
  34.  * @Route("/registration")
  35.  */
  36. class FeRegistrationController extends KobizoFeAbstractController
  37. {
  38.     /**
  39.      * @Route("/register", name="app_register")
  40.      */
  41.     public function register(
  42.         Request $request,
  43.         UserPasswordHasherInterface $passwordHasher,
  44.         MailerInterface $mailer,
  45.         RoleRepository $roleRepository,
  46.         TokenGeneratorInterface $tokenGenerator,
  47.         SiteParamsHelper $siteParamsHelper,
  48.         EntityManagerInterface $entityManager,
  49.         TranslatorInterface $translator,
  50.         MailTemplateProvider $mailTemplateProvider,
  51.         RegistrationEmailConfirmMailTemplateConfig $registrationEmailConfirmTemplateConfig,
  52.         GoogleCaptchaConfig $googleCaptchaConfig,
  53.         GoogleCaptchaPublicKeyConfig $googleCaptchaPublicKeyConfig,
  54.         GoogleReCaptchaV3Helper $googleReCaptchaV3Helper
  55.     ): Response {
  56.         //if user logged in then redirect to dashboard page
  57.         if ($this->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)) {
  58.             return $this->render('@KobizoCore/frontend/user-already-logged-in.twig');
  59.         }
  60.         $user = new User();
  61.         $form $this->createForm(RegistrationFormType::class, $user);
  62.         $form->handleRequest($request);
  63.         if ($form->isSubmitted() && $form->isValid()) {
  64.             // Execute google captcha validation if enabled
  65.             if ($googleCaptchaConfig->isGoogleCaptchaEnabled()) {
  66.                 if (!$googleReCaptchaV3Helper->isValid($form->get('recaptcha_token')->getData())) {
  67.                     throw new LoginCaptchaValidationException($translator->trans('Invalid or low score Login Captcha Checking.'));
  68.                 }
  69.             }
  70.             // encode the plain password
  71.             $user->setPassword(
  72.                 $passwordHasher->hashPassword(
  73.                     $user,
  74.                     $form->get('plainPassword')->getData()
  75.                 )
  76.             );
  77.             $customerRole $roleRepository->findOneByCode(DefaultRolesAttribute::CLIENT);
  78.             if (is_null($customerRole)) {
  79.                 throw new NoRoleExistException();
  80.             }
  81.             $user->addRole($customerRole);
  82.             $user->setNicename($user->getNicename());
  83.             $user->setFirstname($user->getNicename());
  84.             $user->setLastname($user->getNicename());
  85.             $user->setDisplayName($user->getEmail());
  86.             $user->setConfirmationToken($tokenGenerator->generateConfirmationToken($user->getEmail()));
  87.             $entityManager->persist($user);
  88.             $entityManager->flush();
  89.             $url $this->container->get('router')->generate(
  90.                 'app_registration_confirm',
  91.                 [
  92.                     'confirmationToken' => $user->getConfirmationToken(),
  93.                 ],
  94.                 UrlGeneratorInterface::NETWORK_PATH
  95.             );
  96.             $options = [
  97.                 'confirmationUrl' => $url,
  98.                 'siteName' => $siteParamsHelper->getSiteName(),
  99.                 'siteUrl' => $siteParamsHelper->getSiteUrl(),
  100.                 'address' => $siteParamsHelper->getSiteAddress(),
  101.                 MailTemplateProvider::CODE => $registrationEmailConfirmTemplateConfig->getValue()
  102.             ];
  103.             $email = (new TemplatedEmail())
  104.                 ->from(new Address($siteParamsHelper->getNoReplyEmail(), $siteParamsHelper->getSiteName()))
  105.                 ->to($user->getEmail())
  106.                 ->subject($translator->trans('Activate your account'))
  107.                 ->html($mailTemplateProvider->get($options));
  108.             $mailer->send($email);
  109.             return $this->render('@KobizoCore/frontend/register-email-sent.twig', [
  110.                 'userConfirmForm' => $form->createView(),
  111.                 'email' => $user->getEmail(),
  112.             ]);
  113.         }
  114.         $googleCaptchaEnable $googleCaptchaConfig->isGoogleCaptchaEnabled();
  115.         return $this->render('@KobizoCore/frontend/register.twig', [
  116.             'registrationForm' => $form->createView(),
  117.             'isGoogleCaptchaEnabled' => $googleCaptchaEnable,
  118.             'captchaPublicKey' => $googleCaptchaEnable $googleCaptchaPublicKeyConfig->getValue() : "",
  119.         ]);
  120.     }
  121.     /**
  122.      * @Route("/confirm/{confirmationToken}", name="app_registration_confirm")
  123.      */
  124.     public function confirmRegistration(
  125.         Request $request,
  126.         UserAuthenticatorInterface $userAuthenticator,
  127.         LoginFormAuthenticator $authenticator,
  128.         UserRepository $repository,
  129.         EntityManagerInterface $entityManager,
  130.         AdminUrlConfig $adminUrlConfig,
  131.         string $confirmationToken
  132.     ) {
  133.         $user $repository->findOneByConfirmationToken($confirmationToken);
  134.         if (null === $user) {
  135.             return $this->redirectToRoute('app_login', [], 302true);
  136.         }
  137.         $user->setConfirmationToken(null);
  138.         $user->setActive(true);
  139.         $entityManager->persist($user);
  140.         $entityManager->flush();
  141.         $userAuthenticator->authenticateUser(
  142.             $user,
  143.             $authenticator,
  144.             $request,
  145.         );
  146.         if ($this->isGranted(DashboardResource::DASHBOARD_CRM) && $user->isAdmin()) {
  147.             return $this->redirectToRoute('dashboard_analytics', ['admin' => $adminUrlConfig->getValue()]);
  148.         }
  149.         if ($this->isGranted(DefaultRolesAttribute::CLIENT)) {
  150.             return $this->redirectToRoute('customer_account');
  151.         }
  152.         return $this->redirectToRoute('home');
  153.     }
  154. }