<?php
namespace Postroyka\AppBundle\EventSubscriber;
use Postroyka\AppBundle\Entity\Blacklist;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class RestrictIpAddressSubscriber implements EventSubscriberInterface
{
public function __construct(private ManagerRegistry $managerRegistry)
{}
public static function getSubscribedEvents(): array
{
return [RequestEvent::class => 'onKernelRequest'];
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$blacklistedItems = $this->managerRegistry->getRepository(Blacklist::class)->findBy(["ip" => Blacklist::getClientIp()]);
if (count($blacklistedItems)) {
throw new AccessDeniedHttpException('Access Denied');
}
}
}