1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.filter.output; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.lang.reflect.Type; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Date; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.List; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
import javax.inject.Inject; |
31 |
|
import javax.inject.Named; |
32 |
|
import javax.inject.Provider; |
33 |
|
|
34 |
|
import org.apache.commons.lang3.reflect.TypeUtils; |
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
37 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
38 |
|
import org.xwiki.component.manager.ComponentManager; |
39 |
|
import org.xwiki.filter.FilterEventParameters; |
40 |
|
import org.xwiki.filter.FilterException; |
41 |
|
import org.xwiki.filter.event.user.UserFilter; |
42 |
|
import org.xwiki.filter.output.AbstractBeanOutputFilterStream; |
43 |
|
import org.xwiki.model.EntityType; |
44 |
|
import org.xwiki.model.reference.DocumentReference; |
45 |
|
import org.xwiki.model.reference.EntityReference; |
46 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
47 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
48 |
|
import org.xwiki.properties.ConverterManager; |
49 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
50 |
|
|
51 |
|
import com.xpn.xwiki.XWikiContext; |
52 |
|
import com.xpn.xwiki.XWikiException; |
53 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
54 |
|
import com.xpn.xwiki.objects.BaseObject; |
55 |
|
import com.xpn.xwiki.objects.classes.BaseClass; |
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Component |
62 |
|
@Named(UserInstanceOutputFilterStreamFactory.ROLEHINT) |
63 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
|
|
| 83% |
Uncovered Elements: 25 (147) |
Complexity: 46 |
Complexity Density: 0.47 |
|
64 |
|
public class UserInstanceOutputFilterStream extends AbstractBeanOutputFilterStream<UserInstanceOutputProperties> |
65 |
|
implements UserInstanceOutputFilter |
66 |
|
{ |
67 |
|
private static final EntityReference DEFAULT_SPACE = new EntityReference("XWiki", EntityType.SPACE); |
68 |
|
|
69 |
|
@Inject |
70 |
|
@Named("relative") |
71 |
|
private EntityReferenceResolver<String> relativeResolver; |
72 |
|
|
73 |
|
@Inject |
74 |
|
private WikiDescriptorManager wikis; |
75 |
|
|
76 |
|
@Inject |
77 |
|
private EntityReferenceSerializer<String> serializer; |
78 |
|
|
79 |
|
@Inject |
80 |
|
private Provider<XWikiContext> xcontextProvider; |
81 |
|
|
82 |
|
@Inject |
83 |
|
private ConverterManager converter; |
84 |
|
|
85 |
|
@Inject |
86 |
|
@Named("context") |
87 |
|
private Provider<ComponentManager> componentManagerProvider; |
88 |
|
|
89 |
|
private String currentWiki; |
90 |
|
|
91 |
|
private List<String> members; |
92 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
93 |
0 |
@Override... |
94 |
|
public void close() throws IOException |
95 |
|
{ |
96 |
|
|
97 |
|
} |
98 |
|
|
|
|
| 57.1% |
Uncovered Elements: 6 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
99 |
21 |
private <T> T get(Type type, String key, FilterEventParameters parameters, T def)... |
100 |
|
{ |
101 |
21 |
if (!parameters.containsKey(key)) { |
102 |
0 |
return def; |
103 |
|
} |
104 |
|
|
105 |
21 |
Object value = parameters.get(key); |
106 |
|
|
107 |
21 |
if (value == null) { |
108 |
0 |
return null; |
109 |
|
} |
110 |
|
|
111 |
21 |
if (TypeUtils.isInstance(value, type)) { |
112 |
21 |
return (T) value; |
113 |
|
} |
114 |
|
|
115 |
0 |
return this.converter.convert(type, value); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
9 |
private Date getDate(String key, FilterEventParameters parameters, Date def)... |
119 |
|
{ |
120 |
9 |
return get(Date.class, key, parameters, def); |
121 |
|
} |
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
123 |
9 |
private String getString(String key, FilterEventParameters parameters, String def)... |
124 |
|
{ |
125 |
9 |
return get(String.class, key, parameters, def); |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
128 |
3 |
private boolean getBoolean(String key, FilterEventParameters parameters, boolean def)... |
129 |
|
{ |
130 |
3 |
return get(boolean.class, key, parameters, def); |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
133 |
6 |
private String getCurrentWiki()... |
134 |
|
{ |
135 |
6 |
String wiki = this.currentWiki; |
136 |
|
|
137 |
6 |
if (wiki == null) { |
138 |
1 |
wiki = this.wikis.getCurrentWikiId(); |
139 |
|
} |
140 |
|
|
141 |
6 |
return wiki; |
142 |
|
} |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
144 |
3 |
private DocumentReference getUserDocumentReference(String id)... |
145 |
|
{ |
146 |
3 |
return new DocumentReference(getCurrentWiki(), DEFAULT_SPACE.getName(), id); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
149 |
3 |
private XWikiDocument getUserDocument(String id) throws XWikiException... |
150 |
|
{ |
151 |
3 |
XWikiContext xcontext = this.xcontextProvider.get(); |
152 |
|
|
153 |
3 |
return xcontext.getWiki().getDocument(getUserDocumentReference(id), xcontext); |
154 |
|
} |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
156 |
3 |
private DocumentReference getGroupDocumentReference(String id)... |
157 |
|
{ |
158 |
3 |
return new DocumentReference(getCurrentWiki(), DEFAULT_SPACE.getName(), id); |
159 |
|
} |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
161 |
3 |
private XWikiDocument getGroupDocument(String id) throws XWikiException... |
162 |
|
{ |
163 |
3 |
XWikiContext xcontext = this.xcontextProvider.get(); |
164 |
|
|
165 |
3 |
return xcontext.getWiki().getDocument(getGroupDocumentReference(id), xcontext); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
9 |
@Override... |
171 |
|
public void beginWiki(String name, FilterEventParameters parameters) throws FilterException |
172 |
|
{ |
173 |
9 |
this.currentWiki = name; |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
176 |
9 |
@Override... |
177 |
|
public void endWiki(String name, FilterEventParameters parameters) throws FilterException |
178 |
|
{ |
179 |
9 |
this.currentWiki = null; |
180 |
|
} |
181 |
|
|
|
|
| 77.8% |
Uncovered Elements: 14 (63) |
Complexity: 16 |
Complexity Density: 0.36 |
|
182 |
3 |
@Override... |
183 |
|
public void beginUser(String name, FilterEventParameters parameters) throws FilterException |
184 |
|
{ |
185 |
3 |
XWikiDocument userDocument; |
186 |
3 |
try { |
187 |
3 |
userDocument = getUserDocument(name); |
188 |
|
|
189 |
|
|
190 |
3 |
userDocument = userDocument.clone(); |
191 |
|
} catch (XWikiException e) { |
192 |
0 |
throw new FilterException("Failed to get an XWikiDocument for user name [" + name + "]", e); |
193 |
|
} |
194 |
|
|
195 |
3 |
Map<String, Object> map = new HashMap<String, Object>(); |
196 |
|
|
197 |
|
|
198 |
3 |
if (parameters.containsKey(PARAMETER_FIRSTNAME)) { |
199 |
3 |
map.put("first_name", getString(PARAMETER_FIRSTNAME, parameters, "")); |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
3 |
if (parameters.containsKey(PARAMETER_LASTNAME)) { |
204 |
3 |
map.put("last_name", getString(PARAMETER_LASTNAME, parameters, "")); |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
3 |
if (parameters.containsKey(PARAMETER_EMAIL)) { |
209 |
3 |
map.put("email", getString(PARAMETER_EMAIL, parameters, "")); |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
3 |
map.put("active", getBoolean(PARAMETER_ACTIVE, parameters, true) ? 1 : 0); |
214 |
|
|
215 |
3 |
XWikiContext xcontext = this.xcontextProvider.get(); |
216 |
|
|
217 |
3 |
BaseClass userClass; |
218 |
3 |
try { |
219 |
3 |
userClass = xcontext.getWiki().getUserClass(xcontext); |
220 |
|
} catch (XWikiException e) { |
221 |
0 |
throw new FilterException("Failed to get user class", e); |
222 |
|
} |
223 |
|
|
224 |
3 |
BaseObject userObject = userDocument.getXObject(userClass.getReference()); |
225 |
3 |
if (userObject == null) { |
226 |
|
|
227 |
3 |
try { |
228 |
3 |
userObject = userDocument.newXObject(userClass.getReference(), xcontext); |
229 |
|
} catch (XWikiException e) { |
230 |
0 |
throw new FilterException("Failed to create user object", e); |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
3 |
try { |
235 |
3 |
xcontext.getWiki().protectUserPage(userDocument.getFullName(), "edit", userDocument, xcontext); |
236 |
|
} catch (XWikiException e) { |
237 |
0 |
throw new FilterException("Failed to initialize user", e); |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
3 |
userClass.fromValueMap(map, userObject); |
243 |
|
|
244 |
3 |
if (userDocument.isNew()) { |
245 |
|
|
246 |
3 |
userDocument.setCreatorReference(userDocument.getDocumentReference()); |
247 |
3 |
userDocument.setAuthorReference(userDocument.getDocumentReference()); |
248 |
3 |
userDocument.setContentAuthorReference(userDocument.getDocumentReference()); |
249 |
|
|
250 |
|
|
251 |
3 |
if (this.properties.isVersionPreserved()) { |
252 |
3 |
if (parameters.containsKey(UserFilter.PARAMETER_CREATION_DATE)) { |
253 |
3 |
userDocument.setCreationDate(getDate(UserFilter.PARAMETER_CREATION_DATE, parameters, new Date())); |
254 |
|
} |
255 |
3 |
if (parameters.containsKey(UserFilter.PARAMETER_REVISION_DATE)) { |
256 |
3 |
userDocument.setDate(getDate(UserFilter.PARAMETER_REVISION_DATE, parameters, new Date())); |
257 |
3 |
userDocument |
258 |
|
.setContentUpdateDate(getDate(UserFilter.PARAMETER_REVISION_DATE, parameters, new Date())); |
259 |
|
} |
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
3 |
userDocument.setMetaDataDirty(false); |
264 |
3 |
userDocument.setContentDirty(false); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
3 |
try { |
269 |
3 |
xcontext.getWiki().saveDocument(userDocument, this.properties.getSaveComment(), xcontext); |
270 |
|
} catch (XWikiException e) { |
271 |
0 |
throw new FilterException("Failed to save user document", e); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
3 |
try { |
276 |
3 |
xcontext.getWiki().setUserDefaultGroup(userDocument.getFullName(), xcontext); |
277 |
|
} catch (XWikiException e) { |
278 |
0 |
throw new FilterException("Failed to add user to default groups", e); |
279 |
|
} |
280 |
|
} |
281 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
282 |
3 |
@Override... |
283 |
|
public void endUser(String name, FilterEventParameters parameters) throws FilterException |
284 |
|
{ |
285 |
|
|
286 |
|
} |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
288 |
3 |
@Override... |
289 |
|
@Deprecated |
290 |
|
public void beginGroup(String name, FilterEventParameters parameters) throws FilterException |
291 |
|
{ |
292 |
3 |
beginGroupContainer(name, parameters); |
293 |
|
} |
294 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
295 |
3 |
@Override... |
296 |
|
public void beginGroupContainer(String name, FilterEventParameters parameters) throws FilterException |
297 |
|
{ |
298 |
|
|
299 |
3 |
this.members = new ArrayList<String>(); |
300 |
|
} |
301 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
302 |
4 |
private void addMember(String member, XWikiDocument groupDocument, BaseClass groupClass, XWikiContext xcontext)... |
303 |
|
throws FilterException |
304 |
|
{ |
305 |
4 |
BaseObject memberObject; |
306 |
4 |
try { |
307 |
4 |
memberObject = groupDocument.newXObject(groupClass.getReference(), xcontext); |
308 |
|
} catch (XWikiException e) { |
309 |
0 |
throw new FilterException("Failed to add a group member object", e); |
310 |
|
} |
311 |
|
|
312 |
4 |
memberObject.setStringValue("member", member); |
313 |
|
} |
314 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
315 |
3 |
@Override... |
316 |
|
@Deprecated |
317 |
|
public void endGroup(String name, FilterEventParameters parameters) throws FilterException |
318 |
|
{ |
319 |
3 |
endGroupContainer(name, parameters); |
320 |
|
} |
321 |
|
|
|
|
| 84.2% |
Uncovered Elements: 3 (19) |
Complexity: 5 |
Complexity Density: 0.29 |
|
322 |
3 |
@Override... |
323 |
|
public void endGroupContainer(String name, FilterEventParameters parameters) throws FilterException |
324 |
|
{ |
325 |
3 |
XWikiContext xcontext = this.xcontextProvider.get(); |
326 |
|
|
327 |
3 |
XWikiDocument groupDocument; |
328 |
3 |
try { |
329 |
3 |
groupDocument = getGroupDocument(name); |
330 |
|
} catch (XWikiException e) { |
331 |
0 |
throw new FilterException("Failed to get an XWikiDocument for group name [" + name + "]", e); |
332 |
|
} |
333 |
|
|
334 |
3 |
BaseClass groupClass; |
335 |
3 |
try { |
336 |
3 |
groupClass = xcontext.getWiki().getGroupClass(xcontext); |
337 |
|
} catch (XWikiException e) { |
338 |
0 |
throw new FilterException("Failed to get group class", e); |
339 |
|
} |
340 |
|
|
341 |
3 |
if (this.members.isEmpty()) { |
342 |
|
|
343 |
1 |
addMember("", groupDocument, groupClass, xcontext); |
344 |
|
} else { |
345 |
2 |
for (String member : this.members) { |
346 |
3 |
addMember(member, groupDocument, groupClass, xcontext); |
347 |
|
} |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
3 |
try { |
352 |
3 |
xcontext.getWiki().saveDocument(groupDocument, this.properties.getSaveComment(), xcontext); |
353 |
|
} catch (XWikiException e) { |
354 |
0 |
throw new FilterException("Failed to save group document", e); |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
3 |
this.members = null; |
359 |
|
} |
360 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
361 |
3 |
private void addMember(String name)... |
362 |
|
{ |
363 |
3 |
EntityReference memberReference = this.relativeResolver.resolve(name, EntityType.DOCUMENT, DEFAULT_SPACE); |
364 |
|
|
365 |
3 |
this.members.add(this.serializer.serialize(memberReference)); |
366 |
|
} |
367 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
368 |
2 |
@Override... |
369 |
|
public void onGroupMemberUser(String name, FilterEventParameters parameters) throws FilterException |
370 |
|
{ |
371 |
2 |
addMember(name); |
372 |
|
} |
373 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
374 |
1 |
@Override... |
375 |
|
public void onGroupMemberGroup(String name, FilterEventParameters parameters) throws FilterException |
376 |
|
{ |
377 |
1 |
addMember(this.properties.getGroupPrefix() + name + this.properties.getGroupSuffix()); |
378 |
|
} |
379 |
|
} |