1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.filter.output

File UserInstanceOutputFilterStream.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

28
97
22
1
379
269
46
0.47
4.41
22
2.09

Classes

Class Line # Actions
UserInstanceOutputFilterStream 64 97 0% 46 29
0.802721180.3%
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: 7fae3e9da74766b67680f579274b538bd017489e $
59    * @since 6.2M1
60    */
61    @Component
62    @Named(UserInstanceOutputFilterStreamFactory.ROLEHINT)
63    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
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   
 
93  0 toggle @Override
94    public void close() throws IOException
95    {
96    // Nothing to close
97    }
98   
 
99  21 toggle 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   
 
118  9 toggle private Date getDate(String key, FilterEventParameters parameters, Date def)
119    {
120  9 return get(Date.class, key, parameters, def);
121    }
122   
 
123  9 toggle private String getString(String key, FilterEventParameters parameters, String def)
124    {
125  9 return get(String.class, key, parameters, def);
126    }
127   
 
128  3 toggle private boolean getBoolean(String key, FilterEventParameters parameters, boolean def)
129    {
130  3 return get(boolean.class, key, parameters, def);
131    }
132   
 
133  6 toggle 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   
 
144  3 toggle private DocumentReference getUserDocumentReference(String id)
145    {
146  3 return new DocumentReference(getCurrentWiki(), DEFAULT_SPACE.getName(), id);
147    }
148   
 
149  3 toggle 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   
 
156  3 toggle private DocumentReference getGroupDocumentReference(String id)
157    {
158  3 return new DocumentReference(getCurrentWiki(), DEFAULT_SPACE.getName(), id);
159    }
160   
 
161  3 toggle 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    // Events
169   
 
170  11 toggle @Override
171    public void beginWiki(String name, FilterEventParameters parameters) throws FilterException
172    {
173  11 this.currentWiki = name;
174    }
175   
 
176  11 toggle @Override
177    public void endWiki(String name, FilterEventParameters parameters) throws FilterException
178    {
179  11 this.currentWiki = null;
180    }
181   
 
182  3 toggle @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    // Safer to clone for thread safety and in case the save fail
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    // First name
198  3 if (parameters.containsKey(PARAMETER_FIRSTNAME)) {
199  3 map.put("first_name", getString(PARAMETER_FIRSTNAME, parameters, ""));
200    }
201   
202    // Last name
203  3 if (parameters.containsKey(PARAMETER_LASTNAME)) {
204  3 map.put("last_name", getString(PARAMETER_LASTNAME, parameters, ""));
205    }
206   
207    // Email
208  3 if (parameters.containsKey(PARAMETER_EMAIL)) {
209  3 map.put("email", getString(PARAMETER_EMAIL, parameters, ""));
210    }
211   
212    // Active
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    // Create object
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    // Setup right on user profile
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    // Update user properties
242  3 userClass.fromValueMap(map, userObject);
243   
244  3 if (userDocument.isNew()) {
245    // Authors
246  3 userDocument.setCreatorReference(userDocument.getDocumentReference());
247  3 userDocument.setAuthorReference(userDocument.getDocumentReference());
248  3 userDocument.setContentAuthorReference(userDocument.getDocumentReference());
249   
250    // Dates
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    // Set false to force the date and authors we want
263  3 userDocument.setMetaDataDirty(false);
264  3 userDocument.setContentDirty(false);
265    }
266   
267    // Save
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    // Add the user to default groups
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   
 
282  3 toggle @Override
283    public void endUser(String name, FilterEventParameters parameters) throws FilterException
284    {
285   
286    }
287   
 
288  0 toggle @Override
289    @Deprecated
290    public void beginGroup(String name, FilterEventParameters parameters) throws FilterException
291    {
292  0 beginGroupContainer(name, parameters);
293    }
294   
 
295  3 toggle @Override
296    public void beginGroupContainer(String name, FilterEventParameters parameters) throws FilterException
297    {
298    // Init members
299  3 this.members = new ArrayList<String>();
300    }
301   
 
302  4 toggle 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   
 
315  0 toggle @Override
316    @Deprecated
317    public void endGroup(String name, FilterEventParameters parameters) throws FilterException
318    {
319  0 endGroupContainer(name, parameters);
320    }
321   
 
322  3 toggle @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    // Put an empty member so that the document is "marked" as group
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    // Save
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    // Reset members
358  3 this.members = null;
359    }
360   
 
361  3 toggle 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   
 
368  2 toggle @Override
369    public void onGroupMemberUser(String name, FilterEventParameters parameters) throws FilterException
370    {
371  2 addMember(name);
372    }
373   
 
374  1 toggle @Override
375    public void onGroupMemberGroup(String name, FilterEventParameters parameters) throws FilterException
376    {
377  1 addMember(this.properties.getGroupPrefix() + name + this.properties.getGroupSuffix());
378    }
379    }