Fix search user groups without limit

Closes #12649
This commit is contained in:
sui.jieqiang 2022-06-23 12:07:22 +08:00 committed by Marek Posolda
parent fbc9177f27
commit 1f6fa0501c
3 changed files with 45 additions and 5 deletions

View File

@ -74,7 +74,12 @@ public interface UserResource {
List<GroupRepresentation> groups(@QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults,
@QueryParam("briefRepresentation") @DefaultValue("true") boolean briefRepresentation);
@Path("groups")
@GET
List<GroupRepresentation> groups(@QueryParam("search") String search,
@QueryParam("briefRepresentation") @DefaultValue("true") boolean briefRepresentation);
@Path("groups")
@GET
List<GroupRepresentation> groups(@QueryParam("search") String search,

View File

@ -885,12 +885,10 @@ public class UserResource {
@QueryParam("briefRepresentation") @DefaultValue("true") boolean briefRepresentation) {
auth.users().requireView(user);
if (Objects.nonNull(search) && Objects.nonNull(firstResult) && Objects.nonNull(maxResults)) {
if (Objects.nonNull(search)) {
return ModelToRepresentation.searchForGroupByName(user, !briefRepresentation, search.trim(), firstResult, maxResults);
} else if(Objects.nonNull(firstResult) && Objects.nonNull(maxResults)) {
return ModelToRepresentation.toGroupHierarchy(user, !briefRepresentation, firstResult, maxResults);
} else {
return ModelToRepresentation.toGroupHierarchy(user, !briefRepresentation);
return ModelToRepresentation.toGroupHierarchy(user, !briefRepresentation, firstResult, maxResults);
}
}

View File

@ -105,6 +105,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.hamcrest.MatcherAssert.assertThat;
@ -2917,6 +2918,42 @@ public class UserTest extends AbstractAdminTest {
}
}
@Test
public void testGetSearchedGroupsForUserFullRepresentation() {
RealmResource realm = adminClient.realms().realm("test");
String userName = "averagejoe";
String groupName1 = "group1WithAttribute";
String groupName2 = "group2WithAttribute";
Map<String, List<String>> attributes1 = new HashMap<String, List<String>>();
attributes1.put("attribute1", Arrays.asList("attribute1"));
Map<String, List<String>> attributes2 = new HashMap<String, List<String>>();
attributes2.put("attribute2", Arrays.asList("attribute2"));
UserRepresentation userRepresentation = UserBuilder
.edit(createUserRepresentation(userName, "joe@average.com", "average", "joe", true))
.addPassword("password")
.build();
try (Creator<UserResource> u = Creator.create(realm, userRepresentation);
Creator<GroupResource> g1 = Creator.create(realm, GroupBuilder.create().name(groupName1).attributes(attributes1).build());
Creator<GroupResource> g2 = Creator.create(realm, GroupBuilder.create().name(groupName2).attributes(attributes2).build())) {
String group1Id = g1.id();
String group2Id = g2.id();
UserResource user = u.resource();
user.joinGroup(group1Id);
user.joinGroup(group2Id);
List<GroupRepresentation> userGroups = user.groups("group2", false);
assertFalse(userGroups.isEmpty());
assertTrue(userGroups.stream().collect(Collectors.toMap(GroupRepresentation::getName, Function.identity())).get(groupName2).getAttributes().containsKey("attribute2"));
userGroups = user.groups("group3", false);
assertTrue(userGroups.isEmpty());
}
}
@Test
public void groupMembershipPaginated() {
String userId = createUser(UserBuilder.create().username("user-a").build());