1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.user.internal

File DefaultWikiUserManager.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

46
224
30
1
658
459
65
0.29
7.47
30
2.17

Classes

Class Line # Actions
DefaultWikiUserManager 60 224 0% 65 264
0.1212%
 

Contributing tests

No tests hitting this source file were found.

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 org.xwiki.wiki.user.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Date;
25    import java.util.List;
26   
27    import javax.inject.Inject;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.lang3.StringUtils;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35    import org.xwiki.model.reference.LocalDocumentReference;
36    import org.xwiki.wiki.descriptor.WikiDescriptor;
37    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
38    import org.xwiki.wiki.manager.WikiManagerException;
39    import org.xwiki.wiki.user.MemberCandidacy;
40    import org.xwiki.wiki.user.MembershipType;
41    import org.xwiki.wiki.user.UserScope;
42    import org.xwiki.wiki.user.WikiUserConfiguration;
43    import org.xwiki.wiki.user.WikiUserManager;
44    import org.xwiki.wiki.user.WikiUserManagerException;
45   
46    import com.xpn.xwiki.XWiki;
47    import com.xpn.xwiki.XWikiContext;
48    import com.xpn.xwiki.XWikiException;
49    import com.xpn.xwiki.doc.XWikiDocument;
50    import com.xpn.xwiki.objects.BaseObject;
51   
52    /**
53    * Default implementation for {@link WikiUserManager}.
54    *
55    * @version $Id: 001d91b247112852af3c78f7e49c5c28ff962c3f $
56    * @since 5.3M2
57    */
58    @Component
59    @Singleton
 
60    public class DefaultWikiUserManager implements WikiUserManager
61    {
62    private static final LocalDocumentReference GROUPCLASS_REFERENCE = new LocalDocumentReference(XWiki.SYSTEM_SPACE,
63    "XWikiGroups");
64   
65    private static final String GROUP_CLASS_MEMBER_FIELD = "member";
66   
67    @Inject
68    private WikiDescriptorManager wikiDescriptorManager;
69   
70    @Inject
71    private WikiUserConfigurationHelper wikiUserConfigurationHelper;
72   
73    @Inject
74    private EntityReferenceSerializer<String> documentReferenceSerializer;
75   
76    @Inject
77    private Provider<XWikiContext> xcontextProvider;
78   
 
79  60 toggle @Override
80    public UserScope getUserScope(String wikiId) throws WikiUserManagerException
81    {
82  60 return wikiUserConfigurationHelper.getConfiguration(wikiId).getUserScope();
83    }
84   
 
85  3 toggle @Override
86    public void setUserScope(String wikiId, UserScope scope) throws WikiUserManagerException
87    {
88  3 WikiUserConfiguration configuration = wikiUserConfigurationHelper.getConfiguration(wikiId);
89  3 configuration.setUserScope(scope);
90  3 wikiUserConfigurationHelper.saveConfiguration(configuration, wikiId);
91    }
92   
 
93  24 toggle @Override
94    public MembershipType getMembershipType(String wikiId) throws WikiUserManagerException
95    {
96  24 return wikiUserConfigurationHelper.getConfiguration(wikiId).getMembershipType();
97    }
98   
 
99  3 toggle @Override
100    public void setMembershipType(String wikiId, MembershipType type) throws WikiUserManagerException
101    {
102  3 WikiUserConfiguration configuration = wikiUserConfigurationHelper.getConfiguration(wikiId);
103  3 configuration.setMembershipType(type);
104  3 wikiUserConfigurationHelper.saveConfiguration(configuration, wikiId);
105    }
106   
 
107  0 toggle @Override
108    public Collection<String> getLocalUsers(String wikiId) throws WikiUserManagerException
109    {
110    // TODO: Implement this method. This is not urgent because no one needs it yet.
111  0 return null;
112    }
113   
 
114  24 toggle private XWikiDocument getMembersGroupDocument(String wikiId) throws WikiUserManagerException
115    {
116    // Reference to the document
117  24 DocumentReference memberGroupReference = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "XWikiAllGroup");
118   
119    // Get the document
120  24 try {
121  24 XWikiContext xcontext = xcontextProvider.get();
122  24 XWiki xwiki = xcontext.getWiki();
123  24 return xwiki.getDocument(memberGroupReference, xcontext);
124    } catch (XWikiException e) {
125  0 throw new WikiUserManagerException(String.format("Fail to load the member group document [%s].",
126    memberGroupReference.toString()), e);
127    }
128    }
129   
 
