src/Controller/RegistrationController.php line 101
<?phpnamespace App\Controller;use App\Entity\LawOffice;use App\Entity\User;use App\Form\LawOfficeType;use App\Form\RegistrationFormType;use App\Repository\LawOfficeRepository;use App\Security\AppCustomAuthenticator;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\Component\Security\Http\Authentication\UserAuthenticatorInterface;use Symfony\Contracts\Translation\TranslatorInterface;use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;class RegistrationController extends AbstractController{private EmailVerifier $emailVerifier;public function __construct(EmailVerifier $emailVerifier){$this->emailVerifier = $emailVerifier;}#[Route('/register', name: 'app_register')]public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppCustomAuthenticator $authenticator, EntityManagerInterface $entityManager,LawOfficeRepository $lawOfficeRepository): Response{$LawOffice = new LawOffice();$user = new User();$LawOffice->getUsers()->add($user);$form = $this->createForm(LawOfficeType::class, $LawOffice);$form->handleRequest($request);//dd($request);if ($form->isSubmitted() && $form->isValid()) {$users = $request->get('law_office');$email = $users['users'][0]['email'];//dd($email);$EmailCheckExist = $entityManager->getRepository(User::class)->findOneBy(['email'=>$email]);//Begin: Save New Userif (!$EmailCheckExist) {//Begin: Save LawOffice$LawOffice->setCreatedAt(new \DateTime());$LawOffice->setStatus(100);$lawOfficeRepository->save($LawOffice);//End: Save LawOffice// encode the plain password$user->setPassword($userPasswordHasher->hashPassword($user,$users['users'][0]['plainPassword']['first']));$user->setLawOffice($LawOffice);$user->setRoles(['ROLE_ADMIN']);$user->setCreatedAt(new \DateTime());$user->setEnable(false);$entityManager->persist($user);$entityManager->flush();//End: Save User// generate a signed url and email it to the user$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,(new TemplatedEmail())->from(new Address('contact@edesk.com', 'E-desk'))->to($user->getEmail())->subject('Please Confirm your Email')->htmlTemplate('mail/confirmation_email.html.twig'));// do anything else you need here, like send an email$this->addFlash('success', 'Votre compte a été créé avec succès, veuillez vérifier votre boîte de réception /courrier indésirable pour un courriel de confirmation.');//return $this->redirectToRoute('app_login');return $userAuthenticator->authenticateUser($user,$authenticator,$request);} else {$this->addFlash('error', 'Impossible de crée un compte avec l\'email: "'.$email.'".');}}return $this->render('registration/registers.html.twig', ['form' => $form->createView(),]);}#[Route('/verify/email', name: 'app_verify_email')]public function verifyUserEmail(Request $request, TranslatorInterface $translator): Response{$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');// validate email confirmation link, sets User::isVerified=true and persiststry {$this->emailVerifier->handleEmailConfirmation($request, $this->getUser());} catch (VerifyEmailExceptionInterface $exception) {$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));return $this->redirectToRoute('app_login');}// @TODO Change the redirect on success and handle or remove the flash message in your templates$this->addFlash('success', $translator->trans('Your email address has been verified.',[],'translation'));return $this->redirectToRoute('app_login');}/*public function resendVerifyEmail(Request $request,UserRepository $userRepository,ParameterBagInterface $params): Response{if ($this->getUser()) {return $this->redirectToRoute('home');}$form = $this->createForm(RequestVerifyUserEmailFormType::class);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {// generate a signed url and email it to the user$user = $userRepository->findOneByEmail($form->get('email')->getData());//dd($user);// Parameter Mail id 4 fors Confirmation d\'email (Compte d\'utilisateur)$GetParameterMail = $this->GetOptionMail->GetOption(2);if ($user) {$this->emailVerifier->sendEmailConfirmation('app_verify_email',$user,(new TemplatedEmail())->from(new Address($GetParameterMail->getFromAdress(), $GetParameterMail->getFromName()))->to($user->getEmail())->subject($GetParameterMail->getSubject())->htmlTemplate('registration/confirmation_email.html.twig'));// do anything else you need here, like flash message$this->addFlash('success', 'Veuillez vérifier votre boîte de réception /courrier indésirable pour un courriel de confirmation.');return $this->redirectToRoute('home');} else {$this->addFlash('error', 'Email inconnu / Vous avez déjà activé votre compte.');return $this->redirectToRoute('home');}}return $this->render('registration/resend_verify_email.html.twig', ['requestForm' => $form->createView(),]);}*/}