You are here

user_limit.module in User Limit 7

Same filename and directory in other branches
  1. 6 user_limit.module

The User Limit module limits the number of users that can be registered on a Drupal site.

File

user_limit.module
View source
<?php

/**
 * @file
 * The User Limit module limits the number of users that can be registered on a
 * Drupal site.
 */

/*
 * Default message to use when the user limit is reached. Only used when the
 * relevant variable is not set.
 */
define('USER_LIMIT_DEFAULT_MESSAGE', "This site's user limit has been reached. Please contact the site administrator.");

/**
 * Implements hook_user_insert().
 */
function user_limit_user_insert(&$edit, $account, $category) {

  // If the user limit has been reached, delete any created accounts and log
  // account deletion.
  if (user_limit_reached(TRUE)) {
    user_delete($account->uid);
    watchdog('user_limit', 'User limit surpassed', array(), WATCHDOG_NOTICE, 'admin/user/settings');
  }
}

/**
 * Implements hook_FORM_ID_alter().
 *
 * Adds a message to the user_register_form when limit is reached.
 */
function user_limit_form_user_register_form_alter(&$form, &$form_state) {
  $user_limit = variable_get('user_limit', 0);
  if ($user_limit) {

    // Show a message with current limit if configured to do so.
    if (variable_get('user_limit_show_counts', 0)) {
      drupal_set_message(t('User limit (active/allowed): @number_active / @number_allowed', array(
        '@number_allowed' => $user_limit,
        '@number_active' => user_limit_count_users(),
      )));
    }

    // If the user limit has been reached, disable the form.
    if (user_limit_reached()) {
      $form['#access'] = FALSE;
      drupal_set_message(variable_get('user_limit_message', USER_LIMIT_DEFAULT_MESSAGE), 'warning', FALSE);
    }
  }
}

/**
 * Implements hook_FORM_ID_alter().
 *
 * Add settings to the user_admin_settings form.
 */
function user_limit_form_user_admin_settings_alter(&$form, &$form_state) {
  $limit_is_default = variable_get('user_limit', 0) == 0;
  $uid1_is_default = variable_get('user_limit_uid1', 0) == 0;
  $active_is_default = variable_get('user_limit_active', 0) == 0;
  $message_is_default = variable_get('user_limit_message', USER_LIMIT_DEFAULT_MESSAGE) == USER_LIMIT_DEFAULT_MESSAGE;
  $all_options_default = $limit_is_default && $uid1_is_default && $active_is_default && $message_is_default;
  $form['registration_cancellation']['user_limit'] = array(
    '#type' => 'fieldset',
    '#title' => t('User limit'),
    '#collapsible' => TRUE,
    '#collapsed' => $all_options_default,
  );
  $form['registration_cancellation']['user_limit']['user_limit'] = array(
    '#type' => 'textfield',
    '#title' => t('User limit'),
    '#default_value' => variable_get('user_limit', 0),
    '#description' => t('Limit the total number of users that can be created on the site. Set to 0 for unlimited.'),
    '#element_validate' => array(
      'user_limit_validate',
    ),
  );
  $form['registration_cancellation']['user_limit']['user_limit_uid1'] = array(
    '#type' => 'checkbox',
    '#title' => t('Count UID1 towards user limit'),
    '#default_value' => variable_get('user_limit_uid1', 0),
    '#description' => t('Include UID1 in the count for the user limit for the site.'),
  );
  $form['registration_cancellation']['user_limit']['user_limit_active'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only count active users'),
    '#default_value' => variable_get('user_limit_active', 0),
    '#description' => t('Exclude banned or canceled users from the limit for the site.'),
  );
  $form['registration_cancellation']['user_limit']['user_limit_show_counts'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show counts on user registration page'),
    '#default_value' => variable_get('user_limit_show_counts', 0),
    '#description' => t('Show the current number of users and the limit on the user registration page.'),
  );
  $form['registration_cancellation']['user_limit']['user_limit_message'] = array(
    '#type' => 'textarea',
    '#title' => t('Limit surpassed message'),
    '#default_value' => variable_get('user_limit_message', USER_LIMIT_DEFAULT_MESSAGE),
    '#description' => t('Message to display on user registration page if user limit is reached.'),
  );
}

/**
 * Ensure that the entered user limit is a positive integer.
 */
function user_limit_validate($element, &$form_state) {
  $user_limit = (int) $element['#value'];
  if (!is_numeric($element['#value']) || $user_limit < 0) {
    form_error($element, t('User limit must be a positive integer (1, 2, 3...).'));
  }
}

/**
 * Counts the number of users on the site.
 *
 * May or may not count inactive users and user 1 depending on settings. This
 * is the "canonical" function for counting users in User Limit.
 *
 * @return
 *   An int representing the number of relevant users.
 */
function user_limit_count_users() {
  $count_uid1 = variable_get('user_limit_uid1', 0);
  $active_only = variable_get('user_limit_active', 0);

  // Set up query to count all users.
  $query = db_select('users', 'u')
    ->fields('u')
    ->condition('uid', 0, '<>');

  // Optionally exclude UID1.
  if ($count_uid1 == 0) {
    $query
      ->condition('uid', 1, '<>');
  }

  // Optionally exclude banned users.
  if ($active_only) {
    $query
      ->condition('status', 1);
  }
  $user_count = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  return $user_count;
}

/**
 * Calculates whether the number of allowed users on the site has been reached.
 *
 * This limit is based on the settings. The number is calculated with
 * user_limit_count_users(), which makes the result vary according to settings.
 *
 * @param $insert
 *   Set to TRUE if this is being called from a user_insert function.
 *
 * @return
 *   TRUE if the limit is reached, FALSE otherwise.
 */
function user_limit_reached($insert = FALSE) {
  $user_limit = variable_get('user_limit', 0);
  $new_user_limit = $insert ? $user_limit + 1 : $user_limit;
  if ($user_limit && user_limit_count_users() >= $new_user_limit) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

Functions

Namesort descending Description
user_limit_count_users Counts the number of users on the site.
user_limit_form_user_admin_settings_alter Implements hook_FORM_ID_alter().
user_limit_form_user_register_form_alter Implements hook_FORM_ID_alter().
user_limit_reached Calculates whether the number of allowed users on the site has been reached.
user_limit_user_insert Implements hook_user_insert().
user_limit_validate Ensure that the entered user limit is a positive integer.

Constants