src/Controller/ThemesWebsite/Login/SecurityController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ThemesWebsite\Login;
  3. use App\Entity\Core\Mails;
  4. use App\Entity\Core\Users;
  5. use App\Entity\Pages\Pages;
  6. use App\Entity\Pages\PagesHasBlocks;
  7. use App\Form\Core\UsersType;
  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\Http\Authentication\AuthenticationUtils;
  15. class SecurityController extends AbstractController
  16. {
  17. private $passwordEncoder;
  18. private $authenticationUtils;
  19. private $ms;
  20. public function __construct(UserPasswordEncoderInterface $passwordEncoder,
  21. AuthenticationUtils $authenticationUtils,
  22. \App\Services\Mails $ms,
  23. EntityManagerInterface $em
  24. ) {
  25. $this->passwordEncoder = $passwordEncoder;
  26. $this->authenticationUtils = $authenticationUtils;
  27. $this->ms = $ms;
  28. $this->em = $em;
  29. }
  30. /**
  31. * Page de connexion
  32. * @param Request $request
  33. * @param AuthenticationUtils $authenticationUtils
  34. * @return Response
  35. */
  36. public function login(Request $request): Response
  37. {
  38. $user = $this->getUser();
  39. if($user != null) {
  40. return $this->redirectToRoute('homepage');
  41. }
  42. $themeLogin = $_ENV['THEME_LOGIN'];
  43. $error = $this->authenticationUtils->getLastAuthenticationError();
  44. $lastUsername = $this->authenticationUtils->getLastUsername();
  45. if($themeLogin == "BLOG") {
  46. $themeSelection = $_ENV['THEME_BLOG'];
  47. $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'login']);
  48. $blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
  49. $page->setViews((int)$page->getViews() + 1);
  50. $this->em->persist($page);
  51. $this->em->flush();
  52. return $this->render('vitrine/'.$themeSelection.'/login.html.twig',[
  53. 'last_username' => $lastUsername,
  54. 'error' => $error,
  55. 'page' => $page,
  56. 'blocks' => $blocks
  57. ]);
  58. }
  59. return $this->render('security/login/login.html.twig',[
  60. 'last_username' => $lastUsername,
  61. 'error' => $error
  62. ]);
  63. }
  64. /**
  65. * Inscription
  66. * @param Request $request
  67. * @param \App\Services\Mails $ms
  68. * @return Response
  69. */
  70. public function register(Request $request): Response
  71. {
  72. $session = $request->getSession();
  73. $user = $this->getUser();
  74. if($user != null) {
  75. return $this->redirectToRoute('homepage');
  76. }
  77. $typeWebsite = $_ENV['TYPE_WEBSITE'];
  78. $newUser = new Users();
  79. $form = $this->createForm(UsersType::class, $newUser);
  80. $form->handleRequest($request);
  81. if ($form->isSubmitted() && $form->isValid()) {
  82. $data = $request->request->all();
  83. $data = $data['users'];
  84. if($data['password']['first'] != $data['password']['second']) {
  85. $session->getFlashBag()->add('danger', 'Votre second mot de passe n\'est pas identique');
  86. return $this->redirectToRoute('app_register');
  87. }
  88. $verificationUser = $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]);
  89. if ($verificationUser == null) {
  90. $newPassword = $newUser->getPassword();
  91. $newUser->setFirst(false);
  92. $newUser->setEnabled(true);
  93. $newUser->setPassword($this->passwordEncoder->encodePassword($user,$newPassword));
  94. $newUser->setRoles(['ROLE_USER']);
  95. $newUser->setUpdatedAt(new \DateTime("now"));
  96. $newUser->setCreatedAt(new \DateTime("now"));
  97. $this->em->persist($newUser);
  98. $this->em->flush();
  99. // Envoyer un mail d'inscription à l'utilisateur
  100. $templateEntity = $this->em->getRepository(Mails::class)->findOneBy(['typeWebsite' => $typeWebsite, 'name' => "register"]);
  101. $this->ms->sendUserPassword($newUser,$newPassword,$templateEntity);
  102. $session->getFlashBag()->add('success', 'Merci de votre inscription');
  103. return $this->redirectToRoute('app_login');
  104. }
  105. $session->getFlashBag()->add('danger', 'Vous êtes déjà inscris dans notre base de données');
  106. return $this->redirectToRoute('app_register');
  107. }
  108. return $this->render('security/login/register.html.twig',[
  109. 'form' => $form->createView()
  110. ]);
  111. }
  112. #[Route('/logout', name: 'app_logout', defaults: ['_locale' => 'en'])]
  113. public function logout(): void
  114. {
  115. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  116. }
  117. }