Commit 21b4b42a authored by Chris Herberte's avatar Chris Herberte

commited to the wrong branch - duh

parent 7cba9e7e
; $Id$
name = Email Registration
description = "For registration process without a username."
core = 6.x
\ No newline at end of file
name = Email Registration
description = For registration process without a username.
files[] = email_registration.module
files[] = email_registration.test
core = 7.x
......@@ -3,68 +3,65 @@
/**
* @file
* For registration process without a username
* Registration process without a username
*
*/
/**
* Implementation of hook_user().
*
* Implements hook_user_insert().
*/
function email_registration_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
// 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)
$names = module_invoke_all('email_registration_name', $edit, $account);
// Remove any empty entries
$names = array_filter($names);
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)
$names = module_invoke_all('email_registration_name', $edit, $account);
// 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);
if (empty($names)) {
// Default implementation of name generation
$namenew = preg_replace('/@.*$/', '', $edit['mail']);
// Remove unwanted characters
$namenew = preg_replace('/[^a-zA-Z0-9.-]/', '', $namenew);
// 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 username generated from email record already exists, append underscore and number eg:(chris_123)
if (db_result(db_query("SELECT count(*) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
// find the next number available to append to the name
$sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
$nameidx = db_result(db_query($sql, '^'. $namenew .'_[0-9]+$'));
$namenew .= '_'. ($nameidx + 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);
}
$namenew .= '_' . ($nameidx + 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);
}
// replace with generated username
if (db_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $namenew, $account->uid)) {
$edit['name'] = $namenew; // update in the user array for access by other modules
}
// replace with generated username
db_update('users')
->fields(array('name' => $namenew))
->condition('uid', $account->uid)
->execute();
// if email verification is off and a new user is the one creating account, log the new user in with correct name
global $user;
if (!variable_get('user_email_verification', 1) && $user->uid == 0) {
$user = $account;
$user->name = $namenew;
}
$account->name = $namenew;
break;
$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
global $user;
if (!variable_get('user_email_verification', 1) && $user->uid == 0) {
$user = $account;
$user->name = $namenew;
}
$account->name = $namenew;
return;
}
/**
* Implementation of hook_form_alter().
* Implements hook_form_alter().
*
*/
function email_registration_form_alter(&$form, $form_state, $form_id) {
function email_registration_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_register':
case 'user_register_form':
if (isset($form['account']) && is_array($form['account'])) {
$form['account']['name']['#type'] = 'hidden';
$form['account']['name']['#value'] = user_password();
......@@ -99,8 +96,8 @@ function email_registration_form_alter(&$form, $form_state, $form_id) {
/**
* Custom submit handler to fix redirect for immediate logins
* #648450
* Form element submit handler for user registration form
* Fixes redirect for immediate logins
*
*/
function custom_email_registration_name_submit($form, &$form_state) {
......@@ -112,29 +109,22 @@ function custom_email_registration_name_submit($form, &$form_state) {
$account = $form_state['user'];
if (!variable_get('user_email_verification', TRUE) && $account->status && !$admin) {
// No e-mail verification is required, create new user account, and login
// user immediately.
$auth = array(
'pass' => $form_state['values']['pass'],
'name' => $account->name,
);
if (user_authenticate($auth)) {
// 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
// Since the standard workflow doesn't work, no other messages should appear.
drupal_set_message(t('Registration successful. You are now logged in.'));
$form_state['redirect'] = 'user/'. $account->uid;
$form_state['redirect'] = 'user/' . $account->uid;
}
}
}
/**
* Custom validation function for user login form.
* Allows users to authenticate by email only, which is our preferred method.
* Form element validation handler for 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'])) {
if ($name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_state['values']['name']))) {
if ($name = db_query('SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)', array(':name' => $form_state['values']['name']))->fetchField()) {
$form_state['values']['name'] = $name;
}
}
......
<?php
// $Id$
/**
* @file
* email registration simpletest
*/
class EmailRegistrationTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
......@@ -31,7 +38,7 @@ class EmailRegistrationTestCase extends DrupalWebTestCase {
$name = $this->randomName();
$pass = $this->randomName(10);
$register = array(
'mail' => $name .'@example.com',
'mail' => $name . '@example.com',
'pass[pass1]' => $pass,
'pass[pass2]' => $pass,
);
......@@ -39,14 +46,14 @@ class EmailRegistrationTestCase extends DrupalWebTestCase {
$this->drupalPost('/user/register', $register, t('Create new account'));
$login = array(
'name' => $name .'@example.com',
'name' => $name . '@example.com',
'pass' => $pass,
);
$this->drupalPost('user/login', $login, t('Log in'));
$this->assertRaw('<h2>'. $name .'</h2>', t('User properly created, logged in.'));
$this->assertRaw('<h2>' . $name . '</h2>', t('User properly created, logged in.'));
// $this->assertRaw('id="edit-name" size="60" value="'. $register['given_name'] .'"', t('Given name saved.'));
// $this->assertRaw('id="edit-name" size="60" value="' . $register['given_name'] . '"', t('Given name saved.'));
}
}
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