<?phpnamespace App\Controller\ThemesWebsite\Login;use App\Entity\Core\Mails;use App\Entity\Core\Users;use App\Entity\Pages\Pages;use App\Entity\Pages\PagesHasBlocks;use App\Form\Core\UsersType;use Doctrine\ORM\EntityManagerInterface;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\HttpFoundation\Cookie;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;class SecurityController extends AbstractController{ private $passwordEncoder; private $authenticationUtils; private $ms; public function __construct(UserPasswordEncoderInterface $passwordEncoder, AuthenticationUtils $authenticationUtils, \App\Services\Mails $ms, EntityManagerInterface $em ) { $this->passwordEncoder = $passwordEncoder; $this->authenticationUtils = $authenticationUtils; $this->ms = $ms; $this->em = $em; } /** * Page de connexion * @param Request $request * @param AuthenticationUtils $authenticationUtils * @return Response */ public function login(Request $request): Response { $user = $this->getUser(); if($user != null) { return $this->redirectToRoute('homepage'); } $themeLogin = $_ENV['THEME_LOGIN']; $error = $this->authenticationUtils->getLastAuthenticationError(); $lastUsername = $this->authenticationUtils->getLastUsername(); if($themeLogin == "BLOG") { $themeSelection = $_ENV['THEME_BLOG']; $page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'login']); $blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']); $page->setViews((int)$page->getViews() + 1); $this->em->persist($page); $this->em->flush(); return $this->render('vitrine/'.$themeSelection.'/login.html.twig',[ 'last_username' => $lastUsername, 'error' => $error, 'page' => $page, 'blocks' => $blocks ]); } return $this->render('security/login/login.html.twig',[ 'last_username' => $lastUsername, 'error' => $error ]); } /** * Inscription * @param Request $request * @param \App\Services\Mails $ms * @return Response */ public function register(Request $request): Response { $session = $request->getSession(); $user = $this->getUser(); if($user != null) { return $this->redirectToRoute('homepage'); } $typeWebsite = $_ENV['TYPE_WEBSITE']; $newUser = new Users(); $form = $this->createForm(UsersType::class, $newUser); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $request->request->all(); $data = $data['users']; if($data['password']['first'] != $data['password']['second']) { $session->getFlashBag()->add('danger', 'Votre second mot de passe n\'est pas identique'); return $this->redirectToRoute('app_register'); } $verificationUser = $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]); if ($verificationUser == null) { $newPassword = $newUser->getPassword(); $newUser->setFirst(false); $newUser->setEnabled(true); $newUser->setPassword($this->passwordEncoder->encodePassword($user,$newPassword)); $newUser->setRoles(['ROLE_USER']); $newUser->setUpdatedAt(new \DateTime("now")); $newUser->setCreatedAt(new \DateTime("now")); $this->em->persist($newUser); $this->em->flush(); // Envoyer un mail d'inscription à l'utilisateur $templateEntity = $this->em->getRepository(Mails::class)->findOneBy(['typeWebsite' => $typeWebsite, 'name' => "register"]); $this->ms->sendUserPassword($newUser,$newPassword,$templateEntity); $session->getFlashBag()->add('success', 'Merci de votre inscription'); return $this->redirectToRoute('app_login'); } $session->getFlashBag()->add('danger', 'Vous êtes déjà inscris dans notre base de données'); return $this->redirectToRoute('app_register'); } return $this->render('security/login/register.html.twig',[ 'form' => $form->createView() ]); } public function connectCheck(Request $request): Response { if ($this->getUser()) { return $this->redirectToRoute('homepage'); } return $this->redirectToRoute('app_login'); } /** * Déconnexion */ public function logout() { throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall'); }}