src/Controller/RegistrationController.php line 32
<?phpnamespace App\Controller;use App\Entity\User;use App\Form\RegistrationFormType;use App\Repository\UserRepository;use App\Security\EmailVerifier;use Doctrine\ORM\EntityManagerInterface;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\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Routing\Annotation\Route;use Symfony\Contracts\Translation\TranslatorInterface;use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;class RegistrationController extends AbstractController{private EmailVerifier $emailVerifier;public function __construct(EmailVerifier $emailVerifier){$this->emailVerifier = $emailVerifier;}#[Route('/customer/register', name: 'app_customer_register')]public function customerRegister(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->setRoles(["ROLE_USER", "ROLE_CUSTOMER", "ROLE_NO_PROFILE"]);$user->setEnabled(true);$user->setIsVerified(false);$entityManager->persist($user);$entityManager->flush();// generate a signed url and email it to the user$this->emailVerifier->sendEmailConfirmation('app_customer_verify_email', $user,(new TemplatedEmail())->from(new Address('info@izipen.gr', 'iziPen Mailer'))->to($user->getEmail())->subject('Please Confirm your Email')->htmlTemplate('email/customer/confirmation.html.twig'));// do anything else you need here, like send an emailreturn $this->redirectToRoute('app_default');}return $this->render('registration/register.customer.html.twig', ['registrationForm' => $form->createView(),]);}#[Route('/writer/register', name: 'app_writer_register')]public function writerRegister(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->setRoles(["ROLE_USER", "ROLE_WRITER", "ROLE_NO_PROFILE"]);$user->setEnabled(true);$user->setIsVerified(false);$entityManager->persist($user);$entityManager->flush();// generate a signed url and email it to the user$this->emailVerifier->sendEmailConfirmation('app_writer_verify_email', $user,(new TemplatedEmail())->from(new Address('info@izipen.gr', 'iziPen Mailer'))->to($user->getEmail())->subject('Please Confirm your Email')->htmlTemplate('email/writer/confirmation.html.twig'));// do anything else you need here, like send an emailreturn $this->redirectToRoute('app_default');}return $this->render('registration/register.writer.html.twig', ['registrationForm' => $form->createView(),]);}#[Route('/customer/verify/email', name: 'app_customer_verify_email')]public function verifyCustomerUserEmail(Request $request, TranslatorInterface $translator, UserRepository $userRepository, TokenStorageInterface $tokenStorage): Response{$id = $request->get('id');if (null === $id) {return $this->redirectToRoute('app_default');}$user = $userRepository->find($id);if (null === $user || !(in_array('ROLE_CUSTOMER', $user->getRoles(), true))) {return $this->redirectToRoute('app_default');}// validate email confirmation link, sets User::isVerified=true and persiststry {$this->emailVerifier->handleEmailConfirmation($request, $user);} catch (VerifyEmailExceptionInterface $exception) {$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));return $this->redirectToRoute('app_default');}// @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.');$tokenStorage->setToken(new UsernamePasswordToken($user, 'customer', $user->getRoles()));return $this->redirectToRoute('customer_profile_register');}#[Route('/writer/verify/email', name: 'app_writer_verify_email')]public function verifyWriterUserEmail(Request $request, TranslatorInterface $translator, UserRepository $userRepository, TokenStorageInterface $tokenStorage): Response{$id = $request->get('id');if (null === $id) {return $this->redirectToRoute('app_default');}$user = $userRepository->find($id);if (null === $user || !(in_array('ROLE_WRITER', $user->getRoles(), true))) {return $this->redirectToRoute('app_default');}// validate email confirmation link, sets User::isVerified=true and persiststry {$this->emailVerifier->handleEmailConfirmation($request, $user);} catch (VerifyEmailExceptionInterface $exception) {$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));return $this->redirectToRoute('app_default');}// @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.');$tokenStorage->setToken(new UsernamePasswordToken($user, 'writer', $user->getRoles()));return $this->redirectToRoute('writer_profile_register');}}