src/Controller/SecurityController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use ContainerOt7jKaL\getConsole_Command_FormDebugService;
  4. use Firebase\JWT\JWT;
  5. use LogicException;
  6. use Swift_Mailer;
  7. use Swift_Message;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use function strlen;
  18. class SecurityController extends AbstractController
  19. {
  20.     #[Route(path'/login'name'app_login')]
  21.     public function login(Request $requestAuthenticationUtils $authenticationUtilsSessionInterface $session): Response
  22.     {
  23.         if ($this->getUser()) {
  24.             $url_target_path $request->getSession()->get('_security.main.target_path');
  25.             if (!empty($url_target_path)) {
  26.                 $path $url_target_path;
  27.             } else {
  28.                 $path $this->generateUrl("home");
  29.             }
  30.             $response = new RedirectResponse($path);
  31.             $response->sendHeaders();
  32.             return $response;
  33.         }
  34.         // get the login error if there is one
  35.         $error $authenticationUtils->getLastAuthenticationError();
  36.         // last username entered by the user
  37.         $lastUsername $authenticationUtils->getLastUsername();
  38.         $lastKadh $session->get('_security.last_k_adherent');
  39.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'last_kadherent' => $lastKadh'error' => $error]);
  40.     }
  41.     #[Route(path'/logout'name'app_logout')]
  42.     public
  43.     function logout(): never
  44.     {
  45.         throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  46.     }
  47.     #[Route(path'/resetting/init-{init}'name'app_first_password')]
  48.     #[Route(path'/resetting/init'name'app_forget_password')]
  49.     public
  50.     function forgetPassword($init false): Response
  51.     {
  52.         return $this->render('security/forget_password.html.twig', [
  53.             'init' => $init
  54.         ]);
  55.     }
  56.     #[Route(path'/resetting/check/{init}'name'reseting_check')]
  57.     public
  58.     function resetCheckAction(Request $requestTranslatorInterface $translatorSwift_Mailer $mailer$init null): Response
  59.     {
  60.         if ($request->isMethod('post') && !empty($request->request->get('_submit'))) {
  61.             $new_datas = [
  62.                 'K_ADHERENT' => $request->request->get('username'),
  63.                 'EMAIL' => $request->request->get('email')
  64.             ];
  65.             $ch curl_init($this->getParameter("auth_url")."/reset-password-token");
  66.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  67.             curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  68.             curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  69.             curl_setopt($chCURLOPT_POSTFIELDSjson_encode($new_datas));
  70.             curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  71.             $response curl_exec($ch);
  72.             $response json_decode($responsetrue);
  73.             if ($response['status'] == "200" && !empty($response['user']['TOKEN'])) {
  74.                 if ($response['user']['PAYS'] == "BE" && $response['user']['LANGUE'] == "NL") {
  75.                     $translator->setLocale("nl");
  76.                 }
  77.                 $url_confirmed $request->getHttpHost() . $this->generateUrl("resetting_token_check", ['token' => $response['user']['TOKEN']]);
  78.                 $title = (!is_null($init)) ? $translator->trans("Initialisation de votre mot de passe") : $translator->trans("Réinitialisation de votre mot de passe");
  79.                 $message = (new Swift_Message('[Groupe All] - ' $title))
  80.                     ->setFrom('contact@groupeall.fr')
  81.                     ->setTo($response['user']['EMAIL'])
  82.                     ->setBody(
  83.                         $this->renderView(
  84.                             (!is_null($init)) ? 'security/email/email_first_password.html.twig' 'security/email/email_forget_password.html.twig',
  85.                             ['username' => $response['user']['K_ADHERENT'], "confirmationUrl" => $url_confirmed]
  86.                         ),
  87.                         'text/html'
  88.                     );
  89.                 $mailer->send($message);
  90.                 $this->addFlash('success'$translator->trans('Un e-mail a été envoyé sur votre adresse'));
  91.                 return $this->render('security/forget_password.html.twig', ['init' => $init]);
  92.             } else {
  93.                 $this->addFlash('danger''Identifiants invalides');
  94.                 return $this->render('security/forget_password.html.twig', ['init' => $init]);
  95.             }
  96.         }
  97.         return $this->render('security/forget_password.html.twig');
  98.     }
  99.     /**
  100.      * @param $token
  101.      */
  102.     #[Route(path'/resetting/password-{token}'name'resetting_token_check')]
  103.     public function resettingTokenCheck($token): Response
  104.     {
  105.         $new_datas = [];
  106.         if (isset($token) && !empty($token)) {
  107.             $new_datas['TOKEN'] = $token;
  108.             $ch curl_init($this->getParameter("auth_url")."/reset-password-check-token");
  109.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  110.             curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  111.             curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  112.             curl_setopt($chCURLOPT_POSTFIELDSjson_encode($new_datas));
  113.             curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  114.             $response curl_exec($ch);
  115.             $response json_decode($responsetrue);
  116.             if ($response['status'] == 200) {
  117.                 return $this->render('security/password_resetting.html.twig', ['token' => $token]);
  118.             } else {
  119.                 return $this->render('security/password_resetting.html.twig', ['token' => 'Invalid_token']);
  120.             }
  121.         }
  122.     }
  123.     /**
  124.      * @param $token
  125.      */
  126.     #[Route(path'/resetting/resetting-password-{token}'name'resetting_password')]
  127.     public
  128.     function resettingPasswordAction(Request $request$token): Response
  129.     {
  130.         if (!empty($token) && $request->isMethod('post') && !empty($request->request->get('_submit'))) {
  131.             /**
  132.              * Vérifie que le mot de passe choisi réponde aux exigences.
  133.              */
  134.             $errors = [];
  135.             if (!preg_match('#[A-Z]+#'$request->request->get('password_reset'))) {
  136.                 $errors[] = 'Veuillez choisir un mot de passe contenant au moins une majuscule.';
  137.             }
  138.             if (!preg_match('#[0-9]+#'$request->request->get('password_reset'))) {
  139.                 $errors[] = 'Veuillez choisir un mot de passe contenant au moins un chiffre.';
  140.             }
  141.             if (!preg_match('#[()*.!$%@&§]+#'$request->request->get('password_reset'))) {
  142.                 $errors[] = 'Veuillez choisir un mot de passe contenant au moins un caractère spécial parmi ()*.!$%@&§';
  143.             }
  144.             if (strlen($request->request->get('password_reset')) < 10) {
  145.                 $errors[] = 'Veuillez choisir un mot de passe d`\'au moins 10 caractères.';
  146.             }
  147.             if (!preg_match('#^[()*.!$%@&§A-Za-z0-9]*$#'$request->request->get('password_reset'))) {
  148.                 $errors[] = 'Le mot de passe que vous avez choisi contient des caractères non autorisés.';
  149.             }
  150.             if ($request->request->get('password_reset') != $request->request->get('password_reset_repeat')) {
  151.                 $errors[] = 'Le nouveau mot de passe et celui de validation sont différents.';
  152.             }
  153.             /**
  154.              * Retourne les erreurs si il y en a.
  155.              */
  156.             if (!empty($errors)) {
  157.                 return $this->render('security/password_resetting.html.twig', [
  158.                     'token' => $token,
  159.                     'errors' => $errors
  160.                 ]);
  161.             }
  162.             $new_datas = [
  163.                 'TOKEN' => $token,
  164.                 'PWD' => $request->request->get('password_reset')
  165.             ];
  166.             $ch curl_init($this->getParameter("auth_url")."/reset-password");
  167.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  168.             curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  169.             curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  170.             curl_setopt($chCURLOPT_POSTFIELDSjson_encode($new_datas));
  171.             curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  172.             $response curl_exec($ch);
  173.             $response json_decode($responsetrue);
  174.             if ($response['status'] == 200) {
  175.                 return $this->render('security/password_resetting.html.twig', ['token' => $token'success' => true]);
  176.             } else {
  177.                 return $this->render('security/password_resetting.html.twig', ['token' => $token'errors' => ['Une erreur est survenue']]);
  178.             }
  179.         }
  180.     }
  181. }