KEYCLOAK-159

This commit is contained in:
Stian Thorgersen 2013-11-16 10:41:49 +00:00
parent 219c0efbaf
commit 7a0ff0cb66
5 changed files with 56 additions and 44 deletions

View file

@ -133,6 +133,8 @@ public interface RealmModel extends RoleContainerModel, RoleMapperModel, ScopeMa
public void setAutomaticRegistrationAfterSocialLogin(boolean automaticRegistrationAfterSocialLogin); public void setAutomaticRegistrationAfterSocialLogin(boolean automaticRegistrationAfterSocialLogin);
List<UserModel> searchForUser(String search);
List<UserModel> searchForUserByAttributes(Map<String, String> attributes); List<UserModel> searchForUserByAttributes(Map<String, String> attributes);
OAuthClientModel addOAuthClient(String name); OAuthClientModel addOAuthClient(String name);

View file

@ -675,6 +675,16 @@ public class RealmAdapter implements RealmModel {
em.flush(); em.flush();
} }
@Override
public List<UserModel> searchForUser(String search) {
TypedQuery<UserEntity> 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<UserEntity> results = query.getResultList();
List<UserModel> users = new ArrayList<UserModel>();
for (UserEntity entity : results) users.add(new UserAdapter(entity));
return users;
}
@Override @Override
public List<UserModel> searchForUserByAttributes(Map<String, String> attributes) { public List<UserModel> searchForUserByAttributes(Map<String, String> attributes) {
StringBuilder builder = new StringBuilder("select u from UserEntity u"); StringBuilder builder = new StringBuilder("select u from UserEntity u");

View file

@ -36,6 +36,7 @@ import org.picketlink.idm.model.sample.Role;
import org.picketlink.idm.model.sample.SampleModel; import org.picketlink.idm.model.sample.SampleModel;
import org.picketlink.idm.model.sample.User; import org.picketlink.idm.model.sample.User;
import org.picketlink.idm.query.IdentityQuery; import org.picketlink.idm.query.IdentityQuery;
import org.picketlink.idm.query.QueryParameter;
import org.picketlink.idm.query.RelationshipQuery; import org.picketlink.idm.query.RelationshipQuery;
import java.io.IOException; import java.io.IOException;
@ -876,6 +877,26 @@ public class RealmAdapter implements RealmModel {
getRelationshipManager().remove(relationship); getRelationshipManager().remove(relationship);
} }
@Override
public List<UserModel> searchForUser(String search) {
QueryParameter[] params = new QueryParameter[] { User.LOGIN_NAME, User.FIRST_NAME, User.LAST_NAME, User.EMAIL };
Map<String, User> users = new HashMap<String, User>();
for (QueryParameter p : params) {
IdentityQuery<User> query = getIdm().createIdentityQuery(User.class);
query.setParameter(p, search.toLowerCase());
for (User u : query.getResultList()) {
users.put(u.getLoginName(), u);
}
}
List<UserModel> userModels = new ArrayList<UserModel>();
for (User user : users.values()) {
userModels.add(new UserAdapter(user, idm));
}
return userModels;
}
@Override @Override
public List<UserModel> searchForUserByAttributes(Map<String, String> attributes) { public List<UserModel> searchForUserByAttributes(Map<String, String> attributes) {
IdentityQuery<User> query = getIdm().createIdentityQuery(User.class); IdentityQuery<User> query = getIdm().createIdentityQuery(User.class);

View file

@ -350,49 +350,7 @@ public class RealmManager {
if (searchString == null) { if (searchString == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
return realmModel.searchForUser(searchString.trim());
String search = searchString.trim();
if (search.contains(" ")) { //first and last name
String[] split = search.split(" ");
if (split.length != 2) {
return Collections.emptyList();
}
Map<String, String> attributes = new HashMap<String, String>();
attributes.put(UserModel.FIRST_NAME, split[0]);
attributes.put(UserModel.LAST_NAME, split[1]);
return realmModel.searchForUserByAttributes(attributes);
} else if (search.contains("@")) { // email
Map<String, String> attributes = new HashMap<String, String>();
attributes.put(UserModel.EMAIL, search);
return realmModel.searchForUserByAttributes(attributes);
} else { // username and lastname
Map<String, String> attributes = new HashMap<String, String>();
attributes.put(UserModel.LOGIN_NAME, search);
List<UserModel> usernameQuery = realmModel.searchForUserByAttributes(attributes);
attributes.clear();
attributes.put(UserModel.LAST_NAME, search);
List<UserModel> lastnameQuery = realmModel.searchForUserByAttributes(attributes);
if (usernameQuery.size() == 0) {
return lastnameQuery;
} else if (lastnameQuery.size() == 0) {
return usernameQuery;
}
List<UserModel> results = new ArrayList<UserModel>();
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;
}
} }
public void addRequiredCredential(RealmModel newRealm, String requiredCred) { public void addRequiredCredential(RealmModel newRealm, String requiredCred) {

View file

@ -20,6 +20,8 @@ import org.keycloak.services.managers.RealmManager;
import org.keycloak.test.common.AbstractKeycloakTest; import org.keycloak.test.common.AbstractKeycloakTest;
import org.keycloak.test.common.SessionFactoryTestContext; import org.keycloak.test.common.SessionFactoryTestContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -233,6 +235,16 @@ public class AdapterTest extends AbstractKeycloakTest {
user.setLastName("Burke"); user.setLastName("Burke");
user.setFirstName("Bill"); user.setFirstName("Bill");
user.setEmail("bburke@redhat.com"); 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(); RealmManager adapter = getRealmManager();
@ -252,7 +264,7 @@ public class AdapterTest extends AbstractKeycloakTest {
} }
{ {
List<UserModel> userModels = adapter.searchUsers("bil burk", realmModel); List<UserModel> userModels = adapter.searchUsers("bill burk", realmModel);
Assert.assertEquals(userModels.size(), 1); Assert.assertEquals(userModels.size(), 1);
UserModel bburke = userModels.get(0); UserModel bburke = userModels.get(0);
Assert.assertEquals(bburke.getFirstName(), "Bill"); Assert.assertEquals(bburke.getFirstName(), "Bill");
@ -260,6 +272,15 @@ public class AdapterTest extends AbstractKeycloakTest {
Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com"); Assert.assertEquals(bburke.getEmail(), "bburke@redhat.com");
} }
{
ArrayList<String> users = new ArrayList<String>();
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<UserModel> userModels = adapter.searchUsers("bburke@redhat.com", realmModel); List<UserModel> userModels = adapter.searchUsers("bburke@redhat.com", realmModel);