130  0 toggle private void saveGroupDocument(XWikiDocument document, String message) throws WikiUserManagerException {
131    // Get the XWiki objects
132  0 XWikiContext xcontext = xcontextProvider.get();
133  0 XWiki xwiki = xcontext.getWiki();
134   
135    // The document should be hidden
136  0 document.setHidden(true);
137   
138    // The document must have a creator
139  0 if (document.getCreatorReference() == null) {
140  0 document.setCreatorReference(xcontext.getUserReference());
141    }
142    // The document must have an author
143  0 if (document.getAuthorReference() == null) {
144  0 document.setAuthorReference(xcontext.getUserReference());
145    }
146   
147    // Save the document
148  0 try {
149  0 xwiki.saveDocument(document, message, xcontext);
150    } catch (XWikiException e) {
151  0 throw new WikiUserManagerException("Fail to save the member group", e);
152    }
153    }
154   
 
155  24 toggle @Override
156    public Collection<String> getMembers(String wikiId) throws WikiUserManagerException
157    {
158  24 List<String> members = new ArrayList<>();
159   
160  24 try {
161    // Get the descriptor
162  24 WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
163    // Add the wiki owner
164  24 members.add(descriptor.getOwnerId());
165    } catch (WikiManagerException e) {
166  0 throw new WikiUserManagerException(String.format("Failed to get the descriptor for [%s]", wikiId), e);
167    }
168   
169    // Get the other members from the wiki AllGroup
170  24 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
171  24 List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
172  24 if (memberObjects != null) {
173  24 for (BaseObject object : memberObjects) {
174  24 if (object == null) {
175  0 continue;
176    }
177  24 String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
178  24 if (!member.isEmpty() && !members.contains(member)) {
179  0 members.add(member);
180    }
181    }
182    }
183   
184  24 return members;
185    }
186   
 
187  24 toggle @Override
188    public boolean isMember(String userId, String wikiId) throws WikiUserManagerException
189    {
190  24 return getMembers(wikiId).contains(userId);
191    }
192   
 
193  0 toggle private void addMemberObject(XWikiDocument groupDoc, String userId)
194    throws WikiUserManagerException
195    {
196  0 try {
197  0 XWikiContext xcontext = xcontextProvider.get();
198  0 int objectNumber = groupDoc.createXObject(GROUPCLASS_REFERENCE, xcontext);
199  0 BaseObject object = groupDoc.getXObject(GROUPCLASS_REFERENCE, objectNumber);
200  0 object.set(GROUP_CLASS_MEMBER_FIELD, userId, xcontext);
201    } catch (XWikiException e) {
202  0 throw new WikiUserManagerException("Fail to add a member to the group", e);
203    }
204    }
205   
 
206  0 toggle @Override
207    public void addMember(String userId, String wikiId) throws WikiUserManagerException
208    {
209  0 Collection<String> members = getMembers(wikiId);
210  0 if (members.contains(userId)) {
211    // Nothing to do !
212  0 return;
213    }
214   
215    // Get the group document
216  0 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
217   
218    // Add a member object
219    // If the group does not contain any user yet, add an empty member (cf: XWIKI-6275).
220  0 List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
221  0 if (memberObjects == null || memberObjects.isEmpty()) {
222  0 addMemberObject(groupDoc, "");
223    }
224   
225    // Add the user
226  0 addMemberObject(groupDoc, userId);
227   
228    // Save the document
229  0 saveGroupDocument(groupDoc, String.format("Add [%s] to the group.", userId));
230    }
231   
 
