<?php
namespace App\Controller;
use App\Entity\BannedIPs;
use App\Entity\Globals;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
public function login(AuthenticationUtils $authenticationUtils, Request $request)
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
$globals = new Globals();
$globals->setGlobals($this->getDoctrine());
$ip = '';
if ($error) {
$ip = $request->getClientIp();
$ipEntity = $this->getDoctrine()->getRepository(BannedIPs::class)->findOneBy([
'address' => $ip,
]);
if (!$ipEntity) {
$ipEntity = new BannedIPs();
$ipEntity->setAddress($ip);
} else {
if (!is_null($ipEntity->getCounter())) {
$lastTime = strtotime($ipEntity->getCreated()->format('Y-m-d H:i:s'));
if ($lastTime < strtotime('-1 day')) {
$ipEntity->setCounter(0);
}
}
}
$ipEntity->setCreated(new \DateTime('now'));
$ipEntity->setCounter(is_null($ipEntity->getCounter()) ? 1 : $ipEntity->getCounter() + 1);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($ipEntity);
$entityManager->flush();
} else {
$ipEntity = new BannedIPs();
$ipEntity->setCounter(0);
}
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'globals' => $globals->getGlobals(),
'address' => $ip,
'counter' => $ipEntity->getCounter(),
]);
}
}