1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
53
54
55
56
57
58
59 public class AccessControlList<E extends AccessControlList.Entry> {
60
61
62 @Deprecated
63 public static final String PATH_PROPERTY_NAME = "path";
64
65
66 @Deprecated
67 public static final String PERMISSIONS_PROPERTY_NAME = "permissions";
68
69
70 @Deprecated
71 public static final long ACCESS_TYPE_NODE = 1;
72
73
74 @Deprecated
75 public static final long ACCESS_TYPE_CHILDREN = 2;
76
77
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
97
98
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
117
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
127
128 protected E doCreateRawEntry(long permissions, String path) {
129 return (E) new Entry(permissions, path);
130 }
131
132
133
134
135
136 public void saveEntries(Node aclNode) throws RepositoryException {
137 for (E entry : entries) {
138
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
148 @Deprecated
149 public void readEntry(Node entryNode) throws RepositoryException {
150 addEntry(createEntry(entryNode));
151 }
152
153
154 @Deprecated
155 public Entry getEntryByNode(Node entryNode) throws RepositoryException {
156 return createEntry(entryNode);
157 }
158
159
160
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
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
204 @Deprecated
205 public void merge(Entry entry) {
206 }
207
208
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
241
242
243
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 }