<?php/**
* @file
* Definition of Drupal\ban\EventSubscriber\BanSubscriber.
*/namespaceDrupal\ban\EventSubscriber;
useSymfony\Component\HttpFoundation\Response;
useSymfony\Component\HttpKernel\KernelEvents;
useSymfony\Component\HttpKernel\Event\GetResponseEvent;
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
useDrupal\ban\BanIpManager;
/**
* Ban subscriber for controller requests.
*/class BanSubscriberimplements EventSubscriberInterface {
/**
* The manager used to check the IP against.
*
* @var Drupal\ban\BanIpManager
*/
protected $manager;
/**
* Construct the BanSubscriber.
*
* @param Drupal\ban\BanIpManager $manager
* The manager used to check the IP against.
*/
public function__construct(BanIpManager $manager) {
$this->manager = $manager;
}
/**
* Response with 403 if the visitor's IP adress is banned.
*
* @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The Event to process.
*/
public functiononKernelRequestBannedIpCheck(GetResponseEvent $event) {
$ip = $event
->getRequest()
->getClientIp();
if ($this->manager
->isDenied($ip)) {
$response = newResponse('Sorry, ' . check_plain($ip) . ' has been banned.', 403);
$event
->setResponse($response);
}
}
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
static functiongetSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array(
'onKernelRequestBannedIpCheck',
40,
);
return$events;
}
}