Now With More Register
In the interest of creating a faster registration process based off of this post, here's some prototyped code that changes the user/register form into an email form and allows for immediate user creation. Actually this is just used right on top of Login Toboggan and assumes that the setting for register immediately is on. This is a quick (and hopefully simply and somewhat "secure") way to implement this functionality. First alter the registration form to use the users email address.
/**
* Implements hook_form_FORM_ID_alter().
*/
function faster_register_form_user_register_form_alter(&$form, &$form_state, $form_id) {
// Hide the name value
$form['account']['name']['#type'] = 'value';
// Generate password as a placeholder for the user name until validation
$form['account']['name']['#value'] = user_password();
$form['account']['mail']['#title'] = t('Email');
$form['account']['mail']['#description'] = t('Use your email address to register
with us. For now, we will create your user name from your email address. If your
email address is <em>obama@whitehouse.gov</em> your user name will be <em>obama</em>
We will send you a confirmation email and then you will be able to
change your user name as well as your password.');
$form['account']['mail']['#attributes']['placeholder'] = t('Your email address');
$form['account']['pass']['#access'] = FALSE;
$form['account']['pass']['#required'] = FALSE;
$form['account']['pass']['#value'] = user_password();
// Add this validate handler to the top of the validation chain.
array_unshift($form['#validate'], 'faster_register_user_register_validate');
}
The validation process checks to see if the email address already exists.
/**
* Validation handler for registration form.
*
* This function handles a flood event for the register by email. It serves to
* reduce the risk of someone trying to identify email addresses that are registered
* on the site as well as a very poor approach to stopping spam bots from registering
* lots of accounts.
*
* Check for users that have registered but who are still in a non-authenticated
* state. This means that they have not used the registration email they were sent.
* If the user still has the non-authenticated role then send them the email again.
*
* @param type $form
* @param type $form_state
*/
function faster_register_user_register_validate(&$form, &$form_state) {
// Register a flood event to prevent abuse of the registration
flood_register_event('faster_register');
if (! flood_is_allowed('faster_register', 3)) {
form_set_error('mail', t('Sorry, you have made three attempts to register your
account. Maybe you\'ve !forgotten?', array(
'!forgotten' => l(t('forgotten your password'), 'user/password')
)
)
);
return;
}
// Check to see if this user exists but has not submitted their registration
// email yet, otherwise let Drupal do its thing
if ($account = user_load_by_mail($form_state['values']['mail'])) {
if (! empty($account->roles[variable_get('faster_register_nonauth')])) {
// Alert them that the should use the link in the email
// @TODO text should be configurable
drupal_set_message(t('Woops! It looks like you already registered but you
did not confirm your registration. We\'re going to send you another
registration email and you\'ll be able to use that to register for the
site. Give us a moment and check your inbox!'));
'register_no_approval_required';
// Send the authentication email.
_user_mail_notify('register_no_approval_required', $account);
// Break the rest of the validation steps and return the user
// @TODO this is probably not the best way to do this.
drupal_goto('user/register');
}
}
// Create a username based on the email address
$form_state['values']['name'] = faster_register_create_username($form_state['values']['mail']);
}
And for good measure a way to hopefully create a unique user name
/**
* Creat a unique user name from an email address
*
* @param string $mail
* @return string
*/
function faster_register_create_username($email) {
// Strip off the domain information
$name = preg_replace('/@.*$/', '', $email);
// Remove unwanted characters from the name
$name = preg_replace('/[^a-zA-Z0-9.-]/', '', $name);
// Check to see if this name already exists.
$count = db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = LOWER(:name)", array(
':name' => $name))
->fetchField();
$i = 1;
while ($count) {
$count = db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = LOWER(:name)",
array(':name' => $name . '_' . $i))
->fetchField();
if (! $count) {
$name .= '_' . $i;
}
$i++;
}
return $name;
}
Categories: DrupalPlanet Drupal