src/Controller/SecurityController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationType;
  5. use App\Notification\EmailNotification;
  6. use App\Security\LoginFormAuthenticator;
  7. use App\Service\NotificationHelper;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. class SecurityController extends AbstractController
  17. {
  18. /**
  19. * @Route("/login", name="app_login")
  20. */
  21. public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
  22. {
  23. $targetUrl = ($request->getSession()->has('_security.main.target_path'))?$request->getSession()->get('_security.main.target_path'):$request->headers->get('referer');
  24. if($this->getUser() !== null) {
  25. return $this->redirectToRoute('user_dashboard');
  26. }
  27. // get the login error if there is one
  28. $error = $authenticationUtils->getLastAuthenticationError();
  29. // last username entered by the user
  30. $lastUsername = $authenticationUtils->getLastUsername();
  31. return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'referer'=> $targetUrl]);
  32. }
  33. /**
  34. * @Route("/logout", name="app_logout")
  35. */
  36. public function logout()
  37. {
  38. }
  39. /**
  40. * @Route("/register", name="app_register")
  41. */
  42. public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator, EmailNotification $emailNotification, NotificationHelper $notificationHelper, EntityManagerInterface $entityManager)
  43. {
  44. $targetUrl = $request->query->get('referer', null);
  45. $request->getSession()->set('targetUrl', $targetUrl);
  46. $form = $this->createForm(RegistrationType::class, null, ['referer' => $targetUrl]);
  47. $form->handleRequest($request);
  48. if ($form->isSubmitted() && $form->isValid()) {
  49. /** @var User $user */
  50. $user = $form->getData();
  51. $referer = $form->get('referer')->getData();
  52. $encodedPassword = $passwordEncoder->encodePassword($user, $user->getPassword());
  53. $user->setPassword($encodedPassword);
  54. if (true === $form['agreedTerms']->getData()){
  55. $user->agreedTerms();
  56. }
  57. if($request->getLocale() == 'fr'){
  58. $user->setPole('fr');
  59. }else{
  60. $user->setPole('en');
  61. }
  62. $entityManager->persist($user);
  63. $entityManager->flush();
  64. $this->addFlash('success', 'Création de compte effectuée avec success');
  65. $notificationHelper->addNotificationAtRegistration($user);
  66. if($request->getLocale() == 'fr'){
  67. $subject = 'Votre compte a été créé';
  68. }else{
  69. $subject = 'Your account has been created';
  70. }
  71. $emailNotification->registrationUserAccount($user, $subject);
  72. $autoAuth = $guardHandler->authenticateUserAndHandleSuccess(
  73. $user,
  74. $request,
  75. $formAuthenticator,
  76. 'main'
  77. );
  78. if($referer !== "" && $referer!== null && filter_var($referer, FILTER_VALIDATE_URL) !== false) {
  79. return $this->redirect($referer);
  80. } else {
  81. return $this->redirectToRoute('user_dashboard');
  82. }
  83. }
  84. return $this->render('security/register.html.twig', [
  85. 'form' => $form->createView(),
  86. ]);
  87. }
  88. }