From d4c6951114f7b59d40663707eea1171102cea127 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Thu, 12 Dec 2024 08:17:30 +0100 Subject: [PATCH] fix: Allow to an administrator to access Spaces with no template or deleted template - MEED-7938 - Meeds-io/meeds#2668 (#4270) Prior to this change, when having a space without an associated active/enabled template and which is have 'Hidden' visibility, then the Space isn't listed in spaces Administration UI. This change ensures to let Platform Administrators to access the list of Spaces they manage even when it doesn't have an associated template. --- .../social/search/SpaceSearchConnector.java | 3 ++ .../service/SpaceTemplateService.java | 13 ++++++- .../core/jpa/storage/dao/jpa/SpaceDAO.java | 4 +-- .../core/space/spi/SpaceServiceTest.java | 35 +++++++++++++++++-- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/component/core/src/main/java/io/meeds/social/search/SpaceSearchConnector.java b/component/core/src/main/java/io/meeds/social/search/SpaceSearchConnector.java index 29d6bf93d36..4b1595d6257 100644 --- a/component/core/src/main/java/io/meeds/social/search/SpaceSearchConnector.java +++ b/component/core/src/main/java/io/meeds/social/search/SpaceSearchConnector.java @@ -433,6 +433,9 @@ private String buildTermQueryStatement(String phrase) { } private String buildPermissionsQuery(SpaceSearchFilter filter) { + if (CollectionUtils.isNotEmpty(filter.getManagingTemplateIds()) && filter.getManagingTemplateIds().contains(0l)) { + return StringUtils.EMPTY; + } String permissionField = getPermissionField(filter); return PERMISSIONS_QUERY.replace(PERMISSIONS_FIELD_REPLACEMENT, diff --git a/component/core/src/main/java/io/meeds/social/space/template/service/SpaceTemplateService.java b/component/core/src/main/java/io/meeds/social/space/template/service/SpaceTemplateService.java index 26d49dce619..7947ea60720 100644 --- a/component/core/src/main/java/io/meeds/social/space/template/service/SpaceTemplateService.java +++ b/component/core/src/main/java/io/meeds/social/space/template/service/SpaceTemplateService.java @@ -18,6 +18,7 @@ */ package io.meeds.social.space.template.service; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -131,7 +132,17 @@ public List getSpaceTemplates(SpaceTemplateFilter spaceTemplateFi public List getManagingSpaceTemplates(String username) { List spaceTemplates = spaceTemplateStorage.getSpaceTemplates(Pageable.unpaged()); - return spaceTemplates.stream().filter(t -> canManageSpacesWithTemplate(t, username)).map(SpaceTemplate::getId).toList(); + List spaceTemplateIds = spaceTemplates.stream() + .filter(t -> canManageSpacesWithTemplate(t, username)) + .map(SpaceTemplate::getId) + .toList(); + if (canManageTemplates(username)) { + spaceTemplateIds = new ArrayList<>(spaceTemplateIds); + // Include spaces not having an associated template Id + // which should be visible to an administrator + spaceTemplateIds.add(0L); + } + return spaceTemplateIds; } public SpaceTemplate getSpaceTemplate(long templateId) { diff --git a/component/core/src/main/java/org/exoplatform/social/core/jpa/storage/dao/jpa/SpaceDAO.java b/component/core/src/main/java/org/exoplatform/social/core/jpa/storage/dao/jpa/SpaceDAO.java index e5308d3f45b..f097dde2365 100644 --- a/component/core/src/main/java/org/exoplatform/social/core/jpa/storage/dao/jpa/SpaceDAO.java +++ b/component/core/src/main/java/org/exoplatform/social/core/jpa/storage/dao/jpa/SpaceDAO.java @@ -369,7 +369,7 @@ private void buildPermissionPredicates(XSpaceFilter spaceFilter, // NOSONAR parameterNames.add(PARAM_HIDDEN_VISIBILITY); parameterNames.add(PARAM_USER_ID); parameterNames.add(PARAM_STATUSES); - } else { + } else if (!spaceFilter.getManagingTemplateIds().contains(0l)) { suffixes.add("SpacePrivateOrStatusesOrManaging"); predicates.add("(s.visibility <> :hiddenVisibility OR s.templateId IN :managingTemplateIds OR (sm.userId = :userId AND sm.status IN :statuses))"); parameterNames.add(PARAM_HIDDEN_VISIBILITY); @@ -387,7 +387,7 @@ private void buildPermissionPredicates(XSpaceFilter spaceFilter, // NOSONAR predicates.add("sm.userId = :userId AND sm.status IN :statuses"); parameterNames.add(PARAM_USER_ID); parameterNames.add(PARAM_STATUSES); - } else { + } else if (!spaceFilter.getManagingTemplateIds().contains(0l)) { suffixes.add("SpaceWithStatusesOrManaging"); predicates.add("(s.templateId IN :managingTemplateIds OR (sm.userId = :userId AND sm.status IN :statuses))"); parameterNames.add(PARAM_USER_ID); diff --git a/component/core/src/test/java/org/exoplatform/social/core/space/spi/SpaceServiceTest.java b/component/core/src/test/java/org/exoplatform/social/core/space/spi/SpaceServiceTest.java index b90c104b83c..29917e7ec0a 100644 --- a/component/core/src/test/java/org/exoplatform/social/core/space/spi/SpaceServiceTest.java +++ b/component/core/src/test/java/org/exoplatform/social/core/space/spi/SpaceServiceTest.java @@ -400,6 +400,37 @@ public void testGetManagerSpacesWithSpaceTemplateAdmin() throws Exception { assertEquals(0, allSpaces.getSize()); } + public void testGetVisibleSpacesWithNoSpaceTemplateAsAdmin() throws Exception { + SpaceTemplate spaceTemplate = mockSpaceTemplate(); + + int count = 5; + Space firstSpace = null; + for (int i = 0; i < count; i++) { + Space space = this.getSpaceInstance(i); + if (i == 0) { + firstSpace = space; + spaceService.removeMember(space, TOM_NAME); + spaceTemplate.setAdminPermissions(Collections.singletonList(space.getGroupId())); + } else if (i % 2 == 0) { + space.setVisibility(Space.HIDDEN); + space.setTemplateId(spaceTemplate.getId()); + space = spaceService.updateSpace(space); + spaceService.removeMember(space, DEMO_NAME); + spaceService.removeMember(space, TOM_NAME); + } + } + ListAccess allSpaces = spaceService.getVisibleSpacesWithListAccess(ROOT_NAME, new SpaceFilter()); + assertEquals(count, allSpaces.getSize()); + + assertNotNull(firstSpace); + firstSpace.setTemplateId(0l); // NOSONAR + firstSpace.setVisibility(Space.HIDDEN); + spaceService.updateSpace(firstSpace); + + allSpaces = spaceService.getVisibleSpacesWithListAccess(ROOT_NAME, new SpaceFilter()); + assertEquals(count, allSpaces.getSize()); + } + public void testGetEditableSpacesWithSpaceTemplateAdmin() throws Exception { SpaceTemplate spaceTemplate = mockSpaceTemplate(); @@ -2316,7 +2347,7 @@ private Space getSpaceInstance(int number) { String[] members = new String[] { DEMO_NAME, RAUL_NAME, GHOST_NAME, DRAGON_NAME }; String[] invitedUsers = new String[] { REGISTER1_NAME, MARY_NAME }; String[] pendingUsers = new String[] { JAME_NAME, PAUL_NAME, HACKER_NAME }; - Space createdSpace = this.spaceService.createSpace(space, ROOT_NAME); + Space createdSpace = this.spaceService.createSpace(space, DEMO_NAME); Arrays.stream(pendingUsers).forEach(u -> spaceService.addPendingUser(createdSpace, u)); Arrays.stream(invitedUsers).forEach(u -> spaceService.addInvitedUser(createdSpace, u)); Arrays.stream(members).forEach(u -> spaceService.addMember(createdSpace, u)); @@ -2403,7 +2434,7 @@ public void testGetCommonSpaces() throws Exception { spaceService.addMember(space4, OTHER_USER_NAME); spaceService.addMember(space5, OTHER_USER_NAME); - ListAccess resultListCommonSpacesAccessList1 = spaceService.getCommonSpaces(ROOT_NAME, OTHER_USER_NAME); + ListAccess resultListCommonSpacesAccessList1 = spaceService.getCommonSpaces(DEMO_NAME, OTHER_USER_NAME); assertEquals(5, resultListCommonSpacesAccessList1.getSize()); Space[] spaceArray = resultListCommonSpacesAccessList1.load(0, 2); assertEquals(2, spaceArray.length);