diff --git a/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/SimpleACLDefinition.java b/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/SimpleACLDefinition.java new file mode 100644 index 0000000..e9b1e24 --- /dev/null +++ b/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/SimpleACLDefinition.java @@ -0,0 +1,15 @@ +package at.ucs.magnolia.util.bootstrap.simpleUpdate; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +@AllArgsConstructor +public class SimpleACLDefinition { + private String workspace; + private String path; + private long permission; + private boolean includeSubNodes; +} diff --git a/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/SimpleWebACLDefinition.java b/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/SimpleWebACLDefinition.java new file mode 100644 index 0000000..5aa756f --- /dev/null +++ b/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/SimpleWebACLDefinition.java @@ -0,0 +1,11 @@ +package at.ucs.magnolia.util.bootstrap.simpleUpdate; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class SimpleWebACLDefinition { + private String path; + private int permission; +} diff --git a/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/UpdateHelper.java b/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/UpdateHelper.java new file mode 100644 index 0000000..a3b95ee --- /dev/null +++ b/src/main/java/at/ucs/magnolia/util/bootstrap/simpleUpdate/UpdateHelper.java @@ -0,0 +1,153 @@ +package at.ucs.magnolia.util.bootstrap.simpleUpdate; + +import at.ucs.magnolia.util.bootstrap.task.CreateNodeWithFixedUUIDTask; +import at.ucs.magnolia.util.core.Workspace; +import info.magnolia.jcr.util.NodeTypes; +import info.magnolia.module.delta.*; + +import java.nio.file.Paths; +import java.util.Calendar; +import java.util.Collection; +import java.util.List; + + +public class UpdateHelper { + /** + * Create and add tasks,which are required, to add a new role to the system and 'publish' it immediately + * + * @param tasks the list of tasks to which the new tasks will be appended + * @param roleName the name of the newly created role + * @param aclDefinitions list of ACL definitions to add + * @param uuid uuid the new node should have + */ + public static void createAndPublishUserrole(List tasks, String roleName, Collection aclDefinitions, List assignToGroups, String uuid) { + addCreateRoleTask(tasks, roleName, aclDefinitions, null, assignToGroups, uuid); + setActivationStatus(tasks, Workspace.USER_ROLES, "/" + roleName); + + } + + /** + * Create and add tasks,which are required, to add a new role to the system and 'publish' it immediately + * + * @param tasks the list of tasks to which the new tasks will be appended + * @param roleName the name of the newly created role + * @param aclDefinitions list of ACL definitions to add + * @param uuid uuid the new node should have + */ + public static void createAndPublishUserrole(List tasks, String roleName, Collection aclDefinitions, Collection webACLDefinitions, List assignToGroups, String uuid) { + addCreateRoleTask(tasks, roleName, aclDefinitions, webACLDefinitions, assignToGroups, uuid); + setActivationStatus(tasks, Workspace.USER_ROLES, "/" + roleName); + + } + + /** + * Create and add tasks,which are required, to add a new role to the system + * + * @param tasks the list of tasks to which the new tasks will be appended + * @param roleName the name of the newly created role + * @param aclDefinitions list of ACL definitions to add + */ + public static void createUserrole(List tasks, String roleName, Collection aclDefinitions, List assignToGroups) { + addCreateRoleTask(tasks, roleName, aclDefinitions, null, assignToGroups, null); + } + + /** + * Create and add tasks,which are required, to add a new role to the system + * + * @param tasks the list of tasks to which the new tasks will be appended + * @param roleName the name of the newly created role + * @param aclDefinitions list of ACL definitions to add + * @param webACLDefinitions list of web ACL definitions to add + * @param uuid uuid the new node should have, if null a uuid is automatically assigned. + */ + private static void addCreateRoleTask(List tasks, String roleName, Collection aclDefinitions, Collection webACLDefinitions, List assignToGroups, String uuid) { + if (uuid != null) { + tasks.add(new CreateNodeWithFixedUUIDTask("Create default userrole " + roleName, "Creates default userroles", Workspace.USER_ROLES, "/", roleName, NodeTypes.Role.NAME, uuid)); + } else { + tasks.add(new CreateNodeTask("Create default userrole " + roleName, "Creates default userroles", Workspace.USER_ROLES, "/", roleName, NodeTypes.Role.NAME)); + } + + if (aclDefinitions != null) { + for (SimpleACLDefinition definition : aclDefinitions) { + addJcrAcl(tasks, roleName, definition.getWorkspace(), definition.getPath(), definition.getPermission(), definition.isIncludeSubNodes()); + } + } + if (webACLDefinitions != null) { + for (SimpleWebACLDefinition definition : webACLDefinitions) { + addWebAcl(tasks, roleName, definition.getPath(), definition.getPermission()); + } + } + if (assignToGroups != null) { + for (String groupName : assignToGroups) { + addRoleToGroup(tasks, roleName, groupName); + } + } + } + + public static void addJcrAcl(List tasks, String roleName, List jcrAcls) { + jcrAcls.forEach( + simpleACLDefinition -> + tasks.add(new AddPermissionTask("Add permission to default userrole " + roleName, roleName, simpleACLDefinition.getWorkspace(), simpleACLDefinition.getPath(), simpleACLDefinition.getPermission() , simpleACLDefinition.isIncludeSubNodes())) + ); + } + + public static void addJcrAcl(List tasks, String roleName, String workspace, String path, long permission, boolean includingSubNodes) { + tasks.add(new AddPermissionTask("Add permission to default userrole " + roleName, roleName, workspace, path, permission, includingSubNodes)); + } + + public static void addWebAcl(List tasks, String roleName, List webAcls) { + webAcls.forEach( + simpleWebACLDefinition -> + tasks.add(new AddURIPermissionTask("Add web permission to default userrole " + roleName, roleName, simpleWebACLDefinition.getPath(), simpleWebACLDefinition.getPermission())) + ); + } + + public static void addWebAcl(List tasks, String roleName, String path, int permission) { + tasks.add(new AddURIPermissionTask("Add web permission to default userrole " + roleName, roleName, path, permission)); + } + + public static void addRoleToGroup(List tasks, String roleName, String groupName) { + tasks.add(new AddRoleToGroupTask("Assign default role " + roleName + " to default group " + groupName, roleName, groupName)); + } + + public static void addEmptyFolder(List tasks, String workspace, String parentPath, String folderName) { + tasks.add(new CreateNodeTask("Create default folder " + folderName, "Creates default folder", workspace, parentPath, folderName, NodeTypes.Folder.NAME)); + tasks.add(new SetPropertyTask(workspace, folderName, "name", folderName.replaceAll("-", " "))); + } + + public static void addEmptyFolderAndPublish(List tasks, String workspace, String parentPath, String folderName, String uuid) { + tasks.add(new CreateNodeWithFixedUUIDTask("Create default folder " + folderName, "Creates default folder", workspace, parentPath, folderName, NodeTypes.Folder.NAME, uuid)); + tasks.add(new SetPropertyTask(workspace, folderName, "name", folderName.replaceAll("-", " "))); + setActivationStatus(tasks, workspace, Paths.get(parentPath, folderName).toString()); + } + + public static void addGroup(List tasks, String groupName) { + tasks.add(new CreateNodeTask("Create default usergroup " + groupName, "Creates default usergroups", Workspace.USERGROUPS, Workspace.JCR_ROOT_PATH, groupName, NodeTypes.Group.NAME)); + } + + public static void addGroupAndPublish(List tasks, String groupName, String uuid) { + tasks.add(new CreateNodeWithFixedUUIDTask("Create default usergroup " + groupName, "Creates default usergroups", Workspace.USERGROUPS, Workspace.JCR_ROOT_PATH, groupName, NodeTypes.Group.NAME, uuid)); + setActivationStatus(tasks, Workspace.USERGROUPS, "/" + groupName); + } + + /** + * Sets the activation status of a node to true. + * This does not actually publish the node! + * All this does is make the green light show up in admincentral. + * + * @param tasks tasks + * @param workspace workspace + * @param nodePath nodePath + */ + public static void setActivationStatus(List tasks, String workspace, String nodePath) { + Calendar calendar = Calendar.getInstance(); + tasks.add(new SetPropertyTask(workspace, nodePath, "mgnl:activationStatus", "true")); + tasks.add(new SetPropertyTask("Set " + nodePath + "/mgnl:lastModified", workspace, nodePath, + "mgnl:lastModified", calendar)); + calendar.add(Calendar.MINUTE, 2); + tasks.add(new SetPropertyTask("Set " + nodePath + "/mgnl:lastActivated", workspace, nodePath, + "mgnl:lastActivated", calendar)); + tasks.add(new SetPropertyTask(workspace, nodePath, "mgnl:lastActivatedBy", "autopublish")); + } + +} diff --git a/src/main/java/at/ucs/magnolia/util/bootstrap/task/CreateNodeWithFixedUUIDTask.java b/src/main/java/at/ucs/magnolia/util/bootstrap/task/CreateNodeWithFixedUUIDTask.java new file mode 100644 index 0000000..cc7fbfc --- /dev/null +++ b/src/main/java/at/ucs/magnolia/util/bootstrap/task/CreateNodeWithFixedUUIDTask.java @@ -0,0 +1,47 @@ +package at.ucs.magnolia.util.bootstrap.task; + +import info.magnolia.jcr.util.NodeUtil; +import info.magnolia.module.InstallContext; +import info.magnolia.module.delta.AbstractRepositoryTask; +import info.magnolia.module.delta.TaskExecutionException; +import info.magnolia.repository.RepositoryConstants; +import org.apache.jackrabbit.core.NodeImpl; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + +/** + * creates a node with given uuid + */ +public class CreateNodeWithFixedUUIDTask extends AbstractRepositoryTask { + + private final String workspaceName; + private final String parentPath; + private final String nodeName; + private final String type; + private final String uuid; + + public CreateNodeWithFixedUUIDTask(String name, String parentPath, String nodeName, String type, String uuid) { + this(name, String.format("Create node '%s:%s'.", RepositoryConstants.CONFIG, parentPath + "/" + nodeName), + RepositoryConstants.CONFIG, parentPath, nodeName, type, uuid); + } + + public CreateNodeWithFixedUUIDTask(String name, String description, String workspaceName, String parentPath, String nodeName, String type, String uuid) { + super(name, description); + this.workspaceName = workspaceName; + this.parentPath = parentPath; + this.nodeName = nodeName; + this.type = type; + this.uuid = uuid; + } + + @Override + protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException { + final Session session = installContext.getJCRSession(this.workspaceName); + final Node parent = session.getNode(parentPath); + final NodeImpl unwrapped = (NodeImpl) NodeUtil.unwrap(parent); + + unwrapped.addNodeWithUuid(nodeName, type, uuid); + } +} diff --git a/src/main/java/at/ucs/magnolia/util/core/Workspace.java b/src/main/java/at/ucs/magnolia/util/core/Workspace.java new file mode 100644 index 0000000..99c8a0f --- /dev/null +++ b/src/main/java/at/ucs/magnolia/util/core/Workspace.java @@ -0,0 +1,15 @@ +package at.ucs.magnolia.util.core; + +public interface Workspace { + + public static String USER_ROLES= "userroles"; + public static String USERS ="users"; + public static String ROLES="roles"; + public static String DAM="dam"; + public static String CONFIG="config"; + public static String WEBSITE="website"; + public static String USERGROUPS="usergroups"; + + public static String JCR_ROOT_PATH = "/"; + +}