Skip to content

Commit

Permalink
honor "exact" and non brief description search in converting to ances…
Browse files Browse the repository at this point in the history
…try line
  • Loading branch information
eatikrh committed Aug 16, 2023
1 parent 8fe93d7 commit 952ee5c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ public Stream<GroupModel> searchForGroupByNameStream(RealmModel realm, String se

@Override
public Stream<GroupModel> searchForGroupByNameNoAncestryStream(RealmModel realm, String search, Boolean exact, Integer first, Integer max) {
return getGroupDelegate().searchForGroupByNameNoAncestryStream(realm, search, false, first, max);
return getGroupDelegate().searchForGroupByNameNoAncestryStream(realm, search, exact, first, max);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public final Stream<GroupRepresentation> listGroups(@QueryParam("search") @Defau
.filter(group -> canViewGlobal || groupsEvaluator.canView(group))
.skip(first).limit(max);
}
return GroupUtils.toAncestorsLine(groupsEvaluator, stream);
return GroupUtils.toAncestorsLine(groupsEvaluator, stream, false);
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public Stream<GroupRepresentation> getGroups(@QueryParam("search") String search
// We need to be creating a single line for the ancestry that is all parents and grandparents etc should just show one subgroup

stream = ModelToRepresentation.searchForGroupByNameNoAncestryStream(session, realm, !briefRepresentation, search.trim(), exact, firstResult, maxResults);
return GroupUtils.toAncestorsLine(groupsEvaluator, stream);
return GroupUtils.toAncestorsLine(groupsEvaluator, stream, !briefRepresentation);

} else if(Objects.nonNull(firstResult) && Objects.nonNull(maxResults)) {
// briefRepresentation is ignored top level groups are returned, this is not a search since search and searchQuery and search cases handled above
Expand Down
8 changes: 4 additions & 4 deletions services/src/main/java/org/keycloak/utils/GroupUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ private static boolean groupMatchesSearchOrIsPathElement(GroupModel group, Strin
return group.getSubGroupsStream().findAny().isPresent();
}

public static Stream<GroupRepresentation> toAncestorsLine(GroupPermissionEvaluator groupsEvaluator, Stream<GroupModel> stream) {
public static Stream<GroupRepresentation> toAncestorsLine(GroupPermissionEvaluator groupsEvaluator, Stream<GroupModel> stream, boolean full) {
List<GroupRepresentationExtended> tree = new ArrayList<>();
HashMap<String,GroupRepresentationExtended> groupMap = new HashMap<>();

stream.forEach(g -> {
getAncestryStream(groupsEvaluator, g).forEach(group -> {
getAncestryStream(groupsEvaluator, g, full).forEach(group -> {
GroupRepresentationExtended alreadyProcessedGroup = groupMap.get( group.getGroupRep().getId());
String parentId = group.getParentId();
if (parentId == null) {
Expand Down Expand Up @@ -113,12 +113,12 @@ public String getParentId() {
}

}
private static Stream<GroupRepresentationExtended> getAncestryStream(GroupPermissionEvaluator groupsEvaluator, GroupModel group) {
private static Stream<GroupRepresentationExtended> getAncestryStream(GroupPermissionEvaluator groupsEvaluator, GroupModel group, boolean full) {
List<GroupRepresentationExtended> groupsList = new ArrayList<>();
GroupModel currentGroup = group;
while (currentGroup != null) {
Map<String, Boolean> access = groupsEvaluator.getAccess(currentGroup);
GroupRepresentation groupRepresentation = ModelToRepresentation.toRepresentation(currentGroup, false);
GroupRepresentation groupRepresentation = ModelToRepresentation.toRepresentation(currentGroup, full);
groupRepresentation.setAccess(access);
groupsList.add(new GroupRepresentationExtended(groupRepresentation, currentGroup.getParentId()));
currentGroup = currentGroup.getParent();;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,14 @@ public void searchAndCountGroups() throws Exception {
assertNotNull(group0);
assertEquals(2,group0.getSubGroups().size());
assertThat(group0.getSubGroups().stream().map(GroupRepresentation::getName).collect(Collectors.toList()), Matchers.containsInAnyOrder("group1111", "group111111"));
assertEquals(new Long(search.size()), realm.groups().count("group11").get("count"));
// This should match /group0/group1111, /group0/group111111 and /group11 since this is not an exact match query
// search = realm.groups().groups("group11",0,10) returns the top level groups with some matches in subgroups: it returns group0 and group11
// below we are counting matches individually
Long actualCount = realm.groups().count("group11").get("count");
Long expectedCount = search.size()
+ search.stream().filter(t -> t.getSubGroups() != null).flatMap(t -> t.getSubGroups().stream()).filter(g -> g.getName().contains("group11")).count()
- search.stream().filter(g -> !g.getName().contains("group11") && g.getSubGroups().size() > 0).count();
assertEquals(expectedCount, actualCount);
}

@Test
Expand Down

0 comments on commit 952ee5c

Please sign in to comment.