232  0 toggle @Override
233    public void addMembers(Collection<String> userIds, String wikiId) throws WikiUserManagerException
234    {
235  0 Collection<String> members = getMembers(wikiId);
236   
237    // Get the group document
238  0 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
239   
240    // If the group does not contain any user yet, add an empty member (cf: XWIKI-6275).
241  0 List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
242  0 if (memberObjects == null || memberObjects.isEmpty()) {
243  0 addMemberObject(groupDoc, "");
244    }
245   
246    // Add members
247  0 for (String userId : userIds) {
248  0 if (!members.contains(userId)) {
249    // Add a member object
250  0 addMemberObject(groupDoc, userId);
251    }
252    }
253   
254    // Save the document
255  0 saveGroupDocument(groupDoc, "Add members to the group.");
256    }
257   
 
258  0 toggle @Override
259    public void removeMember(String userId, String wikiId) throws WikiUserManagerException
260    {
261    // Get the group document
262  0 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
263   
264    // Get the member objects
265  0 List<BaseObject> objects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
266  0 if (objects != null) {
267   
268    // Get the member objects to remove
269  0 List<BaseObject> objectsToRemove = new ArrayList<>();
270  0 for (BaseObject object : objects) {
271  0 if (object == null) {
272  0 continue;
273    }
274  0 String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
275  0 if (userId.equals(member)) {
276  0 objectsToRemove.add(object);
277    }
278    }
279   
280    // Remove them
281  0 for (BaseObject object : objectsToRemove) {
282  0 groupDoc.removeXObject(object);
283    }
284   
285    // Save the document
286  0 saveGroupDocument(groupDoc, String.format("Remove [%s] from the group.", userId));
287    }
288    }
289   
 
290  0 toggle @Override
291    public void removeMembers(Collection<String> userIds, String wikiId) throws WikiUserManagerException
292    {
293    // Get the group document
294  0 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
295   
296    // Get the member objects
297  0 List<BaseObject> objects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
298  0 if (objects != null) {
299   
300    // Get the member objects to remove
301  0 List<BaseObject> objectsToRemove = new ArrayList<>();
302  0 for (String userId: userIds) {
303  0 for (BaseObject object : objects) {
304  0 if (object == null) {
305  0 continue;
306    }
307  0 String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
308  0 if (userId.equals(member)) {
309  0 objectsToRemove.add(object);
310    }
311    }
312    }
313   
314    // Remove them
315  0 for (BaseObject object : objectsToRemove) {
316  0 groupDoc.removeXObject(object);
317    }
318   
319    // Save the document
320  0 saveGroupDocument(groupDoc, "Remove some users from the group.");
321    }
322    }
323   
 
324  0 toggle private MemberCandidacy readCandidacyFromObject(BaseObject object, String wikiId)
325    {
326  0 MemberCandidacy candidacy = new MemberCandidacy();
327   
328  0 candidacy.setId(object.getNumber());
329  0 candidacy.setWikiId(wikiId);
330  0 candidacy.setUserId(object.getStringValue(WikiCandidateMemberClassInitializer.FIELD_USER));
331  0 candidacy.setUserComment(object.getLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT));
332  0 candidacy.setAdminId(object.getStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN));
333  0 candidacy.setAdminComment(object.getLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT));
334  0 candidacy.setAdminPrivateComment(object.getLargeStringValue(
335    WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT));
336  0 candidacy.setStatus(
337    MemberCandidacy.Status.valueOf(
338    object.getStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS).toUpperCase()));
339  0 candidacy.setType(
340    MemberCandidacy.CandidateType.valueOf(
341    object.getStringValue(WikiCandidateMemberClassInitializer.FIELD_TYPE).toUpperCase())
342    );
343  0 candidacy.setDateOfCreation(object.getDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION));
344  0 candidacy.setDateOfCreation(object.getDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE));
345   
346  0 return candidacy;
347    }
348   
 
