From 93a1374558643ee31e717c1dfacecb8ab8d95f68 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Wed, 12 Feb 2020 14:06:55 +0100 Subject: [PATCH] KEYCLOAK-11129 coalesce possible null values --- .../models/jpa/entities/UserEntity.java | 4 +-- .../keycloak/testsuite/admin/UserTest.java | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java index 17ba643574..ddcabd63f4 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java @@ -45,9 +45,9 @@ import java.util.Collection; @NamedQuery(name="getAllUsersByRealm", query="select u from UserEntity u where u.realmId = :realmId order by u.username"), @NamedQuery(name="getAllUsersByRealmExcludeServiceAccount", query="select u from UserEntity u where u.realmId = :realmId and (u.serviceAccountClientLink is null) order by u.username"), @NamedQuery(name="searchForUser", query="select u from UserEntity u where u.realmId = :realmId and (u.serviceAccountClientLink is null) and " + - "( lower(u.username) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search ) order by u.username"), + "( lower(u.username) like :search or lower(concat(coalesce(u.firstName, ''), ' ', coalesce(u.lastName, ''))) like :search or u.email like :search ) order by u.username"), @NamedQuery(name="searchForUserCount", query="select count(u) from UserEntity u where u.realmId = :realmId and (u.serviceAccountClientLink is null) and " + - "( lower(u.username) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search )"), + "( lower(u.username) like :search or lower(concat(coalesce(u.firstName, ''), ' ', coalesce(u.lastName, ''))) like :search or u.email like :search )"), @NamedQuery(name="getRealmUserByUsername", query="select u from UserEntity u where u.username = :username and u.realmId = :realmId"), @NamedQuery(name="getRealmUserByEmail", query="select u from UserEntity u where u.email = :email and u.realmId = :realmId"), @NamedQuery(name="getRealmUserByLastName", query="select u from UserEntity u where u.lastName = :lastName and u.realmId = :realmId"), 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 24ef778e8e..f1b4a962f9 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 @@ -530,6 +530,34 @@ public class UserTest extends AbstractAdminTest { assertEquals(9, users.size()); } + @Test + public void searchByFirstNameNullForLastName() { + UserRepresentation user = new UserRepresentation(); + user.setUsername("user1"); + user.setFirstName("Erik"); + user.setRequiredActions(Collections.emptyList()); + user.setEnabled(true); + + createUser(user); + + List users = realm.users().search("Erik", 0, 50); + assertEquals(1, users.size()); + } + + @Test + public void searchByLastNameNullForFirstName() { + UserRepresentation user = new UserRepresentation(); + user.setUsername("user1"); + user.setLastName("de Wit"); + user.setRequiredActions(Collections.emptyList()); + user.setEnabled(true); + + createUser(user); + + List users = realm.users().search("wit", null, null); + assertEquals(1, users.size()); + } + @Test public void searchById() { String expectedUserId = createUsers().get(0);