src/Controller/RegistrationController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\ProfilType;
  5. use App\Security\EmailVerifier;
  6. use App\Form\RegistrationFormType;
  7. use App\Repository\UserRepository;
  8. use App\Service\NotificationManager;
  9. use Symfony\Component\Mime\Address;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Routing\Generator\UrlGenerator;
  16. use Symfony\Component\String\Slugger\SluggerInterface;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  19. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  20. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  21. class RegistrationController extends BaseController
  22. {
  23.     private EmailVerifier $emailVerifier;
  24.     public function __construct(EmailVerifier $emailVerifier)
  25.     {
  26.         $this->emailVerifier $emailVerifier;
  27.     }
  28.     #[Route('/register'name'app_register')]
  29.     public function register(
  30.         Request $request,
  31.         UserPasswordHasherInterface $userPasswordHasher,
  32.         EntityManagerInterface $entityManager,
  33.         UserRepository $userRepository,
  34.         NotificationManager $notificationManager,
  35.     ): Response
  36.     {
  37.         $user = new User();
  38.         $form $this->createForm(RegistrationFormType::class, $user);
  39.         $form->handleRequest($request);
  40.         if ($form->isSubmitted()) {
  41.             if ($userRepository->findOneBy(['email' => $form->get('email')->getData()])) {
  42.                 $this->addFlash('ErreurRegistration''Email existe déjà !');
  43.             }else  {
  44.                     $majuscule preg_match('@[A-Z]@'$form->get('plainPassword')->getData());
  45.                     $minuscule preg_match('@[a-z]@'$form->get('plainPassword')->getData());
  46.                     $chiffre preg_match('@[0-9]@'$form->get('plainPassword')->getData());
  47.                     $number strlen($form->get('plainPassword')->getData()) < 8;
  48.                     
  49.                     if (!$majuscule || !$minuscule || !$chiffre || $number) {
  50.                         $this->addFlash('ErreurRegistration''Le mot de passe doit comporter au moins 8 caractères : une lettre majuscule, une minuscule et un chiffre !');
  51.                     }else {
  52.                         if (strcmp($form->get('plainPassword')->getData(), $form->get('confirmpassword')->getData()) == 0) {
  53.                             $user->setRoles(['ROLE_USER']);
  54.                                 $user->setPassword($form->get('plainPassword')->getData());
  55.                                 $entityManager->persist($user);
  56.                                 $entityManager->flush();
  57.     
  58.                             // Email de notification Utilisateur
  59.                             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  60.                             (new TemplatedEmail())
  61.                                 ->from(new Address($this->getParameter('EmailAdmin'), 'IGPPP'))
  62.                                 ->to($user->getEmail())
  63.                                 ->subject('Email de confirmation du compte IGPPP')
  64.                                 ->htmlTemplate('email/email_confirmation_inscription.html.twig')
  65.                             );
  66.                             
  67.                             ///Email de Notification
  68.                             $edit_demande_url $this->generateUrl('app_redirect_email', array( 'id' => $user->getId(), 'type' => 'demandeur' ), UrlGenerator::ABSOLUTE_URL );
  69.                             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  70.                             (new TemplatedEmail())
  71.                                 ->from(new Address($this->getParameter('EmailAdmin'), 'IGPPP'))
  72.                                 ->to($this->getParameter('EmailNotification'))
  73.                                 ->subject('Un nouveau compte a été crée !')
  74.                                 ->htmlTemplate('email/email_notification_inscription.html.twig')
  75.                                 ->context([
  76.                                     'User' => $user->getEmail(),
  77.                                     'demande_url' => $edit_demande_url
  78.                                 ])
  79.                             );
  80.                             $admins $entityManager->getRepository(User::class)->findByRole('ROLE_ADMIN');
  81.                             foreach ( $admins as $admin){
  82.                                 $notification_message 'Un nouveau compte a été crée !';
  83.                                 $notificationManager->pushMessage('Un nouveau compte a été crée !'$notification_message$admin$edit_demande_url);
  84.                             }
  85.             
  86.                             return $this->redirectToRoute('login');
  87.                         }else {
  88.                             $this->addFlash('ErreurRegistration''Les mots de passe ne sont pas identiques !');
  89.                         }
  90.                     }
  91.                   
  92.             }
  93.         }
  94.         return $this->render('registration/register.html.twig', [
  95.             'registrationForm' => $form->createView(),
  96.         ]);
  97.     }
  98.     #[Route('/verify/email'name'app_verify_email')]
  99.     public function verifyUserEmail(Request $requestUserRepository $userRepository): Response
  100.     {
  101.         $id $request->get('id');
  102.         if (null === $id) {
  103.             return $this->redirectToRoute('app_register');
  104.         }
  105.         $user $userRepository->find($id);
  106.         if (null === $user) {
  107.             return $this->redirectToRoute('app_register');
  108.         }
  109.         // validate email confirmation link, sets User::isVerified=true and persists
  110.         try {
  111.             $this->emailVerifier->handleEmailConfirmation($request$user);
  112.         } catch (VerifyEmailExceptionInterface $exception) {
  113.             $this->addFlash('verify_email_error'$exception->getReason());
  114.             return $this->redirectToRoute('app_register');
  115.         }
  116.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  117.         $this->addFlash('success''Votre email a bien été vérifié.');
  118.         return $this->redirectToRoute('login');
  119.     }
  120.     #[Route('/profil'name'app_profil')]
  121.     public function app_profil(Request $requestSluggerInterface $sluggerEntityManagerInterface $entityManager): Response
  122.     {
  123.         if (!$this->getUser()) {
  124.             return $this->redirectToRoute('login');
  125.         }
  126.         if (!$this->getUser()->isVerified()){
  127.             $this->addFlash('VerifAccount''Vous devez vérifier votre compte !');
  128.             return $this->redirectToRoute('app_logout');
  129.         }
  130.         if (!$this->getUser()->getCompleted()){
  131.             $this->addFlash('VerifAccount''Vous devez completer votre compte !');
  132.         }
  133.         
  134.         $form $this->createForm(ProfilType::class, $this->getUser());
  135.         $form->handleRequest($request);
  136.         if ($form->isSubmitted()) {
  137.             $this->getUser()->setCompleted(true);
  138.             $entityManager->persist($this->getUser());
  139.             $entityManager->flush();
  140.             return $this->redirectToRoute('front');
  141.         }
  142.         return $this->render('registration/profil.html.twig', [
  143.             'profilForm' => $form->createView(),
  144.         ]);
  145.     }
  146.     #[Route('/save/profile/picture'name'app_save_profile_picture')]
  147.     public function saveProfilePicture(Request $requestEntityManagerInterface $entityManager): Response
  148.     {
  149.         $photo $request->request->get('imgResult');
  150.         if ($photo) {
  151.             $fileName 'photo' '-' uniqid() . '.png';
  152.             try {
  153.                 if (!file_exists($this->getParameter('PhotoUsers'))) {
  154.                     mkdir($this->getParameter('PhotoUsers'), 0777true);
  155.                 }
  156.                 copy($photo,$this->getParameter('PhotoUsers').$fileName);
  157.                 $this->getUser()->setPhoto($fileName);
  158.                 $entityManager->flush();
  159.             } catch (FileException $e) {
  160.                 dd('a',$e);
  161.             }
  162.         }
  163.         return new Response('ok');
  164.     }
  165. }