user_registrationpassword.module in User registration password 7
Same filename and directory in other branches
Enables password creation on registration form.
File
user_registrationpassword.moduleView source
<?php
/**
* @file
* Enables password creation on registration form.
*/
/**
* No verification email is sent.
*/
define('USER_REGISTRATIONPASSWORD_NO_VERIFICATION', 'none');
/**
* Verification email is sent before password is set.
*/
define('USER_REGISTRATIONPASSWORD_VERIFICATION_DEFAULT', 'default');
/**
* Verification email is sent after password is set.
*/
define('USER_REGISTRATIONPASSWORD_VERIFICATION_PASS', 'with-pass');
/**
* Implements hook_menu().
*/
function user_registrationpassword_menu() {
$items['user/registrationpassword/%/%/%'] = array(
'title' => 'Confirm account',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'user_registrationpassword_confirm_account',
2,
3,
4,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'user_registrationpassword.pages.inc',
);
return $items;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function user_registrationpassword_form_user_admin_settings_alter(&$form, &$form_state) {
// Render our configuration options.
$form['registration_cancellation']['user_registrationpassword_registration'] = array(
'#type' => 'radios',
'#title' => t('Require e-mail verification when a visitor creates an account'),
'#description' => t('Choose whether new users can set their password directly on the registration form and login during registration (without e-mail confirmation), or if they will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password, or they can set a password during registration, but first have to confirm their account via the activation e-mail sent after registration is complete, before they can log in to the site.'),
'#options' => array(
USER_REGISTRATIONPASSWORD_NO_VERIFICATION => t('Do not require a verification e-mail, and let users set their password on the registration form.'),
USER_REGISTRATIONPASSWORD_VERIFICATION_DEFAULT => t('Require a verification e-mail, but wait for the approval e-mail to let users set their password.'),
USER_REGISTRATIONPASSWORD_VERIFICATION_PASS => t('Require a verification e-mail, but let users set their password directly on the registration form.'),
),
'#default_value' => variable_get('user_registrationpassword_registration', USER_REGISTRATIONPASSWORD_VERIFICATION_PASS),
);
// Render an option to change first time login link behaviour.
$form['registration_cancellation']['user_registrationpassword_registration_ftll_expire'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Account activation link expiration'),
'#description' => t('This option enables site admins to expire activation links after the expiration time has passed. People are still able to request a new activation e-mail via the password reset form after this time expires to activate their account via a new activation e-mail.'),
'#default_value' => variable_get('user_registrationpassword_registration_ftll_expire', FALSE),
'#weight' => 7,
);
// Hide this setting by default.
$form['registration_cancellation']['ftll_timeout'] = array(
'#type' => 'container',
'#weight' => 8,
'#states' => array(
'invisible' => array(
'input[name="user_registrationpassword_registration_ftll_expire"]' => array(
'checked' => FALSE,
),
),
),
);
$form['registration_cancellation']['ftll_timeout']['user_registrationpassword_registration_ftll_timeout'] = array(
'#type' => 'textfield',
'#title' => t('Account activation link expiration'),
'#description' => t('Enter the expiration time for the activation link (aka first time login link) sent in the activation e-mail. The default is set to 86400 seconds (= 24 hours).'),
'#default_value' => variable_get('user_registrationpassword_registration_ftll_timeout', ''),
'#maxlength' => 10,
'#weight' => 8,
);
// Hide the default option.
$form['registration_cancellation']['user_email_verification']['#access'] = FALSE;
// Set up available tokens.
if (module_exists('rpt')) {
// Hide the option to generate passwords, because we
// only use the token option the rtp module provides.
$form['registration_cancellation']['rpt_password_generate']['#access'] = FALSE;
// We support the Registration Password Tokens module.
// Add a password to a template with [user:password].
// See http://drupal.org/project/rpt for more information.
$email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [user:password], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url], [user:registrationpassword-url].');
}
else {
$email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url], [user:registrationpassword-url].');
}
// Render e-mail template settings.
$form['email_user_registrationpassword'] = array(
'#type' => 'fieldset',
'#title' => t('Welcome (no approval required, password is set)'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when no administrator approval is required and password has already been set.') . ' ' . $email_token_help,
'#group' => 'email',
);
$form['email_user_registrationpassword']['user_registrationpassword_register_subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => _user_registrationpassword_mail_text('register_subject', NULL, array(), FALSE),
'#maxlength' => 180,
);
$form['email_user_registrationpassword']['user_registrationpassword_register_body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#default_value' => _user_registrationpassword_mail_text('register_body', NULL, array(), FALSE),
'#rows' => 15,
);
// Override the default activation e-mail template with our own.
$form['email_activated']['settings']['user_mail_status_activated_subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => variable_get('user_mail_status_activated_subject', _user_registrationpassword_mail_text('status_activated_subject', NULL, array(), FALSE)),
'#maxlength' => 180,
);
$form['email_activated']['settings']['user_mail_status_activated_body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#default_value' => variable_get('user_mail_status_activated_body', _user_registrationpassword_mail_text('status_activated_body', NULL, array(), FALSE)),
'#rows' => 15,
);
// Reorder the form items back to their original order.
$form['anonymous_settings']['#weight'] = -2;
$form['admin_role']['#weight'] = -1;
$form['registration_cancellation']['#weight'] = 0;
$form['registration_cancellation']['user_cancel_method']['#weight'] = 3;
$form['registration_cancellation']['user_registrationpassword_registration']['#weight'] = 2;
$form['personalization']['#weight'] = 4;
$form['email_title']['#weight'] = 5;
$form['email']['#weight'] = 6;
$form['email_admin_created']['#weight'] = 6;
// Register our extra submit function.
$form['#submit'][] = 'user_registrationpassword_admin_settings_submit';
}
/**
* Submit handler for the user admin form.
*/
function user_registrationpassword_admin_settings_submit(&$form, &$form_state) {
// The option we implement.
$user_registrationpassword_registration = $form_state['values']['user_registrationpassword_registration'];
// Core option.
$user_register = $form_state['values']['user_register'];
// Test if we are enabled. For this, both options have to be set correctly
// because then and only then we can function.
// @see _user_mail_notify()
if ($user_registrationpassword_registration == USER_REGISTRATIONPASSWORD_VERIFICATION_PASS && $user_register == USER_REGISTER_VISITORS) {
// Disable core verification emails, we are taking over.
variable_set('user_email_verification', FALSE);
// Prevent standard notification email to administrators and to user.
variable_set('user_mail_register_pending_approval_notify', FALSE);
variable_set('user_mail_register_no_approval_required_notify', FALSE);
}
else {
// Our option.
switch ($user_registrationpassword_registration) {
case USER_REGISTRATIONPASSWORD_NO_VERIFICATION:
variable_set('user_email_verification', FALSE);
break;
case USER_REGISTRATIONPASSWORD_VERIFICATION_DEFAULT:
case USER_REGISTRATIONPASSWORD_VERIFICATION_PASS:
variable_set('user_email_verification', TRUE);
break;
}
// Core option.
switch ($user_register) {
case USER_REGISTER_ADMINISTRATORS_ONLY:
// The register_pending_approval option.
// Special case tested in _user_mail_notify().
variable_set('user_mail_register_pending_approval_notify', FALSE);
// The no_approval_required option.
variable_set('user_mail_register_no_approval_required_notify', FALSE);
break;
case USER_REGISTER_VISITORS:
case USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL:
// The register_pending_approval option.
// Special case tested in _user_mail_notify().
variable_set('user_mail_register_pending_approval_notify', TRUE);
// The no_approval_required option.
variable_set('user_mail_register_no_approval_required_notify', TRUE);
break;
}
}
// Save or revert expiration limits variables.
if ($form_state['values']['user_registrationpassword_registration_ftll_expire']) {
variable_set('user_registrationpassword_registration_ftll_expire', $form_state['values']['user_registrationpassword_registration_ftll_expire']);
}
else {
variable_del('user_registrationpassword_registration_ftll_expire');
}
if ($form_state['values']['user_registrationpassword_registration_ftll_timeout'] && $form_state['values']['user_registrationpassword_registration_ftll_timeout'] != 86400) {
variable_set('user_registrationpassword_registration_ftll_timeout', $form_state['values']['user_registrationpassword_registration_ftll_timeout']);
}
else {
variable_del('user_registrationpassword_registration_ftll_timeout');
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see user_register_form()
* @see user_registrationpassword_form_user_register_submit()
*/
function user_registrationpassword_form_user_register_form_alter(&$form, &$form_state) {
global $user;
// Add the password field from the user_account_form when visitors can
// register, but require admin approval.
if (variable_get('user_register', USER_REGISTER_VISITORS) == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL && variable_get('user_registrationpassword_registration', USER_REGISTRATIONPASSWORD_VERIFICATION_DEFAULT) == USER_REGISTRATIONPASSWORD_VERIFICATION_PASS && !$user->uid) {
$form['account']['pass'] = array(
'#type' => 'password_confirm',
'#size' => 25,
'#description' => t('Provide a password for the new account in both fields.'),
'#required' => TRUE,
);
}
// Prevent this from being run if approval with password on registration
// form is set and the user is an anonymous user registering to the site.
// When admin users create a user, this does not need to be executed.
// And when this also does not need to be executed 'user_register' is not set
// as 'Visitors can create accounts and no administrator approval is
// required.' User registers, recieves user_registrationpass email, would
// not make sense. Cause that will unblock the user Without
// the admin 'approving'.
if (variable_get('user_register', USER_REGISTER_VISITORS) == USER_REGISTER_VISITORS && variable_get('user_registrationpassword_registration', USER_REGISTRATIONPASSWORD_VERIFICATION_DEFAULT) == USER_REGISTRATIONPASSWORD_VERIFICATION_PASS && !$user->uid) {
// Set the user account to blocked.
$form['account']['status']['#default_value'] = 0;
// Supress any notification.
$form['account']['notify']['#default_value'] = FALSE;
// Register our validate and submit handlers.
$form['#submit'][] = 'user_registrationpassword_form_user_register_submit';
}
}
/**
* Implements submission handler for the user registration form.
*
* @see user_register_form()
* @see user_registrationpassword_form_user_register_form_alter()
*/
function user_registrationpassword_form_user_register_submit(&$form, &$form_state) {
// Define the message we need to remove.
// Yes, this throws a code style error, but this is in core.
// See user.module, that string contains a <br />.
$message = t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.');
// Unset all messages that contain the $message.
foreach ($_SESSION['messages']['status'] as $status => $statusmessage) {
// Test for core message (we need to replace).
if ($message == $statusmessage) {
// And unset it if we find it.
unset($_SESSION['messages']['status'][$status]);
}
}
$_SESSION['messages']['status'] = array_values($_SESSION['messages']['status']);
// Notify the user.
$account = $form_state['user'];
$params['account'] = $account;
$mail = drupal_mail('user_registrationpassword', 'register', $account->mail, user_preferred_language($account), $params);
// Set succes message and redirect to the front page.
drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.'));
}
/**
* Implements hook_mail().
*
* @see drupal_mail()
* @see _user_registrationpassword_mail_text()
* @see user_registrationpassword_mailkeys()
* @see user_registrationpassword_mail_edit_text()
* @see user_registrationpassword_mail_edit_token_types()
*/
function user_registrationpassword_mail($key, &$message, $params) {
$language = $message['language'];
$variables = array(
'user' => $params['account'],
);
$message['subject'] .= _user_registrationpassword_mail_text($key . '_subject', $language, $variables);
$message['body'][] = _user_registrationpassword_mail_text($key . '_body', $language, $variables);
}
/**
* Returns a mail string for a variable name.
*
* @see user_registrationpassword_mail()
* @see user_registrationpassword_mail_tokens()
* @see user_registrationpassword_mailkeys()
* @see user_registrationpassword_mail_edit_text()
* @see user_registrationpassword_mail_edit_token_types()
*/
function _user_registrationpassword_mail_text($key, $language = NULL, $variables = array(), $replace = TRUE) {
$langcode = isset($language) ? $language->language : NULL;
if ($admin_setting = variable_get('user_registrationpassword_' . $key, FALSE)) {
// An admin setting overrides the default string.
$text = $admin_setting;
}
else {
// No override, return default string.
switch ($key) {
case 'register_subject':
$text = t('Account details for [user:name] at [site:name]', array(), array(
'langcode' => $langcode,
));
break;
case 'register_body':
$text = t("[user:name],\n\nThank you for registering at [site:name]. You may now log in and verify your account by clicking this link or copying and pasting it to your browser:\n\n[user:registrationpassword-url]\n\nThis link can only be used once. You will be able to log in at [site:login-url] in the future using:\n\nusername: [user:name]\npassword: Your password\n\n-- [site:name] team", array(), array(
'langcode' => $langcode,
));
break;
case 'status_activated_subject':
$text = t('Welcome message for [user:name] at [site:name]', array(), array(
'langcode' => $langcode,
));
break;
case 'status_activated_body':
$text = t("[user:name],\n\nYour account at [site:name] has been activated.\n\nYou will be able to log in to [site:login-url] in the future using:\n\nusername: [user:name]\npassword: your password.\n\n-- [site:name] team", array(), array(
'langcode' => $langcode,
));
break;
}
}
if ($replace) {
// We do not sanitize the token replacement, since the output of this
// replacement is intended for an e-mail message, not a web browser.
return token_replace($text, $variables, array(
'language' => $language,
'callback' => 'user_registrationpassword_mail_tokens',
'sanitize' => FALSE,
'clear' => TRUE,
));
}
return $text;
}
/**
* Token callback to add unsafe tokens for user mails.
*
* @see user_mail_tokens()
* @see user_registrationpassword_mail()
* @see user_registrationpassword_confirmation_url()
*/
function user_registrationpassword_mail_tokens(&$replacements, $data, $options) {
user_mail_tokens($replacements, $data, $options);
if (isset($data['user'])) {
$replacements['[user:registrationpassword-url]'] = user_registrationpassword_confirmation_url($data['user']);
}
// For D7 we use the rpt module to enable emails to contain
// the user's password, so no need to add the password
// token here, people just need to install rpt.
}
/**
* Generates a unique URL for a user to login with their password already set.
*
* @param object $account
* An object containing the user account.
*
* @return string
* A unique URL that provides a one-time log in for the user, from which
* they can change their password.
*
* @see user_registrationpassword_mail_tokens()
* @see user_pass_rehash()
*/
function user_registrationpassword_confirmation_url($account) {
$timestamp = REQUEST_TIME;
return url("user/registrationpassword/{$account->uid}/{$timestamp}/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), array(
'absolute' => TRUE,
));
}
/**
* Implements hook_mailkeys().
*
* Returns an array with a simple string to identify the mailkey.
*
* @see user_registrationpassword_mail()
* @see _user_registrationpassword_mail_text()
* @see user_registrationpassword_mail_tokens()
* @see user_registrationpassword_mail_edit_text()
* @see user_registrationpassword_mail_edit_token_types()
*/
function user_registrationpassword_mailkeys() {
return array(
'register' => t('Welcome message when user self-registers and sets password during registration'),
);
}
/**
* Implements hook_mail_edit_text().
*
* Returns an array with subject and body, ready for processing.
*
* @see user_registrationpassword_mail()
* @see _user_registrationpassword_mail_text()
* @see user_registrationpassword_mail_tokens()
* @see user_registrationpassword_mailkeys()
* @see user_registrationpassword_mail_edit_text()
* @see user_registrationpassword_mail_edit_token_types()
*/
function user_registrationpassword_mail_edit_text($mailkey, $language) {
$return = array();
$return['subject'] = _user_registrationpassword_mail_text($mailkey . '_subject', $language, array(), FALSE);
$return['body'] = _user_registrationpassword_mail_text($mailkey . '_body', $language, array(), FALSE);
return $return;
}
/**
* Implements hook_mail_edit_token_types().
*
* Returns a simple array for constructing the mail / tokens.
*
* @see user_registrationpassword_mail()
* @see _user_registrationpassword_mail_text()
* @see user_registrationpassword_mail_tokens()
* @see user_registrationpassword_mailkeys()
* @see user_registrationpassword_mail_edit_text()
*/
function user_registrationpassword_mail_edit_token_types($mailkey) {
return array(
'user',
);
}
/**
* Implements hook_mail_alter().
*/
function user_registrationpassword_mail_alter(&$message) {
if ($message['module'] == 'user_registrationpassword') {
// Test if i18n_variable is available.
if (function_exists('i18n_variable_get')) {
$language = isset($message['language']) ? $message['language'] : language_default();
$variables = array(
'user' => $message['params']['account'],
);
$key = $message['key'];
$components = array(
'subject',
'body',
);
foreach ($components as $component) {
$text = i18n_variable_get('user_registrationpassword_' . $key . '_' . $component, $language->language, FALSE);
if ($text) {
$text = token_replace($text, $variables, array(
'language' => $language,
'callback' => 'user_registrationpassword_mail_tokens',
'sanitize' => FALSE,
));
switch ($component) {
case 'subject':
$message[$component] = $text;
break;
case 'body':
$message[$component] = array(
$text,
);
break;
}
}
}
}
}
}
/**
* Default callback for user_registrationpassword mail variables.
*/
function user_registrationpassword_variable_mail_default($variable, $options) {
// Remove 'user_registrationpassword_' prefix.
$name = substr($variable['name'], 26);
return _user_registrationpassword_mail_text($name, $options['language']);
}
/**
* Simple message and redirect.
*/
function user_registrationpassword_set_message($type = 'welcome', $redirect = '') {
// Select what message to display.
switch ($type) {
case 'linkerror':
drupal_set_message(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'));
// Redirect to user/pass.
if (!empty($redirect)) {
drupal_goto('user/password');
}
break;
case 'welcome':
drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
// Redirect to front.
if (!empty($redirect)) {
drupal_goto();
}
break;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function user_registrationpassword_form_user_pass_alter(&$form, &$form_state, $form_id) {
$form['#validate'][0] = '_user_registrationpassword_user_pass_validate';
$form['#submit'][0] = '_user_registrationpassword_user_pass_submit';
}
/**
* Form validation function.
*/
function _user_registrationpassword_user_pass_validate(&$form, &$form_state) {
// We try and load a blocked user that never ever logged in.
// This should only return 'brand new' user accounts.
$name = trim($form_state['values']['name']);
// Try to load by email.
$users = user_load_multiple(array(), array(
'mail' => $name,
'status' => '0',
'access' => '0',
'login' => '0',
));
$account = reset($users);
if (!$account) {
// No success, try to load by name.
$users = user_load_multiple(array(), array(
'name' => $name,
'status' => '0',
'access' => '0',
'login' => '0',
));
$account = reset($users);
}
// If the account has never ever been used, load
// the $account into the form for processing.
if (!empty($account->uid)) {
form_set_value(array(
'#parents' => array(
'account',
),
), $account, $form_state);
}
else {
module_load_include('inc', 'user', 'user.pages');
user_pass_validate($form, $form_state);
}
}
/**
* Implements submit function.
*/
function _user_registrationpassword_user_pass_submit(&$form, &$form_state) {
$account = $form_state['values']['account'];
// Then, if we have a user account
// and it has never ever been used.
if (!empty($account->uid) && !$account->login && !$account->access && !$account->status) {
// Try to load the account in disabled state.
$users = user_load_multiple(array(
$account->uid,
), array(
'login' => '0',
'access' => '0',
'status' => '0',
));
$user = reset($users);
// If the user never ever logged in, resend the activation mail.
if (!empty($user)) {
// Mail one time login URL and instructions using current language.
$params['account'] = $user;
// And on success, throw a message.
if (drupal_mail('user_registrationpassword', 'register', $user->mail, user_preferred_language($user), $params)) {
watchdog('user', 'Password reset instructions mailed to %name at %email.', array(
'%name' => $user->name,
'%email' => $user->mail,
));
user_registrationpassword_set_message();
}
}
}
else {
// If we found no errors execute core submission handler.
// Obviously cache is disabled on this pages, so this should work.
if (empty($_SESSION['messages']['error'])) {
user_pass_submit($form, $form_state);
}
}
}
/**
* Implements hook_mail_debugger_info().
*/
function user_registrationpassword_mail_debugger_info() {
return array(
'user_registrationpassword' => array(
'include' => array(
'inc',
'user_registrationpassword',
'includes/user_registrationpassword.mail_debugger',
),
'title' => 'User Registration Password',
'form' => '_user_registrationpassword_mail_debugger_form',
'validate' => '_user_registrationpassword_mail_debugger_form_validate',
'submit' => '_user_registrationpassword_mail_debugger_form_submit',
'weight' => -80,
),
);
}
Functions
Constants
Name | Description |
---|---|
USER_REGISTRATIONPASSWORD_NO_VERIFICATION | No verification email is sent. |
USER_REGISTRATIONPASSWORD_VERIFICATION_DEFAULT | Verification email is sent before password is set. |
USER_REGISTRATIONPASSWORD_VERIFICATION_PASS | Verification email is sent after password is set. |