<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Form\SuperAdminRegistrationFormType;
use App\Repository\UserRepository;
use App\Security\UserAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(MailerInterface $mailer, Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, VerifyEmailHelperInterface $verifyEmailHelper, UserAuthenticator $authenticator, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if(($form->get('role')->getData()) == 'ROLE_CANDIDAT') {
return $this->createUser($mailer, $user, $userPasswordHasher, $form, $entityManager, $userAuthenticator, $verifyEmailHelper, $authenticator, $request, false, false, true);
}
elseif (($form->get('role')->getData()) == 'ROLE_ADMIN_STRUCTURE') {
return $this->createUser($mailer, $user, $userPasswordHasher, $form, $entityManager, $userAuthenticator, $verifyEmailHelper, $authenticator, $request, false, true, false);
}
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
#[Route('/superAdminRegister', name: 'app_admin_register')]
public function superAdminRegister(MailerInterface $mailer, Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, VerifyEmailHelperInterface $verifyEmailHelper, UserAuthenticator $authenticator, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(SuperAdminRegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return $this->createUser($mailer, $user, $userPasswordHasher, $form, $entityManager, $userAuthenticator, $verifyEmailHelper, $authenticator, $request, true, false, false);
}
return $this->render('registration/superAdminRegister.html.twig', [
'registrationForm' => $form->createView(),
]);
}
/**
* @param User $user
* @param UserPasswordHasherInterface $userPasswordHasher
* @param FormInterface $form
* @param EntityManagerInterface $entityManager
* @param UserAuthenticatorInterface $userAuthenticator
* @param UserAuthenticator $authenticator
* @param Request $request
* @return Response|null
*/
public function createUser(MailerInterface $mailer, User $user, UserPasswordHasherInterface $userPasswordHasher, FormInterface $form, EntityManagerInterface $entityManager, UserAuthenticatorInterface $userAuthenticator, VerifyEmailHelperInterface $verifyEmailHelper, UserAuthenticator $authenticator, Request $request, bool $isSuperAdmin, bool $isAdminStruct, bool $isCandidat): ?Response
{
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('password')->getData(),
)
);
$role = array($form->get('role')->getData());
$user->setRoles($role);
$nom = $form->get('lastName')->getData();
$prenom = $form->get('firstName')->getData();
$structure = $form->get('nomStructure')->getData();
$user->setLastName($nom);
$user->setFirstName($prenom);
if ($isAdminStruct && !$isCandidat){
$user->addStructureUser($structure);
}
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
// $signatureComponents = $verifyEmailHelper->generateSignature(
// 'app_verify_email',
// $user->getId(),
// $user->getEmail(),
// ['id' => $user->getId()]
// );
//
// $email = (new Email())
// ->from('MonAdresseTest@example.com')
// ->to($user->getEmail())
// ->subject('Vérification de votre adresse mail')
// ->text("Finaliser votre inscription en cliquant sur ce lien : {$signatureComponents->getSignedUrl()}");
//
// $mailer->send($email);
if (!$isSuperAdmin)
{
return $userAuthenticator->authenticateUser( // à supprimer pour la vérification par mail
$user,
$authenticator,
$request
);
// return $this->redirectToRoute('app_home');
}
return $this->redirectToRoute('app_alluser');
}
#[Route('/verifyEmail',name: 'app_verify_email')]
public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $verifyEmailHelper, UserRepository $userRepository, EntityManagerInterface $entityManager)
{
$user = $userRepository->find($request->query->get('id'));
if (!$user) {
throw $this->createNotFoundException();
}
try {
$verifyEmailHelper->validateEmailConfirmation(
$request->getUri(),
$user->getId(),
$user->getEmail(),
);
} catch (VerifyEmailExceptionInterface $e) {
$this->addFlash('error', $e->getReason());
return $this->redirectToRoute('app_register');
}
$user->setIsVerified(true);
$entityManager->flush();
$this->addFlash('success', 'Le compte a été vérifié avec succès, vous pouvez désormais vous connecter !');
return $this->redirectToRoute('app_login');
}
}