349  0 toggle private Collection<MemberCandidacy> getAllMemberCandidacies(String wikiId, MemberCandidacy.CandidateType type)
350    throws WikiUserManagerException
351    {
352    // Get the group document
353  0 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
354   
355    // Collect all the candidacy of the good type
356  0 Collection<MemberCandidacy> candidacies = new ArrayList<>();
357  0 String typeString = type.name().toLowerCase();
358  0 List<BaseObject> candidacyObjects = groupDoc.getXObjects(WikiCandidateMemberClassInitializer.REFERENCE);
359  0 if (candidacyObjects != null) {
360  0 for (BaseObject object : candidacyObjects) {
361  0 if (object != null
362    && object.getStringValue(WikiCandidateMemberClassInitializer.FIELD_TYPE).equals(typeString)) {
363  0 candidacies.add(readCandidacyFromObject(object, wikiId));
364    }
365    }
366    }
367   
368  0 return candidacies;
369    }
370   
 
371  0 toggle @Override
372    public Collection<MemberCandidacy> getAllInvitations(String wikiId) throws WikiUserManagerException
373    {
374  0 return getAllMemberCandidacies(wikiId, MemberCandidacy.CandidateType.INVITATION);
375    }
376   
 
377  0 toggle @Override
378    public Collection<MemberCandidacy> getAllRequests(String wikiId) throws WikiUserManagerException
379    {
380  0 return getAllMemberCandidacies(wikiId, MemberCandidacy.CandidateType.REQUEST);
381    }
382   
 
383  0 toggle @Override
384    public boolean hasPendingInvitation(DocumentReference user, String wikiId) throws WikiUserManagerException
385    {
386  0 Collection<MemberCandidacy> invitations = getAllInvitations(wikiId);
387  0 if (invitations != null) {
388  0 String userId = documentReferenceSerializer.serialize(user);
389  0 for (MemberCandidacy invitation : invitations) {
390  0 if (StringUtils.equals(invitation.getUserId(), userId)
391    && invitation.getStatus() == MemberCandidacy.Status.PENDING) {
392  0 return true;
393    }
394    }
395    }
396    // No pending invitation
397  0 return false;
398    }
399   
 
400  0 toggle @Override
401    public boolean hasPendingRequest(DocumentReference user, String wikiId) throws WikiUserManagerException
402    {
403  0 Collection<MemberCandidacy> requests = getAllRequests(wikiId);
404  0 if (requests != null) {
405  0 String userId = documentReferenceSerializer.serialize(user);
406  0 for (MemberCandidacy request : requests) {
407  0 if (StringUtils.equals(request.getUserId(), userId)
408    && request.getStatus() == MemberCandidacy.Status.PENDING) {
409  0 return true;
410    }
411    }
412    }
413    // No pending request
414  0 return false;
415    }
416   
 
417  0 toggle @Override
418    public MemberCandidacy getCandidacy(String wikiId, int candidacyId) throws WikiUserManagerException
419    {
420    // Get the group document
421  0 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
422   
423    // Get the candidacy
424  0 BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, candidacyId);
425  0 return readCandidacyFromObject(object, wikiId);
426    }
427   
 
428  0 toggle @Override
429    public MemberCandidacy askToJoin(String userId, String wikiId, String message)
430    throws WikiUserManagerException
431    {
432  0 MemberCandidacy candidacy = new MemberCandidacy(wikiId, userId, MemberCandidacy.CandidateType.REQUEST);
433  0 candidacy.setUserComment(message);
434   
435    // Get the group document
436  0 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
437   
438    // Add a candidacy object
439  0 XWikiContext xcontext = xcontextProvider.get();
440  0 try {
441  0 int objectNumber = groupDoc.createXObject(WikiCandidateMemberClassInitializer.REFERENCE, xcontext);
442  0 candidacy.setId(objectNumber);
443  0 BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, objectNumber);
444  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_USER, candidacy.getUserId());
445  0 object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT,
446    candidacy.getUserComment());
447  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS,
448    candidacy.getStatus().name().toLowerCase());
449  0 object.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION,
450    candidacy.getDateOfCreation());
451  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_TYPE,
452    candidacy.getType().name().toLowerCase());
453    } catch (XWikiException e) {
454  0 throw new WikiUserManagerException("Failed to create a new join request.", e);
455    }
456   
457    // Save the document
458  0 saveGroupDocument(groupDoc, String.format("[%s] asks to join the wiki.", userId));
459   
460  0 return candidacy;
461    }
462   
 
