From facdd586a33d486bf565fd62223f4bce18162087 Mon Sep 17 00:00:00 2001 From: Stan Silvert Date: Tue, 1 Nov 2016 16:08:30 -0400 Subject: [PATCH] KEYCLOAK-2720: Should not allow two groups with the same path. --- .../keycloak/services/resources/admin/GroupResource.java | 7 +++++++ .../keycloak/services/resources/admin/GroupsResource.java | 7 +++++++ 2 files changed, 14 insertions(+) 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 2d5476b613..5088c93893 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 @@ -48,6 +48,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import org.keycloak.services.ErrorResponse; /** * @author Bill Burke @@ -138,6 +139,12 @@ public class GroupResource { if (group == null) { throw new NotFoundException("Could not find group by id"); } + + for (GroupModel group : group.getSubGroups()) { + if (group.getName().equals(rep.getName())) { + return ErrorResponse.exists("Parent already contains subgroup named '" + rep.getName() + "'"); + } + } Response.ResponseBuilder builder = Response.status(204); GroupModel child = null; diff --git a/services/src/main/java/org/keycloak/services/resources/admin/GroupsResource.java b/services/src/main/java/org/keycloak/services/resources/admin/GroupsResource.java index f670f57413..9d01f35ab1 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/GroupsResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/GroupsResource.java @@ -39,6 +39,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import java.net.URI; import java.util.List; +import org.keycloak.services.ErrorResponse; /** * @author Bill Burke @@ -102,6 +103,12 @@ public class GroupsResource { public Response addTopLevelGroup(GroupRepresentation rep) { auth.requireManage(); + for (GroupModel group : realm.getGroups()) { + if (group.getName().equals(rep.getName())) { + return ErrorResponse.exists("Top level group named '" + rep.getName() + "' already exists."); + } + } + GroupModel child = null; Response.ResponseBuilder builder = Response.status(204); if (rep.getId() != null) {