From dfb67c3aa419674230bb8e345c3583e0282138d8 Mon Sep 17 00:00:00 2001 From: Douglas Palmer Date: Mon, 24 Feb 2020 14:55:36 -0800 Subject: [PATCH] [KEYCLOAK-12980] Username not updated when "Email as username" is enabled --- .../resources/admin/UserResource.java | 10 ++++- .../resources/admin/UsersResource.java | 3 ++ .../keycloak/testsuite/admin/UserTest.java | 38 +++++++++++++++++++ .../admin/resources/js/controllers/users.js | 1 + .../admin/resources/partials/user-detail.html | 2 +- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java b/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java index 4df415fa62..2da72a6c3c 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java @@ -195,10 +195,16 @@ public class UserResource { } public static void updateUserFromRep(UserModel user, UserRepresentation rep, Set attrsToRemove, RealmModel realm, KeycloakSession session, boolean removeMissingRequiredActions) { - if (rep.getUsername() != null && realm.isEditUsernameAllowed()) { + if (rep.getUsername() != null && realm.isEditUsernameAllowed() && !realm.isRegistrationEmailAsUsername()) { user.setUsername(rep.getUsername()); } - if (rep.getEmail() != null) user.setEmail(rep.getEmail()); + if (rep.getEmail() != null) { + String email = rep.getEmail(); + user.setEmail(email); + if(realm.isRegistrationEmailAsUsername()) { + user.setUsername(email); + } + } if (rep.getEmail() == "") user.setEmail(null); if (rep.getFirstName() != null) user.setFirstName(rep.getFirstName()); if (rep.getLastName() != null) user.setLastName(rep.getLastName()); 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 67fc0dc81c..7d5968a884 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 @@ -108,6 +108,9 @@ public class UsersResource { auth.users().requireManage(); String username = rep.getUsername(); + if(realm.isRegistrationEmailAsUsername()) { + username = rep.getEmail(); + } if (ObjectUtil.isBlank(username)) { return ErrorResponse.error("User name is missing", Response.Status.BAD_REQUEST); } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java index f1b4a962f9..c8b06720df 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserTest.java @@ -458,6 +458,18 @@ public class UserTest extends AbstractAdminTest { response.close(); } + @Test + public void createUserWithEmailAsUsername() { + switchRegistrationEmailAsUsername(true); + + String id = createUser(); + UserResource user = realm.users().get(id); + UserRepresentation userRep = user.toRepresentation(); + assertEquals("user1@localhost", userRep.getUsername()); + + switchRegistrationEmailAsUsername(false); + } + @Test public void createUserWithEmptyUsername() { UserRepresentation user = new UserRepresentation(); @@ -1486,6 +1498,25 @@ public class UserTest extends AbstractAdminTest { switchEditUsernameAllowedOn(false); } + @Test + public void updateUserWithEmailAsUsername() { + switchRegistrationEmailAsUsername(true); + + String id = createUser(); + + UserResource user = realm.users().get(id); + UserRepresentation userRep = user.toRepresentation(); + assertEquals("user1@localhost", userRep.getUsername()); + + userRep.setEmail("user11@localhost"); + updateUser(user, userRep); + + userRep = realm.users().get(id).toRepresentation(); + assertEquals("user11@localhost", userRep.getUsername()); + + switchRegistrationEmailAsUsername(false); + } + @Test public void updateUserWithNewUsernameNotPossible() { String id = createUser(); @@ -1809,6 +1840,13 @@ public class UserTest extends AbstractAdminTest { assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, Matchers.nullValue(String.class), rep, ResourceType.REALM); } + private void switchRegistrationEmailAsUsername(boolean enable) { + RealmRepresentation rep = realm.toRepresentation(); + rep.setRegistrationEmailAsUsername(enable); + realm.update(rep); + assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, Matchers.nullValue(String.class), rep, ResourceType.REALM); + } + private void enableBruteForce(boolean enable) { RealmRepresentation rep = realm.toRepresentation(); rep.setBruteForceProtected(enable); diff --git a/themes/src/main/resources/theme/base/admin/resources/js/controllers/users.js b/themes/src/main/resources/theme/base/admin/resources/js/controllers/users.js index b8f4764e59..02564e14b5 100755 --- a/themes/src/main/resources/theme/base/admin/resources/js/controllers/users.js +++ b/themes/src/main/resources/theme/base/admin/resources/js/controllers/users.js @@ -379,6 +379,7 @@ module.controller('UserDetailCtrl', function($scope, realm, user, BruteForceUser $scope.realm = realm; $scope.create = !user.id; $scope.editUsername = $scope.create || $scope.realm.editUsernameAllowed; + $scope.emailAsUsername = $scope.realm.registrationEmailAsUsername; if ($scope.create) { $scope.user = { enabled: true, attributes: {} } diff --git a/themes/src/main/resources/theme/base/admin/resources/partials/user-detail.html b/themes/src/main/resources/theme/base/admin/resources/partials/user-detail.html index 88e66e7438..b985a52af3 100755 --- a/themes/src/main/resources/theme/base/admin/resources/partials/user-detail.html +++ b/themes/src/main/resources/theme/base/admin/resources/partials/user-detail.html @@ -24,7 +24,7 @@ -
+