From 566f129caebc0ea942a142c258df00bfd2443826 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Fri, 8 Nov 2024 14:48:18 +0100 Subject: [PATCH] feat: Allow to create a Group Template Site from Group Site - MEED-7736 - 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. --- .../exoplatform/upload/UploadResource.java | 3 + .../org/exoplatform/upload/UploadService.java | 4 ++ .../config/UserPortalConfigService.java | 14 ++-- .../portal/mop/service/LayoutServiceImpl.java | 65 ++++++++++++++----- .../config/TestUserPortalConfigService.java | 8 +-- .../web/handler/UploadHandler.java | 1 - 6 files changed, 65 insertions(+), 30 deletions(-) diff --git a/component/common/src/main/java/org/exoplatform/upload/UploadResource.java b/component/common/src/main/java/org/exoplatform/upload/UploadResource.java index 72fce43088..b4a6cc8486 100644 --- a/component/common/src/main/java/org/exoplatform/upload/UploadResource.java +++ b/component/common/src/main/java/org/exoplatform/upload/UploadResource.java @@ -19,9 +19,12 @@ package org.exoplatform.upload; +import lombok.AllArgsConstructor; + /** * Created by The eXo Platform SARL Author : Tuan Nguyen tuan08@users.sourceforge.net Dec 26, 2005 */ +@AllArgsConstructor public class UploadResource { public static final int UPLOADING_STATUS = 0; diff --git a/component/common/src/main/java/org/exoplatform/upload/UploadService.java b/component/common/src/main/java/org/exoplatform/upload/UploadService.java index a4efe8353a..766114fea4 100644 --- a/component/common/src/main/java/org/exoplatform/upload/UploadService.java +++ b/component/common/src/main/java/org/exoplatform/upload/UploadService.java @@ -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 '\\'"); diff --git a/component/portal/src/main/java/org/exoplatform/portal/config/UserPortalConfigService.java b/component/portal/src/main/java/org/exoplatform/portal/config/UserPortalConfigService.java index a3e9b7f0ca..c8b980e7ac 100644 --- a/component/portal/src/main/java/org/exoplatform/portal/config/UserPortalConfigService.java +++ b/component/portal/src/main/java/org/exoplatform/portal/config/UserPortalConfigService.java @@ -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); } /** diff --git a/component/portal/src/main/java/org/exoplatform/portal/mop/service/LayoutServiceImpl.java b/component/portal/src/main/java/org/exoplatform/portal/mop/service/LayoutServiceImpl.java index 157d6e0a50..01e053c442 100644 --- a/component/portal/src/main/java/org/exoplatform/portal/mop/service/LayoutServiceImpl.java +++ b/component/portal/src/main/java/org/exoplatform/portal/mop/service/LayoutServiceImpl.java @@ -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; @@ -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"; @@ -119,12 +122,12 @@ 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(); @@ -132,10 +135,14 @@ public void savePortalFromTemplate(SiteKey sourceSiteTemplate, 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()); @@ -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()); @@ -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 children = container.getChildren(); + applyPermissionTemplate(children); + } else if (model instanceof Application application) { + application.setAccessPermissions(new String[] {UserACL.EVERYONE}); + } + } + + private void applyPermissionTemplate(ArrayList children) { + if (CollectionUtils.isNotEmpty(children)) { + children.forEach(this::applyPermissionTemplate); + } } } diff --git a/component/portal/src/test/java/org/exoplatform/portal/config/TestUserPortalConfigService.java b/component/portal/src/test/java/org/exoplatform/portal/config/TestUserPortalConfigService.java index c31169fd7b..dd861c4d77 100644 --- a/component/portal/src/test/java/org/exoplatform/portal/config/TestUserPortalConfigService.java +++ b/component/portal/src/test/java/org/exoplatform/portal/config/TestUserPortalConfigService.java @@ -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 pages = layoutService.findPages(groupSiteKey); @@ -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); @@ -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 pages = layoutService.findPages(targetTemplateSiteKey); @@ -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); diff --git a/component/web/controller/src/main/java/org/exoplatform/web/handler/UploadHandler.java b/component/web/controller/src/main/java/org/exoplatform/web/handler/UploadHandler.java index 1536ede41f..70043faa17 100644 --- a/component/web/controller/src/main/java/org/exoplatform/web/handler/UploadHandler.java +++ b/component/web/controller/src/main/java/org/exoplatform/web/handler/UploadHandler.java @@ -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;