Commit 5b85d131 authored by andypost's avatar andypost

Initial conversion. Works and passes tests.

parent 3702dea9
...@@ -2,9 +2,11 @@ ...@@ -2,9 +2,11 @@
/** /**
* @file * @file
* Documentation for email_registration API. * Documentation for email_registration module API.
*/ */
use Drupal\user\UserInterface;
/** /**
* @addtogroup hooks * @addtogroup hooks
* @{ * @{
...@@ -13,23 +15,21 @@ ...@@ -13,23 +15,21 @@
/** /**
* Implement this hook to generate a username for email_registration module. * Implement this hook to generate a username for email_registration module.
* *
* Other modules may implement hook_email_registration_name($edit, $account) * Other modules may implement hook_email_registration_name($account)
* to generate a username (return a string to be used as the username, NULL * 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).
* *
* @param $edit * @param \Drupal\user\UserInterface $account
* The array of form values submitted by the user.
* @param $account
* The user object on which the operation is being performed. * The user object on which the operation is being performed.
* *
* @return * @return
* A string defining a generated username. * A string defining a generated username.
*/ */
function hook_email_registration_name($edit, $account) { function hook_email_registration_name(UserInterface $account) {
// Your hook implementation should ensure that the resulting string // Your hook implementation should ensure that the resulting string
// works as a username. You can use email_registration_cleanup_username($name) // works as a username. You can use email_registration_cleanup_username($name)
// to clean up the name. // to clean up the name.
return email_registration_cleanup_username('u' . $account->uid); return email_registration_cleanup_username('u' . $account->id());
} }
/** /**
......
name = Email Registration
description = Allows users to register with an e-mail address as their username.
files[] = email_registration.test
core = 7.x
name: Email Registration
type: module
description: 'Allows users to register with an e-mail address as their username.'
core: 8.x
...@@ -9,14 +9,14 @@ ...@@ -9,14 +9,14 @@
* Implements hook_requirements(). * Implements hook_requirements().
*/ */
function email_registration_requirements() { function email_registration_requirements() {
$t = get_t();
$requirements = array(); $requirements = array();
if (module_exists('logintoboggan') && variable_get('logintoboggan_login_with_email', 0)) { // @todo Get rid of variable_get() and check is this conflict still valid.
if (Drupal::moduleHandler()->moduleExists('logintoboggan') && variable_get('logintoboggan_login_with_email', 0)) {
$requirements['email_registration'] = array( $requirements['email_registration'] = array(
'title' => $t('Email Registration / LoginToboggan Conflict'), 'title' => t('Email Registration / LoginToboggan Conflict'),
'value' => $t('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')), '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, 'severity' => REQUIREMENT_ERROR,
); );
} }
......
...@@ -5,25 +5,28 @@ ...@@ -5,25 +5,28 @@
* Allows users to register with an e-mail address as their username. * Allows users to register with an e-mail address as their username.
*/ */
use Drupal\user\UserInterface;
/** /**
* Implements hook_user_insert(). * Implements hook_user_insert().
*/ */
function email_registration_user_insert(&$edit, &$account, $category = NULL) { function email_registration_user_insert(UserInterface $account) {
// Don't create a new username if one is already set. // Don't create a new username if one is already set.
if (!empty($account->name) && strpos($account->name, 'email_registration_') !== 0) { $name = $account->getName();
if (!empty($name) && strpos($name, 'email_registration_') !== 0) {
return; return;
} }
// Other modules may implement hook_email_registration_name($edit, $account) // 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 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); $names = Drupal::moduleHandler()->invokeAll('email_registration_name', array($account));
// Remove any empty entries. // Remove any empty entries.
$names = array_filter($names); $names = array_filter($names);
if (empty($names)) { if (empty($names)) {
// Strip off everything after the @ sign. // Strip off everything after the @ sign.
$new_name = preg_replace('/@.*$/', '', $edit['mail']); $new_name = preg_replace('/@.*$/', '', $account->getEmail());
// Clean up the username. // Clean up the username.
$new_name = email_registration_cleanup_username($new_name); $new_name = email_registration_cleanup_username($new_name);
} }
...@@ -34,17 +37,16 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) { ...@@ -34,17 +37,16 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) {
} }
// Ensure whatever name we have is unique. // Ensure whatever name we have is unique.
$new_name = email_registration_unique_username($new_name, $account->uid); $new_name = email_registration_unique_username($new_name, $account->id());
// @todo Make sure we still need this update.
// Replace with generated username. // Replace with generated username.
db_update('users') db_update('users')
->fields(array('name' => $new_name)) ->fields(array('name' => $new_name))
->condition('uid', $account->uid) ->condition('uid', $account->id())
->execute(); ->execute();
$edit['name'] = $new_name; $account->setUsername($new_name);
$account->name = $new_name;
return;
} }
/** /**
...@@ -55,18 +57,16 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) { ...@@ -55,18 +57,16 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) {
* or a similar function. If your custom code is creating users, you should * or a similar function. If your custom code is creating users, you should
* ensure that the email/name is already validated using something like that. * ensure that the email/name is already validated using something like that.
* *
* @param $name * @param string $name
* A name from which to base the final user name. May contain illegal characters; these will be stripped. * A name from which to base the final user name. May contain illegal characters; these will be stripped.
* * @param int $uid
* @param $uid
* (optional) Uid to ignore when searching for unique user (e.g. if we update the username after the * (optional) Uid to ignore when searching for unique user (e.g. if we update the username after the
* {users} row is inserted) * {users} row is inserted)
* *
* @return * @return string
* A unique user name based on $name. * A unique user name based on $name.
* *
* @see user_validate_name(). * @see user_validate_name().
*
*/ */
function email_registration_unique_username($name, $uid = 0) { function email_registration_unique_username($name, $uid = 0) {
// Iterate until we find a unique name. // Iterate until we find a unique name.
...@@ -81,13 +81,17 @@ function email_registration_unique_username($name, $uid = 0) { ...@@ -81,13 +81,17 @@ function email_registration_unique_username($name, $uid = 0) {
} }
/** /**
* Function to clean up username * Function to clean up username.
*
* e.g. * e.g.
* Replace two or more spaces with a single underscore * Replace two or more spaces with a single underscore
* Strip illegal characters. * Strip illegal characters.
* *
* @param username to be cleaned up * @param string $name
* @return cleaned up username * The username to be cleaned up.
*
* @return string
* Cleaned up username.
*/ */
function email_registration_cleanup_username($name) { function email_registration_cleanup_username($name) {
// Strip illegal characters. // Strip illegal characters.
...@@ -127,19 +131,14 @@ function email_registration_form_user_pass_alter(&$form, &$form_state) { ...@@ -127,19 +131,14 @@ function email_registration_form_user_pass_alter(&$form, &$form_state) {
/** /**
* Implements hook_form_FORM_ID_alter(). * Implements hook_form_FORM_ID_alter().
*/ */
function email_registration_form_user_login_alter(&$form, &$form_state) { function email_registration_form_user_login_form_alter(&$form, &$form_state) {
$form['name']['#title'] = t('E-mail'); $form['name']['#title'] = t('E-mail');
$form['name']['#description'] = t('Enter your e-mail address.'); $form['name']['#description'] = t('Enter your e-mail address.');
$form['name']['#element_validate'][] = 'email_registration_user_login_validate'; $form['name']['#element_validate'][] = 'email_registration_user_login_validate';
$form['pass']['#description'] = t('Enter the password that accompanies your e-mail.'); $form['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
} // Allow client side validation of input format.
$form['name']['#type'] = 'email';
/** $form['name']['#maxlength'] = EMAIL_MAX_LENGTH;
* Implements hook_form_FORM_ID_alter().
*/
function email_registration_form_user_login_block_alter(&$form, &$form_state) {
$form['name']['#title'] = t('E-mail');
$form['name']['#element_validate'][] = 'email_registration_user_login_validate';
} }
/** /**
...@@ -148,7 +147,7 @@ function email_registration_form_user_login_block_alter(&$form, &$form_state) { ...@@ -148,7 +147,7 @@ function email_registration_form_user_login_block_alter(&$form, &$form_state) {
*/ */
function email_registration_user_login_validate($form, &$form_state) { function email_registration_user_login_validate($form, &$form_state) {
if (isset($form_state['values']['name'])) { if (isset($form_state['values']['name'])) {
// Keep the email value in form state for furher validation. // Keep the email value in form state for further validation.
$form_state['values']['email'] = $form_state['values']['name']; $form_state['values']['email'] = $form_state['values']['name'];
if ($name = db_query('SELECT name FROM {users} WHERE LOWER(mail) = LOWER(:name)', array(':name' => $form_state['values']['name']))->fetchField()) { 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; $form_state['values']['name'] = $name;
...@@ -159,6 +158,6 @@ function email_registration_user_login_validate($form, &$form_state) { ...@@ -159,6 +158,6 @@ function email_registration_user_login_validate($form, &$form_state) {
/** /**
* Implements hook_form_FORM_ID_alter(). * Implements hook_form_FORM_ID_alter().
*/ */
function email_registration_form_user_profile_form_alter($form, &$form_state) { function email_registration_form_user_form_alter(&$form, &$form_state) {
$form['account']['name']['#title'] = t('Display name'); $form['account']['name']['#title'] = t('Display name');
} }
...@@ -2,36 +2,42 @@ ...@@ -2,36 +2,42 @@
/** /**
* @file * @file
* email registration simpletest * Contains \Drupal\email_registration\Tests\EmailRegistrationTestCase.
*/ */
class EmailRegistrationTestCase extends DrupalWebTestCase { namespace Drupal\email_registration\Tests;
use Drupal\simpletest\WebTestBase;
class EmailRegistrationTestCase extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('email_registration');
/** /**
* Implementation of getInfo(). * Implementation of getInfo().
*/ */
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => t('Email registration.'), 'name' => 'Email registration.',
'description' => t('Test the email registration module.'), 'description' => 'Test the email registration module.',
'group' => t('Email registration'), 'group' => 'Email registration',
); );
} }
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp('email_registration');
// Configure to allow set password.
variable_set('user_email_verification', FALSE);
}
/** /**
* Test various behaviors for anonymous users. * Test various behaviors for anonymous users.
*/ */
function testRegistration() { function testRegistration() {
variable_set('user_register', USER_REGISTER_VISITORS); $user_config = $this->container->get('config.factory')->get('user.settings');
$user_config
->set('verify_mail', FALSE)
->set('register', USER_REGISTER_VISITORS)
->save();
// Try to register a user. // Try to register a user.
$name = $this->randomName(); $name = $this->randomName();
$pass = $this->randomName(10); $pass = $this->randomName(10);
...@@ -54,7 +60,9 @@ class EmailRegistrationTestCase extends DrupalWebTestCase { ...@@ -54,7 +60,9 @@ class EmailRegistrationTestCase extends DrupalWebTestCase {
// Now try the immediate login. // Now try the immediate login.
$this->drupalLogout(); $this->drupalLogout();
variable_set('user_email_verification', 0); $user_config
->set('verify_mail', FALSE)
->save();
$name = $this->randomName(); $name = $this->randomName();
$pass = $this->randomName(10); $pass = $this->randomName(10);
$register = array( $register = array(
...@@ -64,6 +72,6 @@ class EmailRegistrationTestCase extends DrupalWebTestCase { ...@@ -64,6 +72,6 @@ class EmailRegistrationTestCase extends DrupalWebTestCase {
); );
$this->drupalPost('/user/register', $register, t('Create new account')); $this->drupalPost('/user/register', $register, t('Create new account'));
$this->assertRaw('Registration successful. You are now logged in.', t('User properly created, immediately logged in.')); $this->assertRaw('Registration successful. You are now logged in.', t('User properly created, immediately logged in.'));
} }
} }
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