updated view_send, vews_php, nodeformcols, email_registratio

This commit is contained in:
Bachir Soussi Chiadmi
2016-11-05 16:53:56 +01:00
parent 0bb339fc99
commit 3b6c7b914d
20 changed files with 318 additions and 130 deletions

View File

@@ -3,9 +3,9 @@ description = Allows users to register with an e-mail address as their username.
files[] = email_registration.test
core = 7.x
; Information added by Drupal.org packaging script on 2014-04-23
version = "7.x-1.2"
; Information added by Drupal.org packaging script on 2015-10-05
version = "7.x-1.3"
core = "7.x"
project = "email_registration"
datestamp = "1398265775"
datestamp = "1444050240"

View File

@@ -23,3 +23,10 @@ function email_registration_requirements() {
return $requirements;
}
/**
* Implements hook_uninstall().
*/
function email_registration_uninstall() {
variable_del('email_registration_login_with_username');
}

View File

@@ -53,7 +53,9 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) {
* /admin/people/create forms which have already called user_validate_name,
* valid_email_address 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.
* something like that. Alternatively you can prepend the account user name
* to contain 'email_registration_' to allow for the email registration hooks
* to generate a unique username based on mail.
*
* @param string $name
* A name from which to base the final user name. May contain illegal
@@ -67,12 +69,18 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) {
*
* @see user_validate_name()
*/
function email_registration_unique_username($name, $uid) {
function email_registration_unique_username($name, $uid = NULL) {
// Iterate until we find a unique name.
$i = 0;
do {
$new_name = empty($i) ? $name : $name . '_' . $i;
$found = db_query_range("SELECT uid from {users} WHERE uid <> :uid AND name = :name", 0, 1, array(':uid' => $uid, ':name' => $new_name))->fetchAssoc();
if ($uid) {
$found = db_query_range("SELECT uid from {users} WHERE uid <> :uid AND name = :name", 0, 1, array(':uid' => $uid, ':name' => $new_name))->fetchAssoc();
}
else {
$found = db_query_range("SELECT uid from {users} WHERE name = :name", 0, 1, array(':name' => $new_name))->fetchAssoc();
}
$i++;
} while (!empty($found));
@@ -100,7 +108,7 @@ function email_registration_cleanup_username($name, $uid = NULL) {
$name = trim($name);
// Convert any other series of spaces to a single underscore.
$name = preg_replace('/ +/', '_', $name);
$name = preg_replace('/\s+/', '_', $name);
// If there's nothing left use a default.
$name = ('' === $name) ? t('user') : $name;
@@ -135,8 +143,12 @@ function email_registration_form_user_pass_alter(&$form, &$form_state) {
* Implements hook_form_FORM_ID_alter().
*/
function email_registration_form_user_login_alter(&$form, &$form_state) {
$form['name']['#title'] = t('E-mail');
$form['name']['#description'] = t('Enter your e-mail address.');
$form['name']['#title'] = variable_get('email_registration_login_with_username', TRUE)
? t('E-mail or username')
: t('E-mail');
$form['name']['#description'] = variable_get('email_registration_login_with_username', TRUE)
? t('Enter your e-mail address or username.')
: 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.');
}
@@ -145,22 +157,68 @@ function email_registration_form_user_login_alter(&$form, &$form_state) {
* 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']['#title'] = variable_get('email_registration_login_with_username', TRUE)
? t('E-mail or username')
: t('E-mail');
$form['name']['#element_validate'][] = 'email_registration_user_login_validate';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function email_registration_form_user_admin_settings_alter(&$form, &$form_state) {
$form['registration_cancellation']['email_registration_login_with_username'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users login with e-mail or username.'),
'#description' => t('Allow users to login with their username in addition to their e-mail.'),
'#default_value' => variable_get('email_registration_login_with_username', TRUE),
);
$form['#submit'][] = 'email_registration_form_user_admin_settings_submit';
}
/**
* Submit function for user_admin_settings to save our variable.
*
* @see email_registration_form_user_admin_settings_alter().
*/
function email_registration_form_user_admin_settings_submit($form, &$form_state) {
variable_set('email_registration_login_with_username', $form_state['values']['email_registration_login_with_username']);
}
/**
* 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'])) {
// Keep the email value in form state for further validation.
$name = NULL;
if (isset($form_state['values']['name']) && valid_email_address($form_state['values']['name'])) {
// Try to load the username matching the email, if any exists.
$name = db_select('users')
->fields('users', array('name'))
->condition('mail', db_like($form_state['values']['name']), 'LIKE')
->execute()
->fetchField();
}
// If the value is set, and a valid email, and a match was found, use it.
if (!empty($name)) {
// Keep the email value in form state for further use/validation.
$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()) {
$form_state['values']['name'] = $name;
}
// If the name matches an e-mail, assume that it's the desired name and
// set the username in the form values.
$form_state['values']['name'] = $name;
}
elseif (!variable_get('email_registration_login_with_username', TRUE)) {
// If no username was found for the e-mail, and registration with username
// is not allowed, unset the name from the form. This prevents
// user_login_authenticate_validate() from trying to load a user from the
// value as a username, which in turn causes user_login_final_validate()
// to set a form error telling the user that no account has been found.
// We have to set this to NULL rather than FALSE, because
// user_login_name_validate() uses isset() rather than empty().
$form_state['values']['name'] = NULL;
}
}

View File

@@ -6,23 +6,26 @@
*/
class EmailRegistrationTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
* Implement getInfo().
*/
public static function getInfo() {
return array(
'name' => t('Email registration.'),
'description' => t('Test the email registration module.'),
'group' => t('Email registration'),
'name' => 'Email registration.',
'description' => 'Test the email registration module.',
'group' => 'Email registration',
);
}
/**
* Implementation of setUp().
* {@inheritdoc}
*/
public function setUp() {
parent::setUp('email_registration');
// Allow user registration.
variable_set('user_register', USER_REGISTER_VISITORS);
// Configure to allow set password.
variable_set('user_email_verification', FALSE);
}
@@ -31,7 +34,6 @@ class EmailRegistrationTestCase extends DrupalWebTestCase {
* Test various behaviors for anonymous users.
*/
public function testRegistration() {
variable_set('user_register', USER_REGISTER_VISITORS);
// Try to register a user.
$name = $this->randomName();
$pass = $this->randomName(10);
@@ -49,16 +51,15 @@ class EmailRegistrationTestCase extends DrupalWebTestCase {
);
$this->drupalPost('user/login', $login, t('Log in'));
// Get the uid.
$accounts = user_load_multiple(array(), array('mail' => $name . '@example.com'));
$new_user = reset($accounts);
// Get the account to get uid.
$new_user = user_load_by_mail($name . '@example.com');
$new_name = $name . '_' . $new_user->uid;
// Confirm the user was created and logged in with expected username.
$this->assertRaw('<title>' . $name . '_' . $new_user->uid . ' | Drupal</title>', t('User properly created, logged in.'));
$this->assertTitle($new_name . ' | Drupal', 'User properly created, logged in.');
// Now try the immediate login.
$this->drupalLogout();
variable_set('user_email_verification', 0);
$name = $this->randomName();
$pass = $this->randomName(10);
$register = array(
@@ -67,7 +68,27 @@ class EmailRegistrationTestCase extends DrupalWebTestCase {
'pass[pass2]' => $pass,
);
$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.', 'User properly created, immediately logged in.');
// Try to login with just username, should succeed by default.
$this->drupalLogout();
// User name appended with UID see email_registration_cleanup_username().
$new_user = user_load_by_mail($name . '@example.com');
$new_name = $name . '_' . $new_user->uid;
$login = array(
'name' => $new_name,
'pass' => $pass,
);
$this->drupalPost('user/login', $login, t('Log in'));
$this->assertTitle($new_name . ' | Drupal', 'By default, username can log in.');
$this->drupalLogout();
// Disallow logins with username and try to login with just username, should fail.
variable_set('email_registration_login_with_username', FALSE);
$this->drupalPost('user/login', $login, t('Log in'));
$this->assertTitle('User account | Drupal', 'When disabled, a user cannot login with just their username.');
}
}