From 0e291a87b99030b9538d0857fc5a2e45fa1d9088 Mon Sep 17 00:00:00 2001 From: jensp Date: Fri, 8 Jul 2016 15:21:24 +0000 Subject: [PATCH] CCM NG: Setup for content sections git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4193 8810af33-2d31-482b-a856-94f89814c4df --- ccm-cms/src/main/java/org/librecms/Cms.java | 44 ++++- .../main/java/org/librecms/CmsConstants.java | 23 ++- .../contentsection/ContentSectionSetup.java | 150 ++++++++++++++++++ .../web/AbstractCcmApplicationSetup.java | 15 +- 4 files changed, 223 insertions(+), 9 deletions(-) create mode 100644 ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionSetup.java diff --git a/ccm-cms/src/main/java/org/librecms/Cms.java b/ccm-cms/src/main/java/org/librecms/Cms.java index 224deef28..b023bd252 100644 --- a/ccm-cms/src/main/java/org/librecms/Cms.java +++ b/ccm-cms/src/main/java/org/librecms/Cms.java @@ -3,6 +3,10 @@ */ package org.librecms; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.libreccm.categorization.Category; +import org.libreccm.core.CoreConstants; import org.libreccm.modules.CcmModule; import org.libreccm.modules.InitEvent; import org.libreccm.modules.InstallEvent; @@ -10,6 +14,12 @@ import org.libreccm.modules.Module; import org.libreccm.modules.RequiredModule; import org.libreccm.modules.ShutdownEvent; import org.libreccm.modules.UnInstallEvent; +import org.librecms.contentsection.ContentSection; +import org.librecms.contentsection.ContentSectionSetup; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; @Module(packageName = "org.libreccm.cms", requiredModules = { @@ -18,16 +28,33 @@ import org.libreccm.modules.UnInstallEvent; ) public class Cms implements CcmModule { + private static final Logger LOGGER = LogManager.getLogger(Cms.class); + + + @Override public void install(final InstallEvent event) { //ToDo Create initial data for the module if neccessary - - // Create initial content section + + // Create initial content section(s) // If a list of sections is provided in the integration properties // use that list, otherwise create a content section named 'info'. - + LOGGER.info("Trying to load integration properties..."); + final Properties integrationProps = new Properties(); + try (final InputStream inputStream = getClass().getResourceAsStream( + CoreConstants.INTEGRATION_PROPS)) { + integrationProps.load(inputStream); + } catch (IOException ex) { + LOGGER.warn( + "Failed to load integration properties. Using default values.", + ex); + } + + final ContentSectionSetup contentSectionSetup = new ContentSectionSetup( + event); + contentSectionSetup.setup(); + // Map given domains to content section(s) - // Also create the following roles for each content section and assign // the permissions // @@ -40,6 +67,15 @@ public class Cms implements CcmModule { // * Content Reader: View Published Items } + private void createContentSection(final String contentSectionName) { + final ContentSection section = new ContentSection(); + section.setLabel(contentSectionName); + + final Category rootFolder = new Category(); + rootFolder.setName(String.format("%s_root", contentSectionName)); + + } + @Override public void init(final InitEvent event) { //ToDo Add initialisation logic necessary for your module diff --git a/ccm-cms/src/main/java/org/librecms/CmsConstants.java b/ccm-cms/src/main/java/org/librecms/CmsConstants.java index 4f9bfd3d6..0a33e7302 100644 --- a/ccm-cms/src/main/java/org/librecms/CmsConstants.java +++ b/ccm-cms/src/main/java/org/librecms/CmsConstants.java @@ -23,13 +23,28 @@ package org.librecms; * @author Jens Pelzetter */ public class CmsConstants { - + public static final String CMS_XML_NS = "http://cms.libreccm.org"; - + public static final String DB_SCHEMA = "CCM_CMS"; - + + public static final String PRIVILEGE_ADMINISTER_CATEGORIES = "administer_categories"; + public static final String PRIVILEGE_ADMINISTER_CONTENT_TYPES = "administer_content_types"; + public static final String PRIVILEGE_ADMINISTER_LIFECYLES = "administer_lifecyles"; + public static final String PRIVILEGE_ADMINISTER_ROLES = "administer_roles"; + public static final String PRIVILEGE_ADMINISTER_WORKFLOW = "administer_workflow"; + public static final String PRIVILEGE_ITEMS_APPROVE = "approve_items"; + public static final String PRIVILEGE_ITEMS_PUBLISH = "publish_items"; + public static final String PRIVILEGE_ITEMS_CATEGORIZE = "categories_items"; + public static final String PRIVILEGE_ITEMS_CREATE_NEW = "create_new_items"; + public static final String PRIVILEGE_ITEMS_DELETE = "delete_items"; + public static final String PRIVILEGE_ITEMS_EDIT = "edit_items"; + public static final String PRIVILEGE_ITEMS_PREVIEW = "preview_items"; + public static final String PRIVILEGE_ITEMS_VIEW_PUBLISHED = "view_published_items"; + public static final String PRIVILEGE_APPLY_ALTERNATE_WORKFLOW = "apply_alternate_workflow"; + private CmsConstants() { //Nothing } - + } diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionSetup.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionSetup.java new file mode 100644 index 000000000..99babb9eb --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionSetup.java @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2016 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.librecms.contentsection; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.libreccm.categorization.Category; +import org.libreccm.modules.InstallEvent; +import org.libreccm.security.Role; +import org.libreccm.web.AbstractCcmApplicationSetup; + +import static org.librecms.CmsConstants.*; + +/** + * + * @author Jens Pelzetter + */ +public class ContentSectionSetup extends AbstractCcmApplicationSetup { + + private static final Logger LOGGER = LogManager.getLogger( + ContentSectionSetup.class); + + private static final String INITIAL_CONTENT_SECTIONS + = "org.librecms.initial_content_sections"; + + public ContentSectionSetup(final InstallEvent event) { + super(event); + } + + @Override + public void setup() { + final String contentSectionNames; + if (getIntegrationProps().containsKey(INITIAL_CONTENT_SECTIONS)) { + contentSectionNames = getIntegrationProps().getProperty( + INITIAL_CONTENT_SECTIONS); + } else { + contentSectionNames = "info"; + } + + for (final String contentSectionName : contentSectionNames.split(",")) { + createContentSection(contentSectionName); + } + } + + private void createContentSection(final String contentSectionName) { + final ContentSection section = new ContentSection(); + section.setLabel(contentSectionName); + + final Category rootFolder = new Category(); + rootFolder.setName(String.format("%s_root", contentSectionName)); + + final Category rootAssetFolder = new Category(); + rootFolder.setName(String.format("%s_assets", contentSectionName)); + + section.setRootDocumentFolder(rootFolder); + section.setRootAssetsFolder(rootAssetFolder); + + getEntityManager().persist(section); + getEntityManager().persist(rootFolder); + getEntityManager().persist(rootAssetFolder); + + final Role alertRecipient = createRole(String.format( + "%s_alert_recipient", contentSectionName)); + final Role author = createRole(String.format("%s_author", + contentSectionName)); + final Role editor = createRole(String.format("%s_editor", + contentSectionName)); + final Role manager = createRole(String.format("%s_manager", + contentSectionName)); + final Role publisher = createRole(String.format("%s_publisher", + contentSectionName)); + final Role contentReader = createRole(String.format("%s_content_reader", + contentSectionName)); + + grantPermissions(author, + rootFolder, + PRIVILEGE_ITEMS_CATEGORIZE, + PRIVILEGE_ITEMS_CREATE_NEW, + PRIVILEGE_ITEMS_EDIT, + PRIVILEGE_ITEMS_VIEW_PUBLISHED, + PRIVILEGE_ITEMS_PREVIEW); + + grantPermissions(editor, + rootFolder, + PRIVILEGE_ITEMS_CATEGORIZE, + PRIVILEGE_ITEMS_CREATE_NEW, + PRIVILEGE_ITEMS_EDIT, + PRIVILEGE_ITEMS_APPROVE, + PRIVILEGE_ITEMS_DELETE, + PRIVILEGE_ITEMS_VIEW_PUBLISHED, + PRIVILEGE_ITEMS_PREVIEW); + + grantPermissions(manager, + rootFolder, + PRIVILEGE_ADMINISTER_ROLES, + PRIVILEGE_ADMINISTER_WORKFLOW, + PRIVILEGE_ADMINISTER_LIFECYLES, + PRIVILEGE_ADMINISTER_CATEGORIES, + PRIVILEGE_ADMINISTER_CONTENT_TYPES, + PRIVILEGE_ITEMS_CATEGORIZE, + PRIVILEGE_ITEMS_CREATE_NEW, + PRIVILEGE_ITEMS_EDIT, + PRIVILEGE_ITEMS_APPROVE, + PRIVILEGE_ITEMS_PUBLISH, + PRIVILEGE_ITEMS_DELETE, + PRIVILEGE_ITEMS_VIEW_PUBLISHED, + PRIVILEGE_ITEMS_PREVIEW); + + grantPermissions(editor, + rootFolder, + PRIVILEGE_ITEMS_CATEGORIZE, + PRIVILEGE_ITEMS_CREATE_NEW, + PRIVILEGE_ITEMS_EDIT, + PRIVILEGE_ITEMS_APPROVE, + PRIVILEGE_ITEMS_PUBLISH, + PRIVILEGE_ITEMS_DELETE, + PRIVILEGE_ITEMS_VIEW_PUBLISHED, + PRIVILEGE_ITEMS_PREVIEW); + + grantPermissions(contentReader, + rootFolder, + PRIVILEGE_ITEMS_VIEW_PUBLISHED); + + + getEntityManager().persist(alertRecipient); + getEntityManager().persist(author); + getEntityManager().persist(editor); + getEntityManager().persist(manager); + getEntityManager().persist(publisher); + getEntityManager().persist(contentReader); + + } + +} diff --git a/ccm-core/src/main/java/org/libreccm/web/AbstractCcmApplicationSetup.java b/ccm-core/src/main/java/org/libreccm/web/AbstractCcmApplicationSetup.java index 3e855cba8..3035928a4 100644 --- a/ccm-core/src/main/java/org/libreccm/web/AbstractCcmApplicationSetup.java +++ b/ccm-core/src/main/java/org/libreccm/web/AbstractCcmApplicationSetup.java @@ -24,7 +24,6 @@ import org.libreccm.core.CcmObject; import org.libreccm.core.CoreConstants; import org.libreccm.modules.InstallEvent; import org.libreccm.security.ApplicationRoleSetup; -import org.libreccm.security.Permission; import org.libreccm.security.Role; import java.io.IOException; @@ -82,6 +81,20 @@ public abstract class AbstractCcmApplicationSetup { appRoleSetup.grantPermission(role, privilege, ccmObject); } + public void grantPermissions(final Role role, final String... privileges) { + for(final String privilege : privileges) { + grantPermission(role, privilege); + } + } + + public void grantPermissions(final Role role, + final CcmObject ccmObject, + final String... privileges) { + for(final String privilege : privileges) { + grantPermission(role, privilege, ccmObject); + } + } + public abstract void setup(); }