Skip to content

Commit

Permalink
feat: Allow to create a Group Template Site from Group Site - MEED-7736
Browse files Browse the repository at this point in the history
… - Meeds-io/MIPs#160 (#1000)

This change will update the Site Template Creation process to allow
creating a Site Template from a given Site by deleting at the same time
the permissions on Containers and Applications since the page permission
will be applied in higher 'layout' addon only.
  • Loading branch information
boubaker authored and exo-swf committed Nov 15, 2024
1 parent 7bf8414 commit 566f129
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

package org.exoplatform.upload;

import lombok.AllArgsConstructor;

/**
* Created by The eXo Platform SARL Author : Tuan Nguyen [email protected] Dec 26, 2005
*/
@AllArgsConstructor
public class UploadResource {

public static final int UPLOADING_STATUS = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public void createUploadResource(HttpServletRequest request) throws FileUploadEx
createUploadResource(uploadId, request);
}

public void createUploadResource(UploadResource uploadResource) {
uploadResources.put(uploadResource.getUploadId(), uploadResource);
}

public void createUploadResource(String uploadId, HttpServletRequest request) throws FileUploadException {
if (uploadId == null || !UPLOAD_ID_PATTERN.matcher(uploadId).matches()) {
throw new FileUploadException("Upload id " + uploadId + " is not valid, it cannot be null or contain '.' , '/' or '\\'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ public UserPortalConfig getUserPortalConfig(String portalName, String accessUser
return new UserPortalConfig(portal, this, portalName, accessUser, locale);
}

public void createSiteFromTemplate(SiteKey sourceSiteTemplate,
public void createSiteFromTemplate(SiteKey sourceSiteKey,
SiteKey targetSiteKey) throws ObjectNotFoundException {
createSiteFromTemplate(sourceSiteTemplate, targetSiteKey, null);
createSiteFromTemplate(sourceSiteKey, targetSiteKey, null);
}

public void createSiteFromTemplate(SiteKey sourceSiteTemplate,
public void createSiteFromTemplate(SiteKey sourceSiteKey,
SiteKey targetSiteKey,
String permission) throws ObjectNotFoundException {
layoutService.savePortalFromTemplate(sourceSiteTemplate, targetSiteKey, permission);
layoutService.savePagesFromTemplate(sourceSiteTemplate, targetSiteKey, permission);
navigationService.saveNavigationFromTemplate(sourceSiteTemplate, targetSiteKey);
listenerService.broadcast(SITE_TEMPLATE_INSTANTIATED, sourceSiteTemplate, targetSiteKey);
layoutService.savePortalFromTemplate(sourceSiteKey, targetSiteKey, permission);
layoutService.savePagesFromTemplate(sourceSiteKey, targetSiteKey, permission);
navigationService.saveNavigationFromTemplate(sourceSiteKey, targetSiteKey);
listenerService.broadcast(SITE_TEMPLATE_INSTANTIATED, sourceSiteKey, targetSiteKey);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ValueParam;
import org.exoplatform.portal.config.Query;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.portal.config.model.Application;
import org.exoplatform.portal.config.model.ApplicationState;
import org.exoplatform.portal.config.model.Container;
Expand Down Expand Up @@ -76,6 +77,8 @@

public class LayoutServiceImpl implements LayoutService {

private static final String OWNER_ID_TEMPLATE = "@owner_id@";

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

private static final String SITE_DEAULT_BANNER_PATH = "site.default.banner.path";
Expand Down Expand Up @@ -119,23 +122,27 @@ public void create(PortalConfig config) {
}

@Override
public void savePortalFromTemplate(SiteKey sourceSiteTemplate,
public void savePortalFromTemplate(SiteKey sourceSiteKey,
SiteKey targetSiteKey,
String permission) throws ObjectNotFoundException {
PortalConfig templatePortalConfig = getPortalConfig(sourceSiteTemplate);
PortalConfig templatePortalConfig = getPortalConfig(sourceSiteKey);
if (templatePortalConfig == null) {
throw new ObjectNotFoundException(String.format("Site template %s wasn't found", sourceSiteTemplate));
throw new ObjectNotFoundException(String.format("Site template %s wasn't found", sourceSiteKey));
}
boolean exists = getPortalConfig(targetSiteKey) != null;
PortalConfig portalConfig = templatePortalConfig.clone();
portalConfig.resetStorage();
portalConfig.setType(targetSiteKey.getTypeName());
portalConfig.setName(targetSiteKey.getName());
portalConfig.setBannerFileId(0);
applyPermissionTemplate(portalConfig.getPortalLayout());
if (StringUtils.isNotBlank(permission)) {
portalConfig.setAccessPermissions(getPermissionsFromTemplate(portalConfig.getAccessPermissions(), permission));
portalConfig.setEditPermission(getPermissionFromTemplate(portalConfig.getEditPermission(), permission));
applyPermissionTemplate(portalConfig.getPortalLayout(), permission);
} else if (StringUtils.equalsIgnoreCase(sourceSiteKey.getTypeName(), PortalConfig.GROUP_TYPE)) {
String groupId = sourceSiteKey.getName();
portalConfig.setAccessPermissions(getPermissionsToTemplate(portalConfig.getAccessPermissions(), groupId));
portalConfig.setEditPermission(getPermissionToTemplate(portalConfig.getEditPermission(), groupId));
}
if (exists) {
siteStorage.save(portalConfig.build());
Expand All @@ -157,18 +164,22 @@ public void savePagesFromTemplate(SiteKey sourceSiteTemplate,

@Override
@SneakyThrows
public void savePageFromTemplate(PageKey sourcePageTemplateKey,
public void savePageFromTemplate(PageKey sourcePageKey,
SiteKey targetSiteKey,
String permission) {
Page page = getPage(sourcePageTemplateKey);
Page page = getPage(sourcePageKey);
page = new Page(page.build());
page.resetStorage();
page.setOwnerType(targetSiteKey.getTypeName());
page.setOwnerId(targetSiteKey.getName());
applyPermissionTemplate(page.getChildren());
if (StringUtils.isNotBlank(permission)) {
page.setAccessPermissions(getPermissionsFromTemplate(page.getAccessPermissions(), permission));
page.setEditPermission(getPermissionFromTemplate(page.getEditPermission(), permission));
applyPermissionTemplate(page, permission);
} else if (StringUtils.equalsIgnoreCase(sourcePageKey.getSite().getTypeName(), PortalConfig.GROUP_TYPE)) {
String groupId = sourcePageKey.getSite().getName();
page.setAccessPermissions(getPermissionsToTemplate(page.getAccessPermissions(), groupId));
page.setEditPermission(getPermissionToTemplate(page.getEditPermission(), groupId));
}
pageStorage.savePage(new PageContext(page.getPageKey(), Utils.toPageState(page)));
pageStorage.save(page.build());
Expand Down Expand Up @@ -574,28 +585,46 @@ protected void broadcastEvent(String eventName, Object data) {
}
}

private void applyPermissionTemplate(ModelObject model, String permission) {
if (model instanceof Container container) {
container.setAccessPermissions(getPermissionsFromTemplate(container.getAccessPermissions(), permission));
if (CollectionUtils.isNotEmpty(container.getChildren())) {
container.getChildren().forEach(c -> applyPermissionTemplate(c, permission));
}
} else if (model instanceof Application application) {
application.setAccessPermissions(getPermissionsFromTemplate(application.getAccessPermissions(), permission));
private String[] getPermissionsFromTemplate(String[] accessPermissions, String permission) {
if (ArrayUtils.isEmpty(accessPermissions)) {
return accessPermissions;
}
return Arrays.stream(accessPermissions)
.map(p -> getPermissionFromTemplate(p, permission))
.toArray(String[]::new);
}

private String[] getPermissionsFromTemplate(String[] accessPermissions, String permission) {
private String[] getPermissionsToTemplate(String[] accessPermissions, String permission) {
if (ArrayUtils.isEmpty(accessPermissions)) {
return accessPermissions;
}
return Arrays.stream(accessPermissions)
.map(p -> getPermissionFromTemplate(p, permission))
.map(p -> getPermissionToTemplate(p, permission))
.toArray(String[]::new);
}

private String getPermissionToTemplate(String selectedPermission, String permission) {
return selectedPermission.replace(permission, OWNER_ID_TEMPLATE);
}

private String getPermissionFromTemplate(String selectedPermission, String permission) {
return selectedPermission.replace("@owner_id@", permission);
return selectedPermission.replace(OWNER_ID_TEMPLATE, permission);
}

private void applyPermissionTemplate(ModelObject model) {
if (model instanceof Container container) {
container.setAccessPermissions(new String[] {UserACL.EVERYONE});
ArrayList<ModelObject> children = container.getChildren();
applyPermissionTemplate(children);
} else if (model instanceof Application application) {
application.setAccessPermissions(new String[] {UserACL.EVERYONE});
}
}

private void applyPermissionTemplate(ArrayList<ModelObject> children) {
if (CollectionUtils.isNotEmpty(children)) {
children.forEach(this::applyPermissionTemplate);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void testCreateUserPortalConfigFromTemplate() {
assertEquals(2, portalConfig.getPortalLayout().getChildren().size());
assertEquals(Application.class, portalConfig.getPortalLayout().getChildren().get(0).getClass());
assertEquals(PageBody.class, portalConfig.getPortalLayout().getChildren().get(1).getClass());
assertEquals(accessPermission,
assertEquals(UserACL.EVERYONE,
((Application) portalConfig.getPortalLayout().getChildren().get(0)).getAccessPermissions()[0]);

List<PageContext> pages = layoutService.findPages(groupSiteKey);
Expand All @@ -209,7 +209,7 @@ public void testCreateUserPortalConfigFromTemplate() {
Page page = layoutService.getPage(pageContext.getKey());
assertNotNull(page);
assertEquals(1, page.getChildren().size());
assertEquals(accessPermission, ((Application) page.getChildren().get(0)).getAccessPermissions()[0]);
assertEquals(UserACL.EVERYONE, ((Application) page.getChildren().get(0)).getAccessPermissions()[0]);

NavigationContext navigation = navigationService.loadNavigation(groupSiteKey);
assertNotNull(navigation);
Expand Down Expand Up @@ -269,7 +269,7 @@ public void testDuplicateSiteTemplate() {
assertEquals(2, targetPortalConfig.getPortalLayout().getChildren().size());
assertEquals(Application.class, targetPortalConfig.getPortalLayout().getChildren().get(0).getClass());
assertEquals(PageBody.class, targetPortalConfig.getPortalLayout().getChildren().get(1).getClass());
assertEquals(((Application) sourcePortalConfig.getPortalLayout().getChildren().get(0)).getAccessPermissions()[0],
assertEquals(UserACL.EVERYONE,
((Application) targetPortalConfig.getPortalLayout().getChildren().get(0)).getAccessPermissions()[0]);

List<PageContext> pages = layoutService.findPages(targetTemplateSiteKey);
Expand All @@ -284,7 +284,7 @@ public void testDuplicateSiteTemplate() {
Page page = layoutService.getPage(pageContext.getKey());
assertNotNull(page);
assertEquals(1, page.getChildren().size());
assertEquals(accessPermission, ((Application) page.getChildren().get(0)).getAccessPermissions()[0]);
assertEquals(UserACL.EVERYONE, ((Application) page.getChildren().get(0)).getAccessPermissions()[0]);

NavigationContext navigation = navigationService.loadNavigation(targetTemplateSiteKey);
assertNotNull(navigation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.exoplatform.upload.UploadService;
import org.exoplatform.upload.UploadService.UploadLimit;
import org.exoplatform.web.ControllerContext;
import org.exoplatform.web.WebAppController;
import org.exoplatform.web.WebRequestHandler;

import jakarta.servlet.http.HttpServletRequest;
Expand Down

0 comments on commit 566f129

Please sign in to comment.