src/UserBundle/Controller/RegistrationController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\UserBundle\Controller;
  3. use App\BaseBundle\Controller\AbstractController;
  4. use App\UserBundle\Entity\User;
  5. use App\UserBundle\Event\RegistrationEvent;
  6. use App\UserBundle\Form\RegistrationType;
  7. use App\UserBundle\Security\CustomAuthenticator;
  8. use App\UserBundle\UserEvents;
  9. use PN\ServiceBundle\Utils\General;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  16. use Symfony\Component\Security\Http\FirewallMapInterface;
  17. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class RegistrationController extends AbstractController
  20. {
  21. use TargetPathTrait;
  22. /**
  23. * @Route("/register", name="app_user_registration")
  24. */
  25. public function register(
  26. Request $request,
  27. EventDispatcherInterface $eventDispatcher,
  28. UserAuthenticatorInterface $userAuthenticator,
  29. CustomAuthenticator $authenticator,
  30. FirewallMapInterface $firewallMap,
  31. TranslatorInterface $translator
  32. ): Response
  33. {
  34. //if user is already logged in just redirect him to home and tell him that he needs to log out first
  35. if ($this->getUser()) {
  36. $this->addFlash('warning',
  37. 'You are already logged in as a user, please logout if you want to create another account with different credentials');
  38. return $this->redirectToRoute('fe_home');
  39. }
  40. $user = new User();
  41. $form = $this->createForm(RegistrationType::class, $user);
  42. $form->handleRequest($request);
  43. if ($form->isSubmitted() && $form->isValid()) {
  44. if (!$form->has("plain")) {
  45. $user->setPlainPassword(General::generateRandString());
  46. }
  47. if ($request->getSession()->has("AOuthData")) {
  48. $AOuthData = $request->getSession()->get("AOuthData");
  49. $methodName = "set" . ucfirst($AOuthData['provider']) . "Id";
  50. $user->$methodName($AOuthData['id']);
  51. }
  52. // persisting and adding the user to the database
  53. $this->em()->persist($user);
  54. $this->em()->flush();
  55. $this->addFlash("success", "Signed up successfully");
  56. $event = new RegistrationEvent($user, $request);
  57. $eventDispatcher->dispatch($event, UserEvents::REGISTRATION_COMPLETED);
  58. $userAuthenticator->authenticateUser($user, $authenticator, $request);
  59. return $this->onAuthenticationSuccess($request, $firewallMap);
  60. }
  61. return $this->render('user/registration/index.html.twig', [
  62. 'form' => $form->createView(),
  63. "breadcrumbs" => $this->breadcrumbs($translator)
  64. ]);
  65. }
  66. private function onAuthenticationSuccess(Request $request, FirewallMapInterface $firewallMap): ?Response
  67. {
  68. $firewallConfig = $firewallMap->getFirewallConfig($request);
  69. if ($targetPath = $this->getTargetPath($request->getSession(), $firewallConfig->getName())) {
  70. return new RedirectResponse($targetPath);
  71. }
  72. return new RedirectResponse($this->generateUrl('fe_home'));
  73. }
  74. private function breadcrumbs(TranslatorInterface $translator): array
  75. {
  76. return [
  77. [
  78. "title" => $translator->trans("home_txt"),
  79. "url" => $this->generateUrl("fe_home"),
  80. ],
  81. [
  82. "title" => $translator->trans("signup_txt"),
  83. "url" => null,
  84. ],
  85. ];
  86. }
  87. }