You are here

user_registrationpassword.module in User registration password 6

Same filename and directory in other branches
  1. 8 user_registrationpassword.module
  2. 7 user_registrationpassword.module

Enables password creation on registration form.

File

user_registrationpassword.module
View source
<?php

/**
 * @file
 * Enables password creation on registration form.
 */

/**
 * No verification email is sent.
 */
define('USER_REGISTRATIONPASS_NO_VERIFICATION', 0);

/**
 * Verification email is sent before password is set.
 */
define('USER_REGISTRATIONPASS_VERIFICATION_DEFAULT', 1);

/**
 * Verification email is sent after password is set.
 */
define('USER_REGISTRATIONPASS_VERIFICATION_PASS', 2);

/**
 * 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() for the user administration form.
 */
function user_registrationpassword_form_user_admin_settings_alter(&$form, &$form_state) {

  // Fix the weight of the user_register fieldset so it stays on top.
  $form['#pre_render'][] = 'user_registrationpassword_form_alter_weight';
  $form['registration']['user_registrationpassword_registration'] = array(
    '#type' => 'radios',
    '#title' => t('Require e-mail verification when a visitor creates an account'),
    '#description' => t('Choose whether new users will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password, or if they can set their password directly on the registration form. With this setting disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.'),
    '#weight' => 0,
    '#options' => array(
      USER_REGISTRATIONPASS_NO_VERIFICATION => t('Do not require a verification e-mail, and let users set their password on the registration form.'),
      USER_REGISTRATIONPASS_VERIFICATION_DEFAULT => t('Require a verification e-mail, but wait for the approval e-mail to let users set their password.'),
      USER_REGISTRATIONPASS_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_REGISTRATIONPASS_VERIFICATION_PASS),
  );

  // Remove the default checkbox.
  $form['registration']['user_email_verification']['#access'] = FALSE;
  $email_token_help = t('Available variables are: !username, !site, !password, !uri, !uri_brief, !mailto, !date, !login_uri, !edit_uri, !login_url, !registrationpassword-url.');
  $form['email']['email_user_registrationpassword'] = array(
    '#type' => 'fieldset',
    '#title' => t('Welcome (no approval required, password is set)'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Customize 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']['email_user_registrationpassword']['user_registrationpassword_register_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => _user_registrationpassword_mail_text('register_subject'),
    '#maxlength' => 180,
  );
  $form['email']['email_user_registrationpassword']['user_registrationpassword_register_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#default_value' => _user_registrationpassword_mail_text('register_body'),
    '#rows' => 15,
  );

  // Overwrite the default activation e-mail template with our own.
  $form['email']['activated']['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')),
    '#maxlength' => 180,
  );
  $form['email']['activated']['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')),
    '#rows' => 15,
  );
  $form['#submit'][] = 'user_registrationpassword_admin_settings_submit';
}

/**
 * Submit handler for the user admin form.
 */
