<?php
/**
* Copyright (c) 2019-2022, MND Next GmbH - www.mndnext.de
*/
namespace App\Controller;
use App\Entity\Company;
use App\Entity\Employee;
use App\Entity\User;
use App\Form\EmployeeType;
use App\Services\MailService;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Routing\Annotation\Route;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Listener responsible to change the redirection at the end of the password resetting
*/
class RegistrationController extends AbstractController
{
/** @var UserManagerInterface */
private $userManager;
/** @var TokenGeneratorInterface */
private $tokenGenerator;
/** @var TranslatorInterface */
private $translator;
private $parent;
private $eventDispatcher;
private $formFactory;
public function __construct(\FOS\UserBundle\Controller\RegistrationController $controller, EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager, TokenGeneratorInterface $tokenGenerator, TranslatorInterface $translator, MailService $mailer)
{
$this->parent = $controller;
$this->eventDispatcher = $eventDispatcher;
$this->formFactory = $formFactory;
$this->userManager = $userManager;
$this->tokenGenerator = $tokenGenerator;
$this->translator = $translator;
$this->mailer = $mailer;
}
public function registerAction(Request $request)
{
$employee = new Employee();
$employee->setEnabled(true);
$event = new GetResponseUserEvent($employee, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::REGISTRATION_INITIALIZE);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$form = $this->formFactory->createForm();
$form->setData($employee);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$formData = $request->get('fos_user_registration_form');
if (!empty($formData['email_repeat'])) {
// honey pot is filled
return new RedirectResponse('/');
}
if (!empty($formData['agb'])) {
$duplicated = $this->userManager->findUserByEmail($formData['email']);
if (!$duplicated) {
$event = new FormEvent($form, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::REGISTRATION_SUCCESS);
$company = new Company();
$company->setName($formData['company_name']);
$company->setDepartment($formData['company_department']);
$company->setStreet($formData['company_street']);
$company->setStreetNr($formData['company_streetnr']);
$company->setPostcode($formData['company_postcode']);
$company->setCity($formData['company_city']);
$company->setCountry('Deutschland');
$company->setPhone($formData['company_phone']);
$em = $this->getDoctrine()->getManager();
$em->persist($company);
$em->flush();
$employee->addRole('ROLE_CONTACT');
$employee->setCompany($company);
$this->userManager->updateUser($employee);
$this->mailer->noticeRegisterCustomer($employee);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_registration_confirmed');
$response = new RedirectResponse($url);
}
$this->eventDispatcher->dispatch(new FilterUserResponseEvent($employee, $request, $response), FOSUserEvents::REGISTRATION_COMPLETED);
return $response;
} else {
$form->get('email')->addError(new FormError($this->translator->trans('registration.flash.duplicated_email', ['%email%' => $formData['email']], 'FOSUserBundle'), 'email'));
}
} else {
$form->get('agb')->addError(new FormError($this->translator->trans('registration.flash.agb', [], 'FOSUserBundle')));
}
}
$event = new FormEvent($form, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::REGISTRATION_FAILURE);
if (null !== $response = $event->getResponse()) {
return $response;
}
}
return $this->render('@FOSUser/Registration/register.html.twig', array(
'form' => $form->createView(),
));
}
public function checkEmailAction(Request $request)
{
return $this->parent->checkEmailAction($request);
}
public function confirmAction(Request $request, $token)
{
return $this->parent->confirmAction($request, $token);
}
public function confirmedAction(Request $request)
{
return $this->parent->confirmedAction($request);
}
}