[KEYCLOAK-12980] Username not updated when "Email as username" is enabled

This commit is contained in:
Douglas Palmer 2020-02-24 14:55:36 -08:00 committed by Stian Thorgersen
parent 1e0fcc4883
commit dfb67c3aa4
5 changed files with 51 additions and 3 deletions

View file

@ -195,10 +195,16 @@ public class UserResource {
}
public static void updateUserFromRep(UserModel user, UserRepresentation rep, Set<String> 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());

View file

@ -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);
}

View file

@ -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);

View file

@ -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: {} }

View file

@ -24,7 +24,7 @@
</div>
</div>
<div class="form-group">
<div class="form-group" data-ng-hide="emailAsUsername">
<label class="col-md-2 control-label"for="username">{{:: 'username' | translate}} <span class="required" data-ng-show="create">*</span></label>
<div class="col-md-6">
<!-- Characters >,<,/,\ are forbidden in username -->