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

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