View Javadoc
1   /**
2    * This file Copyright (c) 2016-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.delta;
35  
36  import info.magnolia.jcr.util.NodeNameHelper;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.module.InstallContext;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.repository.RepositoryConstants;
42  
43  import java.util.ArrayList;
44  import java.util.Arrays;
45  import java.util.List;
46  
47  import javax.jcr.Node;
48  import javax.jcr.NodeIterator;
49  import javax.jcr.RepositoryException;
50  
51  import org.apache.commons.lang3.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  /**
56   * A task to add a permission to all users under provided path.
57   *
58   */
59  public class AddUsersPermissionTask extends NodeVisitorTask {
60  
61      public static final String USER_PATH_REPLACEMENT = "${USER_PATH}";
62  
63      private static final Logger log = LoggerFactory.getLogger(AddUsersPermissionTask.class);
64  
65      private final String workspaceName;
66      private final String path;
67      private final long permission;
68      private List<String> excludedUserNames = new ArrayList<>();
69  
70      private final NodeNameHelper nodeNameHelper;
71  
72      /**
73       * @param pathToUsers path pointing to subtree (or a single user) in users workspace
74       * @param workspace workspace to add permission for
75       * @param workspacePath path in workspace to add permission for, string ${USER_PATH} will be replaced with path to user to whom is the permission added
76       * @param permission permission value, see {@link info.magnolia.cms.security.Permission}
77       * @param nodeNameHelper instance of {@link NodeNameHelper}
78       */
79      public AddUsersPermissionTask(String taskName, String pathToUsers, String workspace, String workspacePath, long permission, NodeNameHelper nodeNameHelper) {
80          super(taskName, String.format("Adds permission '%s:%s=%d' to users under the path '%s'", workspace, workspacePath, permission, pathToUsers), RepositoryConstants.USERS, pathToUsers);
81          this.workspaceName = workspace;
82          this.path = workspacePath;
83          this.permission = permission;
84          this.nodeNameHelper = nodeNameHelper;
85      }
86  
87      /**
88       * @deprecated since 5.5.3, use {@link #AddUsersPermissionTask(String, String, String, String, long, NodeNameHelper)}
89       */
90      @Deprecated
91      public AddUsersPermissionTask(String taskName, String pathToUsers, String workspace, String workspacePath, long permission) {
92          this(taskName, pathToUsers, workspace, workspacePath, permission, Components.getComponent(NodeNameHelper.class));
93      }
94  
95      public AddUsersPermissionTask exclude(String... excludedUserNames) {
96          this.excludedUserNames = Arrays.asList(excludedUserNames);
97          return this;
98      }
99  
100     @Override
101     protected boolean nodeMatches(Node node) {
102         try {
103             return NodeTypes.User.NAME.equals(node.getPrimaryNodeType().getName()) && !excludedUserNames.contains(node.getName());
104         } catch (RepositoryException e) {
105             log.error(e.getMessage(), e);
106             return false;
107         }
108     }
109 
110     @Override
111     protected void operateOnNode(InstallContext installContext, Node user) {
112         try {
113             final Node aclNode = NodeUtil.createPath(user, "acl_" + workspaceName, NodeTypes.ContentNode.NAME);
114             final String path = StringUtils.replace(this.path, USER_PATH_REPLACEMENT, user.getPath());
115 
116             if (permissionExists(aclNode, path)) {
117                 installContext.warn(String.format("User '%s' has already permission for '%s:%s'.", user.getPath(), workspaceName, path));
118             } else {
119                 final String nodeName = nodeNameHelper.getUniqueName(aclNode, "0");
120                 final Node permissionNode = aclNode.addNode(nodeName, NodeTypes.ContentNode.NAME);
121                 permissionNode.setProperty("path", path);
122                 permissionNode.setProperty("permissions", permission);
123             }
124         } catch (RepositoryException e) {
125             installContext.error(e.getMessage(), e);
126         }
127     }
128 
129     private boolean permissionExists(Node aclNode, String path) throws RepositoryException {
130         NodeIterator children = aclNode.getNodes();
131         while (children.hasNext()) {
132             Node child = children.nextNode();
133             if (child.hasProperty("path") && child.getProperty("path").getString().equals(path)) {
134                 return true;
135             }
136         }
137         return false;
138     }
139 }