diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index 3a2b57d..3eb4ff2 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -412,40 +412,75 @@ public function submitForm(array &$form, FormStateInterface $form_state) { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { - $name_taken = FALSE; $mail_taken = FALSE; + $account = $this->entity; + $user = $this->currentUser(); // For new registrations, make sure the username does not conflict with // an existing user's email address. - $name_taken = (bool) db_select('users_field_data', 'ufd') - ->condition( - db_or() - ->condition('ufd.name', db_like($form_state->getValue('name')), 'LIKE') - ->condition('ufd.mail', db_like($form_state->getValue('name')), 'LIKE') - ) - ->condition('ufd.status', 1) - ->range(0, 1) - ->countQuery() - ->execute() - ->fetchField(); - - if ($name_taken) { - $form_state->setErrorByName('name', $this->t('The name @name is already taken.', array('@name' => $form_state->getValue('name')))); + if (!empty($form_state->getValue('name'))) { + $name_taken = FALSE; + + if ($user->isAuthenticated()) { + $name_taken = (bool) db_select('users_field_data', 'ufd') + ->condition( + db_or() + ->condition('ufd.name', db_like($form_state->getValue('name')), 'LIKE') + ->condition('ufd.mail', db_like($form_state->getValue('name')), 'LIKE') + ) + ->condition('ufd.uid', $user->id(), '<>') + ->range(0, 1) + ->countQuery() + ->execute() + ->fetchField(); + } + else { + $name_taken = (bool) db_select('users_field_data', 'ufd') + ->condition( + db_or() + ->condition('ufd.name', db_like($form_state->getValue('name')), 'LIKE') + ->condition('ufd.mail', db_like($form_state->getValue('name')), 'LIKE') + ) + ->condition('ufd.status', 1) + ->range(0, 1) + ->countQuery() + ->execute() + ->fetchField(); + } + + if ($name_taken) { + $form_state->setErrorByName('name', $this->t('The name @name is already taken.', array('@name' => $form_state->getValue('name')))); + } } // For new registrations, make sure the email address does not conflict // with an existing user's username. - $mail_taken = (bool) db_select('users_field_data', 'ufd') - ->condition( - db_or() - ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') - ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') - ) - ->condition('ufd.status', 1) - ->range(0, 1) - ->countQuery() - ->execute() - ->fetchField(); + if ($user->isAuthenticated()) { + $mail_taken = (bool) db_select('users_field_data', 'ufd') + ->condition( + db_or() + ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') + ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') + ) + ->condition('ufd.uid', $user->id(), '<>') + ->range(0, 1) + ->countQuery() + ->execute() + ->fetchField(); + } + else { + $mail_taken = (bool) db_select('users_field_data', 'ufd') + ->condition( + db_or() + ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') + ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') + ) + ->condition('ufd.status', 1) + ->range(0, 1) + ->countQuery() + ->execute() + ->fetchField(); + } if ($mail_taken) { $form_state->setErrorByName('mail', $this->t('The email address @email is already registered.', array('@email' => $form_state->getValue('mail')))); diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 370f95a..3e95399 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -50,91 +50,6 @@ const USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL = 'visitors_admin_approval'; /** - * Menu callback; process one time login link and redirects to the user page on success. - * - * @deprecated Use \Drupal\user\Form\UserForm::resetPass() - */ -function user_pass_reset($form, $form_state, $uid, $timestamp, $hashed_pass, $action = NULL) { - global $user; - - // When processing the one-time login link, we have to make sure that a user - // isn't already logged in. - if ($user->isAuthenticated()) { - // The existing user is already logged in. - if ($user->id() == $uid) { - drupal_set_message(t('You are logged in as %user. Change your password.', array('%user' => $user->getUsername(), '!user_edit' => url("user/" . $user->id() . "/edit")))); - } - // A different user is already logged in on the computer. - else { - $reset_link_account = user_load($uid); - if (!empty($reset_link_account)) { - drupal_set_message(t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please logout and try using the link again.', - array('%other_user' => $user->getUsername(), '%resetting_user' => $reset_link_account->getUsername(), '!logout' => url('user/logout')))); - } else { - // Invalid one-time link specifies an unknown user. - drupal_set_message(t('The one-time login link you clicked is invalid.')); - } - } - return new RedirectResponse(url('', array('absolute' => TRUE))); - } - else { - // Time out, in seconds, until login URL expires. - $timeout = \Drupal::config('user.settings')->get('password_reset_timeout'); - $current = REQUEST_TIME; - $account = user_load($uid); - // Verify that the user exists and is active. - if ($timestamp <= $current && $account && $account->isActive()) { - // No time out for first time login. - if ($account->getLastLoginTime() && $current - $timestamp > $timeout) { - drupal_set_message(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.')); - return new RedirectResponse(url('user/password', array('absolute' => TRUE))); - } - elseif ($account->isAuthenticated() && $timestamp >= $account->getLastLoginTime() && $timestamp <= $current && $hashed_pass == user_pass_rehash($account->getPassword(), $timestamp, $account->getLastLoginTime())) { - // First stage is a confirmation form, then login - if ($action == 'login') { - // Set the new user. - // user_login_finalize() also updates the login timestamp of the - // user, which invalidates further use of the one-time login link. - user_login_finalize($account); - watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->getUsername(), '%timestamp' => $timestamp)); - drupal_set_message(t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.')); - // Let the user's password be changed without the current password check. - $token = Crypt::randomStringHashed(55); - $_SESSION['pass_reset_' . $user->id()] = $token; - return new RedirectResponse(url('user/' . $user->id() . '/edit', array( - 'query' => array('pass-reset-token' => $token), - 'absolute' => TRUE, - ))); - } - else { - if (!$account->getLastLoginTime()) { - // No expiration for first time login. - $form['message'] = array('#markup' => t('

This is a one-time login for %user_name.

Click on this button to log in to the site and change your password.

', array('%user_name' => $account->getUsername()))); - } - else { - $form['message'] = array('#markup' => t('

This is a one-time login for %user_name and will expire on %expiration_date.

Click on this button to log in to the site and change your password.

', array('%user_name' => $account->getUsername(), '%expiration_date' => format_date($timestamp + $timeout)))); - } - $form['help'] = array('#markup' => '

' . t('This login can be used only once.') . '

'); - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in')); - $form['#action'] = url("user/reset/$uid/$timestamp/$hashed_pass/login"); - return $form; - } - } - else { - drupal_set_message(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.')); - return new RedirectResponse(url('user/password', array('absolute' => TRUE))); - } - } - else { - // Deny access, no more clues. - // Everything will be in the watchdog's URL for the administrator to check. - throw new AccessDeniedHttpException(); - } - } -} - -/** * Implements hook_help(). */ function user_help($route_name, RouteMatchInterface $route_match) {