You are here

user_limit.module in User Limit 6

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

Everything related to User Limit. There are no include files.

This mostly just contains standard hooks to limit users.

File

user_limit.module
View source
<?php

/**
 * @file
 * Everything related to User Limit. There are no include files.
 *
 * This mostly just contains standard hooks to limit users.
 */

/*
 * Absolute default message to use when the user limit is reached. Only used
 * when the relevant variable is not set.
 */
define('USER_LIMIT_DEFAULT_MESSAGE', 'Your user limit has been reached. To add more users, please contact your service provider.');

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

  // count all users
  $sql = 'SELECT count(*) FROM {users} WHERE uid <> 0';

  // optionally exclude UID1
  if ($count_uid1 == 0) {
    $sql .= ' AND uid <> 1';
  }

  // optionally exclude banned users
  if ($active_only) {
    $sql .= ' AND status = 1';
  }
  $user_count = db_result(db_query($sql));
  return $user_count;
}

/**
 * Calculates whether we reached the number of allowed users on the site.
 *
 * This limit is based on the settings. The number is calculated with
 * user_limit_count_users(), which makes the result vary according to settings.
 *
 * @return
 *   TRUE if the limit is reached, FALSE otherwise.
 */
function user_limit_reached() {
  $user_limit = variable_get('user_limit', 0);
  if ($user_limit > 0 && user_limit_count_users() == $user_limit) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

/**
 * Calculates whether we surpassed the number of allowed users on the site.
 *
 * This limit is based on the settings. The number is calculated with
 * user_limit_count_users(), which makes the result vary according to settings.
 *
 * @return
 *   TRUE if the limit is surpassed, FALSE otherwise.
 */
function user_limit_surpassed() {
  $user_limit = variable_get('user_limit', 0);
  if ($user_limit > 0 && user_limit_count_users() > $user_limit) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

/**
 * Implementation of hook_perm().
 */
function user_limit_perm() {
  return array(
    'set user limit',
  );
}

/**
 * Implementation of hook_FORM_ID_alter().
 *
 * Adds a message to the user_register form based as relevant.
 */
function user_limit_form_user_register_alter(&$form, &$form_state) {
  $user_limit = variable_get('user_limit', 0);
  if ($user_limit != 0) {
    $number_allowed = variable_get('user_limit', 0) > 0 ? variable_get('user_limit', 0) : '&infin';
    $t_array = array(
      '!number_allowed' => $number_allowed,
      '!number_active' => user_limit_count_users(),
    );
    drupal_set_message(t('User limit (active/allowed): !number_active / !number_allowed ', $t_array), 'status', FALSE);
    if (user_limit_reached() || user_limit_surpassed()) {
      drupal_set_message(variable_get('user_limit_message', USER_LIMIT_DEFAULT_MESSAGE), 'warning', FALSE);
    }
  }
}

/**
 * Implementation of hook_user().
 */
function user_limit_user($type, &$edit, $account, $category = NULL) {
  if ($type == 'insert' && user_limit_surpassed()) {
    user_delete($edit, $account->uid);
    watchdog('user limit', 'User limit surpassed', $variables = array(), WATCHDOG_NOTICE, $link = 'admin/user/settings');
  }
}

/**
 * Implementation of hook_FORM_ID_alter().
 *
 * Adds our 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']['user_settings']['user_limit'] = array(
    '#type' => 'fieldset',
    '#title' => t('User limit'),
    '#collapsible' => TRUE,
    '#collapsed' => $all_options_default,
  );
  $form['registration']['user_settings']['user_limit']['user_settings']['user_limit'] = array(
    '#type' => 'textfield',
    '#title' => t('User limit'),
    '#default_value' => variable_get('user_limit', 0),
    '#description' => t('This setting will 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']['user_settings']['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 towards the user limit for the site.'),
  );
  $form['registration']['user_settings']['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 user from the limit for the site.'),
  );
  $form['registration']['user_settings']['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 provide if user limit is reached or surpassed.'),
  );

  // Add our submit handler, but don't override the existing one(s)
  $form['#submit'][] = 'user_limit_submit';
}

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

/**
 * Save our variables when user settings are saved.
 *
 * @see user_limit_form_user_admin_settings_alter()
 */
function user_limit_submit($form, &$form_state) {
  if ($form_state['clicked_button']['#id'] == 'edit-submit') {
    variable_set('user_limit', $form_state['values']['user_limit']);
    variable_set('user_limit_uid1', $form_state['values']['user_limit_uid1']);
    variable_set('user_limit_active', $form_state['values']['user_limit_active']);
    variable_set('user_limit_message', $form_state['values']['user_limit_message']);
  }
  elseif ($form_state['clicked_button']['#id'] == 'edit-reset') {
    variable_del('user_limit');
    variable_del('user_limit_uid1');
    variable_del('user_limit_active');
    variable_del('user_limit_message');
  }
}

Functions

Namesort descending Description
user_limit_count_users Counts the number of users on the site.
user_limit_form_user_admin_settings_alter Implementation of hook_FORM_ID_alter().
user_limit_form_user_register_alter Implementation of hook_FORM_ID_alter().
user_limit_perm Implementation of hook_perm().
user_limit_reached Calculates whether we reached the number of allowed users on the site.
user_limit_submit Save our variables when user settings are saved.
user_limit_surpassed Calculates whether we surpassed the number of allowed users on the site.
user_limit_user Implementation of hook_user().
user_limit_validate Ensure that the entered user limit is a positive integer.

Constants