UserPropertiesServlet.java
17.6 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.xss.JSONUtil
* com.adobe.granite.xss.XSSFilter
* javax.jcr.AccessDeniedException
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFactory
* javax.servlet.ServletException
* javax.servlet.http.HttpServletResponse
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.HtmlResponse
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.security.user.internal.servlets;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesComposite;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.security.user.UserPropertiesService;
import com.adobe.granite.security.user.internal.servlets.AbstractServlet;
import com.adobe.granite.xss.JSONUtil;
import com.adobe.granite.xss.XSSFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import javax.jcr.AccessDeniedException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.paths", value={"rep/User/userproperties.json.GET.servlet", "rep/Group/userproperties.json.GET.servlet", "rep/SystemUser/userproperties.json.GET.servlet", "cq/Preferences/userproperties.json.GET.servlet", "cq/security/components/profile/userproperties.json.GET.servlet", "granite/security/search/profile/userproperties.json.GET.servlet", "rep/User/userproperties.html.POST.servlet", "rep/Group/userproperties.html.POST.servlet", "rep/SystemUser/userproperties.html.POST.servlet", "cq/Preferences/userproperties.html.POST.servlet", "cq/security/components/profile/userproperties.html.POST.servlet"})})
public class UserPropertiesServlet
extends AbstractServlet {
private static final Logger log = LoggerFactory.getLogger(UserPropertiesServlet.class);
private static final Set<String> RESERVED = Collections.singleton("path");
private static final String PARAM_AUTHID = "authId";
@Reference
private UserPropertiesService service;
@Reference
private XSSFilter xss;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block19 : {
HtmlResponse errorResponse = null;
ResourceResolver resolver = request.getResourceResolver();
Resource resource = request.getResource();
try {
if (this.service != null) {
Authorizable authorizable = (Authorizable)resource.adaptTo(Authorizable.class);
if (authorizable == null) {
String authId = request.getParameter("authId");
if (authId != null) {
String relRootPath = request.getParameter("path");
Session session = ((Node)resource.adaptTo(Node.class)).getSession();
UserPropertiesManager mgr = this.service.createUserPropertiesManager(session, resolver);
UserPropertiesComposite upc = mgr.getUserPropertiesComposite(authId, relRootPath);
this.userPropertiesCompositeToJson(request.getRequestURI() + "?" + request.getQueryString(), authId, response, resource, authorizable, upc);
} else {
UserProperties userProperties = (UserProperties)resource.adaptTo(UserProperties.class);
if (userProperties != null) {
UserManager uMgr = (UserManager)resolver.adaptTo(UserManager.class);
authorizable = uMgr.getAuthorizable(userProperties.getAuthorizableID());
}
this.userPropertiesToJson(request, response, resource, authorizable, userProperties);
}
} else {
Session session = ((Node)resource.adaptTo(Node.class)).getSession();
UserPropertiesManager mgr = this.service.createUserPropertiesManager(session, resolver);
String propPath = request.getParameter("path");
if (propPath != null) {
String[] paths = propPath.split(",");
if (paths.length < 2) {
UserProperties userProperties = mgr.getUserProperties(authorizable, paths[0]);
this.userPropertiesToJson(request, response, resource, authorizable, userProperties);
} else {
UserPropertiesComposite upc = mgr.getUserPropertiesComposite(authorizable.getID(), paths);
this.userPropertiesCompositeToJson(request.getRequestURI(), propPath, response, resource, authorizable, upc);
}
}
}
break block19;
}
String msg = "UserPropertiesService not available";
log.warn(msg);
errorResponse = UserPropertiesServlet.createErrorResponse(503, msg);
}
catch (AccessDeniedException e) {
log.debug(e.getMessage());
errorResponse = UserPropertiesServlet.createErrorResponse(401, (Exception)e);
}
catch (PathNotFoundException e) {
log.debug(e.getMessage());
errorResponse = UserPropertiesServlet.createErrorResponse(404, (Exception)e);
}
catch (Exception e) {
log.debug(e.getMessage());
errorResponse = UserPropertiesServlet.createErrorResponse(500, e);
}
finally {
if (errorResponse != null) {
errorResponse.send((HttpServletResponse)response, true);
}
}
}
}
private void userPropertiesToJson(SlingHttpServletRequest request, SlingHttpServletResponse response, Resource resource, Authorizable authorizable, UserProperties userProperties) throws IOException, JSONException, RepositoryException {
UserPropertiesServlet.setJsonResponseHeader((HttpServletResponse)response);
JSONWriter writer = new JSONWriter((Writer)response.getWriter());
writer.object();
if (userProperties != null) {
String path = userProperties.getNode().getPath();
writer.key("path").value((Object)path);
UserPropertiesServlet.writeUser(userProperties.getAuthorizableID(), authorizable, writer);
block4 : for (String propName : userProperties.getPropertyNames()) {
String[] values = userProperties.getProperty(propName, null, String[].class);
switch (values.length) {
case 0: {
writer.key(propName).value((Object)"");
continue block4;
}
case 1: {
JSONUtil.writeWithProtected((JSONWriter)writer, (String)propName, (String)values[0], (XSSFilter)this.xss);
continue block4;
}
default: {
JSONUtil.writeWithProtected((JSONWriter)writer, (String)propName, (String[])values, (XSSFilter)this.xss);
}
}
}
} else {
log.debug("Cannot access user properties at {0} ('path' parameter: {1})", (Object)resource.getPath(), (Object)request.getParameter("path"));
}
writer.endObject();
}
private void userPropertiesCompositeToJson(String path, String param, SlingHttpServletResponse response, Resource resource, Authorizable authorizable, UserPropertiesComposite upc) throws IOException, JSONException, RepositoryException {
UserPropertiesServlet.setJsonResponseHeader((HttpServletResponse)response);
JSONWriter writer = new JSONWriter((Writer)response.getWriter());
writer.object();
if (upc != null) {
writer.key("path").value((Object)path);
writer.key("composite").array();
for (String p : upc.getUserPropertiesPaths()) {
writer.value((Object)p);
}
writer.endArray();
UserPropertiesServlet.writeUser(upc.getAuthorizableId(), authorizable, writer);
block5 : for (String propName : upc.getPropertyNames()) {
String[] values = upc.getProperty(propName, null, String[].class);
switch (values.length) {
case 0: {
writer.key(propName).value((Object)"");
continue block5;
}
case 1: {
JSONUtil.writeWithProtected((JSONWriter)writer, (String)propName, (String)values[0], (XSSFilter)this.xss);
continue block5;
}
}
JSONUtil.writeWithProtected((JSONWriter)writer, (String)propName, (String[])values, (XSSFilter)this.xss);
}
} else {
log.debug("Cannot access user properties at {0} (parameter: {1})", (Object)resource.getPath(), (Object)param);
}
writer.endObject();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block36 : {
HtmlResponse errorResponse = null;
ResourceResolver resolver = request.getResourceResolver();
Resource resource = request.getResource();
Session session = null;
try {
if (this.service != null) {
UserProperties userProperties;
session = ((Node)resource.adaptTo(Node.class)).getSession();
UserPropertiesManager mgr = this.service.createUserPropertiesManager(session, resolver);
String propPath = request.getParameter("path");
Authorizable authorizable = (Authorizable)resource.adaptTo(Authorizable.class);
if (authorizable == null) {
userProperties = (UserProperties)resource.adaptTo(UserProperties.class);
if (userProperties != null) {
UserManager uMgr = (UserManager)resolver.adaptTo(UserManager.class);
authorizable = uMgr.getAuthorizable(userProperties.getAuthorizableID());
}
} else {
userProperties = mgr.getUserProperties(authorizable, propPath);
}
if (userProperties == null && propPath != null && authorizable != null) {
String authId = authorizable.getID();
log.debug("Create new user properties for authorizable {0} and path {1}", (Object)authId, (Object)propPath);
userProperties = mgr.createUserProperties(authId, propPath);
}
if (userProperties != null) {
Node n = userProperties.getNode();
RequestParameterMap params = request.getRequestParameterMap();
ValueFactory vf = session.getValueFactory();
block23 : for (String paramName : params.keySet()) {
if (UserPropertiesServlet.isReservedParameter(paramName, RESERVED)) continue;
RequestParameter[] values = params.getValues(paramName);
Value[] vs = new Value[values.length];
for (int i = 0; i < values.length; ++i) {
vs[i] = values[i].isFormField() ? vf.createValue(values[i].getString()) : vf.createValue(values[i].getInputStream());
}
switch (vs.length) {
case 0: {
if (n.hasProperty(paramName)) {
n.getProperty(paramName).remove();
continue block23;
}
log.debug("Ignoring non existing property {0} with empty value.", (Object)paramName);
continue block23;
}
case 1: {
n.setProperty(paramName, vs[0]);
continue block23;
}
}
n.setProperty(paramName, vs);
}
if (session.hasPendingChanges()) {
session.save();
}
} else {
String msg = "Incomplete request to create or update user properties.";
log.warn(msg);
errorResponse = UserPropertiesServlet.createErrorResponse(400, msg);
}
break block36;
}
String msg = "UserPropertiesService not available";
log.warn(msg);
errorResponse = UserPropertiesServlet.createErrorResponse(503, msg);
}
catch (AccessDeniedException e) {
log.debug(e.getMessage());
errorResponse = UserPropertiesServlet.createErrorResponse(401, (Exception)e);
}
catch (PathNotFoundException e) {
log.debug(e.getMessage());
errorResponse = UserPropertiesServlet.createErrorResponse(404, (Exception)e);
}
catch (Exception e) {
log.debug(e.getMessage());
errorResponse = UserPropertiesServlet.createErrorResponse(500, e);
}
finally {
if (errorResponse != null) {
try {
if (session != null) {
session.refresh(false);
}
}
catch (RepositoryException e) {
log.warn("Cannot revert transient modifications.", (Object)e.getMessage());
}
errorResponse.send((HttpServletResponse)response, true);
}
}
}
}
private static void writeUser(String authzId, Authorizable authorizable, JSONWriter writer) throws JSONException, RepositoryException {
writer.key("user");
writer.object();
if (authorizable != null) {
writer.key("authorizableId").value((Object)authorizable.getID());
writer.key("home").value((Object)authorizable.getPath());
} else {
writer.key("authorizableId").value((Object)authzId);
}
writer.endObject();
}
protected void bindService(UserPropertiesService userPropertiesService) {
this.service = userPropertiesService;
}
protected void unbindService(UserPropertiesService userPropertiesService) {
if (this.service == userPropertiesService) {
this.service = null;
}
}
protected void bindXss(XSSFilter xSSFilter) {
this.xss = xSSFilter;
}
protected void unbindXss(XSSFilter xSSFilter) {
if (this.xss == xSSFilter) {
this.xss = null;
}
}
}