src/Controller/RegistrationController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\LoginFormAuthenticator;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Bridge\Twig\Mime\TemplatedEmail as Email;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19. /**
  20. * @Route("/register", name="app_register")
  21. */
  22. public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, LoginFormAuthenticator $authenticator, MailerInterface $mailer): Response
  23. {
  24. if ($this->isGranted('ROLE_USER')) {
  25. return new RedirectResponse($this->get('router')->generate('app_homepage'));
  26. }
  27. $user = new User();
  28. $form = $this->createForm(RegistrationFormType::class, $user);
  29. $form->handleRequest($request);
  30. if ($form->isSubmitted() && $form->isValid()) {
  31. // encode the plain password
  32. $user->setPassword(
  33. $passwordEncoder->encodePassword(
  34. $user,
  35. $form->get('plainPassword')->getData()
  36. )
  37. );
  38. $entityManager = $this->getDoctrine()->getManager();
  39. $entityManager->persist($user);
  40. $entityManager->flush();
  41. $email = (new Email())
  42. ->from(new Address($this->get('parameter_bag')->get('mailer_from'), $this->get('parameter_bag')->get('mailer_from_name')))
  43. ->to($user->getEmail())
  44. ->subject('Aktywacja konta')
  45. // path of the Twig template to render
  46. ->htmlTemplate('emails/registration.html.twig')
  47. // pass variables (name => value) to the template
  48. ->context([
  49. 'link' => $this->get('router')->generate('app_register_confirm', ['token' => urlencode($user->getPassword())], UrlGeneratorInterface::ABSOLUTE_URL)
  50. ]);
  51. $mailer->send($email);
  52. return new RedirectResponse($this->get('router')->generate('app_register_success'));
  53. }
  54. return $this->render('registration/register.html.twig', [
  55. 'registrationForm' => $form->createView(),
  56. ]);
  57. }
  58. /**
  59. * @Route("/register/confirm/{token}", name="app_register_confirm", requirements={"token"=".+"})
  60. */
  61. public function confirm(Request $request, TranslatorInterface $translator, $token): RedirectResponse
  62. {
  63. $entityManager = $this->getDoctrine()->getManager();
  64. $user = $entityManager->getRepository(User::class)
  65. ->findOneBy(['password' => urldecode($token)]);
  66. if ($user instanceof User && !$user->isEnabled()) {
  67. $user->setEnabled(1);
  68. $entityManager->flush();
  69. $this->addFlash(
  70. 'success',
  71. 'Dziękujemy za potwierdzenie Twojego adresu e-mail. Teraz możesz się zalogować.'
  72. );
  73. }
  74. return new RedirectResponse($this->get('router')->generate('app_login'));
  75. // login user programaticly
  76. // return $guardHandler->authenticateUserAndHandleSuccess(
  77. // $user,
  78. // $request,
  79. // $authenticator,
  80. // 'main' // firewall name in security.yaml
  81. // );
  82. }
  83. /**
  84. * @Route("/register/success" , name="app_register_success")
  85. */
  86. public function success(MailerInterface $mailer): Response
  87. {
  88. // $email = (new Email())
  89. // ->from(new Address($this->get('parameter_bag')->get('mailer_from'), $this->get('parameter_bag')->get('mailer_from_name')))
  90. // ->to('sakoo22@gmail.com')
  91. // ->subject('Aktywacja konta')
  92. // // path of the Twig template to render
  93. // ->htmlTemplate('emails/registration.html.twig')
  94. // // pass variables (name => value) to the template
  95. // ->context([
  96. // 'link' => 'testtesttest'
  97. // ]);
  98. //
  99. //
  100. // $mailer->send($email);
  101. return $this->render('registration/success.html.twig', [
  102. ]);
  103. }
  104. }