From 5c541c5cf0e7abab300e2143b31260647a9a7c4c Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Thu, 14 Nov 2013 17:38:23 +0000 Subject: [PATCH] Fix - KEYCLOAK-99 UI: Users query should support wildcard --- .../org/keycloak/models/jpa/RealmAdapter.java | 14 +++++----- .../java/org/keycloak/test/AdapterTest.java | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java index b888f34a0b..57754226df 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java @@ -643,13 +643,13 @@ public class RealmAdapter implements RealmModel { for (Map.Entry entry : attributes.entrySet()) { String attribute = null; if (entry.getKey().equals(UserModel.LOGIN_NAME)) { - attribute = "loginName"; + attribute = "lower(loginName)"; } else if (entry.getKey().equalsIgnoreCase(UserModel.FIRST_NAME)) { - attribute = "firstName"; + attribute = "lower(firstName)"; } else if (entry.getKey().equalsIgnoreCase(UserModel.LAST_NAME)) { - attribute = "lastName"; + attribute = "lower(lastName)"; } else if (entry.getKey().equalsIgnoreCase(UserModel.EMAIL)) { - attribute = "email"; + attribute = "lower(email)"; } if (attribute == null) continue; if (first) { @@ -658,9 +658,11 @@ public class RealmAdapter implements RealmModel { } else { builder.append(" and "); } - builder.append(attribute).append(" like lower('%").append(entry.getValue().toLowerCase()).append("%')"); + builder.append(attribute).append(" like '%").append(entry.getValue().toLowerCase()).append("%'"); } - TypedQuery query = em.createQuery(builder.toString(), UserEntity.class); + String q = builder.toString(); + System.out.println(q); + TypedQuery query = em.createQuery(q, UserEntity.class); List results = query.getResultList(); List users = new ArrayList(); for (UserEntity entity : results) users.add(new UserAdapter(entity)); diff --git a/services/src/test/java/org/keycloak/test/AdapterTest.java b/services/src/test/java/org/keycloak/test/AdapterTest.java index 1c86dfefa0..4810aa28a5 100755 --- a/services/src/test/java/org/keycloak/test/AdapterTest.java +++ b/services/src/test/java/org/keycloak/test/AdapterTest.java @@ -199,6 +199,15 @@ public class AdapterTest extends AbstractKeycloakTest { Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com"); } + { + List userModels = adapter.searchUsers("bil burk", realmModel); + Assert.assertEquals(userModels.size(), 1); + UserModel bburke = userModels.get(0); + Assert.assertEquals(bburke.getFirstName(), "Bill"); + Assert.assertEquals(bburke.getLastName(), "Burke"); + Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com"); + } + { List userModels = adapter.searchUsers("bburke@redhat.com", realmModel); @@ -209,6 +218,15 @@ public class AdapterTest extends AbstractKeycloakTest { Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com"); } + { + List userModels = adapter.searchUsers("rke@redhat.com", realmModel); + Assert.assertEquals(userModels.size(), 1); + UserModel bburke = userModels.get(0); + Assert.assertEquals(bburke.getFirstName(), "Bill"); + Assert.assertEquals(bburke.getLastName(), "Burke"); + Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com"); + } + { List userModels = adapter.searchUsers("bburke", realmModel); Assert.assertEquals(userModels.size(), 1); @@ -218,6 +236,15 @@ public class AdapterTest extends AbstractKeycloakTest { Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com"); } + { + List userModels = adapter.searchUsers("BurK", realmModel); + Assert.assertEquals(userModels.size(), 1); + UserModel bburke = userModels.get(0); + Assert.assertEquals(bburke.getFirstName(), "Bill"); + Assert.assertEquals(bburke.getLastName(), "Burke"); + Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com"); + } + { List userModels = adapter.searchUsers("Burke", realmModel); Assert.assertEquals(userModels.size(), 1);