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

File DefaultWikiUserConfigurationHelper.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

8
38
3
1
147
92
11
0.29
12.67
3
3.67

Classes

Class Line # Actions
DefaultWikiUserConfigurationHelper 47 38 0% 11 5
0.897959289.8%
 

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 javax.inject.Inject;
23    import javax.inject.Provider;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.model.reference.DocumentReference;
28    import org.xwiki.wiki.user.MembershipType;
29    import org.xwiki.wiki.user.UserScope;
30    import org.xwiki.wiki.user.WikiUserConfiguration;
31    import org.xwiki.wiki.user.WikiUserManagerException;
32   
33    import com.xpn.xwiki.XWiki;
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.objects.BaseObject;
38   
39    /**
40    * Default implementation of {@link WikiUserConfigurationHelper}.
41    *
42    * @version $Id: 32f703422c8bc668023f0faa8169c2d66ce54b57 $
43    * @since 5.3M2
44    */
45    @Component
46    @Singleton
 
47    public class DefaultWikiUserConfigurationHelper implements WikiUserConfigurationHelper
48    {
49    private static final String CONFIGURATION_PAGE_NAME = "WikiUserConfiguration";
50   
51    private static final String CONFIGURATION_SPACE_NAME = WikiUserClassDocumentInitializer.DOCUMENT_SPACE;
52   
53    @Inject
54    private Provider<XWikiContext> xcontextProvider;
55   
 
56  96 toggle private XWikiDocument getDocument(String wikiId) throws WikiUserManagerException
57    {
58  96 try {
59  96 XWikiContext context = xcontextProvider.get();
60  96 XWiki xwiki = context.getWiki();
61   
62  96 DocumentReference reference = new DocumentReference(wikiId, CONFIGURATION_SPACE_NAME,
63    CONFIGURATION_PAGE_NAME);
64   
65  96 return xwiki.getDocument(reference, context);
66    } catch (XWikiException e) {
67  0 throw new WikiUserManagerException(String.format("Fail to get the configuration document for wiki [%s].",
68    wikiId));
69    }
70    }
71   
 
72  90 toggle @Override
73    public WikiUserConfiguration getConfiguration(String wikiId) throws WikiUserManagerException
74    {
75    // Create the configuration object to return
76  90 WikiUserConfiguration configuration = new WikiUserConfiguration();
77   
78    // Get the document
79  90 XWikiDocument document = getDocument(wikiId);
80   
81    // Get the XWiki object
82  90 BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS);
83  90 if (object != null) {
84    // Get the user scope
85  55 String scopeValue = object.getStringValue(WikiUserClassDocumentInitializer.FIELD_USERSCOPE);
86  55 UserScope userScope;
87  55 try {
88  55 userScope = UserScope.valueOf(scopeValue.toUpperCase());
89    } catch (Exception e) {
90    // Default value
91  0 userScope = UserScope.LOCAL_AND_GLOBAL;
92    }
93  55 configuration.setUserScope(userScope);
94   
95    // Get the membershipType value
96  55 String membershipTypeValue = object.getStringValue(
97    WikiUserClassDocumentInitializer.FIELD_MEMBERSHIPTYPE);
98  55 MembershipType membershipType;
99  55 try {
100  55 membershipType = MembershipType.valueOf(membershipTypeValue.toUpperCase());
101    } catch (Exception e) {
102    // Default value
103  0 membershipType = MembershipType.INVITE;
104    }
105  55 configuration.setMembershipType(membershipType);
106    }
107   
108  90 return configuration;
109    }
110   
 
111  6 toggle @Override
112    public void saveConfiguration(WikiUserConfiguration configuration, String wikiId)
113    throws WikiUserManagerException
114    {
115  6 XWikiContext context = xcontextProvider.get();
116   
117    // Get the document
118  6 XWikiDocument document = getDocument(wikiId);
119   
120    // Fill the object
121  6 BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS, true, context);
122  6 object.setStringValue(WikiUserClassDocumentInitializer.FIELD_USERSCOPE,
123    configuration.getUserScope().name().toLowerCase());
124  6 if (configuration.getMembershipType() != null) {
125  6 object.setStringValue(WikiUserClassDocumentInitializer.FIELD_MEMBERSHIPTYPE,
126    configuration.getMembershipType().name().toLowerCase());
127    }
128   
129    // Save the document
130  6 try {
131  6 XWiki xwiki = context.getWiki();
132  6 document.setHidden(true);
133    // The document must have a creator
134  6 if (document.getCreatorReference() == null) {
135  3 document.setCreatorReference(context.getUserReference());
136    }
137    // The document must have an author
138  6 if (document.getAuthorReference() == null) {
139  3 document.setAuthorReference(context.getUserReference());
140    }
141  6 xwiki.saveDocument(document, "Changed configuration.", context);
142    } catch (XWikiException e) {
143  0 throw new WikiUserManagerException(
144    String.format("Fail to save the confguration document for wiki [%s].", wikiId), e);
145    }
146    }
147    }