Skip to content

Commit

Permalink
feat: Allow to apply a Template on a Space - MEED-7735 - Meeds-io/MIP…
Browse files Browse the repository at this point in the history
…s#160 (#4171)

This change will allow to a platform administrator to apply a template's
characteristics on a chosen space.
  • Loading branch information
boubaker committed Nov 14, 2024
1 parent f5577b0 commit d70fbeb
Show file tree
Hide file tree
Showing 28 changed files with 986 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.social.space.administration.model;
package io.meeds.social.space.model;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.social.space.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SpaceTemplatePatch {

private long templateId;

private boolean accessRules;

private boolean editorialMode;

private boolean layoutPermissions;

private boolean publicSitePermissions;

private boolean deletePermissions;

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.meeds.social.space.administration.service;
package io.meeds.social.space.service;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.social.core.space.model.Space;

import io.meeds.social.space.administration.model.SpacePermissions;
import io.meeds.social.space.model.SpacePermissions;
import io.meeds.social.space.model.SpaceTemplatePatch;

public interface SpaceAdministrationService {

Expand All @@ -23,4 +24,11 @@ public interface SpaceAdministrationService {
*/
void updateSpacePermissions(long spaceId, SpacePermissions permissions) throws ObjectNotFoundException;

/**
* @param spaceId {@link Space} technical id
* @param templatePatch SpaceTemplate properties to apply
* @throws ObjectNotFoundException when the space doesn't exist
*/
void applySpaceTemplate(long spaceId, SpaceTemplatePatch templatePatch) throws ObjectNotFoundException;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.social.space.service;

import static org.exoplatform.social.core.space.SpaceUtils.setPermissionsFromTemplate;

import java.util.Arrays;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceService;

import io.meeds.social.space.model.SpacePermissions;
import io.meeds.social.space.model.SpaceTemplatePatch;
import io.meeds.social.space.template.model.SpaceTemplate;
import io.meeds.social.space.template.service.SpaceTemplateService;

import lombok.Setter;

@Service
public class SpaceAdministrationServiceImpl implements SpaceAdministrationService {

private static final String SPACE_NOT_FOUND_MESSAGE = "Space with id %s not found";

private static final String SPACE_TEMPLATE_NOT_FOUND_MESSAGE = "Space Template with id %s not found";

@Setter
@Autowired
private SpaceService spaceService;

@Setter
@Autowired
private SpaceTemplateService spaceTemplateService;

@Setter
@Autowired
private SpaceLayoutService spaceLayoutService;

@Override
public SpacePermissions getSpacePermissions(long spaceId) throws ObjectNotFoundException {
Space space = spaceService.getSpaceById(spaceId);
if (space == null) {
throw new ObjectNotFoundException(String.format(SPACE_NOT_FOUND_MESSAGE, spaceId));
}
return new SpacePermissions(space.getLayoutPermissions(),
space.getPublicSitePermissions(),
space.getDeletePermissions());
}

@Override
public void updateSpacePermissions(long spaceId, SpacePermissions permissions) throws ObjectNotFoundException {
Space space = spaceService.getSpaceById(spaceId);
if (space == null) {
throw new ObjectNotFoundException(String.format(SPACE_NOT_FOUND_MESSAGE, spaceId));
}
space.setLayoutPermissions(permissions.getLayoutPermissions());
space.setPublicSitePermissions(permissions.getPublicSitePermissions());
space.setDeletePermissions(permissions.getDeletePermissions());
spaceService.updateSpace(space);
}

@Override
public void applySpaceTemplate(long spaceId, SpaceTemplatePatch templatePatch) throws ObjectNotFoundException {
Space space = spaceService.getSpaceById(spaceId);
if (space == null) {
throw new ObjectNotFoundException(String.format(SPACE_NOT_FOUND_MESSAGE, spaceId));
}
SpaceTemplate spaceTemplate = spaceTemplateService.getSpaceTemplate(templatePatch.getTemplateId());
if (spaceTemplate == null || spaceTemplate.isDeleted() || !spaceTemplate.isEnabled()) {
throw new ObjectNotFoundException(String.format(SPACE_TEMPLATE_NOT_FOUND_MESSAGE, templatePatch.getTemplateId()));
}
space.setTemplateId(templatePatch.getTemplateId());
if (templatePatch.isAccessRules()) {
space.setRegistration(spaceTemplate.getSpaceDefaultRegistration().name().toLowerCase());
space.setVisibility(spaceTemplate.getSpaceDefaultVisibility().name().toLowerCase());
}
if (templatePatch.isDeletePermissions()) {
setPermissionsFromTemplate(spaceTemplate::getSpaceDeletePermissions,
space::setDeletePermissions,
space.getGroupId());
}
if (templatePatch.isLayoutPermissions()) {
setPermissionsFromTemplate(spaceTemplate::getSpaceLayoutPermissions,
space::setLayoutPermissions,
space.getGroupId());
}
if (templatePatch.isPublicSitePermissions()) {
setPermissionsFromTemplate(spaceTemplate::getSpacePublicSitePermissions,
space::setPublicSitePermissions,
space.getGroupId());
}
spaceService.updateSpace(space);
if (templatePatch.isEditorialMode()) {
if (spaceTemplate.isSpaceAllowContentCreation() && ArrayUtils.isEmpty(space.getRedactors())) {
Arrays.stream(space.getManagers()).forEach(m -> spaceService.addRedactor(space, m));
} else if (!spaceTemplate.isSpaceAllowContentCreation() && ArrayUtils.isNotEmpty(space.getRedactors())) {
Arrays.stream(space.getRedactors()).forEach(m -> spaceService.removeRedactor(space, m));
}
}
spaceLayoutService.updateSpaceSite(space);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ public void createSpaceSite(Space space) throws ObjectNotFoundException {
spaceService.updateSpace(space);
}

/**
* Updates {@link Space} site switch designated templateId characteristics
*
* @param space
* @throws ObjectNotFoundException
*/
public void updateSpaceSite(Space space) throws ObjectNotFoundException {
SiteKey spaceSiteKey = SiteKey.group(space.getGroupId());
PortalConfig portalConfig = layoutService.getPortalConfig(spaceSiteKey);
if (portalConfig != null) {
navigationService.destroyNavigation(spaceSiteKey);
layoutService.removePages(spaceSiteKey);
layoutService.remove(portalConfig);
}
createSpaceSite(space);
}

/**
* Saves the space public site characteristics
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import static org.exoplatform.social.core.space.SpaceUtils.PLATFORM_PUBLISHER_GROUP;
import static org.exoplatform.social.core.space.SpaceUtils.PLATFORM_USERS_GROUP;
import static org.exoplatform.social.core.space.SpaceUtils.PUBLISHER;
import static org.exoplatform.social.core.space.SpaceUtils.SPACE_ADMIN_REFERENCE_NAME;
import static org.exoplatform.social.core.space.SpaceUtils.addUserToGroupWithManagerMembership;
import static org.exoplatform.social.core.space.SpaceUtils.addUserToGroupWithMemberMembership;
import static org.exoplatform.social.core.space.SpaceUtils.addUserToGroupWithPublisherMembership;
Expand All @@ -41,6 +40,7 @@
import static org.exoplatform.social.core.space.SpaceUtils.removeUserFromGroupWithMemberMembership;
import static org.exoplatform.social.core.space.SpaceUtils.removeUserFromGroupWithPublisherMembership;
import static org.exoplatform.social.core.space.SpaceUtils.removeUserFromGroupWithRedactorMembership;
import static org.exoplatform.social.core.space.SpaceUtils.setPermissionsFromTemplate;

import java.time.Instant;
import java.util.ArrayList;
Expand Down Expand Up @@ -101,31 +101,31 @@

public class SpaceServiceImpl implements SpaceService {

private static final Log LOG = ExoLogger.getLogger(SpaceServiceImpl.class);
private static final Log LOG = ExoLogger.getLogger(SpaceServiceImpl.class);

private static final int MAX_SPACE_NAME_LENGTH = 200;
private static final int MAX_SPACE_NAME_LENGTH = 200;

private SpaceStorage spaceStorage;
private SpaceStorage spaceStorage;

private SpaceSearchConnector spaceSearchConnector;
private SpaceSearchConnector spaceSearchConnector;

private GroupSpaceBindingStorage groupSpaceBindingStorage;
private GroupSpaceBindingStorage groupSpaceBindingStorage;

private IdentityManager identityManager;
private IdentityManager identityManager;

private UserACL userAcl;
private UserACL userAcl;

private ResourceBundleService resourceBundleService;
private ResourceBundleService resourceBundleService;

private LocaleConfigService localeConfigService;
private LocaleConfigService localeConfigService;

private OrganizationService organizationService;
private OrganizationService organizationService;

private SpaceTemplateService spaceTemplateService;
private SpaceTemplateService spaceTemplateService;

private FileService fileService;
private FileService fileService;

private SpaceLifecycle spaceLifeCycle = new SpaceLifecycle();
private SpaceLifecycle spaceLifeCycle = new SpaceLifecycle();

public SpaceServiceImpl(SpaceStorage spaceStorage, // NOSONAR
GroupSpaceBindingStorage groupSpaceBindingStorage,
Expand Down Expand Up @@ -1052,40 +1052,21 @@ private String createSpaceGroup(Space space, String username) throws SpaceExcept
}

private void setLayoutPermissions(Space space, SpaceTemplate spaceTemplate, String groupId) {
if (CollectionUtils.isEmpty(spaceTemplate.getSpaceLayoutPermissions())) {
space.setLayoutPermissions(Collections.emptyList());
} else {
space.setLayoutPermissions(spaceTemplate.getSpaceLayoutPermissions()
.stream()
.map(p -> computeSpacePermissionFromTemplate(p, groupId))
.toList());
}
setPermissionsFromTemplate(spaceTemplate::getSpaceLayoutPermissions,
space::setLayoutPermissions,
groupId);
}

private void setPublicSitePermissions(Space space, SpaceTemplate spaceTemplate, String groupId) {
if (CollectionUtils.isEmpty(spaceTemplate.getSpacePublicSitePermissions())) {
space.setPublicSitePermissions(Collections.emptyList());
} else {
space.setPublicSitePermissions(spaceTemplate.getSpacePublicSitePermissions()
.stream()
.map(p -> computeSpacePermissionFromTemplate(p, groupId))
.toList());
}
}

private void setDeletePermissions(Space space, SpaceTemplate spaceTemplate, String groupId) {
if (CollectionUtils.isEmpty(spaceTemplate.getSpaceDeletePermissions())) {
space.setDeletePermissions(Collections.emptyList());
} else {
space.setDeletePermissions(spaceTemplate.getSpaceDeletePermissions()
.stream()
.map(p -> computeSpacePermissionFromTemplate(p, groupId))
.toList());
}
setPermissionsFromTemplate(spaceTemplate::getSpacePublicSitePermissions,
space::setPublicSitePermissions,
groupId);
}

private String computeSpacePermissionFromTemplate(String p, String groupId) {
return SPACE_ADMIN_REFERENCE_NAME.equals(p) ? MANAGER + ":" + groupId : p;
public static void setDeletePermissions(Space space, SpaceTemplate spaceTemplate, String groupId) {
setPermissionsFromTemplate(spaceTemplate::getSpaceDeletePermissions,
space::setDeletePermissions,
groupId);
}

private String checkSpaceEditorPermissions(Space space) {
Expand All @@ -1097,8 +1078,10 @@ private String checkSpaceEditorPermissions(Space space) {
}

private boolean hasSpacePermission(Space space, List<String> permissions, String username) {
if (CollectionUtils.isEmpty(permissions)) {
if (space.getTemplateId() == 0 && CollectionUtils.isEmpty(permissions)) {
return canManageSpace(space, username);
} else if (CollectionUtils.isEmpty(permissions)) {
return isSuperManager(username);
} else {
org.exoplatform.services.security.Identity userIdentity = userAcl.getUserIdentity(username);
return isSuperManager(username)
Expand Down
Loading

0 comments on commit d70fbeb

Please sign in to comment.