View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.security.app.dialog.field;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.cms.security.PermissionImpl;
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.jcr.util.NodeUtil;
40  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.Collections;
44  import java.util.List;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.lang3.StringUtils;
50  
51  /**
52   * A simple ACL representation reading entries from, and saving them to a given JCR node.
53   * Entries simply consist of a permission value and a path.
54   *
55   * @param <E> the type of Access Control Entries (ACEs) managed by this list.
56   *
57   * @see WebAccessFieldFactory
58   */
59  public class AccessControlList<E extends AccessControlList.Entry> {
60  
61      /** @deprecated since 5.4.8, intended for internal usage only (package visibility) */
62      @Deprecated
63      public static final String PATH_PROPERTY_NAME = "path";
64  
65      /** @deprecated since 5.4.8, intended for internal usage only (package visibility) */
66      @Deprecated
67      public static final String PERMISSIONS_PROPERTY_NAME = "permissions";
68  
69      /** @deprecated since 5.4.8, moved to {@link WorkspaceAccessControlList#ACCESS_TYPE_NODE} */
70      @Deprecated
71      public static final long ACCESS_TYPE_NODE = 1;
72  
73      /** @deprecated since 5.4.8, moved to {@link WorkspaceAccessControlList#ACCESS_TYPE_CHILDREN} */
74      @Deprecated
75      public static final long ACCESS_TYPE_CHILDREN = 2;
76  
77      /** @deprecated since 5.4.8, moved to {@link WorkspaceAccessControlList#ACCESS_TYPE_NODE_AND_CHILDREN} */
78      @Deprecated
79      public static final long ACCESS_TYPE_NODE_AND_CHILDREN = ACCESS_TYPE_NODE | ACCESS_TYPE_CHILDREN;
80  
81      private List<E> entries = new ArrayList<>();
82  
83      public Collection<E> getEntries() {
84          return Collections.unmodifiableCollection(entries);
85      }
86  
87      public void addEntry(E entry) {
88          entries.add(entry);
89      }
90  
91      public void removeEntry(E entry) {
92          entries.remove(entry);
93      }
94  
95      /**
96       * Initializes this {@link AccessControlList} by reading existing {@linkplain AccessControlList.Entry entries} from the given ACL node.
97       *
98       * @param aclNode the JCR node corresponding to the given ACL, usually named <code>acl_&lt;foobar&gt;</code>
99       */
100     public void readEntries(Node aclNode) throws RepositoryException {
101         this.entries.clear();
102         Collection<E> entries = createEntries(aclNode);
103         this.entries.addAll(entries);
104     }
105 
106     protected Collection<E> createEntries(Node aclNode) throws RepositoryException {
107         List<E> entries = new ArrayList<>();
108         for (Node entryNode : NodeUtil.getNodes(aclNode)) {
109             E entry = createEntry(entryNode);
110             entries.add(entry);
111         }
112         return entries;
113     }
114 
115     /**
116      * Default implementation simply retrieves the path and permissions values from corresponding JCR properties,
117      * and delegates entry construction to {@link #doCreateRawEntry(long, String)}.
118      */
119     protected E createEntry(Node entryNode) throws RepositoryException {
120         long permissions = entryNode.getProperty(PERMISSIONS_PROPERTY_NAME).getLong();
121         String path = entryNode.getProperty(PATH_PROPERTY_NAME).getString();
122         return doCreateRawEntry(permissions, path);
123     }
124 
125     /**
126      * Subclasses of {@link AccessControlList} may override this to create their own specialized {@link AccessControlList.Entry Entries}.
127      */
128     protected E doCreateRawEntry(long permissions, String path) {
129         return (E) new Entry(permissions, path);
130     }
131 
132     /**
133      * Saves {@linkplain AccessControlList.Entry entries} of this {@link AccessControlList} (back) to the given ACL node.
134      * @param aclNode the JCR node corresponding to the given ACL, usually named <code>acl_&lt;foobar&gt;</code>
135      */
136     public void saveEntries(Node aclNode) throws RepositoryException {
137         for (E entry : entries) {
138             // Ignore entries with empty path
139             if (StringUtils.isNotEmpty(entry.getPath())) {
140                 Node entryNode = aclNode.addNode(Path.getUniqueLabel(aclNode, "0"), NodeTypes.ContentNode.NAME);
141                 entryNode.setProperty(PATH_PROPERTY_NAME, entry.getPath());
142                 entryNode.setProperty(PERMISSIONS_PROPERTY_NAME, entry.getPermissions());
143             }
144         }
145     }
146 
147     /** @deprecated since 5.4.8, replaced by {@link #addEntry(Entry)} and {@link #createEntry(Node)} */
148     @Deprecated
149     public void readEntry(Node entryNode) throws RepositoryException {
150         addEntry(createEntry(entryNode));
151     }
152 
153     /** @deprecated since 5.4.8, replaced by {@link #createEntry(Node)} and {@link #doCreateRawEntry(long, String)}*/
154     @Deprecated
155     public Entry getEntryByNode(Node entryNode) throws RepositoryException {
156         return createEntry(entryNode);
157     }
158 
159     /**
160      * An entry in the access control list.
161      */
162     public static class Entry {
163 
164         private long permissions;
165         private String path;
166 
167         public Entry(long permissions, String path) {
168             this.permissions = permissions;
169             this.path = path;
170         }
171 
172         /** @deprecated since 5.4.8, accessType was moved to {@link WorkspaceAccessControlList.Entry#merge(WorkspaceAccessControlList.Entry)} */
173         @Deprecated
174         public Entry(long permissions, long accessType, String path) {
175             this(permissions, path);
176         }
177 
178         public long getPermissions() {
179             return permissions;
180         }
181 
182         public void setPermissions(long permissions) {
183             this.permissions = permissions;
184         }
185 
186         public String getPath() {
187             return path;
188         }
189 
190         public void setPath(String path) {
191             this.path = path;
192         }
193 
194         @Deprecated
195         public long getAccessType() {
196             return 0;
197         }
198 
199         @Deprecated
200         public void setAccessType(long accessType) {
201         }
202 
203         /** @deprecated since 5.4.8, moved to {@link WorkspaceAccessControlList.Entry#merge(WorkspaceAccessControlList.Entry)} */
204         @Deprecated
205         public void merge(Entry entry) {
206         }
207 
208         /** @deprecated since 5.4.8, keys are not needed any more on simple entries without access type */
209         @Deprecated
210         public EntryKey createKey() {
211             return new EntryKey(permissions, path);
212         }
213 
214         @Override
215         public boolean equals(Object o) {
216             if (this == o) return true;
217             if (o == null || getClass() != o.getClass()) return false;
218 
219             Entry entry = (Entry) o;
220 
221             if (permissions != entry.permissions) return false;
222             return path.equals(entry.path);
223 
224         }
225 
226         @Override
227         public int hashCode() {
228             int result = (int) (permissions ^ (permissions >>> 32));
229             result = 31 * result + path.hashCode();
230             return result;
231         }
232 
233         @Override
234         public String toString() {
235             return String.format("AccessControlList.Entry: %s\t\"%s\"", PermissionImpl.getPermissionAsName(permissions), path);
236         }
237     }
238 
239     /**
240      * Used for testing equality of entries.
241      *
242      * @deprecated since 5.4.8, equality is implemented on the entry directly, while same-key merging logic was moved
243      * to the more specific {@link WorkspaceAccessControlList}
244      */
245     @Deprecated
246     public static final class EntryKey extends Entry {
247         public EntryKey(long permissions, String path) {
248             super(permissions, path);
249         }
250     }
251 }