<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class AccessController extends AbstractController
{
/**
* @Route("/", name="login", methods={"GET", "POST"})
*/
public function logIn(
AuthenticationUtils $authenticationUtils,
AuthorizationCheckerInterface $authChecker,
Request $request
): Response {
if (true === $authChecker->isGranted('ROLE_CUSTOMER')) {
return new RedirectResponse('dashboard');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
// On genere un identifiant unique pour l'usager afin d'afficher des messages personnalisés si besoin
if (!isset($_COOKIE['unique_id'])) {
$unique_id = bin2hex(random_bytes(16)); // Génère un identifiant unique
setcookie('unique_id', $unique_id, time() + (86400 * 1), '/'); // Définit le cookie pour 1 jour
$_COOKIE['unique_id'] = $unique_id; // Met à jour la variable superglobale $_COOKIE
}
$user_id = $_COOKIE['unique_id'];
$projectDir = $this->getParameter('kernel.project_dir');
$varDir = $projectDir.'/var';
$messages = '';
if (file_exists($varDir.'/cookies/'.$user_id)) {
$messages = file_get_contents($varDir.'/cookies/'.$user_id);
unlink($varDir.'/cookies/'.$user_id);
}
/*
* Clé Google Recaptcha v3
* DEV :
* - clé secrète 6LcnjyoaAAAAAPhi6K_AxoW47WPWJHNQCDEgTtMS
* - clé site 6LcnjyoaAAAAANHwcvnepkwrR3Xby2e7FTPTTG_r
*
* PROD :
* - clé secrète 6LcjorMZAAAAAPZ2jHNngd9MpmsUcO9pv6oNB3yx
* - clé site 6LcjorMZAAAAAEKYV5kfyGo_K-oBN_dZGXLAs4N3
*/
/*$host = $request->getHost();
switch ($host) {
case 'ec-eau-2024.iti-communication.net':
$recaptcha_site = '6LcnjyoaAAAAANHwcvnepkwrR3Xby2e7FTPTTG_r';
break;
default:
$recaptcha_site = '6LcjorMZAAAAAEKYV5kfyGo_K-oBN_dZGXLAs4N3';
}*/
$token = hash_hmac('sha256', time(), 'reims_87iticom');
$_SESSION['captcha_token'] = $token;
$_SESSION['formulaire_time'] = time();
$random = substr(md5(mt_rand()), 0, 8);
$_SESSION['formulaire_piege'] = $random;
return $this->render(
'access/login.html.twig',
[
'menu' => 'login',
'last_username' => $lastUsername,
'error' => $error,
'messages' => $messages,
'formulaire_jeton' => $token,
'formulaire_piege' => $random,
'recaptcha_site' => '',
]
);
}
/**
* @Route("/logout", name="logout", methods={"GET"})
*/
public function logOut()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}