You are here

protected function UserInCountry::doEvaluate in IP-based Determination of a Visitor's Country 8

Evaluates if the user has an IP address in one of the selected countries.

Parameters

array $countries: Array of 2-character country codes.

Return value

bool TRUE if the user has an IP in one of the given countries.

File

src/Plugin/Condition/UserInCountry.php, line 141

Class

UserInCountry
Provides a 'User is in country' condition.

Namespace

Drupal\ip2country\Plugin\Condition

Code

protected function doEvaluate(array $countries = []) {
  $userData = $this->userData
    ->get('ip2country', $this->currentUser
    ->id(), 'country_iso_code_2');
  if (isset($userData)) {

    // Use the country stored in the $user object.
    $countryCode = $userData;
  }
  else {

    // Determine the user's country based on IP address of the page request.
    $ip = $this->requestStack
      ->getCurrentRequest()
      ->getClientIp();
    $countryCode = $this->ip2countryLookup
      ->getCountry($ip);
  }
  return in_array($countryCode, $countries);
}