Filter subgroups before paginating

Closes #27512

Signed-off-by: Peter Keuter <github@peterkeuter.nl>
This commit is contained in:
Peter Keuter 2024-03-15 10:57:57 +01:00 committed by GitHub
parent 7ffba27336
commit e26a261e4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 6 deletions

View file

@ -123,7 +123,7 @@ public interface GroupModel extends RoleMapperModel {
* @return Stream of {@link GroupModel}. Never returns {@code null}.
*/
default Stream<GroupModel> getSubGroupsStream(String search, Boolean exact, Integer firstResult, Integer maxResults) {
Stream<GroupModel> allSubgorupsGroups = getSubGroupsStream().filter(group -> {
Stream<GroupModel> allSubgroupsGroups = getSubGroupsStream().filter(group -> {
if (search == null || search.isEmpty()) return true;
if (Boolean.TRUE.equals(exact)) {
return group.getName().equals(search);
@ -134,14 +134,14 @@ public interface GroupModel extends RoleMapperModel {
// Copied over from StreamsUtil from server-spi-private which is not available here
if (firstResult != null && firstResult > 0) {
allSubgorupsGroups = allSubgorupsGroups.skip(firstResult);
allSubgroupsGroups = allSubgroupsGroups.skip(firstResult);
}
if (maxResults != null && maxResults >= 0) {
allSubgorupsGroups = allSubgorupsGroups.limit(maxResults);
allSubgroupsGroups = allSubgroupsGroups.limit(maxResults);
}
return allSubgorupsGroups;
return allSubgroupsGroups;
}
/**

View file

@ -61,6 +61,8 @@ import java.util.Set;
import java.util.stream.Stream;
import org.keycloak.utils.GroupUtils;
import static org.keycloak.utils.StreamsUtil.paginatedStream;
/**
* @resource Groups
* @author Bill Burke
@ -162,8 +164,9 @@ public class GroupResource {
@QueryParam("briefRepresentation") @DefaultValue("false") Boolean briefRepresentation) {
this.auth.groups().requireView(group);
boolean canViewGlobal = auth.groups().canView();
return group.getSubGroupsStream(first, max)
.filter(g -> canViewGlobal || auth.groups().canView(g))
return paginatedStream(
group.getSubGroupsStream()
.filter(g -> canViewGlobal || auth.groups().canView(g)), first, max)
.map(g -> GroupUtils.populateSubGroupCount(g, GroupUtils.toRepresentation(auth.groups(), g, !briefRepresentation)));
}