463  0 toggle @Override
464    public void join(String userId, String wikiId) throws WikiUserManagerException
465    {
466    // Check if the user has the right to join the wiki
467  0 if (!wikiUserConfigurationHelper.getConfiguration(wikiId).getMembershipType().equals(MembershipType.OPEN)) {
468  0 throw new WikiUserManagerException(String.format("The user [%s] is not authorized to join the wiki [%s].",
469    userId, wikiId));
470    }
471   
472    // Join the wiki
473  0 addMember(userId, wikiId);
474    }
475   
 
476  0 toggle @Override
477    public void leave(String userId, String wikiId) throws WikiUserManagerException
478    {
479  0 removeMember(userId, wikiId);
480    }
481   
 
482  0 toggle @Override
483    public void acceptRequest(MemberCandidacy request, String message, String privateComment)
484    throws WikiUserManagerException
485    {
486    // Add the user to the members
487  0 addMember(request.getUserId(), request.getWikiId());
488   
489    // Then, update the candidacy object
490  0 XWikiContext xcontext = xcontextProvider.get();
491   
492    // Set the values
493  0 request.setAdminId(documentReferenceSerializer.serialize(xcontext.getUserReference()));
494  0 request.setAdminComment(message);
495  0 request.setAdminPrivateComment(privateComment);
496  0 request.setStatus(MemberCandidacy.Status.ACCEPTED);
497  0 request.setDateOfClosure(new Date());
498   
499    // Get the group document
500  0 XWikiDocument groupDoc = getMembersGroupDocument(request.getWikiId());
501   
502    // Get the candidacy object
503  0 BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, request.getId());
504   
505    // Set the new values
506  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN, request.getAdminId());
507  0 object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT, request.getAdminComment());
508  0 object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT,
509    request.getAdminPrivateComment());
510  0 object.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE,
511    request.getDateOfClosure());
512  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS,
513    request.getStatus().name().toLowerCase());
514   
515    // Save the document
516  0 saveGroupDocument(groupDoc, String.format("Accept join request from user [%s]", request.getUserId()));
517    }
518   
 
519  0 toggle @Override
520    public void refuseRequest(MemberCandidacy request, String message, String privateComment)
521    throws WikiUserManagerException
522    {
523    // Update the candidacy object
524  0 XWikiContext xcontext = xcontextProvider.get();
525   
526    // Set the values
527  0 request.setAdminId(documentReferenceSerializer.serialize(xcontext.getUserReference()));
528  0 request.setAdminComment(message);
529  0 request.setAdminPrivateComment(privateComment);
530  0 request.setStatus(MemberCandidacy.Status.REJECTED);
531  0 request.setDateOfClosure(new Date());
532   
533    // Get the group document
534  0 XWikiDocument groupDoc = getMembersGroupDocument(request.getWikiId());
535   
536    // Get the candidacy object
537  0 BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, request.getId());
538   
539    // Set the new values
540  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN, request.getAdminId());
541  0 object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT, request.getAdminComment());
542  0 object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT,
543    request.getAdminPrivateComment());
544  0 object.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE,
545    request.getDateOfClosure());
546  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS,
547    request.getStatus().name().toLowerCase());
548   
549    // Save the document
550  0 saveGroupDocument(groupDoc, String.format("Reject join request from user [%s]", request.getUserId()));
551    }
552   
 
