Merge pull request #3618 from abstractj/KEYCLOAK-3685

[KEYCLOAK-3685]: Username not updated when "Email as username" is enabled
This commit is contained in:
Bill Burke 2016-12-06 22:06:55 -05:00 committed by GitHub
commit 75e2b404c8
4 changed files with 41 additions and 7 deletions

View file

@ -65,4 +65,7 @@ public class RealmBean {
return realm.isEditUsernameAllowed();
}
public boolean isRegistrationEmailAsUsername() {
return realm.isRegistrationEmailAsUsername();
}
}

View file

@ -417,6 +417,15 @@ public class AccountService extends AbstractSecuredLocalService {
user.setEmailVerified(false);
event.clone().event(EventType.UPDATE_EMAIL).detail(Details.PREVIOUS_EMAIL, oldEmail).detail(Details.UPDATED_EMAIL, email).success();
}
if (realm.isRegistrationEmailAsUsername()) {
UserModel existing = session.users().getUserByEmail(email, realm);
if (existing != null && !existing.getId().equals(user.getId())) {
throw new ModelDuplicateException(Messages.USERNAME_EXISTS);
}
user.setUsername(email);
}
setReferrerOnPage();
return account.setSuccess(Messages.ACCOUNT_UPDATED).createResponse(AccountPages.ACCOUNT);
} catch (ModelReadOnlyException roe) {

View file

@ -395,6 +395,7 @@ public class AccountTest extends TestRealmKeycloakTest {
@Test
public void changeProfile() throws Exception {
setEditUsernameAllowed(false);
setRegistrationEmailAsUsername(false);
profilePage.open();
loginPage.login("test-user@localhost", "password");
@ -463,12 +464,31 @@ public class AccountTest extends TestRealmKeycloakTest {
setEditUsernameAllowed(true);
}
@Test
public void changeProfileEmailAsUsernameEnabled() throws Exception {
setRegistrationEmailAsUsername(true);
profilePage.open();
loginPage.login("test-user@localhost", "password");
Assert.assertFalse(driver.findElements(By.id("username")).size() > 0);
// Revert
setRegistrationEmailAsUsername(false);
}
private void setEditUsernameAllowed(boolean allowed) {
RealmRepresentation testRealm = testRealm().toRepresentation();
testRealm.setEditUsernameAllowed(allowed);
testRealm().update(testRealm);
}
private void setRegistrationEmailAsUsername(boolean allowed) {
RealmRepresentation testRealm = testRealm().toRepresentation();
testRealm.setRegistrationEmailAsUsername(allowed);
testRealm().update(testRealm);
}
@Test
public void changeUsername() {
// allow to edit the username in realm

View file

@ -14,15 +14,17 @@
<input type="hidden" id="stateChecker" name="stateChecker" value="${stateChecker?html}">
<div class="form-group ${messagesPerField.printIfExists('username','has-error')}">
<div class="col-sm-2 col-md-2">
<label for="username" class="control-label">${msg("username")}</label> <#if realm.editUsernameAllowed><span class="required">*</span></#if>
</div>
<#if !realm.registrationEmailAsUsername>
<div class="form-group ${messagesPerField.printIfExists('username','has-error')}">
<div class="col-sm-2 col-md-2">
<label for="username" class="control-label">${msg("username")}</label> <#if realm.editUsernameAllowed><span class="required">*</span></#if>
</div>
<div class="col-sm-10 col-md-10">
<input type="text" class="form-control" id="username" name="username" <#if !realm.editUsernameAllowed>disabled="disabled"</#if> value="${(account.username!'')?html}"/>
<div class="col-sm-10 col-md-10">
<input type="text" class="form-control" id="username" name="username" <#if !realm.editUsernameAllowed>disabled="disabled"</#if> value="${(account.username!'')?html}"/>
</div>
</div>
</div>
</#if>
<div class="form-group ${messagesPerField.printIfExists('email','has-error')}">
<div class="col-sm-2 col-md-2">