Commit c66c98fd authored by andypost's avatar andypost

Issue #1162186 by andypost, bleen18, amateescu: Feature overlap with Login...

Issue #1162186 by andypost, bleen18, amateescu: Feature overlap with Login Toboggan and general cleanup.
parent 3c949a6e
<?php
/**
* @file
* Documentation for email_registration API.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Implement this hook to generate a username for email_registration module.
*
* Other modules may implement hook_email_registration_name($edit, $account)
* to generate a username (return a string to be used as the username, NULL
* to have email_registration generate it).
*
* @param $edit
* The array of form values submitted by the user.
* @param $account
* The user object on which the operation is being performed.
*
* @return
* A string defining a generated username.
*/
function hook_email_registration_name($edit, $account) {
return 'u' . $account->uid;
}
/**
* @} End of "addtogroup hooks".
*/
name = Email Registration
description = For registration process without a username.
files[] = email_registration.module
description = Allows users to register with an e-mail address as their username.
files[] = email_registration.test
core = 7.x
<?php
/**
* Implements hook_requirements().
*/
function email_registration_requirements() {
$t = get_t();
$requirements = array();
if (module_exists('logintoboggan') && variable_get('logintoboggan_login_with_email', 0)) {
$requirements['email_registration'] = array(
'title' => $t('Email Registration / LoginToboggan Conflict'),
'value' => $t('Conflict'),
'description' => $t('There is a conflict between %email_registration and %logintoboggan. You should disable the "Allow users to login using their e-mail address" option from %logintoboggan.', array('%email_registration' => 'Email registration', '%logintoboggan' => 'Login Toboggan')),
'severity' => REQUIREMENT_ERROR,
);
}
return $requirements;
}
......@@ -2,8 +2,7 @@
/**
* @file
* Registration process without a username
*
* Allows users to register with an e-mail address as their username.
*/
/**
......@@ -12,114 +11,87 @@
function email_registration_user_insert(&$edit, &$account, $category = NULL) {
// Other modules may implement hook_email_registration_name($edit, $account)
// to generate a username (return a string to be used as the username, NULL
// to have email_registration generate it)
// to have email_registration generate it).
$names = module_invoke_all('email_registration_name', $edit, $account);
// Remove any empty entries
// Remove any empty entries.
$names = array_filter($names);
if (empty($names)) {
// Default implementation of name generation
$namenew = preg_replace('/@.*$/', '', $edit['mail']);
// Remove unwanted characters
$namenew = preg_replace('/[^a-zA-Z0-9.-]/', '', $namenew);
// Default implementation of name generation.
$new_name = preg_replace('/@.*$/', '', $edit['mail']);
// Remove unwanted characters.
$new_name = preg_replace('/[^a-zA-Z0-9.-]/', '', $new_name);
// if username generated from email record already exists, append underscore and number eg:(chris_123)
if ((bool) db_query("SELECT 1 FROM {users} WHERE uid <> :uid AND LOWER(name) = LOWER(:namenew)", array(':uid' => $account->uid, ':namenew' => $namenew))->fetchField()) {
$nameidx = db_query_range("SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP :search ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC", 0, 1, array(':search' => '^' . $namenew . '_[0-9]+$'))->fetchField();
if ((bool) db_query("SELECT 1 FROM {users} WHERE uid <> :uid AND LOWER(name) = LOWER(:new_name)", array(':uid' => $account->uid, ':new_name' => $new_name))->fetchField()) {
$name_idx = db_query_range("SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP :search ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC", 0, 1, array(':search' => '^' . $new_name . '_[0-9]+$'))->fetchField();
$namenew .= '_' . ($nameidx + 1);
$new_name .= '_' . ($name_idx + 1);
}
}
else {
// One would expect a single implementation of the hook, but if there
// are multiples out there use the last one
$namenew = array_pop($names);
// are multiples out there use the last one.
$new_name = array_pop($names);
}
// replace with generated username
// Replace with generated username.
db_update('users')
->fields(array('name' => $namenew))
->fields(array('name' => $new_name))
->condition('uid', $account->uid)
->execute();
$edit['name'] = $namenew;
// if email verification is off and a new user is the one creating account, log the new user in with correct name
$edit['name'] = $new_name;
// If email verification is off and a new user is the one creating an account,
// log the new user in with his correct name.
global $user;
if (!variable_get('user_email_verification', 1) && $user->uid == 0) {
$user = $account;
$user->name = $namenew;
$user->name = $new_name;
}
$account->name = $namenew;
$account->name = $new_name;
return;
}
/**
* Implements hook_form_alter().
*
* Implements hook_form_FORM_ID_alter().
*/
function email_registration_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_register_form':
if (isset($form['account']) && is_array($form['account'])) {
$form['account']['name']['#type'] = 'hidden';
$form['account']['name']['#value'] = user_password();
$form['account']['mail']['#title'] = t('E-mail');
}
else {
$form['name']['#type'] = 'hidden';
$form['name']['#value'] = user_password();
$form['mail']['#title'] = t('E-mail');
}
$form['#submit'][] = 'custom_email_registration_name_submit';
break;
case 'user_pass':
$form['name']['#title'] = t('E-mail');
$form['name']['#description'] = t('Enter your e-mail address. You\'ll be sent a new password immediately.');
break;
case 'user_login':
$form['name']['#title'] = t('E-mail');
$form['name']['#description'] = t('Enter your e-mail address.');
$form['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
$form['name']['#element_validate'][] = 'email_registration_user_login_validate';
break;
case 'user_login_block':
$form['name']['#title'] = t('E-mail');
$form['name']['#element_validate'][] = 'email_registration_user_login_validate';
break;
}
function email_registration_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['name']['#type'] = 'hidden';
$form['account']['name']['#value'] = user_password();
$form['account']['mail']['#title'] = t('E-mail');
}
/**
* Form element submit handler for user registration form
* Fixes redirect for immediate logins
*
* Implements hook_form_FORM_ID_alter().
*/
function custom_email_registration_name_submit($form, &$form_state) {
if (!isset($form_state['user'])) {
return;
}
function email_registration_form_user_pass_alter(&$form, &$form_state, $form_id) {
$form['name']['#title'] = t('E-mail');
$form['name']['#description'] = t('A password reset message will be sent to your e-mail address.');
}
$admin = user_access('administer users');
$account = $form_state['user'];
/**
* Implements hook_form_FORM_ID_alter().
*/
function email_registration_form_user_login_alter(&$form, &$form_state, $form_id) {
$form['name']['#title'] = t('E-mail');
$form['name']['#description'] = t('Enter your e-mail address.');
$form['name']['#element_validate'][] = 'email_registration_user_login_validate';
$form['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
}
if (!variable_get('user_email_verification', TRUE) && $account->status && !$admin) {
// No e-mail verification is required, create account, and login user immediately.
if (user_authenticate($account->name, $form_state['values']['pass'])) {
// Authenticated, add a message and go to the users account
$form_state['redirect'] = 'user/' . $account->uid;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function email_registration_form_user_login_block_alter(&$form, &$form_state, $form_id) {
$form['name']['#title'] = t('E-mail');
$form['name']['#element_validate'][] = 'email_registration_user_login_validate';
}
/**
* Form element validation handler for user login form.
* Form element validation handler for the user login form.
* Allows users to authenticate by email, which is our preferred method.
*
*/
function email_registration_user_login_validate($form, &$form_state) {
if (isset($form_state['values']['name'])) {
......@@ -127,4 +99,4 @@ function email_registration_user_login_validate($form, &$form_state) {
$form_state['values']['name'] = $name;
}
}
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment