KEYCLOAK-17263 Add exact searching for users (#8059)

Co-authored-by: Stian Thorgersen <stianst@gmail.com>
This commit is contained in:
Sergey Ch 2022-09-01 23:27:24 +06:00 committed by GitHub
parent 43623ea9d0
commit 860c3fbbd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 1 deletions

View file

@ -143,6 +143,34 @@ public interface UsersResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
List<UserRepresentation> searchByLastName(@QueryParam("lastName") String email, @QueryParam("exact") Boolean exact); List<UserRepresentation> searchByLastName(@QueryParam("lastName") String email, @QueryParam("exact") Boolean exact);
/**
* Search for users based on the given filters.
*
* @param username a value contained in username
* @param firstName a value contained in first name
* @param lastName a value contained in last name
* @param email a value contained in email
* @param firstResult the position of the first result to retrieve
* @param maxResults the maximum number of results to retrieve
* @param enabled only return enabled or disabled users
* @param briefRepresentation Only return basic information (only guaranteed to return id, username, created, first
* and last name, email, enabled state, email verification state, federation link, and access.
* Note that it means that namely user attributes, required actions, and not before are not returned.)
* @param exact search with exact matching by filters (username, email, firstName, lastName)
* @return a list of {@link UserRepresentation}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
List<UserRepresentation> search(@QueryParam("username") String username,
@QueryParam("firstName") String firstName,
@QueryParam("lastName") String lastName,
@QueryParam("email") String email,
@QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults,
@QueryParam("enabled") Boolean enabled,
@QueryParam("briefRepresentation") Boolean briefRepresentation,
@QueryParam("exact") Boolean exact);
/** /**
* Search for users whose username or email matches the value provided by {@code search}. The {@code search} * Search for users whose username or email matches the value provided by {@code search}. The {@code search}
* argument also allows finding users by specific attributes as follows: * argument also allows finding users by specific attributes as follows:

View file

@ -1134,6 +1134,88 @@ public class UserTest extends AbstractAdminTest {
assertThat(userIds.get(0), equalTo(users.get(0).getId())); assertThat(userIds.get(0), equalTo(users.get(0).getId()));
} }
@Test
public void searchWithExactMatch() {
UserRepresentation user = new UserRepresentation();
user.setUsername("test_username");
user.setFirstName("test_first_name");
user.setLastName("test_last_name");
user.setEmail("test_email@test.com");
user.setEnabled(true);
user.setEmailVerified(true);
createUser(user);
UserRepresentation user2 = new UserRepresentation();
user2.setUsername("test_username2");
user2.setFirstName("test_first_name2");
user2.setLastName("test_last_name");
user2.setEmail("test_email@test.com2");
user2.setEnabled(true);
user2.setEmailVerified(true);
createUser(user2);
UserRepresentation user3 = new UserRepresentation();
user3.setUsername("test_username3");
user3.setFirstName("test_first_name");
user3.setLastName("test_last_name3");
user3.setEmail("test_email@test.com3");
user3.setEnabled(true);
user3.setEmailVerified(true);
createUser(user3);
List<UserRepresentation> users = realm.users().search(
null, null, null, "test_email@test.co",
0, 10, null, null, true
);
assertEquals(0, users.size());
users = realm.users().search(
null, null, null, "test_email@test.com",
0, 10, null, null, true
);
assertEquals(1, users.size());
users = realm.users().search(
null, null, "test_last", "test_email@test.com",
0, 10, null, null, true
);
assertEquals(0, users.size());
users = realm.users().search(
null, null, "test_last_name", "test_email@test.com",
0, 10, null, null, true
);
assertEquals(1, users.size());
users = realm.users().search(
null, "test_first", "test_last_name", "test_email@test.com",
0, 10, null, null, true
);
assertEquals(0, users.size());
users = realm.users().search(
null, "test_first_name", "test_last_name", "test_email@test.com",
0, 10, null, null, true
);
assertEquals(1, users.size());
users = realm.users().search(
"test_usernam", "test_first_name", "test_last_name", "test_email@test.com",
0, 10, null, null, true
);
assertEquals(0, users.size());
users = realm.users().search(
"test_username", "test_first_name", "test_last_name", "test_email@test.com",
0, 10, null, null, true
);
assertEquals(1, users.size());
users = realm.users().search(
null, null, "test_last_name", null,
0, 10, null, null, true
);
assertEquals(2, users.size());
users = realm.users().search(
null, "test_first_name", null, null,
0, 10, null, null, true
);
assertEquals(2, users.size());
}
@Test @Test
public void countUsersNotServiceAccount() { public void countUsersNotServiceAccount() {
createUsers(); createUsers();