src/Controller/ThemesWebsite/Blog/RegisterController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ThemesWebsite\Blog;
  3. use App\Entity\Core\Mails;
  4. use App\Entity\Core\Users;
  5. use App\Entity\Cvs\Shares;
  6. use App\Entity\Pages\Pages;
  7. use App\Entity\Pages\PagesHasBlocks;
  8. use App\Form\Core\UsersType;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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\Routing\Generator\UrlGeneratorInterface;
  21. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  23. /**
  24.  * Inscription
  25.  */
  26. class RegisterController extends AbstractController
  27. {
  28.     private $passwordEncoder;
  29.     private $ms;
  30.     private $em;
  31.     public function __construct(UserPasswordEncoderInterface $passwordEncoder,
  32.                                 EntityManagerInterface $em,
  33.                                 \App\Services\Mails $ms
  34.     ) {
  35.         $this->passwordEncoder $passwordEncoder;
  36.         $this->em $em;
  37.         $this->ms $ms;
  38.     }
  39.     /**
  40.      * Page d'inscription
  41.      * @param Request $request
  42.      * @param \App\Services\Mails $ms
  43.      * @return Response
  44.      */
  45.     public function register(Request $request): Response
  46.     {
  47.         $session $request->getSession();
  48.         $themeSelection $_ENV['THEME_SELECTION'];
  49.         $registerOnline $_ENV['APPLICATION_REFISTER'];
  50.         if($registerOnline == "offline") {
  51.             return $this->redirectToRoute('homepage');
  52.         }
  53.         $user $this->getUser();
  54.         if($user != null) {
  55.             return $this->redirectToRoute('homepage');
  56.         }
  57.         $sharingCode $session->get("sharing-code");
  58.         $shareInvitation null;
  59.         if(!empty($sharingCode)) {
  60.             $shareInvitation $this->em->getRepository(Shares::class)->findOneBy(['code' => $sharingCode]);
  61.         }
  62.         $page $this->em->getRepository(Pages::class)->findOneBy(['name' => 'register_nopassword']);
  63.         $blocks $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page'type' => 'prod''startPage' => false],['sequence' => 'ASC']);
  64.         $page->setViews((int)$page->getViews() + 1);
  65.         $this->em->persist($page);
  66.         $this->em->flush();
  67.         $newUser = new Users();
  68.         $newUser->setPremium(false);
  69.         $form $this->createForm(UsersType::class, $newUser);
  70.         $form->handleRequest($request);
  71.         if ($form->isSubmitted() && $form->isValid()) {
  72.             $data $request->request->all();
  73.             $data $data['users'];
  74.             if($data['password']['first'] != $data['password']['second']) {
  75.                 $session->getFlashBag()->add('danger''Votre second mot de passe n\'est pas identique');
  76.                 return $this->redirectToRoute('app_register');
  77.             }
  78.             $verificationUser $this->em->getRepository(Users::class)->findOneBy(['email' => $data['email']]);
  79.             if ($verificationUser == null) {
  80.                 $newPassword $newUser->getPassword();
  81.                 $newUser->setFirst(false);
  82.                 $newUser->setEnabled(true);
  83.                 $newUser->setPassword($this->passwordEncoder->encodePassword($newUser$newUser->getPassword()));
  84.                 $newUser->setRoles(['ROLE_USER']);
  85.                 $newUser->setUpdatedAt(new \DateTime("now"));
  86.                 $newUser->setCreatedAt(new \DateTime("now"));
  87.                 $newUser->setShare($shareInvitation);
  88.                 $this->em->persist($newUser);
  89.                 $this->em->flush();
  90.                 if($shareInvitation !== null) {
  91.                     if($shareInvitation->isMultiple() === false) {
  92.                         $shareInvitation->setUserTarget($newUser);
  93.                         $this->em->persist($shareInvitation);
  94.                         $this->em->flush();
  95.                     }
  96.                 }
  97.                 // Envoyer un mail d'inscription à l'utilisateur
  98.                 $templateEntity $this->em->getRepository(Mails::class)->findOneBy(['name' => "register"]);
  99.                 $this->ms->sendUserPassword($newUser,$newPassword,$templateEntity);
  100.                 $session->getFlashBag()->add('success''Merci de votre inscription');
  101.                 return $this->redirectToRoute('app_login');
  102.             }
  103.             $session->getFlashBag()->add('danger''L\'adresse mail est déjà dans notre base de données');
  104.             return $this->redirectToRoute('app_register');
  105.         }
  106.         $response =  $this->render('themesWebsite/blog'.$themeSelection.'/register.html.twig', [
  107.             'page' => $page,
  108.             'blocks' => $blocks,
  109.             'form' => $form->createView(),
  110.             'invitation' => $shareInvitation
  111.         ]);
  112.         //$response->setSharedMaxAge(3600);
  113.         return $response;
  114.     }
  115. }