<?phpnamespace App\Controller\Vitrine;use App\Entity\Core\Mails;use App\Entity\Core\Users;use App\Entity\Cvs\Shares;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\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\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;/** * Inscription */class RegisterController extends AbstractController{ private $passwordEncoder; private $ms; private $em; public function __construct(UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $em, \App\Services\Mails $ms ) { $this->passwordEncoder = $passwordEncoder; $this->em = $em; $this->ms = $ms; } /** * Page d'inscription * @param Request $request * @param \App\Services\Mails $ms * @return Response */ public function register(Request $request): Response { $session = $request->getSession(); $themeSelection = $_ENV['THEME_BLOG']; $locale = $request->getLocale(); $registerOnline = $_ENV['APPLICATION_REGISTER']; if($registerOnline == "offline") { return $this->redirectToRoute('homepage'); } $user = $this->getUser(); if($user != null) { return $this->redirectToRoute('homepage'); } $sharingCode = $session->get("sharing-code"); $shareInvitation = null; if(!empty($sharingCode)) { $shareInvitation = $this->em->getRepository(Shares::class)->findOneBy(['code' => $sharingCode]); } $page = $this->em->getRepository(Pages::class)->findOneBy(['locale' => $locale, 'name' => 'register_nopassword']); $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(); $newUser = new Users(); $newUser->setPremium(false); $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->setVerification(false); $newUser->setFirst(false); $newUser->setEnabled(true); $newUser->setPassword($this->passwordEncoder->encodePassword($newUser, $newUser->getPassword())); $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(['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', 'L\'adresse mail est déjà dans notre base de données'); return $this->redirectToRoute('app_register'); } $response = $this->render('vitrine/'.$themeSelection.'/register.html.twig', [ 'page' => $page, 'blocks' => $blocks, 'form' => $form->createView(), 'invitation' => $shareInvitation ]); //$response->setSharedMaxAge(3600); return $response; }}