<?php
namespace wbx\UserBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use wbx\AppBundle\Entity\AdminConfiguration;
use wbx\AppBundle\Entity\AdminConfigurationSecurite;
use wbx\AppBundle\Entity\Repository\AdminConfigurationRepository;
use wbx\AppBundle\Manager\AbstractUserManager;
use wbx\AppBundle\Manager\AdminConfigurationManager;
use wbx\AppBundle\Service\LogService;
class SecurityController extends BaseController
{
#[Route(path: '/login', name: 'login')]
public function login(
Request $request,
AuthenticationUtils $authenticationUtils,
AdminConfigurationRepository $adminConfigurationRepository,
LogService $logService,
AbstractUserManager $abstractUserManager,
AdminConfigurationManager $adminConfiguration,
): Response {
/** @var AdminConfiguration[] $varsConnexion */
$varsConnexion = $adminConfigurationRepository->findBy(['key' => AdminConfiguration::KEYS['connexion']]);
$vars = [];
foreach ($varsConnexion as $value) {
$vars[$value->getKey()] = $value->getValue();
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$session = $request->getSession();
if ($error instanceof BadCredentialsException) {
$limiter = $session->get('auth_limiter', []);
if (!empty($limiter) && ($limiter['username'] == $lastUsername)) {
++$limiter['max_attempts'];
} else {
$limiter = [
'username' => $lastUsername,
'max_attempts' => 1,
];
}
$session->set('auth_limiter', $limiter);
$maxAttempts = $adminConfiguration->getValueFromKey('nombre_tentative');
$maxAttempts = !empty($maxAttempts) ? (int) $maxAttempts : AdminConfigurationSecurite::DEFAULT_MAX_AUTH_ATTEMPS;
if ($limiter['max_attempts'] >= $maxAttempts) {
$abstractUserManager->lockUser($lastUsername);
$session->remove('auth_limiter');
}
}
// vérification si le user a les accès pour se connecter ou non : UserLoginListener
$sessionConnexion = $this->get('session')->get('connexion');
$errorConnexion = false;
if ($sessionConnexion !== null && $sessionConnexion == false) {
$errorConnexion = true;
}
// ajout du log si $error
if ($error) {
$datas = [
'email' => $lastUsername,
'ip' => $this->getIP(),
'connexion_type' => $_SERVER['SERVER_PROTOCOL'],
'login' => $sessionConnexion,
'refusal_type' => $error->getMessage(),
];
$logService->addLog($datas);
} else {
$session->remove('auth_limiter');
}
return $this->render(
'@wbxUser/Security/login.html.twig',
[
'last_username' => $lastUsername,
'error' => $error,
'modal' => $this->isModal($request),
'vars' => $vars,
'errorConnexion' => $errorConnexion,
]
);
}
/**
*récupération de l'ip
*
* @return array|false|mixed|string
*/
private function getIP()
{
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$realip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_CLIENT_IP')) {
$realip = getenv('HTTP_CLIENT_IP');
} else {
$realip = getenv('REMOTE_ADDR');
}
}
return $realip;
}
}