KEYCLOAK-2218 Allow updating user without including username

This commit is contained in:
Stian Thorgersen 2016-01-26 13:01:49 +01:00
parent a21f70fcd2
commit f9fd398f5b
2 changed files with 30 additions and 4 deletions

View file

@ -220,12 +220,12 @@ public class UsersResource {
}
public static void updateUserFromRep(UserModel user, UserRepresentation rep, Set<String> attrsToRemove, RealmModel realm, KeycloakSession session) {
if (realm.isEditUsernameAllowed()) {
if (rep.getUsername() != null && realm.isEditUsernameAllowed()) {
user.setUsername(rep.getUsername());
}
user.setEmail(rep.getEmail());
user.setFirstName(rep.getFirstName());
user.setLastName(rep.getLastName());
if (rep.getEmail() != null) user.setEmail(rep.getEmail());
if (rep.getFirstName() != null) user.setFirstName(rep.getFirstName());
if (rep.getLastName() != null) user.setLastName(rep.getLastName());
if (rep.isEnabled() != null) user.setEnabled(rep.isEnabled());
if (rep.isTotp() != null) user.setOtpEnabled(rep.isTotp());

View file

@ -528,6 +528,32 @@ public class UserTest extends AbstractClientTest {
assertEquals("user11", userRep.getUsername());
}
@Test
public void updateUserWithoutUsername() {
switchEditUsernameAllowedOn();
String id = createUser();
UserResource user = realm.users().get(id);
UserRepresentation rep = new UserRepresentation();
rep.setFirstName("Firstname");
user.update(rep);
rep = new UserRepresentation();
rep.setLastName("Lastname");
user.update(rep);
rep = realm.users().get(id).toRepresentation();
assertEquals("user1", rep.getUsername());
assertEquals("user1@localhost", rep.getEmail());
assertEquals("Firstname", rep.getFirstName());
assertEquals("Lastname", rep.getLastName());
}
@Test
public void updateUserWithNewUsernameNotPossible() {
String id = createUser();