You are here

restrict_by_ip.module in Restrict Login or Role Access by IP Address 5

Allows the admin to select which IP Addresses can register for this site.

File

restrict_by_ip.module
View source
<?php

/**
 * @file
 * Allows the admin to select which IP Addresses can register for this site.
 */

/**
 * Implementation of hook_help().
 *  The first case adds a little help text.
 *  The second case adds some submission guidelines on the create content page.
 */
function restrict_by_ip_help($section) {
  switch ($section) {
    case 'admin/help#restrict_by_ip':
      $output = '<p>' . t('The site administrator can limit certain users to being able to login only from certain IP Addresses.') . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_menu().
 *  Add a menu item to the Administer >> Site building menu for displaying the restrict_by_ip.
 */
function restrict_by_ip_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/restrict_by_ip',
      'title' => t('Restrict by IP settings'),
      'description' => t('Limit the IP address a user is allowed to login from.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'restrict_by_ip_settings',
      'access' => user_access('administer restrict by ip'),
    );
  }
  return $items;
}

/**
 * menu callback to configure module settings.
 */
function restrict_by_ip_settings() {
  $form['heading'] = array(
    '#value' => '<b>' . t('Configuration options for the Restrict By IP module:') . '</b>',
    '#weight' => -6,
  );
  $form['restrict_by_ip_error_page'] = array(
    '#type' => 'textfield',
    '#title' => t('Login denied page'),
    '#description' => t("This the address of the page to which the user will be redirected if they are not allowed to login."),
    '#weight' => -5,
    '#default_value' => variable_get('restrict_by_ip_error_page', ''),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_perm().
 */
function restrict_by_ip_perm() {
  return array(
    'administer restrict by ip',
  );
}

/**
 * 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.
 */
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();
}

/**
 * Menu callback: administer roles.
 */

/**
*function restrict_by_ip_role() {
*    // Add field to the edit role form.
*    $form['restrict_by_ip'] = array('#type' => 'fieldset',
*      '#title' => t('Restrict by IP settings'),
*      '#weight' => 5,
*      '#collapsible' => TRUE,
*      '#collapsed' => TRUE,
*    );
*    $form['restrict_by_ip']['restrict_by_ip'] = array('#type' => 'textfield',
*      '#title' => t('Restricted IP Address'),
*      '#default_value' => $role->restrict_by_ip,
*      '#size' => 20,
*      '#maxlength' => 15,
*      '#description' => t('Enter the IP address that this role is allowed to login from (xxx.xxx.xxx.xxx).'),
*      '#required' => FALSE,
*    );
*  return $form;
*}
*/

Functions

Namesort descending Description
restrict_by_ip_help Implementation of hook_help(). The first case adds a little help text. The second case adds some submission guidelines on the create content page.
restrict_by_ip_menu Implementation of hook_menu(). Add a menu item to the Administer >> Site building menu for displaying the restrict_by_ip.
restrict_by_ip_perm Implementation of hook_perm().
restrict_by_ip_settings menu callback to configure module settings.
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.