553  0 toggle @Override
554    public void cancelCandidacy(MemberCandidacy candidacy) throws WikiUserManagerException
555    {
556    // Get the group document
557  0 XWikiDocument groupDoc = getMembersGroupDocument(candidacy.getWikiId());
558   
559    // Get the candidacy object
560  0 BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, candidacy.getId());
561   
562    // Remove the candidacy, if any
563  0 if (object != null) {
564  0 groupDoc.removeXObject(object);
565  0 saveGroupDocument(groupDoc, String.format("Candidacy [%d] is canceled.", candidacy.getId()));
566    }
567    }
568   
 
569  0 toggle @Override
570    public MemberCandidacy invite(String userId, String wikiId, String message)
571    throws WikiUserManagerException
572    {
573  0 XWikiContext xcontext = xcontextProvider.get();
574   
575    // Create the candidacy
576  0 MemberCandidacy candidacy = new MemberCandidacy(wikiId, userId, MemberCandidacy.CandidateType.INVITATION);
577  0 candidacy.setUserComment(message);
578  0 candidacy.setAdminId(documentReferenceSerializer.serialize(xcontext.getUserReference()));
579   
580    // Get the group document
581  0 XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
582   
583    // Add a candidacy object
584  0 try {
585  0 int objectNumber = groupDoc.createXObject(WikiCandidateMemberClassInitializer.REFERENCE, xcontext);
586  0 candidacy.setId(objectNumber);
587  0 BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, objectNumber);
588  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_USER, candidacy.getUserId());
589  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN, candidacy.getAdminId());
590  0 object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT, message);
591  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS,
592    candidacy.getStatus().name().toLowerCase());
593  0 object.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION,
594    candidacy.getDateOfCreation());
595  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_TYPE,
596    candidacy.getType().name().toLowerCase());
597    } catch (XWikiException e) {
598  0 throw new WikiUserManagerException("Failed to create a new invitation object.", e);
599    }
600   
601    // Save the document
602  0 saveGroupDocument(groupDoc, String.format("[%s] is invited to join the wiki.", userId));
603   
604  0 return candidacy;
605    }
606   
 
607  0 toggle @Override
608    public void acceptInvitation(MemberCandidacy invitation, String message) throws WikiUserManagerException
609    {
610    // Add the user to the members
611  0 addMember(invitation.getUserId(), invitation.getWikiId());
612   
613    // Set the values
614  0 invitation.setUserComment(message);
615  0 invitation.setStatus(MemberCandidacy.Status.ACCEPTED);
616  0 invitation.setDateOfClosure(new Date());
617   
618    // Get the group document
619  0 XWikiDocument groupDoc = getMembersGroupDocument(invitation.getWikiId());
620   
621    // Get the candidacy object
622  0 BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, invitation.getId());
623   
624    // Set the new values
625  0 object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT, invitation.getUserComment());
626  0 object.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE, invitation.getDateOfClosure());
627  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS,
628    invitation.getStatus().name().toLowerCase());
629   
630    // Save the document
631  0 saveGroupDocument(groupDoc, String.format("User [%s] has accepted to join the wiki. ", invitation.getUserId()));
632    }
633   
 
634  0 toggle @Override
635    public void refuseInvitation(MemberCandidacy invitation, String message) throws WikiUserManagerException
636    {
637    // Set the values
638  0 invitation.setUserComment(message);
639  0 invitation.setStatus(MemberCandidacy.Status.REJECTED);
640  0 invitation.setDateOfClosure(new Date());
641   
642    // Get the group document
643  0 XWikiDocument groupDoc = getMembersGroupDocument(invitation.getWikiId());
644   
645    // Get the candidacy object
646  0 BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, invitation.getId());
647   
648    // Set the new values
649  0 object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT, invitation.getUserComment());
650  0 object.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE, invitation.getDateOfClosure());
651  0 object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS,
652    invitation.getStatus().name().toLowerCase());
653   
654    // Save the document
655  0 saveGroupDocument(groupDoc, String.format("User [%s] has rejected the invitation to join the wiki.",
656    invitation.getUserId()));
657    }
658    }