src/Controller/RegistrationvendeurController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\UserVendeur;
  4. use App\Form\RegistrationvendeurFormType;
  5. use App\Repository\UserVendeurRepository;
  6. use App\Security\AppAuthenticator;
  7. use App\Security\EmailVerifiers;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. class RegistrationvendeurController extends AbstractController
  20. {
  21.     private $emailVerifier;
  22.     public function __construct(EmailVerifiers $emailVerifier)
  23.     {
  24.         $this->emailVerifier $emailVerifier;
  25.     }
  26.     /**
  27.      * @Route("/inscription-etablissement", name="app_auth")
  28.      */
  29.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppAuthenticator $authenticatorEntityManagerInterface $em): Response
  30.     {
  31.         $user = new UserVendeur();
  32.         $form $this->createForm(RegistrationvendeurFormType::class, $user);
  33.         $form->handleRequest($request);
  34.         if ($form->isSubmitted() && $form->isValid()) {
  35.            
  36.             
  37.             // encode the plain password
  38.             $user->setPassword(
  39.             $userPasswordHasher->hashPassword(
  40.                     $user,
  41.                     $form->get('plainPassword')->getData()
  42.                 )
  43.             );
  44.             $user->setCompte('vendeur');
  45.             $em->persist($user);
  46.             $em->flush();
  47.              
  48.             // generate a signed url and email it to the user
  49.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  50.                 (new TemplatedEmail())
  51.                     ->from(new Address('noreply@aidemarket.cirej.com''Aid Market'))
  52.                     ->to($user->getEmail())
  53.                     ->subject('Please Confirm your Email')
  54.                     ->htmlTemplate('registration/confirmation_email_vendeurs.html.twig')
  55.             );
  56.             // do anything else you need here, like send an email
  57.             return $userAuthenticator->authenticateUser(
  58.                 $user,
  59.                 $authenticator,
  60.                 $request
  61.             );
  62.         }
  63.         $this->addFlash('success''Votre compte à bien été crée ! vendez vos produits');
  64.         return $this->render('auth/inscriptionEtablissement.html.twig', [
  65.             'registrationForm' => $form->createView(),
  66.         ]);
  67.     }
  68.     /**
  69.      * @Route("/verify/email", name="app_verify_email")
  70.      */
  71.     public function verifyUserEmail(Request $requestTranslatorInterface $translatorUserVendeurRepository $userVendeurRepository): Response
  72.     {
  73.         $id $request->get('id');
  74.         if (null === $id) {
  75.             return $this->redirectToRoute('app_auth');
  76.         }
  77.         $user $userVendeurRepository->find($id);
  78.         if (null === $user) {
  79.             return $this->redirectToRoute('app_auth');
  80.         }
  81.         // validate email confirmation link, sets User::isVerified=true and persists
  82.         try {
  83.             $this->emailVerifier->handleEmailConfirmation($request$user);
  84.         } catch (VerifyEmailExceptionInterface $exception) {
  85.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  86.             return $this->redirectToRoute('app_auth');
  87.         }
  88.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  89.         $this->addFlash('success''Your email address has been verified.');
  90.         return $this->redirectToRoute('app_auth');
  91.     }
  92. }