forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: closes keycloak#21095 * Added overloaded version of GroupUtils.toGroupHierarchy with additional full parameter.
- Loading branch information
Showing
4 changed files
with
96 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.keycloak.utils; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import org.keycloak.common.Profile; | ||
import org.keycloak.models.GroupModel; | ||
import org.keycloak.models.utils.ModelToRepresentation; | ||
import org.keycloak.representations.idm.GroupRepresentation; | ||
import org.keycloak.services.resources.admin.permissions.GroupPermissionEvaluator; | ||
|
||
public class GroupUtils { | ||
// Moved out from org.keycloak.admin.ui.rest.GroupsResource | ||
public static GroupRepresentation toGroupHierarchy(GroupPermissionEvaluator groupsEvaluator, GroupModel group, final String search, boolean exact) { | ||
return toGroupHierarchy(groupsEvaluator, group, search, exact, true); | ||
} | ||
|
||
public static GroupRepresentation toGroupHierarchy(GroupPermissionEvaluator groupsEvaluator, GroupModel group, final String search, boolean exact, boolean full) { | ||
GroupRepresentation rep = ModelToRepresentation.toRepresentation(group, full); | ||
rep.setSubGroups(group.getSubGroupsStream().filter(g -> | ||
groupMatchesSearchOrIsPathElement( | ||
g, search | ||
) | ||
).map(subGroup -> | ||
ModelToRepresentation.toGroupHierarchy( | ||
subGroup, full, search, exact | ||
) | ||
|
||
).collect(Collectors.toList())); | ||
|
||
if (Profile.isFeatureEnabled(Profile.Feature.ADMIN_FINE_GRAINED_AUTHZ)) { | ||
setAccess(groupsEvaluator, group, rep); | ||
} | ||
|
||
return rep; | ||
} | ||
|
||
//From org.keycloak.admin.ui.rest.GroupsResource | ||
// set fine-grained access for each group in the tree | ||
private static void setAccess(GroupPermissionEvaluator groupsEvaluator, GroupModel groupTree, GroupRepresentation rootGroup) { | ||
if (rootGroup == null) return; | ||
|
||
rootGroup.setAccess(groupsEvaluator.getAccess(groupTree)); | ||
|
||
rootGroup.getSubGroups().stream().forEach(subGroup -> { | ||
GroupModel foundGroupModel = groupTree.getSubGroupsStream().filter(g -> g.getId().equals(subGroup.getId())).findFirst().get(); | ||
setAccess(groupsEvaluator, foundGroupModel, subGroup); | ||
}); | ||
|
||
} | ||
|
||
private static boolean groupMatchesSearchOrIsPathElement(GroupModel group, String search) { | ||
if (StringUtil.isBlank(search)) { | ||
return true; | ||
} | ||
if (group.getName().contains(search)) { | ||
return true; | ||
} | ||
return group.getSubGroupsStream().findAny().isPresent(); | ||
} | ||
} |