src/Controller/UserController.php line 307

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Formation;
  4. use App\Entity\Trainee;
  5. use App\Entity\TraineeFormation;
  6. use App\Entity\User;
  7. use App\Form\RegistrationFormType;
  8. use App\Form\TraineeFormType;
  9. use App\Form\UpdateUserFormType;
  10. use Doctrine\DBAL\Types\TextType;
  11. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. use PhpOffice\PhpSpreadsheet\Writer\Xls;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\File\UploadedFile;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\StreamedResponse;
  19. use Symfony\Component\Mailer\MailerInterface;
  20. use Symfony\Component\Mime\Email;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  23. use Symfony\Component\Security\Core\Security;
  24. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  25. use App\Security\AppAuthenticator;
  26. use Doctrine\ORM\EntityManagerInterface;
  27. class UserController extends AbstractController
  28. {
  29.     /** User CRUD */
  30.     #[Route('/user'name'app_user')]
  31.     public function index(EntityManagerInterface $entityManager): Response
  32.     {
  33.         $users $entityManager->getRepository(User::class)->findUsers("ROLE_SUPER_ADMIN");
  34.         return $this->render('user/index.html.twig', [
  35.             'users' => $users,
  36.         ]);
  37.     }
  38.     #[Route('/user/add'name'app_add_user')]
  39.     public function addUser(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorEntityManagerInterface $entityManager): Response
  40.     {
  41.         $user = new User();
  42.         $form $this->createForm(RegistrationFormType::class, $user);
  43.         $form->handleRequest($request);
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.              // Créer une instance de l'entité User avec les données du formulaire
  46.             $user $form->getData();
  47.             // Définir le rôle de l'utilisateur en tant qu'utilisateur
  48.             $user->setRoles(['ROLE_SUPER_ADMIN']);
  49.             // encode the plain password
  50.             $user->setPassword(
  51.                 $userPasswordHasher->hashPassword(
  52.                     $user,
  53.                     $form->get('password')->getData()
  54.                 )
  55.             );
  56.             $entityManager->persist($user);
  57.             $entityManager->flush();
  58.             // do anything else you need here, like send an email
  59.             return $this->redirectToRoute('app_user');
  60.         }
  61.         return $this->render('user/new.html.twig', [
  62.             'registrationForm' => $form->createView(),
  63.         ]);
  64.     }
  65.     #[Route('/user/profile/{id}'name'app_edit_user')]
  66.     public function updateUserProfile(Request $requestEntityManagerInterface $entityManager$id): Response
  67.     {
  68.         if($id) {
  69.             $user $entityManager->getRepository(User::class)->findOneBy(['id' => $id]);
  70.         } else {
  71.             $user $this->getUser();
  72.         }
  73.         $form $this->createForm(UpdateUserFormType::class, $user);
  74.         $form->handleRequest($request);
  75.         $teacher false;
  76.         if (in_array('ROLE_TEACHER'$this->getUser()->getRoles(), true)) {
  77.             $teacher true;
  78.         }
  79.         if ($form->isSubmitted() && $form->isValid()) {
  80.             $user $form->getData();
  81.             if ($teacher) {
  82.                 $signatureMode = (string) $request->request->get('signature_mode''draw');
  83.                 $uploadedSignature $request->files->get('signature_upload');
  84.                 if ($signatureMode === 'upload' && $uploadedSignature instanceof UploadedFile) {
  85.                     $allowedMimeTypes = ['image/png''image/jpeg''image/jpg''image/webp'];
  86.                     $mimeType = (string) $uploadedSignature->getMimeType();
  87.                     if (in_array($mimeType$allowedMimeTypestrue) && $uploadedSignature->getSize() <= 1024 1024) {
  88.                         $binary file_get_contents($uploadedSignature->getRealPath());
  89.                         if ($binary !== false) {
  90.                             $sourceImage = @imagecreatefromstring($binary);
  91.                             if ($sourceImage !== false) {
  92.                                 $targetWidth 150;
  93.                                 $targetHeight 150;
  94.                                 $resizedImage imagecreatetruecolor($targetWidth$targetHeight);
  95.                                 imagealphablending($resizedImagefalse);
  96.                                 imagesavealpha($resizedImagetrue);
  97.                                 $transparent imagecolorallocatealpha($resizedImage000127);
  98.                                 imagefilledrectangle($resizedImage00$targetWidth$targetHeight$transparent);
  99.                                 imagecopyresampled(
  100.                                     $resizedImage,
  101.                                     $sourceImage,
  102.                                     0,
  103.                                     0,
  104.                                     0,
  105.                                     0,
  106.                                     $targetWidth,
  107.                                     $targetHeight,
  108.                                     imagesx($sourceImage),
  109.                                     imagesy($sourceImage)
  110.                                 );
  111.                                 ob_start();
  112.                                 $outputMimeType $mimeType === 'image/jpg' 'image/jpeg' $mimeType;
  113.                                 if ($outputMimeType === 'image/png') {
  114.                                     imagepng($resizedImage);
  115.                                 } elseif ($outputMimeType === 'image/webp' && function_exists('imagewebp')) {
  116.                                     imagewebp($resizedImagenull90);
  117.                                 } else {
  118.                                     $outputMimeType 'image/jpeg';
  119.                                     imagejpeg($resizedImagenull90);
  120.                                 }
  121.                                 $resizedBinary ob_get_clean();
  122.                                 imagedestroy($sourceImage);
  123.                                 imagedestroy($resizedImage);
  124.                                 if ($resizedBinary !== false) {
  125.                                     $user->setSignature('data:' $outputMimeType ';base64,' base64_encode($resizedBinary));
  126.                                 } else {
  127.                                     $user->setSignature('data:' $mimeType ';base64,' base64_encode($binary));
  128.                                 }
  129.                             } else {
  130.                                 $user->setSignature('data:' $mimeType ';base64,' base64_encode($binary));
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.             $entityManager->persist($user);
  137.             $entityManager->flush();
  138.             // Stay on the same profile page after save and refresh state.
  139.             return $this->redirectToRoute('app_edit_user', ['id' => $user->getId()]);
  140.         }
  141.         return $this->render('user/update.html.twig', [
  142.             'setUserForm' => $form->createView(),
  143.             'teacher' => $teacher
  144.         ]);
  145.     }
  146.     /** Trainees CRUD */
  147.     #[Route('/user/trainees'name'app_trainees')]
  148.     public function listOfTrainees(EntityManagerInterface $entityManager): Response
  149.     {
  150.         $isTeacher false;
  151.         if (in_array('ROLE_TEACHER'$this->getUser()->getRoles(), true)) {
  152.             $isTeacher true;
  153.         }
  154.         if ($isTeacher) {
  155.             $users $entityManager->createQueryBuilder()
  156.                 ->select('DISTINCT t')
  157.                 ->from(Trainee::class, 't')
  158.                 ->innerJoin(TraineeFormation::class, 'tf''WITH''tf.trainee = t')
  159.                 ->innerJoin('tf.formation''f')
  160.                 ->where('f.formateur = :teacher')
  161.                 ->setParameter('teacher'$this->getUser())
  162.                 ->orderBy('t.id''DESC')
  163.                 ->getQuery()
  164.                 ->getResult();
  165.         } else {
  166.             $users $entityManager->getRepository(Trainee::class)->findBy([], ['id' => 'DESC']);
  167.         }
  168.         return $this->render('trainees/trainees.html.twig', [
  169.             'users' => $users,
  170.             'base_template' => $isTeacher 'baseTeacher.html.twig' 'baseAdmin.html.twig'
  171.         ]);
  172.     }
  173.     #[Route('/user/addTrainee/{formationId}'name'app_add_trainee')]
  174.     public function addTrainees(Request $requestEntityManagerInterface $entityManager$formationId null): Response
  175.     {
  176.         $user = new Trainee();
  177.         $type "";
  178.         $formation null;
  179.         if ($formationId) {
  180.             $formation $entityManager->getRepository(Formation::class)->findOneBy(['id' => $formationId]);
  181.             if ($formation) {
  182.                 $type $formation->getType();
  183.                 if( $formation->getType() == "intra") {
  184.                     $customer $formation->getCustomers();
  185.                     if ($customer) {
  186.                         $user->setCustomer($customer[0]);
  187.                     }
  188.                 }  
  189.             }
  190.         }
  191.         // hide comments field on the trainee edit view
  192.         $form $this->createForm(TraineeFormType::class, $user, ['show_comments' => false]);
  193.         $form->handleRequest($request);
  194.         if ($form->isSubmitted() && $form->isValid()) {
  195.             $user $form->getData();
  196.             $entityManager->persist($user);
  197.             $entityManager->flush();
  198.             if($formationId) {
  199.                 $formation  $entityManager->getRepository(Formation::class)->findOneBy(['id'=> $formationId]);
  200.                 $TraineeFormation = new TraineeFormation();
  201.                 $TraineeFormation->setTrainee($user);
  202.                 $TraineeFormation->setFormation($formation);
  203.                 $entityManager->persist($TraineeFormation);
  204.                 $entityManager->flush();
  205.                 return $this->redirectToRoute('app_courses_manage', ['idFormation' => $formationId'type' => $formation->getType()]);
  206.             } else {
  207.                 return $this->redirectToRoute('app_trainees');
  208.             }
  209.         }
  210.         $isTeacher false;
  211.         if (in_array('ROLE_TEACHER'$this->getUser()->getRoles(), true)) {
  212.             $isTeacher true;
  213.         }
  214.         return $this->render('trainees/new_trainee.html.twig', [
  215.             'registrationForm' => $form->createView(),
  216.             'formationId' => $formationId,
  217.             'typeFormation' => $type,
  218.             'base_template' => $isTeacher 'baseTeacher.html.twig' 'baseAdmin.html.twig'
  219.         ]);
  220.     }
  221.     /** Teacher CRUD */
  222.     #[Route('/user/teachers'name'app_teachers')]
  223.     public function listOfTeacher(EntityManagerInterface $entityManager): Response
  224.     {
  225.         $users $entityManager->getRepository(User::class)->findUsers('ROLE_TEACHER');
  226.         return $this->render('user/teachers.html.twig', [
  227.             'users' => $users,
  228.         ]);
  229.     }
  230.     #[Route('/user/addTeacher'name'app_add_teacher')]
  231.     public function addTeacher(Request $requestEntityManagerInterface $entityManagerUserPasswordHasherInterface $userPasswordHasher): Response
  232.     {
  233.         $user = new User();
  234.         $form $this->createForm(UpdateUserFormType::class, $user);
  235.         $form->handleRequest($request);
  236.         if ($form->isSubmitted() && $form->isValid()) {
  237.             $user $form->getData();
  238.             //set default password for Trainees 00000000
  239.             $user->setPassword(
  240.                 $userPasswordHasher->hashPassword(
  241.                     $user,
  242.                     '00000000'
  243.                 )
  244.             );
  245.             $user->setRoles(['ROLE_TEACHER']);
  246.             $entityManager->persist($user);
  247.             $entityManager->flush();
  248.             return $this->redirectToRoute('app_teachers');
  249.         }
  250.         return $this->render('user/new_teacher.html.twig', [
  251.             'registrationForm' => $form->createView(),
  252.         ]);
  253.     }
  254.     #[Route('/trainee/edit/{id}/{idFormation}'name'app_edit_trainee')]
  255.     public function updateTrainee(Request $requestEntityManagerInterface $entityManager$id$idFormation null): Response
  256.     {
  257.         $user $entityManager->getRepository(Trainee::class)->findOneBy(['id' => $id]);
  258.         $form $this->createForm(TraineeFormType::class, $user, ['show_comments' => false]);
  259.         $type "";
  260.         if($idFormation) {
  261.             $formation $entityManager->getRepository(Formation::class)->findOneBy(['id' => $idFormation]);
  262.             $type $formation->getType();
  263.         }
  264.         $form->handleRequest($request);
  265.         if ($form->isSubmitted() && $form->isValid()) {
  266.             $user $form->getData();
  267.             $entityManager->persist($user);
  268.             $entityManager->flush();
  269.             if ($idFormation !== null) {
  270.                 return $this->redirectToRoute('app_courses_manage', ['type' => $type'idFormation' => $idFormation]);
  271.             }
  272.             return $this->redirectToRoute('app_trainees');
  273.         }
  274.         $isTeacher false;
  275.         if (in_array('ROLE_TEACHER'$this->getUser()->getRoles(), true)) {
  276.             $isTeacher true;
  277.         }
  278.         return $this->render('trainees/update_trainee.html.twig', [
  279.             'setTraineeForm' => $form->createView(),
  280.             'typeFormation' => $type,
  281.             'base_template' => $isTeacher 'baseTeacher.html.twig' 'baseAdmin.html.twig'
  282.         ]);
  283.     }
  284.     #[Route('/update-password'name'app_update_password')]
  285.     public function updatePassword(Request $requestEntityManagerInterface $entityManagerMailerInterface $mailer): Response
  286.     {
  287.         if ($request->isMethod('POST')) {
  288.             $email $request->request->get('email');
  289.             if($email != "") {
  290.                 $user $entityManager->getRepository(User::class)->findOneBy(['email' => $email]);
  291.                 if ($user) {
  292.                     //send mail to user with token
  293.                     $link 'https://adformation.online'.$this->generateUrl('app_update_user_password',['email'=>$email]);
  294.                     $emailToSend = (new Email())
  295.                         ->from('noreply-formation@adconseil.eu')
  296.                         ->subject('Modification de mot de passe')
  297.                         ->html('<p>Bonjour, cliquer sur le lien pour modifier votre mot de passe:<br><a href="'.$link.'">'.$link.'</a></p>')
  298.                         ->to($user->getEmail());
  299.                     $mailer->send($emailToSend);
  300.                     $this->addFlash('success'"Un email est envoyé à votre compte.");
  301.                     return $this->redirectToRoute('app_update_password');
  302.                 } else {
  303.                     // show error message
  304.                     $this->addFlash('warning'"Cet email n'existe pas!");
  305.                     return $this->redirectToRoute('app_update_password');
  306.                 }
  307.             }
  308.             return $this->redirectToRoute('app_update_password');
  309.         }
  310.         return $this->render('security/resetPassword.html.twig', [
  311.         ]);
  312.     }
  313.     #[Route('/update-user-password/{email}'name'app_update_user_password')]
  314.     public function updateUserPassword(Request $requestEntityManagerInterface $entityManager,UserPasswordHasherInterface $userPasswordHasher$email): Response
  315.     {
  316.         if ($request->isMethod('POST')) {
  317.             $password$request->request->get('password');
  318.             if($email != "" && $password != "") {
  319.                 $user $entityManager->getRepository(User::class)->findOneBy(['email' => $email]);
  320.                 if ($user) {
  321.                     //update password
  322.                     $user->setPassword(
  323.                         $userPasswordHasher->hashPassword(
  324.                             $user,
  325.                             $password
  326.                         )
  327.                     );
  328.                     $entityManager->persist($user);
  329.                     $entityManager->flush();
  330.                     $this->addFlash('success'"Votre mot de passe aura été changé avec succès");
  331.                     return $this->redirectToRoute('app_login');
  332.                 } else {
  333.                     // show error message
  334.                     $this->addFlash('warning'"Le lien est incorrect");
  335.                     return $this->redirectToRoute('app_update_user_password');
  336.                 }
  337.             }
  338.             return $this->redirectToRoute('app_login');
  339.         }
  340.         return $this->render('security/newPassword.html.twig', [
  341.         ]);
  342.     }
  343.     #[Route('/trainee/delete/{id}'name'app_delete_trainee')]
  344.     public function deleteTrainee(EntityManagerInterface $entityManager$id): Response
  345.     {
  346.         $trainee $entityManager->getRepository(Trainee::class)->findOneBy(['id' => $id]);
  347.         $traineeFormation $entityManager->getRepository(TraineeFormation::class)->findBy(['trainee' => $trainee]);
  348.         //find if trainee is affected to formation
  349.         $object = new \stdClass();
  350.         if($traineeFormation) {
  351.             foreach ($traineeFormation as $trfor) {
  352.                  $entityManager->remove($trfor);
  353.             }
  354.             $entityManager->flush();
  355.         }
  356.         $entityManager->remove($trainee);
  357.         $entityManager->flush();
  358.         $object->status true;
  359.         $object->message "Le stagiaire est supprimé avec succès";
  360.         return new Response(json_encode($object));
  361.     }
  362.     #[Route('/teacher/delete/{id}'name'app_delete_teacher')]
  363.     public function deleteTeacher(EntityManagerInterface $entityManager$id): Response
  364.     {
  365.         $teacher $entityManager->getRepository(User::class)->findOneBy(['id' => $id]);
  366.         //find if trainee is affected to formation
  367.         $teacherFormation $entityManager->getRepository(Formation::class)->findOneBy(['formateur' => $teacher]);
  368.         $object = new \stdClass();
  369.         if ($teacherFormation) {
  370.             $object->status false;
  371.             $object->message "Ce formateur est enregistré dans une formation et il est impossible de le supprimer.";
  372.         } else {
  373.             $entityManager->remove($teacher);
  374.             $entityManager->flush();
  375.             $object->status true;
  376.             $object->message "Le formateur est supprimé avec succès";
  377.         }
  378.         return new Response(json_encode($object));
  379.     }
  380.     #[Route('/user/delete/{id}'name'app_delete_user')]
  381.     public function deleteUser(EntityManagerInterface $entityManager$id): Response
  382.     {
  383.         $user $entityManager->getRepository(User::class)->findOneBy(['id' => $id]);
  384.         $object = new \stdClass();
  385.         $entityManager->remove($user);
  386.         $entityManager->flush();
  387.         $object->status true;
  388.         $object->message "L'utilisateur est supprimé avec succès";
  389.         return new Response(json_encode($object));
  390.     }
  391.     #[Route('/downloadTrainee'name'app_download_trainee')]
  392.     public function downloadTrainee(EntityManagerInterface $entityManager): Response
  393.     {
  394.         $users $entityManager->getRepository(Trainee::class)->findBy([],['id' => 'DESC']);
  395.         $spreadsheet = new Spreadsheet();
  396.         $sheet $spreadsheet->getActiveSheet();
  397.         $sheet->setCellValue('A1''Nom');
  398.         $sheet->setCellValue('B1''Prénom');
  399.         $sheet->setCellValue('C1''Fonction');
  400.         $sheet->setCellValue('D1''Email');
  401.         $counter 2;
  402.         foreach ($users as $user) {
  403.             $sheet->setCellValue('A' $counter$user->getFirstName());
  404.             $sheet->setCellValue('B' $counter$user->getLastName());
  405.             $sheet->setCellValue('C' $counter$user->getPosition());
  406.             $sheet->setCellValue('D' $counter$user->getEmail());
  407.             $counter++;
  408.         }
  409.         $writer = new Xls($spreadsheet);
  410.         $response =  new StreamedResponse(
  411.             function () use ($writer) {
  412.                 $writer->save('php://output');
  413.             }
  414.         );
  415.         $fileName "ExportEmails_".date('m-d-Y_hia').".xls";
  416.         $response->headers->set('Content-Type''application/vnd.ms-excel');
  417.         $response->headers->set('Content-Disposition''attachment; filename=' '"' $fileName '"');
  418.         $response->headers->set('Cache-Control','max-age=0');
  419.         return $response;
  420.         //$this->addFlash('success', "Les stagiaires sont télechargées avec succès.");
  421.        // return $this->redirectToRoute('app_trainees');
  422.     }
  423.     #[Route('/downloadTraineeByFormation/{idFormation}'name'app_download_trainee_by_formation')]
  424.     public function downloadTraineeByFormation(EntityManagerInterface $entityManager$idFormation null): Response
  425.     {
  426.         $course =  $entityManager->getRepository(Formation::class)->find($idFormation);
  427.         $formationUser $entityManager->getRepository(TraineeFormation::class)->findBy(['formation' => $course]);
  428.         $spreadsheet = new Spreadsheet();
  429.         $sheet $spreadsheet->getActiveSheet();
  430.         $sheet->setCellValue('A1''Nom');
  431.         $sheet->setCellValue('B1''Prénom');
  432.         $sheet->setCellValue('C1''Fonction');
  433.         $sheet->setCellValue('D1''Email');
  434.         $counter 2;
  435.         foreach ($formationUser as $item) {
  436.             $sheet->setCellValue('A' $counter$item->getTrainee()->getFirstName());
  437.             $sheet->setCellValue('B' $counter$item->getTrainee()->getLastName());
  438.             $sheet->setCellValue('C' $counter$item->getTrainee()->getPosition());
  439.             $sheet->setCellValue('D' $counter$item->getTrainee()->getEmail());
  440.             $counter++;
  441.         }
  442.         $writer = new Xls($spreadsheet);
  443.         $response =  new StreamedResponse(
  444.             function () use ($writer) {
  445.                 $writer->save('php://output');
  446.             }
  447.         );
  448.         $fileName "ExportEmails_".str_replace(' ','',$course->getNomFormation())."_".date('m-d-Y_hia').".xls";
  449.         $response->headers->set('Content-Type''application/vnd.ms-excel');
  450.         $response->headers->set('Content-Disposition''attachment; filename=' '"' $fileName '"');
  451.         $response->headers->set('Cache-Control','max-age=0');
  452.         return $response;
  453.         //$this->addFlash('success', "Les stagiaires sont télechargées avec succès.");
  454.         // return $this->redirectToRoute('app_trainees');
  455.     }
  456.     #[Route('/user/profileAdmin/{id}'name'app_edit_admin')]
  457.     public function updateAdminProfile(Request $requestEntityManagerInterface $entityManager$id): Response
  458.     {
  459.         if($id) {
  460.             $user $entityManager->getRepository(User::class)->findOneBy(['id' => $id]);
  461.         } else {
  462.             $user $this->getUser();
  463.         }
  464.         $form $this->createForm(UpdateUserFormType::class, $user);
  465.         $form->handleRequest($request);
  466.         $teacher false;
  467.         if (in_array('ROLE_TEACHER'$this->getUser()->getRoles(), true)) {
  468.             $teacher true;
  469.         }
  470.         if ($form->isSubmitted() && $form->isValid()) {
  471.             $user $form->getData();
  472.             $entityManager->persist($user);
  473.             $entityManager->flush();
  474.             // do anything else you need here, like send an email
  475.             if ($teacher) {
  476.                 return $this->redirectToRoute('app_trainer');
  477.             }
  478.             return $this->redirectToRoute('app_user');
  479.         }
  480.         return $this->render('user/update.html.twig', [
  481.             'setUserForm' => $form->createView(),
  482.             'teacher' => $teacher
  483.         ]);
  484.     }
  485.     #[Route('/user/profileFormateur/{id}'name'app_edit_formateur')]
  486.     public function updateFormateurProfile(Request $requestEntityManagerInterface $entityManager$id): Response
  487.     {
  488.         if($id) {
  489.             $user $entityManager->getRepository(User::class)->findOneBy(['id' => $id]);
  490.         } else {
  491.             $user $this->getUser();
  492.         }
  493.         $form $this->createForm(UpdateUserFormType::class, $user);
  494.         $form->handleRequest($request);
  495.         $teacher false;
  496.         if (in_array('ROLE_TEACHER'$this->getUser()->getRoles(), true)) {
  497.             $teacher true;
  498.         }
  499.         if ($form->isSubmitted() && $form->isValid()) {
  500.             $user $form->getData();
  501.             $entityManager->persist($user);
  502.             $entityManager->flush();
  503.             // do anything else you need here, like send an email
  504.             if ($teacher) {
  505.                 return $this->redirectToRoute('app_trainer');
  506.             }
  507.             return $this->redirectToRoute('app_user');
  508.         }
  509.         return $this->render('user/update.html.twig', [
  510.             'setUserForm' => $form->createView(),
  511.             'teacher' => $teacher
  512.         ]);
  513.     }
  514.     #[Route('/user/loginTeacher/{id}'name'app_login_formateur')]
  515.     public function loginTeacher(EntityManagerInterface $entityManager,
  516.                                  UserAuthenticatorInterface $userAuthenticator,
  517.                                  AppAuthenticator $authenticator,
  518.                                  Request $request,
  519.         $id): Response
  520.     {
  521.         if (in_array('ROLE_SUPER_ADMIN'$this->getUser()->getRoles(), true)) {
  522.             $admin =$this->getUser();
  523.             $request->getSession()->set('adminId'$admin->getId());
  524.         }
  525.         // Load the user by ID (assuming Doctrine)
  526.         $user $entityManager->getRepository(User::class)->findOneBy(['id' => $id]);
  527.         // Authenticate the user
  528.         $userAuthenticator->authenticateUser($user$authenticator$request);
  529.         // Redirect to homepage (or another route)
  530.         if (in_array('ROLE_SUPER_ADMIN'$this->getUser()->getRoles(), true)) {
  531.             return $this->redirectToRoute('app_home');
  532.         } else {
  533.             return $this->redirectToRoute('app_home_trainer');
  534.         }
  535.     }
  536. }