From 7a0ff0cb66e3e5c9f3898c5d9fa9da23657e967b Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Sat, 16 Nov 2013 10:41:49 +0000 Subject: [PATCH] KEYCLOAK-159 --- .../java/org/keycloak/models/RealmModel.java | 2 + .../org/keycloak/models/jpa/RealmAdapter.java | 10 +++++ .../models/picketlink/RealmAdapter.java | 21 +++++++++ .../services/managers/RealmManager.java | 44 +------------------ .../java/org/keycloak/test/AdapterTest.java | 23 +++++++++- 5 files changed, 56 insertions(+), 44 deletions(-) diff --git a/model/api/src/main/java/org/keycloak/models/RealmModel.java b/model/api/src/main/java/org/keycloak/models/RealmModel.java index f49cf8e947..57ec2ae7d8 100755 --- a/model/api/src/main/java/org/keycloak/models/RealmModel.java +++ b/model/api/src/main/java/org/keycloak/models/RealmModel.java @@ -133,6 +133,8 @@ public interface RealmModel extends RoleContainerModel, RoleMapperModel, ScopeMa public void setAutomaticRegistrationAfterSocialLogin(boolean automaticRegistrationAfterSocialLogin); + List searchForUser(String search); + List searchForUserByAttributes(Map attributes); OAuthClientModel addOAuthClient(String name); 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 faeea27253..f3463c78ff 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 @@ -675,6 +675,16 @@ public class RealmAdapter implements RealmModel { em.flush(); } + @Override + public List searchForUser(String search) { + TypedQuery query = em.createQuery("select u from UserEntity u where lower(u.loginName) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search", UserEntity.class); + query.setParameter("search", "%" + search.toLowerCase() + "%"); + List results = query.getResultList(); + List users = new ArrayList(); + for (UserEntity entity : results) users.add(new UserAdapter(entity)); + return users; + } + @Override public List searchForUserByAttributes(Map attributes) { StringBuilder builder = new StringBuilder("select u from UserEntity u"); diff --git a/model/picketlink/src/main/java/org/keycloak/models/picketlink/RealmAdapter.java b/model/picketlink/src/main/java/org/keycloak/models/picketlink/RealmAdapter.java index 88bad1f976..3767e656c6 100755 --- a/model/picketlink/src/main/java/org/keycloak/models/picketlink/RealmAdapter.java +++ b/model/picketlink/src/main/java/org/keycloak/models/picketlink/RealmAdapter.java @@ -36,6 +36,7 @@ import org.picketlink.idm.model.sample.Role; import org.picketlink.idm.model.sample.SampleModel; import org.picketlink.idm.model.sample.User; import org.picketlink.idm.query.IdentityQuery; +import org.picketlink.idm.query.QueryParameter; import org.picketlink.idm.query.RelationshipQuery; import java.io.IOException; @@ -876,6 +877,26 @@ public class RealmAdapter implements RealmModel { getRelationshipManager().remove(relationship); } + @Override + public List searchForUser(String search) { + QueryParameter[] params = new QueryParameter[] { User.LOGIN_NAME, User.FIRST_NAME, User.LAST_NAME, User.EMAIL }; + + Map users = new HashMap(); + for (QueryParameter p : params) { + IdentityQuery query = getIdm().createIdentityQuery(User.class); + query.setParameter(p, search.toLowerCase()); + for (User u : query.getResultList()) { + users.put(u.getLoginName(), u); + } + } + + List userModels = new ArrayList(); + for (User user : users.values()) { + userModels.add(new UserAdapter(user, idm)); + } + return userModels; + } + @Override public List searchForUserByAttributes(Map attributes) { IdentityQuery query = getIdm().createIdentityQuery(User.class); diff --git a/services/src/main/java/org/keycloak/services/managers/RealmManager.java b/services/src/main/java/org/keycloak/services/managers/RealmManager.java index 6c72da6619..2b6aa611a6 100755 --- a/services/src/main/java/org/keycloak/services/managers/RealmManager.java +++ b/services/src/main/java/org/keycloak/services/managers/RealmManager.java @@ -350,49 +350,7 @@ public class RealmManager { if (searchString == null) { return Collections.emptyList(); } - - String search = searchString.trim(); - if (search.contains(" ")) { //first and last name - String[] split = search.split(" "); - if (split.length != 2) { - return Collections.emptyList(); - } - Map attributes = new HashMap(); - attributes.put(UserModel.FIRST_NAME, split[0]); - attributes.put(UserModel.LAST_NAME, split[1]); - return realmModel.searchForUserByAttributes(attributes); - } else if (search.contains("@")) { // email - Map attributes = new HashMap(); - attributes.put(UserModel.EMAIL, search); - return realmModel.searchForUserByAttributes(attributes); - } else { // username and lastname - Map attributes = new HashMap(); - attributes.put(UserModel.LOGIN_NAME, search); - List usernameQuery = realmModel.searchForUserByAttributes(attributes); - attributes.clear(); - attributes.put(UserModel.LAST_NAME, search); - List lastnameQuery = realmModel.searchForUserByAttributes(attributes); - if (usernameQuery.size() == 0) { - return lastnameQuery; - } else if (lastnameQuery.size() == 0) { - return usernameQuery; - } - List results = new ArrayList(); - results.addAll(usernameQuery); - for (UserModel lastnameUser : lastnameQuery) { - boolean found = false; - for (UserModel usernameUser : usernameQuery) { - if (usernameUser.getLoginName().equals(lastnameUser.getLoginName())) { - found = true; - break; - } - } - if (!found) { - results.add(lastnameUser); - } - } - return results; - } + return realmModel.searchForUser(searchString.trim()); } public void addRequiredCredential(RealmModel newRealm, String requiredCred) { diff --git a/services/src/test/java/org/keycloak/test/AdapterTest.java b/services/src/test/java/org/keycloak/test/AdapterTest.java index 0ad0169c9f..7c2d405dec 100755 --- a/services/src/test/java/org/keycloak/test/AdapterTest.java +++ b/services/src/test/java/org/keycloak/test/AdapterTest.java @@ -20,6 +20,8 @@ import org.keycloak.services.managers.RealmManager; import org.keycloak.test.common.AbstractKeycloakTest; import org.keycloak.test.common.SessionFactoryTestContext; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -233,6 +235,16 @@ public class AdapterTest extends AbstractKeycloakTest { user.setLastName("Burke"); user.setFirstName("Bill"); user.setEmail("bburke@redhat.com"); + + UserModel user2 = realmModel.addUser("doublefirst"); + user2.setFirstName("Knut Ole"); + user2.setLastName("Alver"); + user2.setEmail("knut@redhat.com"); + + UserModel user3 = realmModel.addUser("doublelast"); + user3.setFirstName("Ole"); + user3.setLastName("Alver Veland"); + user3.setEmail("knut@redhat.com"); } RealmManager adapter = getRealmManager(); @@ -252,7 +264,7 @@ public class AdapterTest extends AbstractKeycloakTest { } { - List userModels = adapter.searchUsers("bil burk", realmModel); + List userModels = adapter.searchUsers("bill burk", realmModel); Assert.assertEquals(userModels.size(), 1); UserModel bburke = userModels.get(0); Assert.assertEquals(bburke.getFirstName(), "Bill"); @@ -260,6 +272,15 @@ public class AdapterTest extends AbstractKeycloakTest { Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com"); } + { + ArrayList users = new ArrayList(); + for (UserModel u : adapter.searchUsers("ole alver", realmModel)) { + users.add(u.getLoginName()); + } + String[] usernames = users.toArray(new String[users.size()]); + Arrays.sort(usernames); + Assert.assertArrayEquals(new String[] { "doublefirst", "doublelast"}, usernames); + } { List userModels = adapter.searchUsers("bburke@redhat.com", realmModel);