Merge pull request #3618 from abstractj/KEYCLOAK-3685
[KEYCLOAK-3685]: Username not updated when "Email as username" is enabled
This commit is contained in:
commit
75e2b404c8
4 changed files with 41 additions and 7 deletions
|
@ -65,4 +65,7 @@ public class RealmBean {
|
|||
return realm.isEditUsernameAllowed();
|
||||
}
|
||||
|
||||
public boolean isRegistrationEmailAsUsername() {
|
||||
return realm.isRegistrationEmailAsUsername();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
<input type="hidden" id="stateChecker" name="stateChecker" value="${stateChecker?html}">
|
||||
|
||||
<#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>
|
||||
|
@ -23,6 +24,7 @@
|
|||
<input type="text" class="form-control" id="username" name="username" <#if !realm.editUsernameAllowed>disabled="disabled"</#if> value="${(account.username!'')?html}"/>
|
||||
</div>
|
||||
</div>
|
||||
</#if>
|
||||
|
||||
<div class="form-group ${messagesPerField.printIfExists('email','has-error')}">
|
||||
<div class="col-sm-2 col-md-2">
|
||||
|
|
Loading…
Reference in a new issue