<?php
/*
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential.
*
* @author Bilel AZRI <azri.bilel@gmail.com>
* @author Assma BEN SASSI <bensassiasma.bws@gmail.com>
*
* Bicking man (c) 2019-present.
*/
declare(strict_types=1);
namespace App\Controller\Front;
use App\Entity\User\Participant;
use App\Form\Front\User\Registration\RegistrationFormType;
use App\Repository\User\ParticipantRepository;
use App\Security\EmailVerifier;
use App\Security\FrontAuthenticator;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use Twig\Environment;
class RegistrationController extends AbstractController
{
private $emailVerifier;
private Environment $twig;
private UrlGeneratorInterface $urlGenerator;
public function __construct(Environment $twig, EmailVerifier $emailVerifier, UrlGeneratorInterface $urlGenerator)
{
$this->twig = $twig;
$this->emailVerifier = $emailVerifier;
$this->urlGenerator = $urlGenerator;
}
/**
* @Route("/register", name="app_register")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, FrontAuthenticator $authenticator): Response
{
$user = new Participant();
$form = $this->createForm(RegistrationFormType::class, $user, [
'action' => $this->urlGenerator->generate('front.app_register'),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager = $this->getDoctrine()->getManager();
$user->setProgressiveStatus(0);
$entityManager->persist($user);
$entityManager->flush();
// generate a signed url and email it to the user
// ASMAAAA disable Email send
$emailContent = $this->twig->render('emails/user/registration_confirmation_email.html.twig', [
'user' => $user,
]);
$this->emailVerifier->sendEmailConfirmation('front.app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('app@bikingman.com', 'BikingMan'))
->to($user->getEmail())
->subject('Votre inscription au dashboard BikingMan')
->html($emailContent)
);
// do anything else you need here, like send an email
return $guardHandler->authenticateUserAndHandleSuccess(
$user,
$request,
$authenticator,
'main' // firewall name in security.yaml
);
}
return $this->render('front/security/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
/**
* @Route("/verify/email", name="app_verify_email")
*/
public function verifyUserEmail(Request $request, ParticipantRepository $participantRepository): Response
{
$id = $request->get('id');
if (null === $id) {
return $this->redirectToRoute('front.app_register');
}
$user = $participantRepository->find($id);
if (null === $user) {
return $this->redirectToRoute('front.app_register');
}
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $user);
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $exception->getReason());
return $this->redirectToRoute('front.app_register');
}
// @TODO Change the redirect on success and handle or remove the flash message in your templates
$this->addFlash('success', 'Your email address has been verified.');
return $this->redirectToRoute('front.app_register');
}
}