src/Controller/SecurityController.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BannedIPs;
  4. use App\Entity\Globals;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. class SecurityController extends AbstractController
  9. {
  10.     public function login(AuthenticationUtils $authenticationUtilsRequest $request)
  11.     {
  12.         $error $authenticationUtils->getLastAuthenticationError();
  13.         $lastUsername $authenticationUtils->getLastUsername();
  14.         $globals = new Globals();
  15.         $globals->setGlobals($this->getDoctrine());
  16.         $ip '';
  17.         if ($error) {
  18.             $ip $request->getClientIp();
  19.             $ipEntity $this->getDoctrine()->getRepository(BannedIPs::class)->findOneBy([
  20.                 'address' => $ip,
  21.             ]);
  22.             if (!$ipEntity) {
  23.                 $ipEntity = new BannedIPs();
  24.                 $ipEntity->setAddress($ip);
  25.             } else {
  26.                 if (!is_null($ipEntity->getCounter())) {
  27.                     $lastTime strtotime($ipEntity->getCreated()->format('Y-m-d H:i:s'));
  28.                     if ($lastTime strtotime('-1 day')) {
  29.                         $ipEntity->setCounter(0);
  30.                     }
  31.                 }
  32.             }
  33.             $ipEntity->setCreated(new \DateTime('now'));
  34.             $ipEntity->setCounter(is_null($ipEntity->getCounter()) ? $ipEntity->getCounter() + 1);
  35.             $entityManager $this->getDoctrine()->getManager();
  36.             $entityManager->persist($ipEntity);
  37.             $entityManager->flush();
  38.         } else {
  39.             $ipEntity = new BannedIPs();
  40.             $ipEntity->setCounter(0);
  41.         }
  42.         return $this->render('security/login.html.twig', [
  43.             'last_username' => $lastUsername,
  44.             'error' => $error,
  45.             'globals' => $globals->getGlobals(),
  46.             'address' => $ip,
  47.             'counter' => $ipEntity->getCounter(),
  48.         ]);
  49.     }
  50. }