src/wbx/UserBundle/Controller/SecurityController.php line 64

Open in your IDE?
  1. <?php
  2. namespace wbx\UserBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use wbx\AppBundle\Entity\AdminConfiguration;
  9. use wbx\AppBundle\Entity\AdminConfigurationSecurite;
  10. use wbx\AppBundle\Entity\Repository\AdminConfigurationRepository;
  11. use wbx\AppBundle\Manager\AbstractUserManager;
  12. use wbx\AppBundle\Manager\AdminConfigurationManager;
  13. use wbx\AppBundle\Service\LogService;
  14. class SecurityController extends BaseController
  15. {
  16.     #[Route(path'/login'name'login')]
  17.     public function login(
  18.         Request $request,
  19.         AuthenticationUtils $authenticationUtils,
  20.         AdminConfigurationRepository $adminConfigurationRepository,
  21.         LogService $logService,
  22.         AbstractUserManager $abstractUserManager,
  23.         AdminConfigurationManager $adminConfiguration,
  24.     ): Response {
  25.         /** @var AdminConfiguration[] $varsConnexion */
  26.         $varsConnexion $adminConfigurationRepository->findBy(['key' => AdminConfiguration::KEYS['connexion']]);
  27.         $vars = [];
  28.         foreach ($varsConnexion as $value) {
  29.             $vars[$value->getKey()] = $value->getValue();
  30.         }
  31.         // get the login error if there is one
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         // last username entered by the user
  34.         $lastUsername $authenticationUtils->getLastUsername();
  35.         $session $request->getSession();
  36.         if ($error instanceof BadCredentialsException) {
  37.             $limiter $session->get('auth_limiter', []);
  38.             if (!empty($limiter) && ($limiter['username'] == $lastUsername)) {
  39.                 ++$limiter['max_attempts'];
  40.             } else {
  41.                 $limiter = [
  42.                     'username' => $lastUsername,
  43.                     'max_attempts' => 1,
  44.                 ];
  45.             }
  46.             $session->set('auth_limiter'$limiter);
  47.             $maxAttempts $adminConfiguration->getValueFromKey('nombre_tentative');
  48.             $maxAttempts = !empty($maxAttempts) ? (int) $maxAttempts AdminConfigurationSecurite::DEFAULT_MAX_AUTH_ATTEMPS;
  49.             if ($limiter['max_attempts'] >= $maxAttempts) {
  50.                 $abstractUserManager->lockUser($lastUsername);
  51.                 $session->remove('auth_limiter');
  52.             }
  53.         }
  54.         // vérification si le user a les accès pour se connecter ou non : UserLoginListener
  55.         $sessionConnexion $this->get('session')->get('connexion');
  56.         $errorConnexion false;
  57.         if ($sessionConnexion !== null && $sessionConnexion == false) {
  58.             $errorConnexion true;
  59.         }
  60.         // ajout du log si $error
  61.         if ($error) {
  62.             $datas = [
  63.                 'email' => $lastUsername,
  64.                 'ip' => $this->getIP(),
  65.                 'connexion_type' => $_SERVER['SERVER_PROTOCOL'],
  66.                 'login' => $sessionConnexion,
  67.                 'refusal_type' => $error->getMessage(),
  68.             ];
  69.             $logService->addLog($datas);
  70.         } else {
  71.             $session->remove('auth_limiter');
  72.         }
  73.         return $this->render(
  74.             '@wbxUser/Security/login.html.twig',
  75.             [
  76.                 'last_username' => $lastUsername,
  77.                 'error' => $error,
  78.                 'modal' => $this->isModal($request),
  79.                 'vars' => $vars,
  80.                 'errorConnexion' => $errorConnexion,
  81.             ]
  82.         );
  83.     }
  84.     /**
  85.      *récupération de l'ip
  86.      *
  87.      * @return array|false|mixed|string
  88.      */
  89.     private function getIP()
  90.     {
  91.         if (isset($_SERVER)) {
  92.             if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  93.                 $realip $_SERVER['HTTP_X_FORWARDED_FOR'];
  94.             } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  95.                 $realip $_SERVER['HTTP_CLIENT_IP'];
  96.             } else {
  97.                 $realip $_SERVER['REMOTE_ADDR'];
  98.             }
  99.         } else {
  100.             if (getenv('HTTP_X_FORWARDED_FOR')) {
  101.                 $realip getenv('HTTP_X_FORWARDED_FOR');
  102.             } elseif (getenv('HTTP_CLIENT_IP')) {
  103.                 $realip getenv('HTTP_CLIENT_IP');
  104.             } else {
  105.                 $realip getenv('REMOTE_ADDR');
  106.             }
  107.         }
  108.         return $realip;
  109.     }
  110. }