src/Controller/AccessController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  10. class AccessController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/", name="login", methods={"GET", "POST"})
  14.      */
  15.     public function logIn(
  16.         AuthenticationUtils $authenticationUtils,
  17.         AuthorizationCheckerInterface $authChecker,
  18.         Request $request
  19.     ): Response {
  20.         if (true === $authChecker->isGranted('ROLE_CUSTOMER')) {
  21.             return new RedirectResponse('dashboard');
  22.         }
  23.         $error $authenticationUtils->getLastAuthenticationError();
  24.         $lastUsername $authenticationUtils->getLastUsername();
  25.         // On genere un identifiant unique pour l'usager afin d'afficher des messages personnalisés si besoin
  26.         if (!isset($_COOKIE['unique_id'])) {
  27.             $unique_id bin2hex(random_bytes(16)); // Génère un identifiant unique
  28.             setcookie('unique_id'$unique_idtime() + (86400 1), '/'); // Définit le cookie pour 1 jour
  29.             $_COOKIE['unique_id'] = $unique_id// Met à jour la variable superglobale $_COOKIE
  30.         }
  31.         $user_id $_COOKIE['unique_id'];
  32.         $projectDir $this->getParameter('kernel.project_dir');
  33.         $varDir $projectDir.'/var';
  34.         $messages '';
  35.         if (file_exists($varDir.'/cookies/'.$user_id)) {
  36.             $messages file_get_contents($varDir.'/cookies/'.$user_id);
  37.             unlink($varDir.'/cookies/'.$user_id);
  38.         }
  39.         /*
  40.          * Clé Google Recaptcha v3
  41.          * DEV :
  42.          *      - clé secrète 6LcnjyoaAAAAAPhi6K_AxoW47WPWJHNQCDEgTtMS
  43.          *      - clé site 6LcnjyoaAAAAANHwcvnepkwrR3Xby2e7FTPTTG_r
  44.          *
  45.          * PROD :
  46.          *      - clé secrète 6LcjorMZAAAAAPZ2jHNngd9MpmsUcO9pv6oNB3yx
  47.          *      - clé site 6LcjorMZAAAAAEKYV5kfyGo_K-oBN_dZGXLAs4N3
  48.          */
  49.         /*$host = $request->getHost();
  50.         switch ($host) {
  51.             case 'ec-eau-2024.iti-communication.net':
  52.                 $recaptcha_site = '6LcnjyoaAAAAANHwcvnepkwrR3Xby2e7FTPTTG_r';
  53.                 break;
  54.             default:
  55.                 $recaptcha_site = '6LcjorMZAAAAAEKYV5kfyGo_K-oBN_dZGXLAs4N3';
  56.         }*/
  57.         $token hash_hmac('sha256'time(), 'reims_87iticom');
  58.         $_SESSION['captcha_token'] = $token;
  59.         $_SESSION['formulaire_time'] = time();
  60.         $random substr(md5(mt_rand()), 08);
  61.         $_SESSION['formulaire_piege'] = $random;
  62.         return $this->render(
  63.             'access/login.html.twig',
  64.             [
  65.                 'menu' => 'login',
  66.                 'last_username' => $lastUsername,
  67.                 'error' => $error,
  68.                 'messages' => $messages,
  69.                 'formulaire_jeton' => $token,
  70.                 'formulaire_piege' => $random,
  71.                 'recaptcha_site' => '',
  72.             ]
  73.         );
  74.     }
  75.     /**
  76.      * @Route("/logout", name="logout", methods={"GET"})
  77.      */
  78.     public function logOut()
  79.     {
  80.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  81.     }
  82. }