You are here

class RestrictIpMapper in Restrict IP 8

Same name and namespace in other branches
  1. 8.2 src/Mapper/RestrictIpMapper.php \Drupal\restrict_ip\Mapper\RestrictIpMapper
  2. 3.x src/Mapper/RestrictIpMapper.php \Drupal\restrict_ip\Mapper\RestrictIpMapper

Hierarchy

Expanded class hierarchy of RestrictIpMapper

1 string reference to 'RestrictIpMapper'
restrict_ip.services.yml in ./restrict_ip.services.yml
restrict_ip.services.yml

File

src/Mapper/RestrictIpMapper.php, line 7

Namespace

Drupal\restrict_ip\Mapper
View source
class RestrictIpMapper implements RestrictIpMapperInterface {

  /**
   * The database connection
   *
   * @var Drupal\Core\Database\Connection
   */
  protected $connection;
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public function getWhitelistedIpAddresses() {
    return $this->connection
      ->query('SELECT ip_address FROM {restrict_ip_whitelisted_ip_addresses} ORDER BY ip_address ASC')
      ->fetchCol();
  }

  /**
   * {@inheritdoc}
   */
  public function saveWhitelistedIpAddresses(array $ip_addresses, $overwriteExisting = TRUE) {
    if ($overwriteExisting) {
      $this->connection
        ->query('DELETE FROM {restrict_ip_whitelisted_ip_addresses}');
    }
    $query = $this->connection
      ->insert('restrict_ip_whitelisted_ip_addresses')
      ->fields([
      'ip_address',
    ]);
    foreach ($ip_addresses as $ip_address) {
      $query
        ->values([
        'ip_address' => $ip_address,
      ]);
    }
    $query
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getWhitelistedPaths() {
    return $this->connection
      ->query('SELECT path FROM {restrict_ip_paths} WHERE type = :white ORDER BY path ASC', [
      ':white' => 'white',
    ])
      ->fetchCol();
  }

  /**
   * {@inheritdoc}
   */
  public function saveWhitelistedPaths(array $whitelistedPaths, $overwriteExisting = TRUE) {
    if ($overwriteExisting) {
      $this->connection
        ->query('DELETE FROM {restrict_ip_paths} WHERE type = :white', [
        ':white' => 'white',
      ]);
    }
    $query = $this->connection
      ->insert('restrict_ip_paths')
      ->fields([
      'type',
      'path',
    ]);
    foreach ($whitelistedPaths as $whitelisted_path) {
      $query
        ->values([
        'type' => 'white',
        'path' => $whitelisted_path,
      ]);
    }
    $query
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getBlacklistedPaths() {
    return $this->connection
      ->query('SELECT path FROM {restrict_ip_paths} WHERE type = :black ORDER BY path ASC', [
      ':black' => 'black',
    ])
      ->fetchCol();
  }

  /**
   * {@inheritdoc}
   */
  public function saveBlacklistedPaths(array $blacklistedPaths, $overwriteExisting = TRUE) {
    if ($overwriteExisting) {
      $this->connection
        ->query('DELETE FROM {restrict_ip_paths} WHERE type = :black', [
        ':black' => 'black',
      ]);
    }
    $query = $this->connection
      ->insert('restrict_ip_paths')
      ->fields([
      'type',
      'path',
    ]);
    foreach ($blacklistedPaths as $blacklisted_path) {
      $query
        ->values([
        'type' => 'black',
        'path' => $blacklisted_path,
      ]);
    }
    $query
      ->execute();
  }

}

Members