diff --git a/core/modules/user/src/Tests/UserRegistrationTest.php b/core/modules/user/src/Tests/UserRegistrationTest.php index cc5785a..01418b7 100644 --- a/core/modules/user/src/Tests/UserRegistrationTest.php +++ b/core/modules/user/src/Tests/UserRegistrationTest.php @@ -7,6 +7,7 @@ namespace Drupal\user\Tests; +use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\simpletest\WebTestBase; @@ -193,6 +194,24 @@ function testRegistrationDefaultValues() { } /** + * Ensures that you cannot register with the same name / mail twice. + * + * @see \Drupal\user\Plugin\Validation\Constraint\UserNameUnique + * @see \Drupal\user\Plugin\Validation\Constraint\UserMailUnique + */ + public function testUniqueFields() { + $account = $this->drupalCreateUser([]); + + // Existing username. + $this->drupalPostForm('user/register', ['mail' => 'test@example.com', 'name' => $account->getUsername()], t('Create new account')); + $this->assertRaw(Safemarkup::format('The username %value is already taken.', ['%value' => $account->getUsername()])); + + // Existing mail address. + $this->drupalPostForm('user/register', ['mail' => $account->getEmail(), 'name' => $this->randomString()], t('Create new account')); + $this->assertRaw(Safemarkup::format('The email address %value is already taken.', ['%value' => $account->getEmail()])); + } + + /** * Tests Field API fields on user registration forms. */ function testRegistrationWithUserFields() { diff --git a/core/modules/user/src/UserAccessControlHandler.php b/core/modules/user/src/UserAccessControlHandler.php index 4b29b4e..b4bc2b3 100644 --- a/core/modules/user/src/UserAccessControlHandler.php +++ b/core/modules/user/src/UserAccessControlHandler.php @@ -81,9 +81,13 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_ switch ($field_definition->getName()) { case 'name': // Allow view access to anyone with access to the entity. - if ($operation == 'view') { + // Anonymous users should be able to change their usernamae + if ($operation == 'view' || ($items && $account->isAnonymous() && $items->getEntity()->isAnonymous())) { return AccessResult::allowed()->cachePerPermissions(); } + if ($account->isAnonymous()) { + return AccessResult::allowed(); + } // Allow edit access for the own user name if the permission is // satisfied. if ($is_own_account && $account->hasPermission('change own username')) {