src/Controller/ProfilController.php line 1430

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Contracts\HttpClient\HttpClientInterface;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use App\Model\Globals;
  14. use App\Entity\DicGouvernorat;
  15. use App\Entity\DicDelegation;
  16. use App\Entity\DicImada;
  17. use App\Entity\DicCircuit;
  18. use App\Entity\Eleveur;
  19. use App\Entity\Inseminateur;
  20. use App\Entity\AgentOep;
  21. use App\Entity\Agent;
  22. use App\Entity\User;
  23. use App\Entity\Partenaire;
  24. use App\Form\EleveurType;
  25. use App\Form\InseminateurType;
  26. use App\Form\AgentOepType;
  27. use App\Form\AgentType;
  28. use App\Form\UserType;
  29. use Psr\Log\LoggerInterface;
  30. /**
  31.  * @Route("/{_locale}", requirements={"_locale": "ar|fr"})
  32.  */
  33. class ProfilController extends AbstractController
  34. {
  35.     private $httpClient;
  36.     public function __construct(HttpClientInterface $httpClient)
  37.     {
  38.         $this->httpClient $httpClient;
  39.     }
  40.     private function isPasswordStrong($password): bool
  41.     {
  42.         $pattern '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/';
  43.         return preg_match($pattern$password);
  44.     }
  45.     #[Route('/eleveur/register'name'eleveur_register'methods: ['GET''POST'])]
  46.     public function registerEleveur(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher,  LoggerInterface $logger): Response
  47.     {
  48.         $logger->info('eleveur register ');
  49.         $em $doctrine->getManager();
  50.         $user = new User();
  51.         $eleveur = new Eleveur();
  52.         $gouvernorats $doctrine->getRepository(DicGouvernorat::class)->findAll();
  53.         $delegations $doctrine->getRepository(DicDelegation::class)->findAll();
  54.         $imadas $doctrine->getRepository(DicImada::class)->findAll();
  55.         $form $this->createForm(EleveurType::class, $eleveur);
  56.         $form->handleRequest($request);
  57.         //&& $form->isValid()
  58.         if ($form->isSubmitted() && $form->isValid()) {
  59.             $logger->info('eleveur register post valid ');
  60.             if ($request->request->has('g-recaptcha-response')) {
  61.                // dump($request);die;
  62.                
  63.             /**** verification recaptcha avec siteverify ****************/   
  64.                $recapResp $request->get('g-recaptcha-response');
  65.                 $logger->info('eleveur register post '.$recapResp);
  66.                 if ($recapResp && $recapResp != '') {
  67.                     $recapResp $request->get('g-recaptcha-response');
  68.                     $recaptchaSecret '6LdCul0hAAAAALOxxC6Dz8Uqyh77-Q8zw4NhSAf7';
  69.                     $verificationResponse $this->httpClient->request(
  70.                         'POST',
  71.                         'https://www.google.com/recaptcha/api/siteverify',
  72.                         [
  73.                             'body' => [
  74.                                 'secret' => $recaptchaSecret,
  75.                                 'response' => $recapResp,
  76.                             ],
  77.                         ]
  78.                     );
  79.                     $verificationData $verificationResponse->toArray();
  80.                     //dump($recapResp, $verificationData);die;
  81.                     $logger->info('eleveur secret post '.$recaptchaSecret);
  82.                     $logger->info('recap response '.$recapResp);
  83.                     $logger->info('eleveur register repsonse  post ' json_encode($verificationData));
  84.                 /**** end verification avec google recaptcha verify  */
  85.                     /*if (isset($verificationData['success']) && $verificationData['success'] === true) {*/
  86.                         $isExist $doctrine->getRepository(Eleveur::class)->findOneBy(['codElv' => $request->request->get('username')]);
  87.                         if ($isExist != null) {
  88.                             try {
  89.                                 //$isExistUser= $doctrine->getRepository(User::class)->isExist($request->request->get('email'),$request->request->get('username'));
  90.                                 $isExistUser $doctrine->getRepository(User::class)->isExist($request->request->get('username'));
  91.                                 if (count($isExistUser) == 0) {
  92.                                     $password $request->request->get('password');
  93.                                     // Server-side password validation
  94.                                     if (!$this->isPasswordStrong($password)) {
  95.                                         // $this->addFlash('pass_error', 'Le mot de passe doit contenir au moins 8 caractères, une majuscule, une minuscule et un chiffre.');
  96.                                         //dump($password);die;
  97.                                         $this->addFlash('pass_error''true');
  98.                                         return $this->redirectToRoute('eleveur_register');
  99.                                     }
  100.                                     $user = new User();
  101.                                     $user->setEmail($request->request->get('email'));
  102.                                     $user->setPassword(
  103.                                         $passwordHasher->hashPassword(
  104.                                             $user,
  105.                                             $request->request->get('password')
  106.                                         )
  107.                                     );
  108.                                     $user->setRoles(["ROLE_ELEVEUR"]);
  109.                                     $user->setUsername($request->request->get('username'));
  110.                                     $user->setEtat(1);
  111.                                     $user->setCreatedAt(new \DateTime());
  112.                                     $em->persist($user);
  113.                                     $em->flush();
  114.                                     $isExist->setUser($user);
  115.                                     $isExist->setImada($eleveur->getImada());
  116.                                     $isExist->setDelegation($isExist->getDelegation());
  117.                                     $isExist->setGouvernorat($isExist->getGouvernorat());
  118.                                     $isExist->setNprElvFr($eleveur->getNprElvFr());
  119.                                     $isExist->setNprElvAr($eleveur->getNprElvAr());
  120.                                     $isExist->setAdresse($eleveur->getAdresse());
  121.                                     $isExist->setNumTel($eleveur->getNumTel());
  122.                                     $isExist->setNumFax($eleveur->getNumFax());
  123.                                     $isExist->setTypeElv($eleveur->getTypeElv());
  124.                                     $isExist->setIdentElv($eleveur->getIdentElv());
  125.                                     $isExist->setUpdatedAt(new \DateTime());
  126.                                     $em->persist($isExist);
  127.                                     $em->flush();
  128.                                     $em->flush();
  129.                                     $this->addFlash('success''Votre inscription a été effectuée avec succès !');
  130.                                     return $this->redirectToRoute('login');
  131.                                 } else {
  132.                                     $this->addFlash(
  133.                                         'failure_register_user_exist',
  134.                                         'true'
  135.                                     );
  136.                                     $user->setEmail($request->request->get('email'));
  137.                                     $user->setUsername($request->request->get('username'));
  138.                                     return $this->render('profil/register_eleveur.html.twig', [
  139.                                         'controller_name' => 'ProfilController',
  140.                                         'gouvernorats' => $gouvernorats,
  141.                                         'delegations' => $delegations,
  142.                                         'imadas' => $imadas,
  143.                                         'eleveur' => $eleveur,
  144.                                         'user' => $user,
  145.                                         'form' => $form->createView(),
  146.                                     ]);
  147.                                 }
  148.                             } catch (Exception $e) {
  149.                                 return $this->redirectToRoute('eleveur_register');
  150.                             }
  151.                         } else {
  152.                             $user->setEmail($request->request->get('email'));
  153.                             $user->setUsername($request->request->get('username'));
  154.                             $this->addFlash(
  155.                                 'failure_register_no_eleveur',
  156.                                 'true'
  157.                             );
  158.                             return $this->render('profil/register_eleveur.html.twig', [
  159.                                 'controller_name' => 'ProfilController',
  160.                                 'gouvernorats' => $gouvernorats,
  161.                                 'delegations' => $delegations,
  162.                                 'imadas' => $imadas,
  163.                                 'eleveur' => $eleveur,
  164.                                 'user' => $user,
  165.                                 'form' => $form->createView(),
  166.                             ]);
  167.                         } //end is exist user
  168.                     /*}*/
  169.                 }
  170.                 //captcha is null
  171.             }
  172.             $this->addFlash(
  173.                 'failure_register',
  174.                 'true'
  175.             );
  176.             $user->setEmail($request->request->get('email'));
  177.             $user->setUsername($request->request->get('username'));
  178.             return $this->render('profil/register_eleveur.html.twig', [
  179.                 'controller_name' => 'ProfilController',
  180.                 'gouvernorats' => $gouvernorats,
  181.                 'delegations' => $delegations,
  182.                 'imadas' => $imadas,
  183.                 'eleveur' => $eleveur,
  184.                 'user' => $user,
  185.                 'form' => $form->createView(),
  186.             ]);
  187.         } //end submit form
  188.         return $this->render('profil/register_eleveur.html.twig', [
  189.             'controller_name' => 'ProfilController',
  190.             'gouvernorats' => $gouvernorats,
  191.             'delegations' => $delegations,
  192.             'imadas' => $imadas,
  193.             'eleveur' => $eleveur,
  194.             'user' => $user,
  195.             'form' => $form->createView(),
  196.         ]);
  197.     }
  198.     #[Route('/eleveur/rest_password'name'eleveur_reset_password'methods: ['GET''POST'])]
  199.     public function resetPasswordEleveur(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasherMailerInterface $mailer): Response
  200.     {
  201.         $em $doctrine->getManager();
  202.         $user = new User();
  203.         $form $this->createForm(UserType::class, $user);
  204.         $form->handleRequest($request);
  205.         //&& $form->isValid()
  206.         if ($form->isSubmitted()) {
  207.             if ($request->request->has('g-recaptcha-response')) {
  208.                 if ($request->request->get('g-recaptcha-response') != '') {
  209.                     $userExist $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
  210.                     if ($userExist !== null) {
  211.                         try {
  212.                             $password = (string)(random_int(100000999999));
  213.                             $userExist->setPassword(
  214.                                 $passwordHasher->hashPassword(
  215.                                     $userExist,
  216.                                     $password
  217.                                 )
  218.                             );
  219.                             $em->persist($userExist);
  220.                             $em->flush();
  221.                             $to $user->getEmail();
  222.                             //$to='ryahyaoui@progress.tn';
  223.                             $sub '[OEP] Votre mot de passe';
  224.                             $msg '<p>Bonjour ' $user->getUsername() . ', <br/> Votre mot de passe est ' $password '.</p>';
  225.                             $globals = new Globals();
  226.                             $globals->sendEmail($mailer$to$sub$msg);
  227.                             return $this->redirectToRoute('login');
  228.                         } catch (Exception $e) {
  229.                             return $this->redirectToRoute('eleveur_reset_password');
  230.                         }
  231.                     } else {
  232.                         $user->setEmail($user->getEmail());
  233.                         $this->addFlash(
  234.                             'failure_register',
  235.                             'true'
  236.                         );
  237.                         return $this->render('security/reset_password_eleveur.html.twig', [
  238.                             'controller_name' => 'ProfilController',
  239.                             'user' => $user,
  240.                             'form' => $form->createView(),
  241.                         ]);
  242.                     } //end user exist
  243.                 }
  244.             }
  245.             $user->setEmail($user->getEmail());
  246.             $this->addFlash(
  247.                 'failure_recaptcha',
  248.                 'true'
  249.             );
  250.             return $this->render('security/reset_password_eleveur.html.twig', [
  251.                 'controller_name' => 'ProfilController',
  252.                 'user' => $user,
  253.                 'form' => $form->createView(),
  254.             ]);
  255.         } //end submit
  256.         return $this->render('security/reset_password_eleveur.html.twig', [
  257.             'controller_name' => 'ProfilController',
  258.             'user' => $user,
  259.             'form' => $form->createView(),
  260.         ]);
  261.     }
  262.     /**
  263.      * @IsGranted("ROLE_ELEVEUR")
  264.      */
  265.     #[Route('/eleveur/profil'name'eleveur_profil'methods: ['GET''POST'])]
  266.     public function profilEleveur(Request $requestManagerRegistry $doctrine): Response
  267.     {
  268.         if ($this->isGranted('ROLE_ELEVEUR') == false) {
  269.             return $this->render('erreur/access_denied.html.twig', []);
  270.         }
  271.         $em $doctrine->getManager();
  272.         $user $this->getUser();
  273.         $eleveur $doctrine->getRepository(Eleveur::class)->findOneBy(['user' => $user]);
  274.         $gouvernorats $doctrine->getRepository(DicGouvernorat::class)->findAll();
  275.         $delegations null;
  276.         $imadas null;
  277.         if ($eleveur->getGouvernorat() != null) {
  278.             $delegations $doctrine->getRepository(DicDelegation::class)->findBy(['gouvernorat' => $eleveur->getGouvernorat()]);
  279.         }
  280.         if ($eleveur->getDelegation() != null) {
  281.             $imadas $doctrine->getRepository(DicImada::class)->findBy(['delegation' => $eleveur->getDelegation()]);
  282.         }
  283.         $form $this->createForm(EleveurType::class, $eleveur);
  284.         $form->handleRequest($request);
  285.         //&& $form->isValid()
  286.         if ($form->isSubmitted()) {
  287.             $eleveur->setUpdatedAt(new \DateTime());
  288.             $em->persist($eleveur);
  289.             $em->flush();
  290.             return $this->redirectToRoute('eleveur_profil');
  291.         }
  292.         return $this->render('profil/profil_eleveur.html.twig', [
  293.             'controller_name' => 'ProfilController',
  294.             'gouvernorats' => $gouvernorats,
  295.             'delegations' => $delegations,
  296.             'imadas' => $imadas,
  297.             'eleveur' => $eleveur,
  298.             'form' => $form->createView(),
  299.         ]);
  300.     }
  301.     /**
  302.      * @IsGranted("ROLE_ELEVEUR")
  303.      */
  304.     #[Route('/eleveur/password/edit'name'eleveur_edit_password'methods: ['GET''POST'])]
  305.     public function passwordEleveur(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  306.     {
  307.         if ($this->isGranted('ROLE_ELEVEUR') == false) {
  308.             return $this->render('erreur/access_denied.html.twig', []);
  309.         }
  310.         $em $doctrine->getManager();
  311.         $user $this->getUser();
  312.         if ($passwordHasher->isPasswordValid($user$request->request->get('password'))) {
  313.             if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
  314.                 $password $request->request->get('newPassword');
  315.                 if (!$this->isPasswordStrong($password)) {
  316.                     $this->addFlash('pass_error''true');
  317.                     return $this->redirectToRoute('eleveur_profil');
  318.                 }
  319.                 $user->setPassword(
  320.                     $passwordHasher->hashPassword(
  321.                         $user,
  322.                         $request->request->get('newPassword')
  323.                     )
  324.                 );
  325.                 $user->setUpdatedAt(new \DateTime());
  326.                 $em->persist($user);
  327.                 $em->flush();
  328.                 $this->addFlash(
  329.                     'success_password_edit',
  330.                     'true'
  331.                 );
  332.             } else {
  333.                 $this->addFlash(
  334.                     'success_password_edit',
  335.                     'false'
  336.                 );
  337.             }
  338.         } else {
  339.             $this->addFlash(
  340.                 'success_password_edit',
  341.                 'false'
  342.             );
  343.         }
  344.         return $this->redirectToRoute('eleveur_profil');
  345.     }
  346.     /*====================================== Inseminateur ========================================*/
  347.     #[Route('/inseminateur/register'name'inseminateur_register'methods: ['GET''POST'])]
  348.     public function registerInseminateur(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  349.     {
  350.         $em $doctrine->getManager();
  351.         $user = new User();
  352.         $inseminateur = new Inseminateur();
  353.         $gouvernorats $doctrine->getRepository(DicGouvernorat::class)->findAll();
  354.         $qb $em->createQueryBuilder();
  355.         $query $qb->select('c')
  356.             ->from('App:DicCircuit''c')
  357.             ->orderBy('c.desCirLt''ASC');
  358.         $circuits $query->getQuery()->getResult();
  359.         $form $this->createForm(InseminateurType::class, $inseminateur);
  360.         $form->handleRequest($request);
  361.         //&& $form->isValid()
  362.         if ($form->isSubmitted() && $form->isValid()) {
  363.             if ($request->request->has('g-recaptcha-response')) {
  364.                 if ($request->request->get('g-recaptcha-response') != '') {
  365.                     $isExist $doctrine->getRepository(inseminateur::class)->findOneBy(['codIns' => $request->request->get('username')]);
  366.                     if ($isExist != null) {
  367.                         try {
  368.                             $isExistUser $doctrine->getRepository(User::class)->isExist($request->request->get('email'), $request->request->get('username'));
  369.                             $circuitId $request->request->get('circuitt');
  370.                             $dicCircuit $doctrine->getRepository(DicCircuit::class)->find($circuitId);
  371.                             if (count($isExistUser) == 0) {
  372.                                 $password $request->request->get('password');
  373.                                 if (!$this->isPasswordStrong($password)) {
  374.                                     $this->addFlash('pass_error''true');
  375.                                     return $this->redirectToRoute('inseminateur_register');
  376.                                 }
  377.                                 $user = new User();
  378.                                 $user->setEmail($request->request->get('email'));
  379.                                 $user->setPassword(
  380.                                     $passwordHasher->hashPassword(
  381.                                         $user,
  382.                                         $request->request->get('password')
  383.                                     )
  384.                                 );
  385.                                 $user->setRoles(["ROLE_INSEMINATEUR"]);
  386.                                 $user->setUsername($request->request->get('username'));
  387.                                 $user->setEtat(1);
  388.                                 $user->setCreatedAt(new \DateTime());
  389.                                 $em->persist($user);
  390.                                 $em->flush();
  391.                                 $isExist->setUser($user);
  392.                                 $isExist->setDicCircuit($isExist->getDicCircuit());
  393.                                 $isExist->setDicGouvernorat($isExist->getDicGouvernorat());
  394.                                 $isExist->setNprInsFr($inseminateur->getNprInsFr());
  395.                                 $isExist->setNprInsAr($inseminateur->getNprInsAr());
  396.                                 $isExist->setAdresse($inseminateur->getAdresse());
  397.                                 $isExist->setNumTel($inseminateur->getNumTel());
  398.                                 $isExist->setNumFax($isExist->getNumFax());
  399.                                 $isExist->setTypeIns($isExist->getTypeIns());
  400.                                 $isExist->setIdentIns($isExist->getIdentIns());
  401.                                 $isExist->setUpdatedAt(new \DateTime());
  402.                                 $em->persist($isExist);
  403.                                 $em->flush();
  404.                                 $this->addFlash('success''Votre inscription a été effectuée avec succès !');
  405.                                 return $this->redirectToRoute('login');
  406.                             } else {
  407.                                 $this->addFlash(
  408.                                     'failure_register_user_exist',
  409.                                     'true'
  410.                                 );
  411.                                 $user->setEmail($request->request->get('email'));
  412.                                 $user->setUsername($request->request->get('username'));
  413.                                 return $this->render('profil/register_inseminateur.html.twig', [
  414.                                     'controller_name' => 'ProfilController',
  415.                                     'inseminateur' => $inseminateur,
  416.                                     'circuit' => $circuits,
  417.                                     'user' => $user,
  418.                                     'form' => $form->createView(),
  419.                                 ]);
  420.                             }
  421.                         } catch (Exception $e) {
  422.                             return $this->redirectToRoute('inseminateur_register');
  423.                         }
  424.                     } else {
  425.                         $user->setEmail($request->request->get('email'));
  426.                         $user->setUsername($request->request->get('username'));
  427.                         $this->addFlash(
  428.                             'failure_register_no_ins',
  429.                             'true'
  430.                         );
  431.                         return $this->render('profil/register_inseminateur.html.twig', [
  432.                             'controller_name' => 'ProfilController',
  433.                             'inseminateur' => $inseminateur,
  434.                             'circuit' => $circuits,
  435.                             'user' => $user,
  436.                             'form' => $form->createView(),
  437.                         ]);
  438.                     } //end user exist
  439.                 //has captcha
  440.             //captcha is null
  441.             $user->setEmail($request->request->get('email'));
  442.             $user->setUsername($request->request->get('username'));
  443.             $this->addFlash(
  444.                 'failure_register',
  445.                 'true'
  446.             );
  447.             return $this->render('profil/register_inseminateur.html.twig', [
  448.                 'controller_name' => 'ProfilController',
  449.                 'inseminateur' => $inseminateur,
  450.                 'user' => $user,
  451.                 'circuit' => $circuits,
  452.                 'form' => $form->createView(),
  453.             ]);
  454.         } //end submit
  455.         $qb $em->createQueryBuilder();
  456.         $query $qb->select('c')
  457.             ->from('App:DicCircuit''c')
  458.             ->orderBy('c.desCirLt''ASC');
  459.         $circuits $query->getQuery()->getResult();
  460.         //dump($circuits);die;
  461.         return $this->render('profil/register_inseminateur.html.twig', [
  462.             'controller_name' => 'ProfilController',
  463.             'inseminateur' => $inseminateur,
  464.             'user' => $user,
  465.             'circuit' => $circuits,
  466.             'form' => $form->createView(),
  467.         ]);
  468.     }
  469.     /**
  470.      * @IsGranted("ROLE_INSEMINATEUR")
  471.      */
  472.     #[Route('/inseminateur/rest_password'name'inseminateur_reset_password'methods: ['GET''POST'])]
  473.     public function resetPasswordInseminateur(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasherMailerInterface $mailer): Response
  474.     {
  475.         $em $doctrine->getManager();
  476.         $user = new User();
  477.         $form $this->createForm(UserType::class, $user);
  478.         $form->handleRequest($request);
  479.         //&& $form->isValid()
  480.         if ($form->isSubmitted()) {
  481.             if ($request->request->has('g-recaptcha-response')) {
  482.                 if ($request->request->get('g-recaptcha-response') != '') {
  483.                     $userExist $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
  484.                     if ($userExist !== null) {
  485.                         try {
  486.                             $password = (string)(random_int(100000999999));
  487.                             $userExist->setPassword(
  488.                                 $passwordHasher->hashPassword(
  489.                                     $userExist,
  490.                                     $password
  491.                                 )
  492.                             );
  493.                             $em->persist($userExist);
  494.                             $em->flush();
  495.                             $to $user->getEmail();
  496.                             $sub '[OEP] Votre mot de passe';
  497.                             $msg '<p>Bonjour ' $user->getUsername() . ', <br/> Votre mot de passe est ' $password '.</p>';
  498.                             $globals = new Globals();
  499.                             $globals->sendEmail($mailer$to$sub$msg);
  500.                             return $this->redirectToRoute('login');
  501.                         } catch (Exception $e) {
  502.                             return $this->redirectToRoute('inseminateur_reset_password');
  503.                         }
  504.                     } else {
  505.                         $user->setEmail($user->getEmail());
  506.                         $this->addFlash(
  507.                             'failure_register',
  508.                             'true'
  509.                         );
  510.                         return $this->render('security/reset_password_inseminateur.html.twig', [
  511.                             'controller_name' => 'ProfilController',
  512.                             'user' => $user,
  513.                             'form' => $form->createView(),
  514.                         ]);
  515.                     } //end exist user
  516.                 }
  517.             }
  518.             $user->setEmail($user->getEmail());
  519.             $this->addFlash(
  520.                 'failure_recaptcha',
  521.                 'true'
  522.             );
  523.             return $this->render('security/reset_password_inseminateur.html.twig', [
  524.                 'controller_name' => 'ProfilController',
  525.                 'user' => $user,
  526.                 'form' => $form->createView(),
  527.             ]);
  528.         } //end submit
  529.         return $this->render('security/reset_password_inseminateur.html.twig', [
  530.             'controller_name' => 'ProfilController',
  531.             'user' => $user,
  532.             'form' => $form->createView(),
  533.         ]);
  534.     }
  535.     /**
  536.      * @IsGranted("ROLE_INSEMINATEUR")
  537.      */
  538.     #[Route('/inseminateur/profil'name'inseminateur_profil'methods: ['GET''POST'])]
  539.     public function profilInseminateur(Request $requestManagerRegistry $doctrine): Response
  540.     {
  541.         if ($this->isGranted('ROLE_INSEMINATEUR') == false) {
  542.             return $this->render('erreur/access_denied.html.twig', []);
  543.         }
  544.         $locale $request->getLocale();
  545.         $em $doctrine->getManager();
  546.         $user $this->getUser();
  547.         $inseminateur $doctrine->getRepository(Inseminateur::class)->findOneBy(['user' => $user]);
  548.         $gouvernorats $doctrine->getRepository(DicGouvernorat::class)->findAll();
  549.         $form $this->createForm(InseminateurType::class, $inseminateur, ['locale' => $locale]);
  550.         $form->handleRequest($request);
  551.         //&& $form->isValid()
  552.         if ($form->isSubmitted()) {
  553.             $inseminateur->setUpdatedAt(new \DateTime());
  554.             $em->persist($inseminateur);
  555.             $em->flush();
  556.             return $this->redirectToRoute('inseminateur_profil');
  557.         }
  558.         return $this->render('profil/profil_inseminateur.html.twig', [
  559.             'controller_name' => 'ProfilController',
  560.             'gouvernorats' => $gouvernorats,
  561.             'inseminateur' => $inseminateur,
  562.             'form' => $form->createView(),
  563.         ]);
  564.     }
  565.     /**
  566.      * @IsGranted("ROLE_INSEMINATEUR")
  567.      */
  568.     #[Route('/inseminateur/password/edit'name'inseminateur_edit_password'methods: ['GET''POST'])]
  569.     public function passwordInseminateur(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  570.     {
  571.         if ($this->isGranted('ROLE_INSEMINATEUR') == false) {
  572.             return $this->render('erreur/access_denied.html.twig', []);
  573.         }
  574.         $em $doctrine->getManager();
  575.         $user $this->getUser();
  576.         if ($passwordHasher->isPasswordValid($user$request->request->get('password'))) {
  577.             if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
  578.                 $password $request->request->get('newPassword');
  579.                 if (!$this->isPasswordStrong($password)) {
  580.                     $this->addFlash('pass_error''true');
  581.                     return $this->redirectToRoute('inseminateur_profil');
  582.                 }
  583.                 $user->setPassword(
  584.                     $passwordHasher->hashPassword(
  585.                         $user,
  586.                         $request->request->get('newPassword')
  587.                     )
  588.                 );
  589.                 $user->setUpdatedAt(new \DateTime());
  590.                 $em->persist($user);
  591.                 $em->flush();
  592.                 $this->addFlash(
  593.                     'success_password_edit',
  594.                     'true'
  595.                 );
  596.             } else {
  597.                 $this->addFlash(
  598.                     'success_password_edit',
  599.                     'false'
  600.                 );
  601.             }
  602.         } else {
  603.             $this->addFlash(
  604.                 'success_password_edit',
  605.                 'false'
  606.             );
  607.         }
  608.         return $this->redirectToRoute('inseminateur_profil');
  609.     }
  610.     /*====================================== Agent Oep ========================================*/
  611.     /**
  612.      * @IsGranted("ROLE_ADMIN")
  613.      */
  614.     #[Route('/agentOEP/register'name'agent_oep_register'methods: ['GET''POST'])]
  615.     public function registerAgentOep(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  616.     {
  617.         $em $doctrine->getManager();
  618.         $user = new User();
  619.         $agent = new AgentOep();
  620.         $gouvernorats $doctrine->getRepository(DicGouvernorat::class)->findAll();
  621.         $delegations $doctrine->getRepository(DicDelegation::class)->findAll();
  622.         $imadas $doctrine->getRepository(DicImada::class)->findAll();
  623.         $form $this->createForm(AgentOepType::class, $agent);
  624.         $form->handleRequest($request);
  625.         //&& $form->isValid()
  626.         if ($form->isSubmitted()) {
  627.             if ($request->request->has('g-recaptcha-response')) {
  628.                 if ($request->request->get('g-recaptcha-response') != '') {
  629.                     $isExist $doctrine->getRepository(AgentOep::class)->findOneBy(['codAgent' => $request->request->get('username')]);
  630.                     if ($isExist != null) {
  631.                         try {
  632.                             $isExistUser $doctrine->getRepository(User::class)->isExist($request->request->get('email'), $request->request->get('username'));
  633.                             if (count($isExistUser) == 0) {
  634.                                 $user = new User();
  635.                                 $user->setEmail($request->request->get('email'));
  636.                                 $user->setPassword(
  637.                                     $passwordHasher->hashPassword(
  638.                                         $user,
  639.                                         $request->request->get('password')
  640.                                     )
  641.                                 );
  642.                                 $user->setRoles(["ROLE_AGENT_OEP"]);
  643.                                 $user->setUsername($request->request->get('username'));
  644.                                 $user->setEtat(1);
  645.                                 $user->setCreatedAt(new \DateTime());
  646.                                 $em->persist($user);
  647.                                 $em->flush();
  648.                                 $isExist->setUser($user);
  649.                                 $isExist->setImada($agent->getImada());
  650.                                 $isExist->setDelegation($agent->getDelegation());
  651.                                 $isExist->setGouvernorat($agent->getGouvernorat());
  652.                                 $isExist->setNprAgentFr($agent->getNprAgentFr());
  653.                                 $isExist->setNprAgentAr($agent->getNprAgentAr());
  654.                                 $isExist->setAdresse($agent->getAdresse());
  655.                                 $isExist->setNumTel($agent->getNumTel());
  656.                                 $isExist->setNumFax($agent->getNumFax());
  657.                                 $isExist->getTypeAgent($agent->getTypeAgent());
  658.                                 $isExist->setIdentAgent($agent->getIdentAgent());
  659.                                 $isExist->setUpdatedAt(new \DateTime());
  660.                                 $em->persist($isExist);
  661.                                 $em->flush();
  662.                                 return $this->redirectToRoute('login');
  663.                             } else {
  664.                                 $this->addFlash(
  665.                                     'failure_register_user_exist',
  666.                                     'true'
  667.                                 );
  668.                                 $user->setEmail($request->request->get('email'));
  669.                                 $user->setUsername($request->request->get('username'));
  670.                                 return $this->render('profil/register_agent_oep.html.twig', [
  671.                                     'controller_name' => 'ProfilController',
  672.                                     'gouvernorats' => $gouvernorats,
  673.                                     'delegations' => $delegations,
  674.                                     'imadas' => $imadas,
  675.                                     'agent' => $agent,
  676.                                     'user' => $user,
  677.                                     'form' => $form->createView(),
  678.                                 ]);
  679.                             }
  680.                         } catch (Exception $e) {
  681.                             return $this->redirectToRoute('agent_oep_register');
  682.                         }
  683.                     } else {
  684.                         $user->setEmail($request->request->get('email'));
  685.                         $user->setUsername($request->request->get('username'));
  686.                         $this->addFlash(
  687.                             'failure_register_no_agent',
  688.                             'true'
  689.                         );
  690.                         return $this->render('profil/register_agent_oep.html.twig', [
  691.                             'controller_name' => 'ProfilController',
  692.                             'gouvernorats' => $gouvernorats,
  693.                             'delegations' => $delegations,
  694.                             'imadas' => $imadas,
  695.                             'agent' => $agent,
  696.                             'user' => $user,
  697.                             'form' => $form->createView(),
  698.                         ]);
  699.                     } //end exist
  700.                 //has captcha
  701.             //captcha is null
  702.             $user->setEmail($request->request->get('email'));
  703.             $user->setUsername($request->request->get('username'));
  704.             $this->addFlash(
  705.                 'failure_register',
  706.                 'true'
  707.             );
  708.             return $this->render('profil/register_agent_oep.html.twig', [
  709.                 'controller_name' => 'ProfilController',
  710.                 'gouvernorats' => $gouvernorats,
  711.                 'delegations' => $delegations,
  712.                 'imadas' => $imadas,
  713.                 'agent' => $agent,
  714.                 'user' => $user,
  715.                 'form' => $form->createView(),
  716.             ]);
  717.         } //end submit
  718.         return $this->render('profil/register_agent_oep.html.twig', [
  719.             'controller_name' => 'ProfilController',
  720.             'gouvernorats' => $gouvernorats,
  721.             'delegations' => $delegations,
  722.             'imadas' => $imadas,
  723.             'agent' => $agent,
  724.             'user' => $user,
  725.             'form' => $form->createView(),
  726.         ]);
  727.     }
  728.     #[Route('/agentOEP/rest_password'name'agent_oep_reset_password'methods: ['GET''POST'])]
  729.     public function resetPasswordAgentOEP(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasherMailerInterface $mailer): Response
  730.     {
  731.         $em $doctrine->getManager();
  732.         $user = new User();
  733.         $form $this->createForm(UserType::class, $user);
  734.         $form->handleRequest($request);
  735.         //&& $form->isValid()
  736.         if ($form->isSubmitted()) {
  737.             if ($request->request->has('g-recaptcha-response')) {
  738.                 if ($request->request->get('g-recaptcha-response') != '') {
  739.                     $userExist $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
  740.                     if ($userExist !== null) {
  741.                         try {
  742.                             $password = (string)(random_int(100000999999));
  743.                             $userExist->setPassword(
  744.                                 $passwordHasher->hashPassword(
  745.                                     $userExist,
  746.                                     $password
  747.                                 )
  748.                             );
  749.                             $em->persist($userExist);
  750.                             $em->flush();
  751.                             $to $user->getEmail();
  752.                             $sub '[OEP] Votre mot de passe';
  753.                             $msg '<p>Bonjour ' $user->getUsername() . ', <br/> Votre mot de passe est ' $password '.</p>';
  754.                             $globals = new Globals();
  755.                             $globals->sendEmail($mailer$to$sub$msg);
  756.                             return $this->redirectToRoute('login');
  757.                         } catch (Exception $e) {
  758.                             return $this->redirectToRoute('agent_oep_reset_password');
  759.                         }
  760.                     } else {
  761.                         $user->setEmail($user->getEmail());
  762.                         $this->addFlash(
  763.                             'failure_register',
  764.                             'true'
  765.                         );
  766.                         return $this->render('security/reset_password_agent_oep.html.twig', [
  767.                             'controller_name' => 'ProfilController',
  768.                             'user' => $user,
  769.                             'form' => $form->createView(),
  770.                         ]);
  771.                     } //end exist user
  772.                 }
  773.             }
  774.             $user->setEmail($user->getEmail());
  775.             $this->addFlash(
  776.                 'failure_recaptcha',
  777.                 'true'
  778.             );
  779.             return $this->render('security/reset_password_agent_oep.html.twig', [
  780.                 'controller_name' => 'ProfilController',
  781.                 'user' => $user,
  782.                 'form' => $form->createView(),
  783.             ]);
  784.         } //end submit
  785.         return $this->render('security/reset_password_agent_oep.html.twig', [
  786.             'controller_name' => 'ProfilController',
  787.             'user' => $user,
  788.             'form' => $form->createView(),
  789.         ]);
  790.     }
  791.     /**
  792.      * @IsGranted("ROLE_AGENT_OEP")
  793.      */
  794.     #[Route('/agentOEP/profil'name'agent_oep_profil'methods: ['GET''POST'])]
  795.     public function profilAgentOep(Request $requestManagerRegistry $doctrine): Response
  796.     {
  797.         if ($this->isGranted('ROLE_AGENT_OEP') == false) {
  798.             return $this->render('erreur/access_denied.html.twig', []);
  799.         }
  800.         $em $doctrine->getManager();
  801.         $user $this->getUser();
  802.         $agent $doctrine->getRepository(AgentOep::class)->findOneBy(['user' => $user]);
  803.         $gouvernorats $doctrine->getRepository(DicGouvernorat::class)->findAll();
  804.         $delegations $doctrine->getRepository(DicDelegation::class)->findBy(['gouvernorat' => $agent->getGouvernorat()]);
  805.         $imadas $doctrine->getRepository(DicImada::class)->findBy(['delegation' => $agent->getDelegation()]);
  806.         $form $this->createForm(AgentOepType::class, $agent);
  807.         $form->handleRequest($request);
  808.         //&& $form->isValid()
  809.         if ($form->isSubmitted()) {
  810.             $agent->setUpdatedAt(new \DateTime());
  811.             $em->persist($agent);
  812.             $em->flush();
  813.             return $this->redirectToRoute('agent_oep_profil');
  814.         }
  815.         return $this->render('profil/profil_agent_oep.html.twig', [
  816.             'controller_name' => 'ProfilController',
  817.             'gouvernorats' => $gouvernorats,
  818.             'delegations' => $delegations,
  819.             'imadas' => $imadas,
  820.             'agent' => $agent,
  821.             'form' => $form->createView(),
  822.         ]);
  823.     }
  824.     /**
  825.      * @IsGranted("ROLE_AGENT_OEP")
  826.      */
  827.     #[Route('/agentOEP/password/edit'name'agent_oep_edit_password'methods: ['GET''POST'])]
  828.     public function passwordAgentOep(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  829.     {
  830.         if ($this->isGranted('ROLE_AGENT_OEP') == false) {
  831.             return $this->render('erreur/access_denied.html.twig', []);
  832.         }
  833.         $em $doctrine->getManager();
  834.         $user $this->getUser();
  835.         if ($passwordHasher->isPasswordValid($user$request->request->get('password'))) {
  836.             if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
  837.                 $password $request->request->get('newPassword');
  838.                 if (!$this->isPasswordStrong($password)) {
  839.                     $this->addFlash('pass_error''true');
  840.                     return $this->redirectToRoute('agent_oep_profil');
  841.                 }
  842.                 $user->setPassword(
  843.                     $passwordHasher->hashPassword(
  844.                         $user,
  845.                         $request->request->get('newPassword')
  846.                     )
  847.                 );
  848.                 $user->setUpdatedAt(new \DateTime());
  849.                 $em->persist($user);
  850.                 $em->flush();
  851.                 $this->addFlash(
  852.                     'success_password_edit',
  853.                     'true'
  854.                 );
  855.             } else {
  856.                 $this->addFlash(
  857.                     'success_password_edit',
  858.                     'false'
  859.                 );
  860.             }
  861.         } else {
  862.             $this->addFlash(
  863.                 'success_password_edit',
  864.                 'false'
  865.             );
  866.         }
  867.         return $this->redirectToRoute('agent_oep_profil');
  868.     }
  869.     /*====================================== Agent ========================================*/
  870.     #[Route('/agent/register'name'agent_register'methods: ['GET''POST'])]
  871.     public function registerAgent(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  872.     {
  873.         $em $doctrine->getManager();
  874.         $user = new User();
  875.         $agent = new Agent();
  876.         $gouvernorats $doctrine->getRepository(DicGouvernorat::class)->findAll();
  877.         $delegations $doctrine->getRepository(DicDelegation::class)->findAll();
  878.         $imadas $doctrine->getRepository(DicImada::class)->findAll();
  879.         $form $this->createForm(AgentType::class, $agent);
  880.         $form->handleRequest($request);
  881.         //&& $form->isValid()
  882.         if ($form->isSubmitted() && $form->isValid()) {
  883.             if ($request->request->has('g-recaptcha-response')) {
  884.                 if ($request->request->get('g-recaptcha-response') != '') {
  885.                     $isExist $doctrine->getRepository(Agent::class)->findOneBy(['codAgent' => $request->request->get('username')]);
  886.                     if ($isExist != null) {
  887.                         try {
  888.                             $isExistUser $doctrine->getRepository(User::class)->isExist($request->request->get('email'), $request->request->get('username'));
  889.                             if (count($isExistUser) == 0) {
  890.                                 $password $request->request->get('password');
  891.                                 if (!$this->isPasswordStrong($password)) {
  892.                                     $this->addFlash('pass_error''true');
  893.                                     return $this->redirectToRoute('inseminateur_register');
  894.                                 }
  895.                                 $user = new User();
  896.                                 $user->setEmail($request->request->get('email'));
  897.                                 $user->setPassword(
  898.                                     $passwordHasher->hashPassword(
  899.                                         $user,
  900.                                         $request->request->get('password')
  901.                                     )
  902.                                 );
  903.                                 $user->setRoles(["ROLE_AGENT"]);
  904.                                 $user->setUsername($request->request->get('username'));
  905.                                 $user->setEtat(1);
  906.                                 $user->setCreatedAt(new \DateTime());
  907.                                 //dump($agent,$user);die;
  908.                                 $em->persist($user);
  909.                                 $em->flush();
  910.                                 $isExist->setUser($user);
  911.                                 $isExist->setImada($agent->getImada());
  912.                                 $isExist->setDelegation($agent->getDelegation());
  913.                                 $isExist->setGouvernorat($agent->getGouvernorat());
  914.                                 $isExist->setNprAgentFr($agent->getNprAgentFr());
  915.                                 $isExist->setNprAgentAr($agent->getNprAgentAr());
  916.                                 $isExist->setAdresse($agent->getAdresse());
  917.                                 $isExist->setNumTel($agent->getNumTel());
  918.                                 $isExist->setNumFax($agent->getNumFax());
  919.                                 $isExist->getTypeAgent($agent->getTypeAgent());
  920.                                 $isExist->setIdentAgent($agent->getIdentAgent());
  921.                                 $isExist->setUpdatedAt(new \DateTime());
  922.                                 $em->persist($isExist);
  923.                                 $em->flush();
  924.                                 $this->addFlash('success''Votre inscription a été effectuée avec succès !');
  925.                                 return $this->redirectToRoute('login');
  926.                             } else {
  927.                                 $this->addFlash(
  928.                                     'failure_register_user_exist',
  929.                                     'true'
  930.                                 );
  931.                                 $user->setEmail($request->request->get('email'));
  932.                                 $user->setUsername($request->request->get('username'));
  933.                                 return $this->render('profil/register_agent.html.twig', [
  934.                                     'controller_name' => 'ProfilController',
  935.                                     'gouvernorats' => $gouvernorats,
  936.                                     'delegations' => $delegations,
  937.                                     'imadas' => $imadas,
  938.                                     'agent' => $agent,
  939.                                     'user' => $user,
  940.                                     'form' => $form->createView(),
  941.                                 ]);
  942.                             }
  943.                         } catch (Exception $e) {
  944.                             return $this->redirectToRoute('agent_register');
  945.                         }
  946.                     } else {
  947.                         $user->setEmail($request->request->get('email'));
  948.                         $user->setUsername($request->request->get('username'));
  949.                         $this->addFlash(
  950.                             'failure_register_no_agent',
  951.                             'true'
  952.                         );
  953.                         return $this->render('profil/register_agent.html.twig', [
  954.                             'controller_name' => 'ProfilController',
  955.                             'gouvernorats' => $gouvernorats,
  956.                             'delegations' => $delegations,
  957.                             'imadas' => $imadas,
  958.                             'agent' => $agent,
  959.                             'user' => $user,
  960.                             'form' => $form->createView(),
  961.                         ]);
  962.                     } //end exist
  963.                 //has captcha
  964.             //captcha is null
  965.             $user->setEmail($request->request->get('email'));
  966.             $user->setUsername($request->request->get('username'));
  967.             $this->addFlash(
  968.                 'failure_register',
  969.                 'true'
  970.             );
  971.             return $this->render('profil/register_agent.html.twig', [
  972.                 'controller_name' => 'ProfilController',
  973.                 'gouvernorats' => $gouvernorats,
  974.                 'delegations' => $delegations,
  975.                 'imadas' => $imadas,
  976.                 'agent' => $agent,
  977.                 'user' => $user,
  978.                 'form' => $form->createView(),
  979.             ]);
  980.         } //end submit
  981.         return $this->render('profil/register_agent.html.twig', [
  982.             'controller_name' => 'ProfilController',
  983.             'gouvernorats' => $gouvernorats,
  984.             'delegations' => $delegations,
  985.             'imadas' => $imadas,
  986.             'agent' => $agent,
  987.             'user' => $user,
  988.             'form' => $form->createView(),
  989.         ]);
  990.     }
  991.     #[Route('/agent/rest_password'name'agent_reset_password'methods: ['GET''POST'])]
  992.     public function resetPasswordAgent(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasherMailerInterface $mailer): Response
  993.     {
  994.         $em $doctrine->getManager();
  995.         $user = new User();
  996.         $form $this->createForm(UserType::class, $user);
  997.         $form->handleRequest($request);
  998.         //&& $form->isValid()
  999.         if ($form->isSubmitted()) {
  1000.             if ($request->request->has('g-recaptcha-response')) {
  1001.                 if ($request->request->get('g-recaptcha-response') != '') {
  1002.                     $userExist $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
  1003.                     if ($userExist !== null) {
  1004.                         try {
  1005.                             $password = (string)(random_int(100000999999));
  1006.                             $userExist->setPassword(
  1007.                                 $passwordHasher->hashPassword(
  1008.                                     $userExist,
  1009.                                     $password
  1010.                                 )
  1011.                             );
  1012.                             $em->persist($userExist);
  1013.                             $em->flush();
  1014.                             $to $user->getEmail();
  1015.                             $sub '[OEP] Votre mot de passe';
  1016.                             $msg '<p>Bonjour ' $user->getUsername() . ', <br/> Votre mot de passe est ' $password '.</p>';
  1017.                             $globals = new Globals();
  1018.                             $globals->sendEmail($mailer$to$sub$msg);
  1019.                             return $this->redirectToRoute('login');
  1020.                         } catch (Exception $e) {
  1021.                             return $this->redirectToRoute('agent_reset_password');
  1022.                         }
  1023.                     } else {
  1024.                         $user->setEmail($user->getEmail());
  1025.                         $this->addFlash(
  1026.                             'failure_register',
  1027.                             'true'
  1028.                         );
  1029.                         return $this->render('security/reset_password_agent.html.twig', [
  1030.                             'controller_name' => 'ProfilController',
  1031.                             'user' => $user,
  1032.                             'form' => $form->createView(),
  1033.                         ]);
  1034.                     } //end exist user
  1035.                 }
  1036.             }
  1037.             $user->setEmail($user->getEmail());
  1038.             $this->addFlash(
  1039.                 'failure_recaptcha',
  1040.                 'true'
  1041.             );
  1042.             return $this->render('security/reset_password_agent.html.twig', [
  1043.                 'controller_name' => 'ProfilController',
  1044.                 'user' => $user,
  1045.                 'form' => $form->createView(),
  1046.             ]);
  1047.         } //end submit
  1048.         return $this->render('security/reset_password_agent.html.twig', [
  1049.             'controller_name' => 'ProfilController',
  1050.             'user' => $user,
  1051.             'form' => $form->createView(),
  1052.         ]);
  1053.     }
  1054.     /**
  1055.      * @IsGranted("ROLE_AGENT")
  1056.      */
  1057.     #[Route('/agent/profil'name'agent_profil'methods: ['GET''POST'])]
  1058.     public function profilAgent(Request $requestManagerRegistry $doctrine): Response
  1059.     {
  1060.         if ($this->isGranted('ROLE_AGENT') == false) {
  1061.             return $this->render('erreur/access_denied.html.twig', []);
  1062.         }
  1063.         $em $doctrine->getManager();
  1064.         $user $this->getUser();
  1065.         $agent $doctrine->getRepository(Agent::class)->findOneBy(['user' => $user]);
  1066.         $gouvernorats $doctrine->getRepository(DicGouvernorat::class)->findAll();
  1067.         $delegations $doctrine->getRepository(DicDelegation::class)->findBy(['gouvernorat' => $agent->getGouvernorat()]);
  1068.         $imadas $doctrine->getRepository(DicImada::class)->findBy(['delegation' => $agent->getDelegation()]);
  1069.         $form $this->createForm(AgentType::class, $agent);
  1070.         $form->handleRequest($request);
  1071.         //&& $form->isValid()
  1072.         if ($form->isSubmitted()) {
  1073.             $agent->setUpdatedAt(new \DateTime());
  1074.             $em->persist($agent);
  1075.             $em->flush();
  1076.             return $this->redirectToRoute('agent_profil');
  1077.         }
  1078.         return $this->render('profil/profil_agent.html.twig', [
  1079.             'controller_name' => 'ProfilController',
  1080.             'gouvernorats' => $gouvernorats,
  1081.             'delegations' => $delegations,
  1082.             'imadas' => $imadas,
  1083.             'agent' => $agent,
  1084.             'form' => $form->createView(),
  1085.         ]);
  1086.     }
  1087.     /**
  1088.      * @IsGranted("ROLE_AGENT")
  1089.      */
  1090.     #[Route('/agent/password/edit'name'agent_edit_password'methods: ['GET''POST'])]
  1091.     public function passwordAgent(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  1092.     {
  1093.         if ($this->isGranted('ROLE_AGENT') == false) {
  1094.             return $this->render('erreur/access_denied.html.twig', []);
  1095.         }
  1096.         $em $doctrine->getManager();
  1097.         $user $this->getUser();
  1098.         if ($passwordHasher->isPasswordValid($user$request->request->get('password'))) {
  1099.             if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
  1100.                 $password $request->request->get('newPassword');
  1101.                 if (!$this->isPasswordStrong($password)) {
  1102.                     $this->addFlash('pass_error''true');
  1103.                     return $this->redirectToRoute('agent_profil');
  1104.                 }
  1105.                 $user->setPassword(
  1106.                     $passwordHasher->hashPassword(
  1107.                         $user,
  1108.                         $request->request->get('newPassword')
  1109.                     )
  1110.                 );
  1111.                 $user->setUpdatedAt(new \DateTime());
  1112.                 $em->persist($user);
  1113.                 $em->flush();
  1114.                 $this->addFlash(
  1115.                     'success_password_edit',
  1116.                     'true'
  1117.                 );
  1118.             } else {
  1119.                 $this->addFlash(
  1120.                     'success_password_edit',
  1121.                     'false'
  1122.                 );
  1123.             }
  1124.         } else {
  1125.             $this->addFlash(
  1126.                 'success_password_edit',
  1127.                 'false'
  1128.             );
  1129.         }
  1130.         return $this->redirectToRoute('agent_profil');
  1131.     }
  1132.     /*====================================== Admin ========================================*/
  1133.     /**
  1134.      * @IsGranted("ROLE_ADMIN")
  1135.      */
  1136.     #[Route('/admin/password/edit'name'admin_edit_password'methods: ['GET''POST'])]
  1137.     public function passwordAdmin(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  1138.     {
  1139.         if ($this->isGranted('ROLE_ADMIN') == false) {
  1140.             return $this->render('erreur/access_denied.html.twig', []);
  1141.         }
  1142.         $em $doctrine->getManager();
  1143.         $user $this->getUser();
  1144.         if ($passwordHasher->isPasswordValid($user$request->request->get('password'))) {
  1145.             if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
  1146.                 $password $request->request->get('newPassword');
  1147.                 if (!$this->isPasswordStrong($password)) {
  1148.                     $this->addFlash('pass_error''true');
  1149.                     return $this->redirectToRoute('admin_user_admin_add');
  1150.                 }
  1151.                 $user->setPassword(
  1152.                     $passwordHasher->hashPassword(
  1153.                         $user,
  1154.                         $request->request->get('newPassword')
  1155.                     )
  1156.                 );
  1157.                 $user->setUpdatedAt(new \DateTime());
  1158.                 $em->persist($user);
  1159.                 $em->flush();
  1160.                 $this->addFlash(
  1161.                     'success_password_edit',
  1162.                     'true'
  1163.                 );
  1164.             } else {
  1165.                 $this->addFlash(
  1166.                     'success_password_edit',
  1167.                     'false'
  1168.                 );
  1169.             }
  1170.         } else {
  1171.             $this->addFlash(
  1172.                 'success_password_edit',
  1173.                 'false'
  1174.             );
  1175.         }
  1176.         return $this->redirectToRoute('admin_dashboard');
  1177.     }
  1178.     /**************************consultation********************************** */
  1179.     /**
  1180.      * @IsGranted("ROLE_CONSULTATION")
  1181.      */
  1182.     #[Route('/consultation/password/edit'name'consultation_edit_password'methods: ['GET''POST'])]
  1183.     public function passwordConsultation(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  1184.     {
  1185.         if ($this->isGranted('ROLE_CONSULTATION') == false) {
  1186.             return $this->render('erreur/access_denied.html.twig', []);
  1187.         }
  1188.         $em $doctrine->getManager();
  1189.         $user $this->getUser();
  1190.         if ($passwordHasher->isPasswordValid($user$request->request->get('password'))) {
  1191.             if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
  1192.                 $password $request->request->get('newPassword');
  1193.                 if (!$this->isPasswordStrong($password)) {
  1194.                     $this->addFlash('pass_error''true');
  1195.                     return $this->redirectToRoute('app_consultation');
  1196.                 }
  1197.                 $user->setPassword(
  1198.                     $passwordHasher->hashPassword(
  1199.                         $user,
  1200.                         $request->request->get('newPassword')
  1201.                     )
  1202.                 );
  1203.                 $user->setUpdatedAt(new \DateTime());
  1204.                 $em->persist($user);
  1205.                 $em->flush();
  1206.                 $this->addFlash(
  1207.                     'success_password_edit',
  1208.                     'true'
  1209.                 );
  1210.             } else {
  1211.                 $this->addFlash(
  1212.                     'success_password_edit',
  1213.                     'false'
  1214.                 );
  1215.             }
  1216.         } else {
  1217.             $this->addFlash(
  1218.                 'success_password_edit',
  1219.                 'false'
  1220.             );
  1221.         }
  1222.         return $this->redirectToRoute('app_consultation');
  1223.     }
  1224.     /**************************partenaire********************************* */
  1225.     /**
  1226.      * @IsGranted("ROLE_PARTENAIRE")
  1227.      */
  1228.     #[Route('/partenaire/password/edit'name'partenaire_edit_password'methods: ['GET''POST'])]
  1229.     public function passwordPartenaire(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  1230.     {
  1231.         if ($this->isGranted('ROLE_PARTENAIRE') == false) {
  1232.             return $this->render('erreur/access_denied.html.twig', []);
  1233.         }
  1234.         $em $doctrine->getManager();
  1235.         $user $this->getUser();
  1236.         if ($passwordHasher->isPasswordValid($user$request->request->get('password'))) {
  1237.             if ($request->request->get('newPassword') === $request->request->get('confirmPassword')) {
  1238.                 $password $request->request->get('newPassword');
  1239.                 if (!$this->isPasswordStrong($password)) {
  1240.                     $this->addFlash('pass_error''true');
  1241.                     return $this->redirectToRoute('app_partenaire');
  1242.                 }
  1243.                 $user->setPassword(
  1244.                     $passwordHasher->hashPassword(
  1245.                         $user,
  1246.                         $request->request->get('newPassword')
  1247.                     )
  1248.                 );
  1249.                 $user->setUpdatedAt(new \DateTime());
  1250.                 $em->persist($user);
  1251.                 $em->flush();
  1252.                 $this->addFlash(
  1253.                     'success_password_edit',
  1254.                     'true'
  1255.                 );
  1256.             } else {
  1257.                 $this->addFlash(
  1258.                     'success_password_edit',
  1259.                     'false'
  1260.                 );
  1261.             }
  1262.         } else {
  1263.             $this->addFlash(
  1264.                 'success_password_edit',
  1265.                 'false'
  1266.             );
  1267.         }
  1268.         return $this->redirectToRoute('app_partenaire');
  1269.     }
  1270.     /***********************User************************************/
  1271.     #[Route('/rest_password'name'reset_password'methods: ['GET''POST'])]
  1272.     public function resetPasswordUser(Request $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasherMailerInterface $mailer): Response
  1273.     {
  1274.         $em $doctrine->getManager();
  1275.         $user = new User();
  1276.         $form $this->createForm(UserType::class, $user);
  1277.         $form->handleRequest($request);
  1278.         //&& $form->isValid()
  1279.         if ($form->isSubmitted()) {
  1280.             if ($request->request->has('g-recaptcha-response')) {
  1281.                 if ($request->request->get('g-recaptcha-response') != '') {
  1282.                     $userExist $doctrine->getRepository(User::class)->findOneBy(['email' => $user->getEmail()]);
  1283.                     if ($userExist !== null) {
  1284.                         try {
  1285.                             $password = (string)(random_int(100000999999));
  1286.                             $userExist->setPassword(
  1287.                                 $passwordHasher->hashPassword(
  1288.                                     $userExist,
  1289.                                     $password
  1290.                                 )
  1291.                             );
  1292.                             $em->persist($userExist);
  1293.                             $em->flush();
  1294.                             $to $user->getEmail();
  1295.                             //$to='ryahyaoui@progress.tn';
  1296.                             $sub '[OEP] Votre mot de passe';
  1297.                             $msg '<p>Bonjour ' $user->getUsername() . ', <br/> Votre mot de passe est ' $password '.</p>';
  1298.                             $globals = new Globals();
  1299.                             $globals->sendEmail($mailer$to$sub$msg);
  1300.                             return $this->redirectToRoute('login');
  1301.                         } catch (\Exception $e) {
  1302.                             return $this->redirectToRoute('reset_password');
  1303.                         }
  1304.                     } else {
  1305.                         $user->setEmail($user->getEmail());
  1306.                         $this->addFlash(
  1307.                             'failure_register',
  1308.                             'true'
  1309.                         );
  1310.                         return $this->render('security/reset_password_user.html.twig', [
  1311.                             'controller_name' => 'ProfilController',
  1312.                             'user' => $user,
  1313.                             'form' => $form->createView(),
  1314.                         ]);
  1315.                     } //end user exist
  1316.                 }
  1317.             }
  1318.             $user->setEmail($user->getEmail());
  1319.             $this->addFlash(
  1320.                 'failure_recaptcha',
  1321.                 'true'
  1322.             );
  1323.             return $this->render('security/reset_password_user.html.twig', [
  1324.                 'controller_name' => 'ProfilController',
  1325.                 'user' => $user,
  1326.                 'form' => $form->createView(),
  1327.             ]);
  1328.         } //end submit
  1329.         return $this->render('security/reset_password_user.html.twig', [
  1330.             'controller_name' => 'ProfilController',
  1331.             'user' => $user,
  1332.             'form' => $form->createView(),
  1333.         ]);
  1334.     }
  1335. }