Show error when username already exists (KEYCLOAK-1857)

This commit is contained in:
dcampagna 2015-09-18 10:15:14 +02:00 committed by Stian Thorgersen
parent 880e831e71
commit 43541d3028
2 changed files with 42 additions and 1 deletions

View file

@ -63,7 +63,25 @@ public class UpdateProfile implements RequiredActionProvider, RequiredActionFact
}
if (realm.isEditUsernameAllowed()) {
user.setUsername(formData.getFirst("username"));
String username = formData.getFirst("username");
String oldUsername = user.getUsername();
boolean usernameChanged = oldUsername != null ? !oldUsername.equals(username) : username != null;
if (usernameChanged) {
if (session.users().getUserByUsername(username, realm) != null) {
Response challenge = context.form()
.setError(Messages.USERNAME_EXISTS)
.setFormData(formData)
.createResponse(UserModel.RequiredAction.UPDATE_PROFILE);
context.challenge(challenge);
return;
}
user.setUsername(username);
}
}
user.setFirstName(formData.getFirst("firstName"));

View file

@ -247,6 +247,29 @@ public class RequiredActionUpdateProfileTest {
events.assertEmpty();
}
@Test
public void updateProfileDuplicateUsername() {
loginPage.open();
loginPage.login("john-doh@localhost", "password");
updateProfilePage.assertCurrent();
updateProfilePage.update("New first", "New last", "new@email.com", "test-user@localhost");
updateProfilePage.assertCurrent();
// assert that form holds submitted values during validation error
Assert.assertEquals("New first", updateProfilePage.getFirstName());
Assert.assertEquals("New last", updateProfilePage.getLastName());
Assert.assertEquals("new@email.com", updateProfilePage.getEmail());
Assert.assertEquals("", updateProfilePage.getUsername());
Assert.assertEquals("Username already exists.", updateProfilePage.getError());
events.assertEmpty();
}
@Test
public void updateProfileDuplicatedEmail() {
loginPage.open();