src/Postroyka/AppBundle/EventSubscriber/RestrictIpAddressSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace Postroyka\AppBundle\EventSubscriber;
  3. use Postroyka\AppBundle\Entity\Blacklist;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. class RestrictIpAddressSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private ManagerRegistry $managerRegistry)
  11.     {}
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [RequestEvent::class => 'onKernelRequest'];
  15.     }
  16.     public function onKernelRequest(RequestEvent $event): void
  17.     {
  18.         if (!$event->isMainRequest()) {
  19.             return;
  20.         }
  21.         $blacklistedItems $this->managerRegistry->getRepository(Blacklist::class)->findBy(["ip" => Blacklist::getClientIp()]);
  22.         if (count($blacklistedItems)) {
  23.             throw new AccessDeniedHttpException('Access Denied');
  24.         }
  25.     }
  26. }