KEYCLOAK-658 Fix searching for users

This commit is contained in:
Stian Thorgersen 2014-09-02 10:01:44 +02:00
parent 4f3a980f8b
commit 8c8c44e50f
4 changed files with 82 additions and 8 deletions

View file

@ -16,14 +16,18 @@ public interface UsersResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<UserRepresentation> search(@QueryParam("username") String username,
public List<UserRepresentation> search(@QueryParam("username") String username,
@QueryParam("firstName") String firstName,
@QueryParam("lastName") String lastName,
@QueryParam("email") String email);
@QueryParam("lastName") String lastName,
@QueryParam("email") String email,
@QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults);
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<UserRepresentation> search(@QueryParam("search") String search);
public List<UserRepresentation> search(@QueryParam("search") String search,
@QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults);
@POST
@Consumes(MediaType.APPLICATION_JSON)

View file

@ -193,7 +193,7 @@ public class UserFederationManager implements UserProvider {
protected List<UserModel> query(PaginatedQuery pagedQuery, RealmModel realm, int firstResult, int maxResults) {
List<UserModel> results = new LinkedList<UserModel>();
if (maxResults <= 0) return results;
if (maxResults == 0) return results;
int first = firstResult;
int max = maxResults;
do {
@ -210,6 +210,7 @@ public class UserFederationManager implements UserProvider {
if (query.size() < max) return results;
first = query.size();
max -= added;
if (max <= 0) return results;
} while (true);
}

View file

@ -376,9 +376,6 @@ public class UsersResource {
attributes.put(UserModel.USERNAME, username);
}
userModels = session.users().searchForUserByAttributes(attributes, realm, firstResult, maxResults);
for (UserModel user : userModels) {
results.add(ModelToRepresentation.toRepresentation(user));
}
} else {
userModels = session.users().getUsers(realm, firstResult, maxResults);
}

View file

@ -5,6 +5,8 @@ import org.keycloak.representations.idm.UserRepresentation;
import javax.ws.rs.ClientErrorException;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@ -36,4 +38,74 @@ public class UserTest extends AbstractClientTest {
}
}
private void createUsers() {
for (int i = 1; i < 10; i++) {
UserRepresentation user = new UserRepresentation();
user.setUsername("username" + i);
user.setEmail("user" + i + "@localhost");
user.setFirstName("First" + i);
user.setLastName("Last" + i);
realm.users().create(user);
}
}
@Test
public void searchByEmail() {
createUsers();
List<UserRepresentation> users = realm.users().search(null, null, null, "user1@localhost", null, null);
assertEquals(1, users.size());
users = realm.users().search(null, null, null, "@localhost", null, null);
assertEquals(9, users.size());
}
@Test
public void searchByUsername() {
createUsers();
List<UserRepresentation> users = realm.users().search("username1", null, null, null, null, null);
assertEquals(1, users.size());
users = realm.users().search("user", null, null, null, null, null);
assertEquals(9, users.size());
}
@Test
public void search() {
createUsers();
List<UserRepresentation> users = realm.users().search("username1", null, null);
assertEquals(1, users.size());
users = realm.users().search("first1", null, null);
assertEquals(1, users.size());
users = realm.users().search("last", null, null);
assertEquals(9, users.size());
}
@Test
public void searchPaginated() {
createUsers();
List<UserRepresentation> users = realm.users().search("username", 0, 1);
assertEquals(1, users.size());
assertEquals("username1", users.get(0).getUsername());
users = realm.users().search("username", 5, 2);
assertEquals(2, users.size());
assertEquals("username6", users.get(0).getUsername());
assertEquals("username7", users.get(1).getUsername());
users = realm.users().search("username", 7, 20);
assertEquals(2, users.size());
assertEquals("username8", users.get(0).getUsername());
assertEquals("username9", users.get(1).getUsername());
users = realm.users().search("username", 0, 20);
assertEquals(9, users.size());
}
}