diff --git a/core/modules/ban/lib/Drupal/ban/BanIpManager.php b/core/modules/ban/lib/Drupal/ban/BanIpManager.php index 0dcab0c..8ee2e0d 100644 --- a/core/modules/ban/lib/Drupal/ban/BanIpManager.php +++ b/core/modules/ban/lib/Drupal/ban/BanIpManager.php @@ -22,36 +22,27 @@ class BanIpManager { protected $connection; /** - * The IP address to check. - * - * @var string - */ - protected $ip; - - /** * Construct the BanSubscriber. * * @param Drupal\Core\Database\Connection $connection * The database connection which will be used to check the IP against. - * @param string $ip - * The IP address to check. - * If omitted or empty the IP of the current user will be used instead. */ - public function __construct(Connection $connection, $ip = '') { + public function __construct(Connection $connection) { $this->connection = $connection; - // @todo convert this to Request::getClientIP(). - $this->ip = !empty($ip) ? $ip : ip_address(); } /** * Returns whether an IP address is blocked. * + * @param string $ip + * The IP address to check. + * * @return bool * TRUE if access is denied, FALSE if access is allowed. */ - public function isDenied() { + public function isDenied($ip) { $denied = $this->connection - ->query('SELECT 1 FROM {ban_ip} WHERE ip = :ip', array(':ip' => $this->ip)) + ->query('SELECT 1 FROM {ban_ip} WHERE ip = :ip', array(':ip' => $ip)) ->fetchField(); return (bool) $denied; } diff --git a/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php b/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php index a77eaef..7dc141c 100644 --- a/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php +++ b/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php @@ -37,6 +37,21 @@ public function __construct(BanIpManager $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) { + // @todo convert this to Request::getClientIP(). + $ip = ip_address(); + 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 @@ -47,16 +62,4 @@ static function getSubscribedEvents() { return $events; } - /** - * 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) { - if ($this->manager->isDenied()) { - $response = new Response('Sorry, your IP has been banned.', 403); - $event->setResponse($response); - } - } }