Skip to content

Commit

Permalink
fix: Allow to an administrator to access Spaces with no template or d…
Browse files Browse the repository at this point in the history
…eleted 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.
  • Loading branch information
boubaker committed Dec 12, 2024
1 parent b2a19c5 commit d4c6951
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,7 +132,17 @@ public List<SpaceTemplate> getSpaceTemplates(SpaceTemplateFilter spaceTemplateFi

public List<Long> getManagingSpaceTemplates(String username) {
List<SpaceTemplate> spaceTemplates = spaceTemplateStorage.getSpaceTemplates(Pageable.unpaged());
return spaceTemplates.stream().filter(t -> canManageSpacesWithTemplate(t, username)).map(SpaceTemplate::getId).toList();
List<Long> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Space> 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();

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -2403,7 +2434,7 @@ public void testGetCommonSpaces() throws Exception {
spaceService.addMember(space4, OTHER_USER_NAME);
spaceService.addMember(space5, OTHER_USER_NAME);

ListAccess<Space> resultListCommonSpacesAccessList1 = spaceService.getCommonSpaces(ROOT_NAME, OTHER_USER_NAME);
ListAccess<Space> resultListCommonSpacesAccessList1 = spaceService.getCommonSpaces(DEMO_NAME, OTHER_USER_NAME);
assertEquals(5, resultListCommonSpacesAccessList1.getSize());
Space[] spaceArray = resultListCommonSpacesAccessList1.load(0, 2);
assertEquals(2, spaceArray.length);
Expand Down

0 comments on commit d4c6951

Please sign in to comment.