KEYCLOAK-16112 don't remove username attribute

This commit is contained in:
Cédric Couralet 2020-10-30 11:38:53 +01:00 committed by Marek Posolda
parent 200b53ed1e
commit f4abc86a66
2 changed files with 31 additions and 2 deletions

View file

@ -213,10 +213,13 @@ public class UserAttributeLDAPStorageMapper extends AbstractLDAPStorageMapper {
@Override
public void removeAttribute(String name) {
if(!UserModel.USERNAME.equals(name)){
//do not remove username
if (setLDAPAttribute(name, null)) {
super.removeAttribute(name);
}
}
}
@Override
public void setUsername(String username) {

View file

@ -118,6 +118,32 @@ public class LDAPAccountRestApiTest extends AbstractLDAPTest {
Assert.assertNull(userPassword.getSecretData());
}
@Test
public void testUpdateProfile() throws IOException {
testingClient.server().run(session -> {
LDAPTestContext ctx = LDAPTestContext.init(session);
RealmModel appRealm = ctx.getRealm();
appRealm.setEditUsernameAllowed(false);
});
UserRepresentation user = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
user.setEmail("john-alias@email.org");
SimpleHttp.doPost(getAccountUrl(null), httpClient).json(user).auth(tokenUtil.getToken()).asStatus();
UserRepresentation usernew = SimpleHttp.doGet(getAccountUrl(null), httpClient).auth(tokenUtil.getToken()).asJson(UserRepresentation.class);
assertEquals("johnkeycloak", usernew.getUsername());
assertEquals("John", usernew.getFirstName());
assertEquals("Doe", usernew.getLastName());
assertEquals("john-alias@email.org", usernew.getEmail());
assertFalse(usernew.isEmailVerified());
//clean up
usernew.setEmail("john@email.org");
SimpleHttp.doPost(getAccountUrl(null), httpClient).json(usernew).auth(tokenUtil.getToken()).asStatus();
}
private String getAccountUrl(String resource) {
return suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth/realms/test/account" + (resource != null ? "/" + resource : "");
}