You are here

function restrict_by_ip_role_check in Restrict Login or Role Access by IP Address 8.4

Same name and namespace in other branches
  1. 6.3 restrict_by_ip.module \restrict_by_ip_role_check()
  2. 6.2 restrict_by_ip.module \restrict_by_ip_role_check()
  3. 7.3 restrict_by_ip.module \restrict_by_ip_role_check()

Perform an IP restriction check for all roles belonging to the given user.

@TODO

1 call to restrict_by_ip_role_check()
restrict_by_ip_boot in ./restrict_by_ip.module
Implements hook_boot().

File

./restrict_by_ip.module, line 150
Restrict logins or roles to whitelisted IP addresses.

Code

function restrict_by_ip_role_check(&$user) {
  $ip2check = _restrict_by_ip_get_ip();

  // Check each role belonging to specified user
  foreach ($user->roles as $rid => $name) {
    $form_name = _restrict_by_ip_hash_role_name($name);
    $ranges = variable_get('restrict_by_ip_role_' . $form_name, '');

    // Only check IP if an IP restriction is set for this role
    if (strlen($ranges) > 0) {
      $ipaddresses = explode(';', $ranges);
      $match = FALSE;
      foreach ($ipaddresses as $ipaddress) {
        if (_restrict_by_ip_cidrcheck($ip2check, $ipaddress)) {
          $match = TRUE;
        }
      }
      if (!$match) {
        unset($user->roles[$rid]);
      }
    }
  }
}