You are here

public function IpBanSetBanManager::iPBanSetValue in IP Ban 8

Set the user's ban value based on their country or individual IP address.

Load the user's country code based on their IP address, then check if that country is set to complete ban, read-only, or has no access restrictions. We then do the same for any additional read-only or complete ban IP addresses added. If the user matched a country or an IP address entered, then they are shown a message and/or redirected based on complete ban or read-only.

Overrides IpBanSetBanInterface::iPBanSetValue

File

src/IpBanSetBanManager.php, line 29
Contains \Drupal\ip_ban\IpBanSetBanInterface.

Class

IpBanSetBanManager

Namespace

Drupal\ip_ban

Code

public function iPBanSetValue() {

  // If code is being run from drush, we don't want to take any action.
  if (PHP_SAPI === "cli" && function_exists('drush_main')) {
    return;
  }

  // If user has permission to bypass ban, set to IP_BAN_NOBAN and return
  if (\Drupal::currentUser()
    ->hasPermission('ignore ip_ban')) {
    $this->banvalue = IP_BAN_NOBAN;
    return;
  }
  $test_ip = \Drupal::config('ip_ban.settings')
    ->get('ip_ban_test_ip');

  // Grab the test IP address or the user's real address.
  $ip = empty($test_ip) ? \Drupal::request()
    ->getClientIp() : $test_ip;
  $country_code = ip2country_get_country($ip);

  // Determine if the current user is banned or read only.
  // Individual IP complete ban trumps individual read only IP, both of which
  // trump a country setting.
  if (!isset($this->banvalue)) {
    $banvalue = (int) \Drupal::config('ip_ban.settings')
      ->get('ip_ban_' . $country_code);
    $this->banvalue = $banvalue;

    // Check the read-only IP list.
    $readonly_ips = \Drupal::config('ip_ban.settings')
      ->get('ip_ban_readonly_ips');
    if (!empty($readonly_ips)) {
      $ip_readonly_array = explode(PHP_EOL, $readonly_ips);
      if (in_array($ip, $ip_readonly_array)) {
        $this->{$banvalue} = IP_BAN_READONLY;
      }
    }

    // Check the complete ban list.
    $banned_ips = \Drupal::config('ip_ban.settings')
      ->get('ip_ban_additional_ips');
    if (!empty($banned_ips)) {
      $ip_ban_array = explode(PHP_EOL, $banned_ips);
      if (in_array($ip, $ip_ban_array)) {
        $this->banvalue = IP_BAN_BANNED;
      }
    }
  }
  return;
}