View source
<?php
define('USER_LIMIT_DEFAULT_MESSAGE', "This site's user limit has been reached. Please contact the site administrator.");
function user_limit_user_insert(&$edit, $account, $category) {
if (user_limit_reached(TRUE)) {
user_delete($account->uid);
watchdog('user_limit', 'User limit surpassed', array(), WATCHDOG_NOTICE, 'admin/user/settings');
}
}
function user_limit_form_user_register_form_alter(&$form, &$form_state) {
$user_limit = variable_get('user_limit', 0);
if ($user_limit) {
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 (user_limit_reached()) {
$form['#access'] = FALSE;
drupal_set_message(variable_get('user_limit_message', USER_LIMIT_DEFAULT_MESSAGE), 'warning', FALSE);
}
}
}
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.'),
);
}
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...).'));
}
}
function user_limit_count_users() {
$count_uid1 = variable_get('user_limit_uid1', 0);
$active_only = variable_get('user_limit_active', 0);
$query = db_select('users', 'u')
->fields('u')
->condition('uid', 0, '<>');
if ($count_uid1 == 0) {
$query
->condition('uid', 1, '<>');
}
if ($active_only) {
$query
->condition('status', 1);
}
$user_count = $query
->countQuery()
->execute()
->fetchField();
return $user_count;
}
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;
}
}