add simple update classes

This commit is contained in:
Schneider Roland 2024-09-20 18:01:55 +02:00
parent d9d134cfc1
commit 5f0df61fe1
5 changed files with 241 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<Task> tasks, String roleName, Collection<SimpleACLDefinition> aclDefinitions, List<String> 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<Task> tasks, String roleName, Collection<SimpleACLDefinition> aclDefinitions, Collection<SimpleWebACLDefinition> webACLDefinitions, List<String> 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<Task> tasks, String roleName, Collection<SimpleACLDefinition> aclDefinitions, List<String> 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<Task> tasks, String roleName, Collection<SimpleACLDefinition> aclDefinitions, Collection<SimpleWebACLDefinition> webACLDefinitions, List<String> 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<Task> tasks, String roleName, List<SimpleACLDefinition> 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<Task> 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<Task> tasks, String roleName, List<SimpleWebACLDefinition> 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<Task> 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<Task> tasks, String roleName, String groupName) {
tasks.add(new AddRoleToGroupTask("Assign default role " + roleName + " to default group " + groupName, roleName, groupName));
}
public static void addEmptyFolder(List<Task> 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<Task> 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<Task> 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<Task> 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<Task> 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"));
}
}

View File

@ -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);
}
}

View File

@ -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 = "/";
}