You are here

function logintoboggan_user_register_submit in LoginToboggan 6

Same name and namespace in other branches
  1. 5 logintoboggan.module \logintoboggan_user_register_submit()
  2. 7 logintoboggan.module \logintoboggan_user_register_submit()

Custom submit function for user registration form

1 string reference to 'logintoboggan_user_register_submit'
logintoboggan_form_alter in ./logintoboggan.module
Implementation of hook_form_alter()

File

./logintoboggan.module, line 316
Logintoboggan Module

Code

function logintoboggan_user_register_submit($form, &$form_state) {
  $reg_pass_set = !variable_get('user_email_verification', TRUE);

  // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
  // handle things a bit differently.
  $pre_auth = logintoboggan_validating_id() != DRUPAL_AUTHENTICATED_RID;

  // If we are allowing user selected passwords then skip the auto-generate function
  // The new user's status should default to the site settings, unless reg_passwd_set == 1
  // (immediate login, we are going to assign a pre-auth role), and we want to allow
  // admin approval accounts access to the site.
  if ($reg_pass_set) {
    $pass = $form_state['values']['pass'];
    $status = 1;
  }
  else {
    $pass = user_password();
    $status = variable_get('user_register', 1) == 1;
  }

  // Must unset mail confirmation to prevent it from being saved in the user table's 'data' field.
  if (isset($form_state['values']['conf_mail'])) {
    unset($form_state['values']['conf_mail']);
  }
  if (array_intersect(array_keys($form_state['values']), array(
    'uid',
    'roles',
    'init',
    'session',
    'status',
  ))) {
    watchdog('security', 'Detected malicious attempt to alter protected user fields.', array(), WATCHDOG_WARNING);
    $form_state['redirect'] = 'user/register';
    return;
  }

  // The unset below is needed to prevent these form values from being saved as user data.
  unset($form_state['values']['form_token'], $form_state['values']['submit'], $form_state['values']['op'], $form_state['values']['notify'], $form_state['values']['form_id'], $form_state['values']['form_build_id'], $form_state['values']['affiliates'], $form_state['values']['destination']);

  // Set the roles for the new user -- add the pre-auth role if they can pick their own password,
  // and the pre-auth role isn't anon or auth user.
  $validating_id = logintoboggan_validating_id();
  $roles = isset($form_state['values']['roles']) ? array_filter($form_state['values']['roles']) : array();
  if ($reg_pass_set && $validating_id > DRUPAL_AUTHENTICATED_RID) {
    $roles[$validating_id] = 1;
  }
  $edit = array_merge($form_state['values'], array(
    'pass' => $pass,
    'init' => $form_state['values']['mail'],
    'roles' => $roles,
    'status' => $status,
  ));
  $account = user_save('', $edit);

  // Add plain text password into user account to generate mail tokens.
  $account->password = $pass;
  $form_state['user'] = $account;
  watchdog('user', 'New user: %name (%email).', array(
    '%name' => $account->name,
    '%email' => $account->mail,
  ), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));
  $login_url = variable_get('user_register', 1) == 1 ? logintoboggan_eml_validate_url($account) : NULL;

  // Compose the appropriate user message--admin approvals don't require a validation email.
  if ($reg_pass_set && variable_get('user_register', 1) == 1) {
    if ($pre_auth) {
      $message = t('A validation e-mail has been sent to your e-mail address. In order to gain full access to the site, you will need to follow the instructions in that message.');
    }
    else {
      $message = '';
    }
  }
  else {
    $message = t('Your password and further instructions have been sent to your e-mail address.');
  }
  if (variable_get('user_register', 1) == 1) {

    // Create new user account, no administrator approval required.
    $mailkey = 'register_no_approval_required';
  }
  elseif (variable_get('user_register', 1) == 2) {

    // Create new user account, administrator approval required.
    $mailkey = 'register_pending_approval';
    $message = t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />Once it has been approved, you will receive an e-mail containing further instructions.');
  }

  // Mail the user.
  _logintoboggan_mail_notify($mailkey, $account, $login_url);
  drupal_set_message($message);

  // where do we need to redirect after registration?
  $redirect = _logintoboggan_process_redirect(variable_get('logintoboggan_redirect_on_register', ''), $account);

  // Log the user in if they created the account and immediate login is enabled.
  if ($reg_pass_set && variable_get('logintoboggan_immediate_login_on_register', TRUE)) {
    $form_state['redirect'] = logintoboggan_process_login($account, $edit, $redirect);
  }
  else {

    // Redirect to the appropriate page.
    $form_state['redirect'] = $redirect;
  }
}