From: <fg...@us...> - 2008-07-27 09:19:24
|
Revision: 897 http://openutils.svn.sourceforge.net/openutils/?rev=897&view=rev Author: fgiust Date: 2008-07-27 09:19:30 +0000 (Sun, 27 Jul 2008) Log Message: ----------- new tasks: AddPermissionTask and CreateDefaultRepositoryAclForAllUsersTask Modified Paths: -------------- trunk/openutils-mgnltasks/src/site/changes/changes.xml Added Paths: ----------- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/AddPermissionTask.java trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CreateDefaultRepositoryAclForAllUsersTask.java Added: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/AddPermissionTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/AddPermissionTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/AddPermissionTask.java 2008-07-27 09:19:30 UTC (rev 897) @@ -0,0 +1,137 @@ +/** + * Copyright Openmind http://www.openmindonline.it + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package it.openutils.mgnltasks; + +import info.magnolia.cms.beans.config.ContentRepository; +import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.HierarchyManager; +import info.magnolia.cms.core.Path; +import info.magnolia.cms.security.AccessDeniedException; +import info.magnolia.cms.util.NodeDataUtil; +import info.magnolia.module.InstallContext; +import info.magnolia.module.delta.AbstractRepositoryTask; +import info.magnolia.module.delta.Task; +import info.magnolia.module.delta.TaskExecutionException; + +import java.util.Collection; + +import javax.jcr.RepositoryException; + + +/** + * Sets or add permissions on a repository:path for a given role. + * @author fgiust + * @version $Id: $ + */ +public class AddPermissionTask extends AbstractRepositoryTask implements Task +{ + + private final String role; + + private final String repo; + + private final String path; + + private final long permission; + + public AddPermissionTask(String role, String repo, String path, long permission) + { + super("Setup permissions on " + repo + ":" + path + " for " + role, "Setup permissions on " + + repo + + ":" + + path + + " for " + + role); + this.role = role; + this.repo = repo; + this.path = path; + this.permission = permission; + + } + + /** + * {@inheritDoc} + */ + @Override + protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException + { + + HierarchyManager hm = installContext.getHierarchyManager(ContentRepository.USER_ROLES); + + Content roleNode = hm.getContent("/" + role); + + setupAcl(roleNode, repo, path, permission); + } + + /** + * @param role + * @param repository + * @param newpermissions + * @throws RepositoryException + * @throws AccessDeniedException + */ + @SuppressWarnings("unchecked") + private void setupAcl(Content role, String repository, String path, long newpermissions) + throws RepositoryException, AccessDeniedException + { + Content acls = role.getChildByName("acl_" + repository); + + if (acls == null) + { + acls = role.createContent("acl_" + repository, "mgnl:contentNode"); + } + + Collection<Content> children = acls.getChildren(); + + boolean found = false; + for (Content acl : children) + { + String aclPath = acl.getNodeData("path").getString(); + if (path.equals(aclPath)) + { + found = true; + + long permissions = acl.getNodeData("permissions").getLong(); + if (permissions != newpermissions) + { + setPermission(acl, path, newpermissions); + } + } + } + if (!found) + { + Content acl = acls.createContent(Path.getUniqueLabel(acls, "0"), "mgnl:contentNode"); + setPermission(acl, path, newpermissions); + + } + } + + /** + * @param acl + * @param newpermissions + * @throws RepositoryException + * @throws AccessDeniedException + */ + private void setPermission(Content acl, String path, long newpermissions) throws RepositoryException, + AccessDeniedException + { + log.info("Setting permissions for {} to {}", this.repo + ":" + path, this.role); + NodeDataUtil.getOrCreate(acl, "path").setValue(path); + NodeDataUtil.getOrCreate(acl, "permissions").setValue(newpermissions); + } +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/AddPermissionTask.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CreateDefaultRepositoryAclForAllUsersTask.java =================================================================== --- trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CreateDefaultRepositoryAclForAllUsersTask.java (rev 0) +++ trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CreateDefaultRepositoryAclForAllUsersTask.java 2008-07-27 09:19:30 UTC (rev 897) @@ -0,0 +1,93 @@ +/** + * Copyright Openmind http://www.openmindonline.it + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package it.openutils.mgnltasks; + +import info.magnolia.cms.beans.config.ContentRepository; +import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.HierarchyManager; +import info.magnolia.cms.core.ItemType; +import info.magnolia.cms.util.ContentUtil; +import info.magnolia.module.InstallContext; +import info.magnolia.module.delta.AbstractRepositoryTask; +import info.magnolia.module.delta.TaskExecutionException; + +import java.util.Collection; + +import javax.jcr.RepositoryException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Adds a default repository permissions for all the configured users, only if there is no acl set for the given + * repository. + * @author fgiust + * @version $Id: $ + */ +public class CreateDefaultRepositoryAclForAllUsersTask extends AbstractRepositoryTask +{ + + private static Logger log = LoggerFactory.getLogger(CreateDefaultRepositoryAclForAllUsersTask.class); + + private String repository; + + private long permissions; + + public CreateDefaultRepositoryAclForAllUsersTask(String repository, long permissions) + { + super("Adding permissions on " + repository + " repository", "Adding permissions on " + + repository + + " repository"); + this.repository = repository; + this.permissions = permissions; + } + + @SuppressWarnings("unchecked") + @Override + protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException + { + HierarchyManager hm = ctx.getHierarchyManager(ContentRepository.USER_ROLES); + final Content parentNode = hm.getContent("/"); + + final Collection<Content> childNodes = ContentUtil.collectAllChildren(parentNode, ItemType.ROLE); + + for (Content content : childNodes) + { + operateOnChildNode(content, ctx); + } + } + + protected void operateOnChildNode(Content node, InstallContext ctx) throws RepositoryException, + TaskExecutionException + { + + String aclpath = "acl_" + repository; + + if (!node.hasContent(aclpath)) + { + log.info("adding permissions on {} to role {}", repository, node.getName()); + + Content aclnode = node.createContent(aclpath, ItemType.CONTENTNODE); + Content permNode = aclnode.createContent("0", ItemType.CONTENTNODE); + permNode.createNodeData("path", "/*"); + permNode.createNodeData("permissions", new Long(permissions)); + } + } + +} Property changes on: trunk/openutils-mgnltasks/src/main/java/it/openutils/mgnltasks/CreateDefaultRepositoryAclForAllUsersTask.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-mgnltasks/src/site/changes/changes.xml =================================================================== --- trunk/openutils-mgnltasks/src/site/changes/changes.xml 2008-07-24 08:49:42 UTC (rev 896) +++ trunk/openutils-mgnltasks/src/site/changes/changes.xml 2008-07-27 09:19:30 UTC (rev 897) @@ -8,6 +8,9 @@ <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author> </properties> <body> + <release version="3.5.5" date="2008-07-27" description=""> + <action type="add" dev="fgiust">new tasks: AddPermissionTask, CreateDefaultRepositoryAclForAllUsersTask</action> + </release> <release version="3.5.4" date="2008-07-24" description=""> <action type="add" dev="fgiust">new tasks: CheckMissingTemplatesTask, CheckMissingParagraphsTask, ReplaceTemplateTask</action> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |