src/Controller/RegistrationController.php line 65

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2019-2022, MND Next GmbH - www.mndnext.de
  4.  */
  5. namespace App\Controller;
  6. use App\Entity\Company;
  7. use App\Entity\Employee;
  8. use App\Entity\User;
  9. use App\Form\EmployeeType;
  10. use App\Services\MailService;
  11. use FOS\UserBundle\Model\UserManagerInterface;
  12. use FOS\UserBundle\Util\TokenGeneratorInterface;
  13. use Symfony\Component\Form\FormError;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use FOS\UserBundle\Model\UserInterface;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use FOS\UserBundle\Event\FilterUserResponseEvent;
  22. use FOS\UserBundle\Event\FormEvent;
  23. use FOS\UserBundle\Event\GetResponseUserEvent;
  24. use FOS\UserBundle\Form\Factory\FactoryInterface;
  25. use FOS\UserBundle\FOSUserEvents;
  26. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  29. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  30. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  31. use Symfony\Contracts\Translation\TranslatorInterface;
  32. /**
  33.  * Listener responsible to change the redirection at the end of the password resetting
  34.  */
  35. class RegistrationController extends AbstractController
  36. {
  37.     /** @var UserManagerInterface */
  38.     private $userManager;
  39.     /** @var TokenGeneratorInterface */
  40.     private $tokenGenerator;
  41.     /** @var TranslatorInterface */
  42.     private $translator;
  43.     private $parent;
  44.     private $eventDispatcher;
  45.     private $formFactory;
  46.     public function __construct(\FOS\UserBundle\Controller\RegistrationController $controllerEventDispatcherInterface $eventDispatcherFactoryInterface $formFactoryUserManagerInterface $userManagerTokenGeneratorInterface $tokenGeneratorTranslatorInterface $translatorMailService $mailer)
  47.     {
  48.         $this->parent $controller;
  49.         $this->eventDispatcher $eventDispatcher;
  50.         $this->formFactory $formFactory;
  51.         $this->userManager $userManager;
  52.         $this->tokenGenerator $tokenGenerator;
  53.         $this->translator $translator;
  54.         $this->mailer $mailer;
  55.     }
  56.     public function registerAction(Request $request)
  57.     {
  58.         $employee = new Employee();
  59.         $employee->setEnabled(true);
  60.         $event = new GetResponseUserEvent($employee$request);
  61.         $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_INITIALIZE);
  62.         if (null !== $event->getResponse()) {
  63.             return $event->getResponse();
  64.         }
  65.         $form $this->formFactory->createForm();
  66.         $form->setData($employee);
  67.         $form->handleRequest($request);
  68.         if ($form->isSubmitted()) {
  69.             if ($form->isValid()) {
  70.                 $formData $request->get('fos_user_registration_form');
  71.                 if (!empty($formData['email_repeat'])) {
  72.                     // honey pot is filled
  73.                     return new RedirectResponse('/');
  74.                 }
  75.                 if (!empty($formData['agb'])) {
  76.                     $duplicated $this->userManager->findUserByEmail($formData['email']);
  77.                     if (!$duplicated) {
  78.                         $event = new FormEvent($form$request);
  79.                         $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  80.                         $company = new Company();
  81.                         $company->setName($formData['company_name']);
  82.                         $company->setDepartment($formData['company_department']);
  83.                         $company->setStreet($formData['company_street']);
  84.                         $company->setStreetNr($formData['company_streetnr']);
  85.                         $company->setPostcode($formData['company_postcode']);
  86.                         $company->setCity($formData['company_city']);
  87.                         $company->setCountry('Deutschland');
  88.                         $company->setPhone($formData['company_phone']);
  89.                         $em $this->getDoctrine()->getManager();
  90.                         $em->persist($company);
  91.                         $em->flush();
  92.                         $employee->addRole('ROLE_CONTACT');
  93.                         $employee->setCompany($company);
  94.                         $this->userManager->updateUser($employee);
  95.                         $this->mailer->noticeRegisterCustomer($employee);
  96.                         if (null === $response $event->getResponse()) {
  97.                             $url $this->generateUrl('fos_user_registration_confirmed');
  98.                             $response = new RedirectResponse($url);
  99.                         }
  100.                         $this->eventDispatcher->dispatch(new FilterUserResponseEvent($employee$request$response), FOSUserEvents::REGISTRATION_COMPLETED);
  101.                         return $response;
  102.                     } else {
  103.                         $form->get('email')->addError(new FormError($this->translator->trans('registration.flash.duplicated_email', ['%email%' => $formData['email']], 'FOSUserBundle'), 'email'));
  104.                     }
  105.                 } else {
  106.                     $form->get('agb')->addError(new FormError($this->translator->trans('registration.flash.agb', [], 'FOSUserBundle')));
  107.                 }
  108.             }
  109.             $event = new FormEvent($form$request);
  110.             $this->eventDispatcher->dispatch($eventFOSUserEvents::REGISTRATION_FAILURE);
  111.             if (null !== $response $event->getResponse()) {
  112.                 return $response;
  113.             }
  114.         }
  115.         return $this->render('@FOSUser/Registration/register.html.twig', array(
  116.             'form' => $form->createView(),
  117.         ));
  118.     }
  119.     public function checkEmailAction(Request $request)
  120.     {
  121.         return $this->parent->checkEmailAction($request);
  122.     }
  123.     public function confirmAction(Request $request$token)
  124.     {
  125.         return $this->parent->confirmAction($request$token);
  126.     }
  127.     public function confirmedAction(Request $request)
  128.     {
  129.         return $this->parent->confirmedAction($request);
  130.     }
  131. }