class BanSubscriber

Ban subscriber for controller requests.

Hierarchy

Expanded class hierarchy of BanSubscriber

1 string reference to 'BanSubscriber'
ban.services.yml in drupal/core/modules/ban/ban.services.yml
drupal/core/modules/ban/ban.services.yml
1 service uses BanSubscriber

File

drupal/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php, line 20
Definition of Drupal\ban\EventSubscriber\BanSubscriber.

Namespace

Drupal\ban\EventSubscriber
View source
class BanSubscriber implements 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 function onKernelRequestBannedIpCheck(GetResponseEvent $event) {
    $ip = $event
      ->getRequest()
      ->getClientIp();
    if ($this->manager
      ->isDenied($ip)) {
      $response = new Response('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 function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array(
      'onKernelRequestBannedIpCheck',
      40,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BanSubscriber::$manager protected property The manager used to check the IP against.
BanSubscriber::getSubscribedEvents static function Registers the methods in this class that should be listeners. Overrides EventSubscriberInterface::getSubscribedEvents
BanSubscriber::onKernelRequestBannedIpCheck public function Response with 403 if the visitor's IP adress is banned.
BanSubscriber::__construct public function Construct the BanSubscriber.