diff --git a/modules/user/user.module b/modules/user/user.module
index d38de69b..f1bc45f1 100644
--- a/modules/user/user.module
+++ b/modules/user/user.module
@@ -2356,14 +2356,26 @@ function user_external_login_register($name, $module) {
* following properties:
* - uid: The user ID number.
* - login: The UNIX timestamp of the user's last login.
+ * @param array $options
+ * (optional) A keyed array of settings. Supported options are:
+ * - langcode: A language code to be used when generating locale-sensitive
+ * urls. If langcode is NULL the users preferred language is used.
*
* @return
* A unique URL that provides a one-time log in for the user, from which
* they can change their password.
*/
-function user_pass_reset_url($account) {
+function user_pass_reset_url($account, $options = array()) {
$timestamp = REQUEST_TIME;
- return url("user/reset/$account->uid/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), array('absolute' => TRUE));
+ $url_options = array('absolute' => TRUE);
+ if (isset($options['langcode'])) {
+ $languages = language_list();
+ $url_options['language'] = $languages[$options['langcode']];
+ }
+ else {
+ $url_options['language'] = user_preferred_language($account);
+ }
+ return url("user/reset/$account->uid/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), $url_options);
}
/**
@@ -2375,6 +2387,10 @@ function user_pass_reset_url($account) {
* - uid: The user ID number.
* - pass: The hashed user password string.
* - login: The UNIX timestamp of the user's last login.
+ * @param array $options
+ * (optional) A keyed array of settings. Supported options are:
+ * - langcode: A language code to be used when generating locale-sensitive
+ * urls. If langcode is NULL the users preferred language is used.
*
* @return
* A unique URL that may be used to confirm the cancellation of the user
@@ -2383,9 +2399,17 @@ function user_pass_reset_url($account) {
* @see user_mail_tokens()
* @see user_cancel_confirm()
*/
-function user_cancel_url($account) {
+function user_cancel_url($account, $options = array()) {
$timestamp = REQUEST_TIME;
- return url("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), array('absolute' => TRUE));
+ $url_options = array('absolute' => TRUE);
+ if (isset($options['langcode'])) {
+ $languages = language_list();
+ $url_options['language'] = $languages[$options['langcode']];
+ }
+ else {
+ $url_options['language'] = user_preferred_language($account);
+ }
+ return url("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), $url_options);
}
/**
@@ -2875,7 +2899,7 @@ Your account on [site:name] has been canceled.
if ($replace) {
// We do not sanitize the token replacement, since the output of this
// replacement is intended for an e-mail message, not a web browser.
- return token_replace($text, $variables, array('language' => $language, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
+ return token_replace($text, $variables, array('language' => $language, 'langcode' => $langcode, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
}
return $text;
@@ -2902,8 +2926,8 @@ Your account on [site:name] has been canceled.
*/
function user_mail_tokens(&$replacements, $data, $options) {
if (isset($data['user'])) {
- $replacements['[user:one-time-login-url]'] = user_pass_reset_url($data['user']);
- $replacements['[user:cancel-url]'] = user_cancel_url($data['user']);
+ $replacements['[user:one-time-login-url]'] = user_pass_reset_url($data['user'], $options);
+ $replacements['[user:cancel-url]'] = user_cancel_url($data['user'], $options);
}
}
diff --git a/modules/user/user.module.orig b/modules/user/user.module.orig
index 9637a716..d38de69b 100644
--- a/modules/user/user.module.orig
+++ b/modules/user/user.module.orig
@@ -958,6 +958,8 @@ function user_search_access() {
*/
function user_search_execute($keys = NULL, $conditions = NULL) {
$find = array();
+ // Escape for LIKE matching.
+ $keys = db_like($keys);
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = preg_replace('!\*+!', '%', $keys);
$query = db_select('users')->extend('PagerDefault');
@@ -967,13 +969,13 @@ function user_search_execute($keys = NULL, $conditions = NULL) {
// and they don't need to be restricted to only active users.
$query->fields('users', array('mail'));
$query->condition(db_or()->
- condition('name', '%' . db_like($keys) . '%', 'LIKE')->
- condition('mail', '%' . db_like($keys) . '%', 'LIKE'));
+ condition('name', '%' . $keys . '%', 'LIKE')->
+ condition('mail', '%' . $keys . '%', 'LIKE'));
}
else {
// Regular users can only search via usernames, and we do not show them
// blocked accounts.
- $query->condition('name', '%' . db_like($keys) . '%', 'LIKE')
+ $query->condition('name', '%' . $keys . '%', 'LIKE')
->condition('status', 1);
}
$uids = $query
@@ -1306,10 +1308,12 @@ function user_user_presave(&$edit, $account, $category) {
elseif (!empty($edit['picture_delete'])) {
$edit['picture'] = NULL;
}
- // Prepare user roles.
- if (isset($edit['roles'])) {
- $edit['roles'] = array_filter($edit['roles']);
- }
+ }
+
+ // Filter out roles with empty values to avoid granting extra roles when
+ // processing custom form submissions.
+ if (isset($edit['roles'])) {
+ $edit['roles'] = array_filter($edit['roles']);
}
// Move account cancellation information into $user->data.
@@ -1911,13 +1915,13 @@ function user_menu_link_alter(&$link) {
// for authenticated users. Authenticated users should see "My account", but
// anonymous users should not see it at all. Therefore, invoke
// user_translated_menu_link_alter() to conditionally hide the link.
- if ($link['link_path'] == 'user' && $link['module'] == 'system') {
+ if ($link['link_path'] == 'user' && isset($link['module']) && $link['module'] == 'system') {
$link['options']['alter'] = TRUE;
}
// Force the Logout link to appear on the top-level of 'user-menu' menu by
// default (i.e., unless it has been customized).
- if ($link['link_path'] == 'user/logout' && $link['module'] == 'system' && empty($link['customized'])) {
+ if ($link['link_path'] == 'user/logout' && isset($link['module']) && $link['module'] == 'system' && empty($link['customized'])) {
$link['plid'] = 0;
}
}
@@ -2225,7 +2229,11 @@ function user_login_final_validate($form, &$form_state) {
}
}
else {
- form_set_error('name', t('Sorry, unrecognized username or password. Have you forgotten your password?', array('@password' => url('user/password', array('query' => array('name' => $form_state['values']['name']))))));
+ // Use $form_state['input']['name'] here to guarantee that we send
+ // exactly what the user typed in. $form_state['values']['name'] may have
+ // been modified by validation handlers that ran earlier than this one.
+ $query = isset($form_state['input']['name']) ? array('name' => $form_state['input']['name']) : array();
+ form_set_error('name', t('Sorry, unrecognized username or password. Have you forgotten your password?', array('@password' => url('user/password', array('query' => $query)))));
watchdog('user', 'Login attempt failed for %user.', array('%user' => $form_state['values']['name']));
}
}
@@ -2488,7 +2496,9 @@ function user_cancel($edit, $uid, $method) {
}
/**
- * Last batch processing step for cancelling a user account.
+ * Implements callback_batch_operation().
+ *
+ * Last step for cancelling a user account.
*
* Since batch and session API require a valid user account, the actual
* cancellation of a user account needs to happen last.
@@ -2536,6 +2546,8 @@ function _user_cancel($edit, $account, $method) {
}
/**
+ * Implements callback_batch_finished().
+ *
* Finished batch processing callback for cancelling a user account.
*
* @see user_cancel()
@@ -3039,6 +3051,11 @@ function user_role_delete($role) {
$role = user_role_load_by_name($role);
}
+ // If this is the administrator role, delete the user_admin_role variable.
+ if ($role->rid == variable_get('user_admin_role')) {
+ variable_del('user_admin_role');
+ }
+
db_delete('role')
->condition('rid', $role->rid)
->execute();
@@ -3654,12 +3671,7 @@ function user_form_process_password_confirm($element) {
);
$element['#attached']['js'][] = drupal_get_path('module', 'user') . '/user.js';
- // Ensure settings are only added once per page.
- static $already_added = FALSE;
- if (!$already_added) {
- $already_added = TRUE;
- $element['#attached']['js'][] = array('data' => $js_settings, 'type' => 'setting');
- }
+ $element['#attached']['js'][] = array('data' => $js_settings, 'type' => 'setting');
return $element;
}
diff --git a/modules/user/user.test b/modules/user/user.test
index b9729c50..81fbff21 100644
--- a/modules/user/user.test
+++ b/modules/user/user.test
@@ -2149,6 +2149,26 @@ class UserTokenReplaceTestCase extends DrupalWebTestCase {
);
}
+ public function setUp() {
+ parent::setUp('locale');
+
+ $account = $this->drupalCreateUser(array('access administration pages', 'administer languages'));
+ $this->drupalLogin($account);
+
+ // Add language.
+ $edit = array('langcode' => 'de');
+ $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
+
+ // Enable URL language detection and selection.
+ $edit = array('language[enabled][locale-url]' => 1);
+ $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
+
+ // Reset static caching.
+ drupal_static_reset('language_list');
+ drupal_static_reset('locale_url_outbound_alter');
+ drupal_static_reset('locale_language_url_rewrite_url');
+ }
+
/**
* Creates a user, then tests the tokens generated from it.
*/
@@ -2199,6 +2219,39 @@ class UserTokenReplaceTestCase extends DrupalWebTestCase {
$output = token_replace($input, array('user' => $account), array('language' => $language, 'sanitize' => FALSE));
$this->assertEqual($output, $expected, format_string('Unsanitized user token %token replaced.', array('%token' => $input)));
}
+
+ $languages = language_list();
+
+ // Generate login and cancel link.
+ $tests = array();
+ $tests['[user:one-time-login-url]'] = user_pass_reset_url($account);
+ $tests['[user:cancel-url]'] = user_cancel_url($account);
+
+ // Generate tokens with interface language.
+ $link = url('user', array('absolute' => TRUE));
+ foreach ($tests as $input => $expected) {
+ $output = token_replace($input, array('user' => $account), array('langcode' => $language->language, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
+ $this->assertTrue(strpos($output, $link) === 0, 'Generated URL is in interface language.');
+ }
+
+ // Generate tokens with the user's preferred language.
+ $edit['language'] = 'de';
+ $account = user_save($account, $edit);
+ $link = url('user', array('language' => $languages[$account->language], 'absolute' => TRUE));
+ foreach ($tests as $input => $expected) {
+ $output = token_replace($input, array('user' => $account), array('callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
+ $this->assertTrue(strpos($output, $link) === 0, "Generated URL is in the user's preferred language.");
+ }
+
+ // Generate tokens with one specific language.
+ $link = url('user', array('language' => $languages['de'], 'absolute' => TRUE));
+ foreach ($tests as $input => $expected) {
+ foreach (array($user1, $user2) as $account) {
+ $output = token_replace($input, array('user' => $account), array('langcode' => 'de', 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
+ $this->assertTrue(strpos($output, $link) === 0, "Generated URL in in the requested language.");
+ }
+ }
+
}
}
diff --git a/modules/user/user.test.orig b/modules/user/user.test.orig
new file mode 100644
index 00000000..b9729c50
--- /dev/null
+++ b/modules/user/user.test.orig
@@ -0,0 +1,2465 @@
+ 'User registration',
+ 'description' => 'Test registration of user under different configurations.',
+ 'group' => 'User'
+ );
+ }
+
+ function setUp() {
+ parent::setUp('field_test');
+ }
+
+ function testRegistrationWithEmailVerification() {
+ // Require e-mail verification.
+ variable_set('user_email_verification', TRUE);
+
+ // Set registration to administrator only.
+ variable_set('user_register', USER_REGISTER_ADMINISTRATORS_ONLY);
+ $this->drupalGet('user/register');
+ $this->assertResponse(403, 'Registration page is inaccessible when only administrators can create accounts.');
+
+ // Allow registration by site visitors without administrator approval.
+ variable_set('user_register', USER_REGISTER_VISITORS);
+ $edit = array();
+ $edit['name'] = $name = $this->randomName();
+ $edit['mail'] = $mail = $edit['name'] . '@example.com';
+ $this->drupalPost('user/register', $edit, t('Create new account'));
+ $this->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), 'User registered successfully.');
+ $accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
+ $new_user = reset($accounts);
+ $this->assertTrue($new_user->status, 'New account is active after registration.');
+
+ // Allow registration by site visitors, but require administrator approval.
+ variable_set('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
+ $edit = array();
+ $edit['name'] = $name = $this->randomName();
+ $edit['mail'] = $mail = $edit['name'] . '@example.com';
+ $this->drupalPost('user/register', $edit, t('Create new account'));
+ $accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
+ $new_user = reset($accounts);
+ $this->assertFalse($new_user->status, 'New account is blocked until approved by an administrator.');
+ }
+
+ function testRegistrationWithoutEmailVerification() {
+ // Don't require e-mail verification.
+ variable_set('user_email_verification', FALSE);
+
+ // Allow registration by site visitors without administrator approval.
+ variable_set('user_register', USER_REGISTER_VISITORS);
+ $edit = array();
+ $edit['name'] = $name = $this->randomName();
+ $edit['mail'] = $mail = $edit['name'] . '@example.com';
+
+ // Try entering a mismatching password.
+ $edit['pass[pass1]'] = '99999.0';
+ $edit['pass[pass2]'] = '99999';
+ $this->drupalPost('user/register', $edit, t('Create new account'));
+ $this->assertText(t('The specified passwords do not match.'), 'Typing mismatched passwords displays an error message.');
+
+ // Enter a correct password.
+ $edit['pass[pass1]'] = $new_pass = $this->randomName();
+ $edit['pass[pass2]'] = $new_pass;
+ $this->drupalPost('user/register', $edit, t('Create new account'));
+ $accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
+ $new_user = reset($accounts);
+ $this->assertText(t('Registration successful. You are now logged in.'), 'Users are logged in after registering.');
+ $this->drupalLogout();
+
+ // Allow registration by site visitors, but require administrator approval.
+ variable_set('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
+ $edit = array();
+ $edit['name'] = $name = $this->randomName();
+ $edit['mail'] = $mail = $edit['name'] . '@example.com';
+ $edit['pass[pass1]'] = $pass = $this->randomName();
+ $edit['pass[pass2]'] = $pass;
+ $this->drupalPost('user/register', $edit, t('Create new account'));
+ $this->assertText(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'), 'Users are notified of pending approval');
+
+ // Try to login before administrator approval.
+ $auth = array(
+ 'name' => $name,
+ 'pass' => $pass,
+ );
+ $this->drupalPost('user/login', $auth, t('Log in'));
+ $this->assertText(t('The username @name has not been activated or is blocked.', array('@name' => $name)), 'User cannot login yet.');
+
+ // Activate the new account.
+ $accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
+ $new_user = reset($accounts);
+ $admin_user = $this->drupalCreateUser(array('administer users'));
+ $this->drupalLogin($admin_user);
+ $edit = array(
+ 'status' => 1,
+ );
+ $this->drupalPost('user/' . $new_user->uid . '/edit', $edit, t('Save'));
+ $this->drupalLogout();
+
+ // Login after administrator approval.
+ $this->drupalPost('user/login', $auth, t('Log in'));
+ $this->assertText(t('Member for'), 'User can log in after administrator approval.');
+ }
+
+ function testRegistrationEmailDuplicates() {
+ // Don't require e-mail verification.
+ variable_set('user_email_verification', FALSE);
+
+ // Allow registration by site visitors without administrator approval.
+ variable_set('user_register', USER_REGISTER_VISITORS);
+
+ // Set up a user to check for duplicates.
+ $duplicate_user = $this->drupalCreateUser();
+
+ $edit = array();
+ $edit['name'] = $this->randomName();
+ $edit['mail'] = $duplicate_user->mail;
+
+ // Attempt to create a new account using an existing e-mail address.
+ $this->drupalPost('user/register', $edit, t('Create new account'));
+ $this->assertText(t('The e-mail address @email is already registered.', array('@email' => $duplicate_user->mail)), 'Supplying an exact duplicate email address displays an error message');
+
+ // Attempt to bypass duplicate email registration validation by adding spaces.
+ $edit['mail'] = ' ' . $duplicate_user->mail . ' ';
+
+ $this->drupalPost('user/register', $edit, t('Create new account'));
+ $this->assertText(t('The e-mail address @email is already registered.', array('@email' => $duplicate_user->mail)), 'Supplying a duplicate email address with added whitespace displays an error message');
+ }
+
+ function testRegistrationDefaultValues() {
+ // Allow registration by site visitors without administrator approval.
+ variable_set('user_register', USER_REGISTER_VISITORS);
+
+ // Don't require e-mail verification.
+ variable_set('user_email_verification', FALSE);
+
+ // Set the default timezone to Brussels.
+ variable_set('configurable_timezones', 1);
+ variable_set('date_default_timezone', 'Europe/Brussels');
+
+ // Check that the account information fieldset's options are not displayed
+ // is a fieldset if there is not more than one fieldset in the form.
+ $this->drupalGet('user/register');
+ $this->assertNoRaw('