function user_registrationpassword_admin_settings_submit(&$form, &$form_state) {
  $value = $form_state['values']['user_registrationpassword_registration'];
  if ($value == USER_REGISTRATIONPASS_VERIFICATION_PASS) {
    variable_set('user_email_verification', 0);

    // Prevent standard notification email to administrators and to user
    // When user_register is set to 1, else send the e-mail by default.
    if ($form_state['values']['user_register'] == 1 && $form_state['values']['user_registrationpassword_registration'] != 1) {
      variable_set('user_mail_register_pending_approval_notify', 0);
      variable_set('user_mail_register_no_approval_required_notify', 0);
      drupal_set_message('User Registration Password Enabled', 'warning');
    }
    else {
      variable_del('user_mail_register_pending_approval_notify');
      variable_set('user_mail_register_no_approval_required_notify', 0);
      drupal_set_message('User Registration Password Disabled', 'warning');
      drupal_set_message('Don\'t forget, you also need to change your Account activation e-mail template again to include a one-time login link so the user can login again, because you selected \'Visitors can create accounts but administrator approval is required.\' at <b>Public registrations: </b>.');
    }
  }
  else {
    variable_set('user_email_verification', (int) $value);

    // Let default value.
    // @see _user_mail_notify()
    variable_del('user_mail_register_pending_approval_notify');
    variable_del('user_mail_register_no_approval_required_notify');
    drupal_set_message('User Registration Password Disabled', 'warning');
    drupal_set_message('Don\'t forget, you also need to change your Account activation e-mail template again to include a one-time login link so the user can login again, because you selected \'Visitors can create accounts but administrator approval is required.\' at <b>Public registrations: </b>.');
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for the user registration form.
 */
function user_registrationpassword_form_user_register_alter(&$form, &$form_state) {
  global $user;
  if (variable_get('user_register', 0) == 1 && variable_get('user_registrationpassword_registration', USER_REGISTRATIONPASS_VERIFICATION_PASS) == USER_REGISTRATIONPASS_VERIFICATION_PASS && $user->uid < 1) {
    $form['#submit'][] = 'user_registrationpassword_form_user_register_submit';
  }
}

/**
 * Submit handler for the user registration form.
 */
function user_registrationpassword_form_user_register_submit(&$form, &$form_state) {

  // Load the user by e-mail address.
  $account = user_load(array(
    'mail' => $form_state['values']['mail'],
  ));

  // Save the account as blocked, so users first have to verify
  // via e-mail and do not get logged in straight away.
  // This is a required step in D6, D7 does not require this.
  user_save($account, array(
    'status' => 0,
  ));

  // Remove the message sent by default.
  $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.');
  $position = array_search($message, $_SESSION['messages']['status']);
  if ($position !== FALSE) {
    unset($_SESSION['messages']['status'][$position]);
  }
  $_SESSION['messages']['status'] = array_values($_SESSION['messages']['status']);

  // Make sure the password is taken from the form and added to our object.
  // This is required to send out passwords via tokens on registration.
  $account->password = $form_state['user']->password;
  $params['account'] = $account;

  // Notify the user and send the e-mail.
  $mail = drupal_mail('user_registrationpassword', 'register', $account->mail, user_preferred_language($account), $params);
  drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.'));
}

/**
 * Implements hook_mail().
 */
function user_registrationpassword_mail($key, &$message, $params) {
  $language = $message['language'];
  $variables = array_merge(user_mail_tokens($params['account'], $language), user_registrationpassword_mail_tokens($params['account'], $language));
  $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.
 *
 * Used by user_registrationpassword_mail() and the settings forms to retrieve strings.
 *
 * @param string $key
 * @param string $language
 * @param array $variables
 *
 * @return string
 */
function _user_registrationpassword_mail_text($key, $language = NULL, $variables = array()) {
  $langcode = isset($language) ? $language->language : NULL;
  if ($admin_setting = variable_get('user_registrationpassword_' . $key, FALSE)) {

    // An admin setting overrides the default string.
    return strtr($admin_setting, $variables);
  }
  else {

    // No override, return default string.
    switch ($key) {
      case 'status_activated_subject':
        return t('Account details for !username at !site', $variables, $langcode);
      case 'status_activated_body':
        return t("!username,\n\nYour account at !site has been activated.\n\nYou will be able to log in to !login_uri in the future using:\n\nusername: !username\npassword: your password.\n\n--  !site team\n", $variables, $langcode);
      case 'register_subject':
        return t('Account details for !username at !site', $variables, $langcode);
      case 'register_body':
        return t("!username,\n\nThank you for registering at !site. You may now log in and verify your account by clicking this link or copying and pasting it to your browser:\n\n!registrationpassword-url\n\nThis link can only be used once. You will be able to log in at !login_uri in the future using:\n\nusername: !username\npassword: Your password\n\n--  !site team", $variables, $langcode);
    }
  }
}

/**
 * Token callback to add unsafe tokens for user mails.
 *
 * @see user_mail_tokens()
 * @see user_registrationpassword_mail()
 */
function user_registrationpassword_mail_tokens($account, $language) {
  $tokens = array(
    '!registrationpassword-url' => user_registrationpassword_confirmation_url($account),
  );
  if (!empty($account->password)) {
    $tokens['!password'] = $account->password;
  }
  return $tokens;
}

/**
 * Generates a unique URL for a user to login with their password already set.
 *
 * @param object $account
 *   An object containing the user account.
 *
 * @return
 *   A unique URL that provides a one-time log in for the user, from which
 *   they can change their password.
 */
function user_registrationpassword_confirmation_url($account) {
  $timestamp = time();
  return url("user/registrationpassword/{$account->uid}/{$timestamp}/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), array(
    'absolute' => TRUE,
  ));
}

/**
 * Implements hook_mailkeys().
 *
 * @return array
 */
function user_registrationpassword_mailkeys() {
  return array(
    'register' => t('Welcome message when user self-registers and sets password during registration'),
  );
}

/**
 * Implements hook_mail_edit_text().
 *
 * @param string $mailkey
 * @param object $language
 *
 * @return array
 */
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().
 *
 * @param string $mailkey
 *
 * @return array
 */
function user_registrationpassword_mail_edit_token_types($mailkey) {
  return array(
    'user',
  );
}

/**
 * Implements hook_form_alter_weights().
 *
 * @See user_registrationpassword_form_user_admin_settings_alter()
 */
function user_registrationpassword_form_alter_weight($elements) {
  $elements['registration']['user_register']['#weight'] = -1;
  return $elements;
}

/**
 * Implements hook_user().
 */
function user_registrationpassword_user($op, &$edit, &$account, $category = NULL) {

  // Test if we are on the user register page and not on an admin page.
  // The only downside of this, is that we have to run this check on every user
  // account form. Sad thing, fixed in D7. If anyone knows a better way, patch!
  // If we'r loading the user object, the user is never logged in, but
  // registered (and the module is enabled obviously), set
  // $account->status to 0, else users will log in and the default welcome
  // e-mail is send next to ours.
  // Also tested this while activating/blocking/deleting a user via the admin
  // interface, all still seems to work ok, but more_contrib_modules_tests++.
  if ($op == 'load' && $account->login == 0 && $account->access == 0 && $account->uid > 1 && variable_get('user_register', 0) == 1 && variable_get('user_registrationpassword_registration', USER_REGISTRATIONPASS_VERIFICATION_PASS) == USER_REGISTRATIONPASS_VERIFICATION_PASS) {
    $account->status = 0;
  }
}

Functions

Namesort descending Description
user_registrationpassword_admin_settings_submit Submit handler for the user admin form.
user_registrationpassword_confirmation_url Generates a unique URL for a user to login with their password already set.
user_registrationpassword_form_alter_weight Implements hook_form_alter_weights().
user_registrationpassword_form_user_admin_settings_alter Implements hook_form_FORM_ID_alter() for the user administration form.
user_registrationpassword_form_user_register_alter Implements hook_form_FORM_ID_alter() for the user registration form.
user_registrationpassword_form_user_register_submit Submit handler for the user registration form.
user_registrationpassword_mail Implements hook_mail().
user_registrationpassword_mailkeys Implements hook_mailkeys().
user_registrationpassword_mail_edit_text Implements hook_mail_edit_text().
user_registrationpassword_mail_edit_token_types Implements hook_mail_edit_token_types().
user_registrationpassword_mail_tokens Token callback to add unsafe tokens for user mails.
user_registrationpassword_menu Implements hook_menu().
user_registrationpassword_user Implements hook_user().
_user_registrationpassword_mail_text Returns a mail string for a variable name.

Constants

Namesort descending Description
USER_REGISTRATIONPASS_NO_VERIFICATION No verification email is sent.
USER_REGISTRATIONPASS_VERIFICATION_DEFAULT Verification email is sent before password is set.
USER_REGISTRATIONPASS_VERIFICATION_PASS Verification email is sent after password is set.