| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 41 |
|
|
| 42 |
|
@version |
| 43 |
|
@since |
| 44 |
|
|
| 45 |
|
@Component |
| 46 |
|
@Singleton |
| |
|
| 89.8% |
Uncovered Elements: 5 (49) |
Complexity: 11 |
Complexity Density: 0.29 |
|
| 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 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 56 |
96 |
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 |
|
|
| |
|
| 89.5% |
Uncovered Elements: 2 (19) |
Complexity: 4 |
Complexity Density: 0.24 |
|
| 72 |
90 |
@Override... |
| 73 |
|
public WikiUserConfiguration getConfiguration(String wikiId) throws WikiUserManagerException |
| 74 |
|
{ |
| 75 |
|
|
| 76 |
90 |
WikiUserConfiguration configuration = new WikiUserConfiguration(); |
| 77 |
|
|
| 78 |
|
|
| 79 |
90 |
XWikiDocument document = getDocument(wikiId); |
| 80 |
|
|
| 81 |
|
|
| 82 |
90 |
BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS); |
| 83 |
90 |
if (object != null) { |
| 84 |
|
|
| 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 |
|
|
| 91 |
0 |
userScope = UserScope.LOCAL_AND_GLOBAL; |
| 92 |
|
} |
| 93 |
55 |
configuration.setUserScope(userScope); |
| 94 |
|
|
| 95 |
|
|
| 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 |
|
|
| 103 |
0 |
membershipType = MembershipType.INVITE; |
| 104 |
|
} |
| 105 |
55 |
configuration.setMembershipType(membershipType); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
90 |
return configuration; |
| 109 |
|
} |
| 110 |
|
|
| |
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 5 |
Complexity Density: 0.33 |
|
| 111 |
6 |
@Override... |
| 112 |
|
public void saveConfiguration(WikiUserConfiguration configuration, String wikiId) |
| 113 |
|
throws WikiUserManagerException |
| 114 |
|
{ |
| 115 |
6 |
XWikiContext context = xcontextProvider.get(); |
| 116 |
|
|
| 117 |
|
|
| 118 |
6 |
XWikiDocument document = getDocument(wikiId); |
| 119 |
|
|
| 120 |
|
|
| 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 |
|
|
| 130 |
6 |
try { |
| 131 |
6 |
XWiki xwiki = context.getWiki(); |
| 132 |
6 |
document.setHidden(true); |
| 133 |
|
|
| 134 |
6 |
if (document.getCreatorReference() == null) { |
| 135 |
3 |
document.setCreatorReference(context.getUserReference()); |
| 136 |
|
} |
| 137 |
|
|
| 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 |
|
} |