class BanIpManager

Ban IP manager.

Hierarchy

Expanded class hierarchy of BanIpManager

3 files declare their use of BanIpManager
BanAdmin.php in drupal/core/modules/ban/lib/Drupal/ban/Form/BanAdmin.php
Contains \Drupal\ban\Form\BanAdmin.
BanDelete.php in drupal/core/modules/ban/lib/Drupal/ban/Form/BanDelete.php
Contains \Drupal\ban\Form\BanDelete.
BanSubscriber.php in drupal/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php
Definition of Drupal\ban\EventSubscriber\BanSubscriber.
1 string reference to 'BanIpManager'
ban.services.yml in drupal/core/modules/ban/ban.services.yml
drupal/core/modules/ban/ban.services.yml
1 service uses BanIpManager

File

drupal/core/modules/ban/lib/Drupal/ban/BanIpManager.php, line 15
Definition of Drupal\ban\BanIpManager.

Namespace

Drupal\ban
View source
class BanIpManager {

  /**
   * The database connection used to check the IP against.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Construct the BanSubscriber.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection which will be used to check the IP against.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * 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($ip) {
    $denied = $this->connection
      ->query('SELECT 1 FROM {ban_ip} WHERE ip = :ip', array(
      ':ip' => $ip,
    ))
      ->fetchField();
    return (bool) $denied;
  }

  /**
   * Returns if this IP address is banned.
   *
   * @param string $ip
   *   The IP address to check.
   *
   * @return bool
   *   TRUE if the IP address is banned, FALSE otherwise.
   */
  public function isBanned($ip) {
    return (bool) $this->connection
      ->query("SELECT * FROM {ban_ip} WHERE ip = :ip", array(
      ':ip' => $ip,
    ))
      ->fetchField();
  }

  /**
   * Finds all banned IP addresses.
   *
   * @return \Drupal\Core\Database\StatementInterface
   *   The result of the database query.
   */
  public function findAll() {
    return $this->connection
      ->query('SELECT * FROM {ban_ip}');
  }

  /**
   * Bans an IP address.
   *
   * @param string $ip
   *   The IP address to ban.
   */
  public function banIp($ip) {
    $this->connection
      ->insert('ban_ip')
      ->fields(array(
      'ip' => $ip,
    ))
      ->execute();
  }

  /**
   * Unbans an IP address.
   *
   * @param string $id
   *   The IP address to unban.
   */
  public function unbanIp($id) {
    $this->connection
      ->delete('ban_ip')
      ->condition('ip', $id)
      ->execute();
  }

  /**
   * Finds a banned IP address by its ID.
   *
   * @param int $ban_id
   *   The ID for a banned IP address.
   *
   * @return string|false
   *   Either the banned IP address or FALSE if none exist with that ID.
   */
  public function findById($ban_id) {
    return $this->connection
      ->query("SELECT ip FROM {ban_ip} WHERE iid = :iid", array(
      ':iid' => $ban_id,
    ))
      ->fetchField();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BanIpManager::$connection protected property The database connection used to check the IP against.
BanIpManager::banIp public function Bans an IP address.
BanIpManager::findAll public function Finds all banned IP addresses.
BanIpManager::findById public function Finds a banned IP address by its ID.
BanIpManager::isBanned public function Returns if this IP address is banned.
BanIpManager::isDenied public function Returns whether an IP address is blocked.
BanIpManager::unbanIp public function Unbans an IP address.
BanIpManager::__construct public function Construct the BanSubscriber.