<?php
namespace App\Controller;
use App\Entity\User;
use App\Utils\Toast;
use App\Service\ToastService;
use App\Security\EmailVerifier;
use App\Form\RegistrationFormType;
use App\Repository\UserRepository;
use Symfony\Component\Mime\Address;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
class RegistrationController extends AbstractController
{
private EmailVerifier $emailVerifier;
/**
* @var ToastService
*/
private $toastService;
public function __construct(EmailVerifier $emailVerifier, ToastService $toastService)
{
$this->emailVerifier = $emailVerifier;
$this->toastService = $toastService;
}
/**
* @Route("register", name="app_register", options = { "expose" = true })
*/
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$user->setStatus('Inactif');
$user->setRoles(['ROLE_USER']);
$user->setIsVerified(false);
$entityManager->persist($user);
$entityManager->flush();
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation(
'app_verify_email',
$user,
(new TemplatedEmail())
->from(new Address($this->getParameter('mail_from'), 'Plateforme Q-Sqor'))
->to($user->getEmail())
->subject('Q-Sqor - Merci de confirmer votre adresse email')
->htmlTemplate('registration/confirmation_email.html.twig')
);
// do anything else you need here, like send an email
$this->toastService->toast(Toast::TYPE_SUCCESS, 'Inscription réussie', 'Vérifier vos emails pour valider votre compte.');
return $this->redirectToRoute('app_login');
} else {
$errors = '';
foreach ($form->getErrors(true) as $error) {
$errors .= $error->getMessage() . "<br />";
}
if ($form->isSubmitted())
$this->toastService->toast(Toast::TYPE_ERROR, 'Erreur lors de votre inscription', $errors);
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
/**
* @Route("/verify/email", name="app_verify_email", options = { "expose" = true })
*/
public function verifyUserEmail(Request $request, UserRepository $userRepository): Response
{
$id = $request->get('id');
if (null === $id) {
$this->toastService->toast(Toast::TYPE_ERROR, 'Erreur lors de la vérification de votre compte', 'Id manquant');
return $this->redirectToRoute('app_register');
}
$user = $userRepository->find($id);
if (null === $user) {
$this->toastService->toast(Toast::TYPE_ERROR, 'Erreur lors de la vérification de votre compte', 'Utilisateur introuvable');
return $this->redirectToRoute('app_register');
}
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $user);
} catch (VerifyEmailExceptionInterface $exception) {
$this->toastService->toast(Toast::TYPE_ERROR, 'Erreur lors de la vérification de votre compte', $exception->getReason());
return $this->redirectToRoute('app_register');
}
// @TODO Change the redirect on success and handle or remove the flash message in your templates
$this->toastService->toast(Toast::TYPE_SUCCESS, 'Votre email a été vérifiée.', 'Un administrateur va vérifier et activer voter compte.');
return $this->redirectToRoute('client_tdb');
}
}