You are here

function restrict_by_ip_user in Restrict Login or Role Access by IP Address 5

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

Implementation of hook_user(). Checks the user's IP Address on login. If they are not restricted, or logging in from the appropriate address allow registration to continue. If not, then redirect to a designated page.

File

./restrict_by_ip.module, line 74
Allows the admin to select which IP Addresses can register for this site.

Code

function restrict_by_ip_user($op, &$edit, &$user, $category = NULL) {
  global $user;
  switch ($op) {
    case 'load':
      if ($user->uid) {
        $result = db_query("SELECT restrict_by_ip_address FROM {restrict_by_ip} WHERE uid=%d", $user->uid);
        if ($ur = db_fetch_object($result)) {
          $user->restrict_by_ip = $ur->restrict_by_ip;
        }
      }
      $addr = $_SERVER['REMOTE_ADDR'];
      print $addr;

      // If $user->restrict_by_ip is Null, then the user isn't restricted.
      if ($user->restrict_by_ip) {

        // See if it's an allowed IP address for this user.
        if ($addr == $user->restrict_by_ip) {

          /* okay */
        }
        else {
          drupal_goto(variable_get('restrict_by_ip_error_page', ''));
        }
      }

      /* end if restrict by ip */
      break;

    /* end of load */
    case 'form':
      if ($user->uid) {
        $result = db_query("SELECT restrict_by_ip_address FROM {restrict_by_ip} WHERE uid=%d", $user->uid);
        if ($ur = db_fetch_object($result)) {
          $user->restrict_by_ip = $ur->restrict_by_ip_address;
        }
      }
      $form['restrict_by_ip'] = array(
        '#type' => 'fieldset',
        '#title' => t('Restrict by IP settings'),
        '#weight' => 5,
        '#collapsible' => TRUE,
        //collapse if there is no restricted ip address
        '#collapsed' => !$user->restrict_by_ip,
      );
      $form['restrict_by_ip']['restrict_by_ip'] = array(
        '#type' => 'textfield',
        '#title' => t('Restricted IP Address'),
        '#default_value' => $user->restrict_by_ip,
        '#size' => 20,
        '#maxlength' => 15,
        '#description' => t('Enter the IP address that this user is allowed to login from (xxx.xxx.xxx.xxx).'),
        '#required' => FALSE,
      );
      return $form;
      break;

    /* end of form */
    case 'update':
      $result = db_query(" SELECT * FROM {restrict_by_ip} WHERE {restrict_by_ip}.uid=%d ", $user->uid);
      if (!($oldnode = db_fetch_object($result))) {
        db_query("INSERT INTO {restrict_by_ip} (uid, restrict_by_ip_address) VALUES(%d, '%s')", $user->uid, $edit['restrict_by_ip']);
        $edit['restrict_by_ip'] = NULL;
      }
      else {
        db_query("UPDATE {restrict_by_ip} SET restrict_by_ip_address='%s' WHERE uid=%d", $edit['restrict_by_ip'], $user->uid);
        $edit['restrict_by_ip'] = NULL;
      }
      break;
  }

  /* end of switch */
  return array();
}