From f9fd398f5b453fca077cbcd0105c5f3925960bbf Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Tue, 26 Jan 2016 13:01:49 +0100 Subject: [PATCH] KEYCLOAK-2218 Allow updating user without including username --- .../resources/admin/UsersResource.java | 8 +++--- .../keycloak/testsuite/admin/UserTest.java | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java index d77f0a99e0..184c7d220f 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java @@ -220,12 +220,12 @@ public class UsersResource { } public static void updateUserFromRep(UserModel user, UserRepresentation rep, Set 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()); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java index 4990f58275..611e2c926f 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/admin/UserTest.java @@ -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();