src/Controller/RegistrationController.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ArticleSearch;
  4. use App\Entity\Client;
  5. use App\Entity\Personne;
  6. use App\Entity\User;
  7. use App\Form\RegistrationFormType;
  8. use App\Form\RegistrationClientFormType;
  9. use App\Repository\UserRepository;
  10. use App\Security\EmailVerifier;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. use App\Form\ArticleSearchType;
  20. use App\Service\Email\EmailService;
  21. use App\Service\Service;
  22. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  23. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  24. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  25. use Symfony\Component\Security\Http\SecurityEvents;
  26. class RegistrationController extends AbstractController
  27. {
  28.     private $emailVerifier;
  29.     public function __construct(EmailVerifier $emailVerifier)
  30.     {
  31.         $this->emailVerifier $emailVerifier;
  32.     }
  33.     /**
  34.      * @Route("/register", name="app_register")
  35.      */
  36.     public function appRegister(EmailService $emailServiceService $serviceRequest $requestUserPasswordHasherInterface $passwordEncoderEventDispatcherInterface $eventDispatcherInterface): Response
  37.     {
  38.         if ($this->getUser()) {
  39.             $this->addFlash('success','Vous etes déja connecté');
  40.             return $this->redirectToRoute('home');
  41.         }
  42.         $search = new ArticleSearch();
  43.         $formSearch $this->createForm(ArticleSearchType::class,$search);
  44.         $user = new User();
  45.         $user->setRoles(['ROLE_CLIENT'])->setCle($service->aleatoire(100));
  46.         
  47.         $form $this->createForm(RegistrationFormType::class, $user);
  48.         $form->handleRequest($request);
  49.         if ($form->isSubmitted() && $form->isValid()) {
  50.             // encode the plain password
  51.             $user->setPassword(
  52.                 $passwordEncoder->hashPassword(
  53.                     $user,
  54.                     $form->get('password')->getData()
  55.                 )
  56.             );
  57.             //initialise le nouveau
  58.             $client = new Client();
  59.             $user->setClient($client);
  60.             $user->setIsActive(true);
  61.             $entityManager $this->getDoctrine()->getManager();
  62.             $entityManager->persist($user);
  63.             $entityManager->flush();
  64.             // generate a signed url and email it to the user
  65.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  66.                 (new TemplatedEmail())
  67.                     ->from(new Address('contact@pmd-devloepper.com''PMD boutique - Inscription'))
  68.                     ->to($user->getEmail())
  69.                     ->subject('Veuillez confirmer votre email')
  70.                     ->htmlTemplate('email/confirmation_out.html.twig')
  71.                     ->context([
  72.                         'user'=>$user,
  73.                         'theme'=>$emailService->theme(1)
  74.                         ])
  75.             );
  76.             //Se connecter automatiquement
  77.             // $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
  78.             // $this->get("security.token_storage")->setToken($token);
  79.             $event =  new SecurityEvents($request);
  80.             $eventDispatcherInterface->dispatch($eventSecurityEvents::INTERACTIVE_LOGIN);
  81.             $this->addFlash('info','Un email de confirmation vous a été envoyé, Merci de vérifier votre boite de méssagerie');
  82.             // $this->addFlash('success','Votre compte a été enregistré. Vous pouvez poursuivre vos achats');
  83.             // do anything else you need here, like send an email
  84.             return $this->redirectToRoute('app_login');
  85.         }
  86.         return $this->renderForm('registration/index.html.twig', [
  87.             'registrationForm' => $form,
  88.         ]);
  89.     }
  90.     /**
  91.      * @Route("/verify/email", name="app_verify_email")
  92.      */
  93.     public function verifyUserEmail(Request $requestUserRepository $userRepository): Response
  94.     {
  95.         $id $request->get('id');
  96.         if (null === $id) {
  97.             return $this->redirectToRoute('app_register');
  98.         }
  99.         $user $userRepository->find($id);
  100.         if (null === $user) {
  101.             return $this->redirectToRoute('app_register');
  102.         }
  103.         // validate email confirmation link, sets User::isVerified=true and persists
  104.         try {
  105.             $this->emailVerifier->handleEmailConfirmation($request$user);
  106.         } catch (VerifyEmailExceptionInterface $exception) {
  107.             $this->addFlash('verify_email_error'$exception->getReason());
  108.             return $this->redirectToRoute('app_register');
  109.         }
  110.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  111.         $this->addFlash('success''Votre adresse e-mail a été vérifiée.');
  112.         return $this->redirectToRoute('client_index');
  113.     }
  114. }