diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/GroupEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/GroupEntity.java index 1f7dc09796..7215df6a6e 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/GroupEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/GroupEntity.java @@ -30,7 +30,7 @@ import java.util.Collection; @NamedQuery(name="getGroupIdsByNameContaining", query="select u.id from GroupEntity u where u.realm.id = :realm and u.name like concat('%',:search,'%') order by u.name ASC"), @NamedQuery(name="getTopLevelGroupIds", query="select u.id from GroupEntity u where u.parent is null and u.realm.id = :realm"), @NamedQuery(name="getGroupCount", query="select count(u) from GroupEntity u where u.realm.id = :realm"), - @NamedQuery(name="getGroupCountByNameContaining", query="select count(u) from GroupEntity u where u.realm.id = :realm and u.name like concat('%',:name,'%')") + @NamedQuery(name="getGroupCountByNameContaining", query="select count(u) from GroupEntity u where u.realm.id = :realm and u.name like concat('%',:name,'%')"), }) @Entity @Table(name="KEYCLOAK_GROUP") 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 e5b4676961..1ef8aff7ae 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 @@ -131,13 +131,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."); - } + List search = ModelToRepresentation.searchForGroupByName(realm, rep.getName(), 0, 1); + if (search != null && !search.isEmpty() && Objects.equals(search.get(0).getName(), rep.getName())) { + return ErrorResponse.exists("Top level group named '" + rep.getName() + "' already exists."); } - GroupModel child = null; + GroupModel child; Response.ResponseBuilder builder = Response.status(204); if (rep.getId() != null) { child = realm.getGroupById(rep.getId());