You are here

function _restrict_by_ip_login in Restrict Login or Role Access by IP Address 6.2

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

Login function Checks the user's ip address on login If they are not restricted, or logging in from the appropriate address allow logon to continue. If not redirect to a designated page

1 call to _restrict_by_ip_login()
restrict_by_ip_user in ./restrict_by_ip.module
Implementation of hook_user()

File

./restrict_by_ip.module, line 202
Allows the admin to select which ip addresses role or a user can login from for this site Some of the code below is taken from the cck_ipaddress_module

Code

function _restrict_by_ip_login(&$user) {
  if ($user->uid > 1) {
    $usrdata = db_fetch_object(db_query('SELECT * FROM {restrict_by_ip} WHERE uid = %d', $user->uid));
    $logonvalid = FALSE;
    $ip2check = $_SERVER['REMOTE_ADDR'];

    // If the user has restrict by ip address set
    if ($usrdata->restrict_by_ip_type) {
      $ipaddresses = explode(";", $usrdata->restrict_by_ip_address);

      // Check each valid ip address in database against users ip address
      // If one matches allow logon
      foreach ($ipaddresses as $ipaddress) {
        if (_restrict_by_ip_cidrcheck($ip2check, $ipaddress)) {
          $logonvalid = TRUE;
        }

        //else if(_restrict_by_role($user->$roles){}
      }

      // Restrict by ip address is set and no addresses match users ip address
      if (!$logonvalid) {

        // Log the error with the ip address
        watchdog('user', t('Session closed for %name - Invalid IP. ' . $ip2check, array(
          '%name' => $user->name,
        )));

        // Destroy the current session
        session_destroy();
        module_invoke_all('user', 'logout', NULL, $user);

        // Load the anonymous user
        $user = drupal_anonymous_user();

        // unset destination required to force them to the ip page during drupal_goto()
        if (isset($_REQUEST['destination'])) {
          unset($_REQUEST['destination']);
        }

        // Goto the page detailed in the site configuration - restrict by ip - settings page
        drupal_goto(variable_get('restrict_by_ip_error_page', '0'));
      }
    }
    return $logonvalid;
  }
  else {
    return TRUE;
  }
}