UserPropertiesImpl.java
6.64 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.granite.security.user.internal;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.internal.AuthorizableInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
public class UserPropertiesImpl
implements UserProperties {
private static final Set<String> RESERVED_PREFIXES = new HashSet<String>();
private final AuthorizableInfo info;
private final Node node;
private final ResourceResolver resourceResolver;
private Resource resource;
UserPropertiesImpl(AuthorizableInfo info, Node node, ResourceResolver resourceResolver) {
if (info == null || node == null) {
throw new IllegalArgumentException("AuthorizableId and user properties node may not be null.");
}
this.info = info;
this.node = node;
this.resourceResolver = resourceResolver;
}
@Override
public String getAuthorizableID() {
return this.info.getId();
}
@Override
public Node getNode() {
return this.node;
}
@Override
public String[] getPropertyNames() throws RepositoryException {
return this.getPropertyNames(null);
}
@Override
public String[] getPropertyNames(String relPath) throws RepositoryException {
ArrayList<String> propNames = new ArrayList<String>();
Node n = relPath == null ? this.node : this.node.getNode(relPath);
PropertyIterator it = n.getProperties();
while (it.hasNext()) {
String propName = it.nextProperty().getName();
if (UserPropertiesImpl.isInternal(propName)) continue;
propNames.add(propName);
}
return propNames.toArray(new String[propNames.size()]);
}
@Override
public String getProperty(String relativePath) throws RepositoryException {
return this.getProperty(relativePath, null, String.class);
}
@Override
public <T> T getProperty(String relativePath, T defaultValue, Class<T> type) throws RepositoryException {
UserPropertiesImpl.checkIsInternal(relativePath);
ValueMap valueMap = this.getValueMap();
Object v = valueMap.get(relativePath, type);
return v == null ? defaultValue : v;
}
@Override
public Resource getResource(String relativePath) throws RepositoryException {
UserPropertiesImpl.checkIsInternal(relativePath);
return this.resourceResolver.getResource(this.getResource(), relativePath);
}
@Override
public Iterator<Resource> getResources(String relativePath) throws RepositoryException {
UserPropertiesImpl.checkIsInternal(relativePath);
Resource resource = this.getResource(relativePath);
if (resource == null) {
return Collections.EMPTY_SET.iterator();
}
return this.resourceResolver.listChildren(resource);
}
@Override
public String getResourcePath(String relativePath, String suffix, String defaultPath) throws RepositoryException {
UserPropertiesImpl.checkIsInternal(relativePath);
Resource resource = this.getResource(relativePath);
if (resource == null) {
return defaultPath;
}
String path = resource.getPath();
return suffix != null ? path + suffix : path;
}
@Override
public String getDisplayName() throws RepositoryException {
return this.getDisplayName(null);
}
@Override
public String getDisplayName(String nameDisplayOrder) throws RepositoryException {
String formattedName = this.getProperty("displayName");
if (formattedName == null) {
StringBuilder bld = new StringBuilder();
if (nameDisplayOrder != null) {
String[] nameTypes;
for (String nameType : nameTypes = nameDisplayOrder.split(" ")) {
UserPropertiesImpl.append(bld, this.getProperty(nameType));
}
} else {
UserPropertiesImpl.append(bld, this.getProperty("givenName"));
UserPropertiesImpl.append(bld, this.getProperty("middleName"));
UserPropertiesImpl.append(bld, this.getProperty("familyName"));
}
formattedName = bld.toString();
}
return formattedName.length() > 0 ? formattedName : this.getAuthorizableID();
}
private Resource getResource() throws RepositoryException {
if (this.resource == null) {
this.resource = this.resourceResolver.getResource(this.node.getPath());
}
return this.resource;
}
private ValueMap getValueMap() throws RepositoryException {
Resource resource = this.getResource();
return resource == null ? ValueMap.EMPTY : ResourceUtil.getValueMap((Resource)resource);
}
private static void append(StringBuilder formatted, String prop) {
if (prop != null && prop.length() > 0) {
if (formatted.length() > 0) {
formatted.append(" ");
}
formatted.append(prop);
}
}
private static boolean isInternal(String relPath) {
String propertyName = Text.getName((String)relPath);
for (String reservedPrefix : RESERVED_PREFIXES) {
if (!propertyName.startsWith(reservedPrefix)) continue;
return true;
}
return false;
}
private static void checkIsInternal(String relPath) {
if (UserPropertiesImpl.isInternal(relPath)) {
throw new IllegalArgumentException("Internal property name " + relPath);
}
}
@Override
public boolean isGroupProperties() {
return this.info.isGroup();
}
@Override
public String getAuthorizablePath() {
return this.info.getPath();
}
static {
RESERVED_PREFIXES.add("jcr:");
RESERVED_PREFIXES.add("rep:");
RESERVED_PREFIXES.add("sling:");
}
}