diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index ba11c0e..85cff65 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -73,6 +73,8 @@ public function form(array $form, FormStateInterface $form_state) { $user = $this->currentUser(); $config = \Drupal::config('user.settings'); + $form = parent::form($form, $form_state, $account); + $language_interface = \Drupal::languageManager()->getCurrentLanguage(); $register = $account->isAnonymous(); $admin = $user->hasPermission('administer users'); @@ -83,42 +85,14 @@ public function form(array $form, FormStateInterface $form_state) { '#weight' => -10, ); - // The mail field is NOT required if account originally had no mail set - // and the user performing the edit has 'administer users' permission. - // This allows users without email address to be edited and deleted. - $form['account']['mail'] = array( - '#type' => 'email', - '#title' => $this->t('Email address'), - '#description' => $this->t('A valid email address. All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email.'), - '#required' => !(!$account->getEmail() && $user->hasPermission('administer users')), - '#default_value' => (!$register ? $account->getEmail() : ''), - ); - - // Only show name field on registration form or user can change own username. - $form['account']['name'] = array( - '#type' => 'textfield', - '#title' => $this->t('Username'), - '#maxlength' => USERNAME_MAX_LENGTH, - '#description' => $this->t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, apostrophes, and underscores.'), - '#required' => TRUE, - '#attributes' => array( - 'class' => array('username'), - 'autocorrect' => 'off', - 'autocapitalize' => 'off', - 'spellcheck' => 'false', - ), - '#default_value' => (!$register ? $account->getUsername() : ''), - '#access' => ($register || ($user->id() == $account->id() && $user->hasPermission('change own username')) || $admin), - ); + $form['name']['#group'] = 'account'; + $form['mail']['#group'] = 'account'; // Display password field only for existing users or when user is allowed to // assign a password during registration. if (!$register) { - $form['account']['pass'] = array( - '#type' => 'password_confirm', - '#size' => 25, - '#description' => $this->t('To change the current user password, enter the new password in both fields.'), - ); + $form['pass']['#group'] = 'account'; + // To skip the current password field, the user must have logged in via a // one-time link and have the token in the URL. @@ -130,7 +104,7 @@ public function form(array $form, FormStateInterface $form_state) { // The user may only change their own password without their current // password if they logged in via a one-time login link. if (!$pass_reset) { - $protected_values['mail'] = $form['account']['mail']['#title']; + $protected_values['mail'] = $this->t('E-mail address'); $protected_values['pass'] = $this->t('Password'); $request_new = l($this->t('Request new password'), 'user/password', array('attributes' => array('title' => $this->t('Request new password via email.')))); $current_pass_description = $this->t('Required if you want to change the %mail or %pass below. !request_new.', array('%mail' => $protected_values['mail'], '%pass' => $protected_values['pass'], '!request_new' => $request_new)); @@ -161,12 +135,8 @@ public function form(array $form, FormStateInterface $form_state) { } } elseif (!$config->get('verify_mail') || $admin) { - $form['account']['pass'] = array( - '#type' => 'password_confirm', - '#size' => 25, - '#description' => $this->t('Provide a password for the new account in both fields.'), - '#required' => TRUE, - ); + $form['pass'][0]['value']['#description'] = $this->t('Provide a password for the new account in both fields.'); + $form['pass'][0]['value']['#required'] = TRUE; } // When not building the user registration form, prevent web browsers from @@ -298,7 +268,7 @@ public function form(array $form, FormStateInterface $form_state) { '#weight' => 100, ); - return parent::form($form, $form_state, $account); + return $form; } /** @@ -323,49 +293,6 @@ public function buildEntity(array $form, FormStateInterface $form_state) { public function validate(array $form, FormStateInterface $form_state) { parent::validate($form, $form_state); - $account = $this->entity; - // Validate new or changing username. - if ($form_state->hasValue('name')) { - if ($error = user_validate_name($form_state->getValue('name'))) { - $form_state->setErrorByName('name', $error); - } - // Cast the user ID as an integer. It might have been set to NULL, which - // could lead to unexpected results. - else { - $name_taken = (bool) $this->entityQuery->get('user') - ->condition('uid', (int) $account->id(), '<>') - ->condition('name', $form_state->getValue('name')) - ->range(0, 1) - ->count() - ->execute(); - - if ($name_taken) { - $form_state->setErrorByName('name', $this->t('The name %name is already taken.', array('%name' => $form_state->getValue('name')))); - } - } - } - - $mail = $form_state->getValue('mail'); - - if (!empty($mail)) { - $mail_taken = (bool) $this->entityQuery->get('user') - ->condition('uid', (int) $account->id(), '<>') - ->condition('mail', $mail) - ->range(0, 1) - ->count() - ->execute(); - - if ($mail_taken) { - // Format error message dependent on whether the user is logged in or not. - if (\Drupal::currentUser()->isAuthenticated()) { - $form_state->setErrorByName('mail', $this->t('The email address %email is already taken.', array('%email' => $mail))); - } - else { - $form_state->setErrorByName('mail', $this->t('The email address %email is already registered. Have you forgotten your password?', array('%email' => $mail, '@password' => url('user/password')))); - } - } - } - // Make sure the signature isn't longer than the size of the database field. // Signatures are disabled by default, so make sure it exists first. if ($signature = $form_state->getValue('signature')) { diff --git a/core/modules/user/src/Entity/User.php b/core/modules/user/src/Entity/User.php index 55ccb9f..3e3ca4e 100644 --- a/core/modules/user/src/Entity/User.php +++ b/core/modules/user/src/Entity/User.php @@ -503,11 +503,14 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t('E-mail address')) ->setDescription(t('The email of this user.')) ->setDefaultValue('') - ->setPropertyConstraints('value', array('UserMailUnique' => array())) +// ->setPropertyConstraints('value', array('UserMailUnique' => array())) ->setDisplayOptions('form', array( 'type' => 'email_user', 'weight' => -10, )) + ->setConstraints(array( + 'UserMailUnique' => array(), + )) ->setDisplayConfigurable('form', TRUE); // @todo Convert to a text field in https://drupal.org/node/1548204. diff --git a/core/modules/user/src/RegisterForm.php b/core/modules/user/src/RegisterForm.php index 5d2d5ec..d1a6845 100644 --- a/core/modules/user/src/RegisterForm.php +++ b/core/modules/user/src/RegisterForm.php @@ -115,7 +115,7 @@ public function save(array $form, FormStateInterface $form_state) { $form_state->set('user', $account); $form_state->setValue('uid', $account->id()); - $this->logger('user')->notice('New user: %name %email.', array('%name' => $form_state->getValue('name'), '%email' => '<' . $form_state->getValue('mail') . '>', 'type' => l($this->t('Edit'), 'user/' . $account->id() . '/edit'))); + $this->logger('user')->notice('New user: %name %email.', array('%name' => $form_state->getValue('name')[0]['value'], '%email' => '<' . $form_state->getValue('mail')[0]['value'] . '>', 'type' => l($this->t('Edit'), 'user/' . $account->id() . '/edit'))); // Add plain text password into user account to generate mail tokens. $account->password = $pass; diff --git a/core/modules/user/src/Tests/UserCreateFailMailTest.php b/core/modules/user/src/Tests/UserCreateFailMailTest.php index 2516d4a..3eac839 100644 --- a/core/modules/user/src/Tests/UserCreateFailMailTest.php +++ b/core/modules/user/src/Tests/UserCreateFailMailTest.php @@ -37,8 +37,8 @@ protected function testUserAdd() { $edit = array( 'name[0][value]' => $name, 'mail[0][value]' => $this->randomMachineName() . '@example.com', - 'pass[0][pass1]' => $pass = $this->randomString(), - 'pass[0][pass2]' => $pass, + 'pass[0][value][pass1]' => $pass = $this->randomString(), + 'pass[0][value][pass2]' => $pass, 'notify' => TRUE, ); $this->drupalPostForm('admin/people/create', $edit, t('Create new account')); diff --git a/core/modules/user/src/Tests/UserCreateTest.php b/core/modules/user/src/Tests/UserCreateTest.php index 4fcbe9e..364e671 100644 --- a/core/modules/user/src/Tests/UserCreateTest.php +++ b/core/modules/user/src/Tests/UserCreateTest.php @@ -94,8 +94,8 @@ protected function testUserAdd() { $edit = array( 'name[0][value]' => $name, 'mail[0][value]' => $this->randomMachineName() . '@example.com', - 'pass[0][pass1]' => $pass = $this->randomString(), - 'pass[0][pass2]' => $pass, + 'pass[0][value][pass1]' => $pass = $this->randomString(), + 'pass[0][value][pass2]' => $pass, 'notify' => $notify, ); $this->drupalPostForm('admin/people/create', $edit, t('Create new account')); diff --git a/core/modules/user/src/Tests/UserLanguageCreationTest.php b/core/modules/user/src/Tests/UserLanguageCreationTest.php index 09ba4a1..12e7f5c 100644 --- a/core/modules/user/src/Tests/UserLanguageCreationTest.php +++ b/core/modules/user/src/Tests/UserLanguageCreationTest.php @@ -55,8 +55,8 @@ function testLocalUserCreation() { $edit = array( 'name[0][value]' => $username, 'mail[0][value]' => $this->randomMachineName(4) . '@example.com', - 'pass[0][pass1]' => $username, - 'pass[0][pass2]' => $username, + 'pass[0][value][pass1]' => $username, + 'pass[0][value][pass2]' => $username, ); $this->drupalPostForm($langcode . '/admin/people/create', $edit, t('Create new account')); @@ -94,8 +94,8 @@ function testLocalUserCreation() { // Set pass_raw so we can login the new user. $user->pass_raw = $this->randomMachineName(10); $edit = array( - 'pass[0][pass1]' => $user->pass_raw, - 'pass[0][pass2]' => $user->pass_raw, + 'pass[0][value][pass1]' => $user->pass_raw, + 'pass[0][value][pass2]' => $user->pass_raw, ); $this->drupalPostForm($user_edit, $edit, t('Save')); diff --git a/core/modules/user/src/Tests/UserRolesAssignmentTest.php b/core/modules/user/src/Tests/UserRolesAssignmentTest.php index cae591e..2906b04 100644 --- a/core/modules/user/src/Tests/UserRolesAssignmentTest.php +++ b/core/modules/user/src/Tests/UserRolesAssignmentTest.php @@ -54,14 +54,14 @@ function testCreateUserWithRole() { $edit = array( 'name[0][value]' => $this->randomMachineName(), 'mail[0][value]' => $this->randomMachineName() . '@example.com', - 'pass[0][pass1]' => $pass = $this->randomString(), - 'pass[0][pass2]' => $pass, + 'pass[0][value][pass1]' => $pass = $this->randomString(), + 'pass[0][value][pass2]' => $pass, "roles[$rid]" => $rid, ); $this->drupalPostForm('admin/people/create', $edit, t('Create new account')); - $this->assertText(t('Created a new user account for !name.', array('!name' => $edit['name']))); + $this->assertText(t('Created a new user account for !name.', array('!name' => $edit['name[0][value]']))); // Get the newly added user. - $account = user_load_by_name($edit['name']); + $account = user_load_by_name($edit['name[0][value]']); $this->drupalGet('user/' . $account->id() . '/edit'); $this->assertFieldChecked('edit-roles-' . $rid, 'Role is assigned.'); diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 9e0b705..d21abbc 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -504,7 +504,8 @@ function user_validate_current_pass(&$form, FormStateInterface $form_state) { // form values like password_confirm that have their own validation // that prevent them from being empty if they are changed. $current_value = $account->hasField($key) ? $account->get($key)->value : $account->$key; - if ((strlen(trim($form_state->getValue($key)[0]['value'])) > 0) && ($form_state->getValue($key)[0]['value'] != $current_value)) { + $form_value = isset($form_state->getValue($key)[0]['value']) ? $form_state->getValue($key)[0]['value'] : $form_state->getValue($key); + if ((strlen(trim($form_value)) > 0) && ($form_value != $current_value)) { $current_pass_failed = $form_state->isValueEmpty('current_pass') || !\Drupal::service('password')->check($form_state->getValue('current_pass'), $account); if ($current_pass_failed) { $form_state->setErrorByName('current_pass', t("Your current password is missing or incorrect; it's required to change the %name.", array('%name' => $name)));