diff --git a/services/src/main/java/org/keycloak/services/resources/admin/GroupResource.java b/services/src/main/java/org/keycloak/services/resources/admin/GroupResource.java index 576f3ee2f821..449ecdcd61c1 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/GroupResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/GroupResource.java @@ -120,8 +120,14 @@ public Response updateGroup(GroupRepresentation rep) { } if (!Objects.equals(groupName, group.getName())) { - boolean exists = siblings().filter(s -> !Objects.equals(s.getId(), group.getId())) - .anyMatch(s -> Objects.equals(s.getName(), groupName)); + boolean exists; + if (group.getParentId() == null) { + exists = session.groups().searchForGroupByNameStream(realm, groupName, true, null, null) + .anyMatch(s -> !Objects.equals(s.getId(), group.getId())); + } else { + exists = group.getParent().getSubGroupsStream().filter(s -> !Objects.equals(s.getId(), group.getId())) + .anyMatch(s -> Objects.equals(s.getName(), groupName)); + } if (exists) { throw ErrorResponse.exists("Sibling group named '" + groupName + "' already exists."); } @@ -133,14 +139,6 @@ public Response updateGroup(GroupRepresentation rep) { return Response.noContent().build(); } - private Stream siblings() { - if (group.getParentId() == null) { - return session.groups().getTopLevelGroupsStream(realm); - } else { - return group.getParent().getSubGroupsStream(); - } - } - @DELETE @Tag(name = KeycloakOpenAPI.Admin.Tags.GROUPS) @Operation()