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 files changed
+41
-7
No files matched your search
@@ -65,4 +65,7 @@ public class RealmBean {
|
|||||||
return realm.isEditUsernameAllowed();
|
return realm.isEditUsernameAllowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isRegistrationEmailAsUsername() {
|
||||||
|
return realm.isRegistrationEmailAsUsername();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -417,6 +417,15 @@ public class AccountService extends AbstractSecuredLocalService {
|
|||||||
user.setEmailVerified(false);
|
user.setEmailVerified(false);
|
||||||
event.clone().event(EventType.UPDATE_EMAIL).detail(Details.PREVIOUS_EMAIL, oldEmail).detail(Details.UPDATED_EMAIL, email).success();
|
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();
|
setReferrerOnPage();
|
||||||
return account.setSuccess(Messages.ACCOUNT_UPDATED).createResponse(AccountPages.ACCOUNT);
|
return account.setSuccess(Messages.ACCOUNT_UPDATED).createResponse(AccountPages.ACCOUNT);
|
||||||
} catch (ModelReadOnlyException roe) {
|
} catch (ModelReadOnlyException roe) {
|
||||||
|
|||||||
+20
@@ -395,6 +395,7 @@ public class AccountTest extends TestRealmKeycloakTest {
|
|||||||
@Test
|
@Test
|
||||||
public void changeProfile() throws Exception {
|
public void changeProfile() throws Exception {
|
||||||
setEditUsernameAllowed(false);
|
setEditUsernameAllowed(false);
|
||||||
|
setRegistrationEmailAsUsername(false);
|
||||||
|
|
||||||
profilePage.open();
|
profilePage.open();
|
||||||
loginPage.login("test-user@localhost", "password");
|
loginPage.login("test-user@localhost", "password");
|
||||||
@@ -463,12 +464,31 @@ public class AccountTest extends TestRealmKeycloakTest {
|
|||||||
setEditUsernameAllowed(true);
|
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) {
|
private void setEditUsernameAllowed(boolean allowed) {
|
||||||
RealmRepresentation testRealm = testRealm().toRepresentation();
|
RealmRepresentation testRealm = testRealm().toRepresentation();
|
||||||
testRealm.setEditUsernameAllowed(allowed);
|
testRealm.setEditUsernameAllowed(allowed);
|
||||||
testRealm().update(testRealm);
|
testRealm().update(testRealm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setRegistrationEmailAsUsername(boolean allowed) {
|
||||||
|
RealmRepresentation testRealm = testRealm().toRepresentation();
|
||||||
|
testRealm.setRegistrationEmailAsUsername(allowed);
|
||||||
|
testRealm().update(testRealm);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void changeUsername() {
|
public void changeUsername() {
|
||||||
// allow to edit the username in realm
|
// allow to edit the username in realm
|
||||||
|
|||||||
@@ -14,15 +14,17 @@
|
|||||||
|
|
||||||
<input type="hidden" id="stateChecker" name="stateChecker" value="${stateChecker?html}">
|
<input type="hidden" id="stateChecker" name="stateChecker" value="${stateChecker?html}">
|
||||||
|
|
||||||
<div class="form-group ${messagesPerField.printIfExists('username','has-error')}">
|
<#if !realm.registrationEmailAsUsername>
|
||||||
<div class="col-sm-2 col-md-2">
|
<div class="form-group ${messagesPerField.printIfExists('username','has-error')}">
|
||||||
<label for="username" class="control-label">${msg("username")}</label> <#if realm.editUsernameAllowed><span class="required">*</span></#if>
|
<div class="col-sm-2 col-md-2">
|
||||||
</div>
|
<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">
|
<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}"/>
|
<input type="text" class="form-control" id="username" name="username" <#if !realm.editUsernameAllowed>disabled="disabled"</#if> value="${(account.username!'')?html}"/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</#if>
|
||||||
|
|
||||||
<div class="form-group ${messagesPerField.printIfExists('email','has-error')}">
|
<div class="form-group ${messagesPerField.printIfExists('email','has-error')}">
|
||||||
<div class="col-sm-2 col-md-2">
|
<div class="col-sm-2 col-md-2">
|
||||||
|
|||||||
Reference in new issue
Block a user