Search in sources :

Example 16 with WikiUserManagerException

use of org.xwiki.wiki.user.WikiUserManagerException in project xwiki-platform by xwiki.

the class WikiUserManagerScriptServiceTest method setUserScopeError.

@Test
public void setUserScopeError() throws Exception {
    // Mocks
    WikiUserManagerException expectedException = new WikiUserManagerException("error in setUserScope");
    doThrow(expectedException).when(wikiUserManager).setUserScope(any(), any(UserScope.class));
    // Test
    boolean result = mocker.getComponentUnderTest().setUserScope("subwiki", "LOCAL_ONLY");
    // Asserts
    assertFalse(result);
    assertEquals(expectedException, mocker.getComponentUnderTest().getLastError());
}
Also used : WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) UserScope(org.xwiki.wiki.user.UserScope) Test(org.junit.Test)

Example 17 with WikiUserManagerException

use of org.xwiki.wiki.user.WikiUserManagerException in project xwiki-platform by xwiki.

the class DefaultWikiUserConfigurationHelper method saveConfiguration.

@Override
public void saveConfiguration(WikiUserConfiguration configuration, String wikiId) throws WikiUserManagerException {
    XWikiContext context = xcontextProvider.get();
    // Get the document
    XWikiDocument document = getDocument(wikiId);
    // Fill the object
    BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS, true, context);
    object.setStringValue(WikiUserClassDocumentInitializer.FIELD_USERSCOPE, configuration.getUserScope().name().toLowerCase());
    if (configuration.getMembershipType() != null) {
        object.setStringValue(WikiUserClassDocumentInitializer.FIELD_MEMBERSHIPTYPE, configuration.getMembershipType().name().toLowerCase());
    }
    // Save the document
    try {
        XWiki xwiki = context.getWiki();
        document.setHidden(true);
        // The document must have a creator
        if (document.getCreatorReference() == null) {
            document.setCreatorReference(context.getUserReference());
        }
        // The document must have an author
        if (document.getAuthorReference() == null) {
            document.setAuthorReference(context.getUserReference());
        }
        xwiki.saveDocument(document, "Changed configuration.", context);
    } catch (XWikiException e) {
        throw new WikiUserManagerException(String.format("Fail to save the confguration document for wiki [%s].", wikiId), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 18 with WikiUserManagerException

use of org.xwiki.wiki.user.WikiUserManagerException in project xwiki-platform by xwiki.

the class DefaultWikiUserConfigurationHelper method getConfiguration.

@Override
public WikiUserConfiguration getConfiguration(String wikiId) throws WikiUserManagerException {
    // Create the configuration object to return
    WikiUserConfiguration configuration = new WikiUserConfiguration();
    // Get the document
    XWikiDocument document = getDocument(wikiId);
    // Get the XWiki object
    BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS);
    if (object != null) {
        // Get the user scope
        String scopeValue = object.getStringValue(WikiUserClassDocumentInitializer.FIELD_USERSCOPE);
        UserScope userScope;
        try {
            userScope = UserScope.valueOf(scopeValue.toUpperCase());
        } catch (Exception e) {
            // Default value
            userScope = UserScope.LOCAL_AND_GLOBAL;
        }
        configuration.setUserScope(userScope);
        // Get the membershipType value
        String membershipTypeValue = object.getStringValue(WikiUserClassDocumentInitializer.FIELD_MEMBERSHIPTYPE);
        MembershipType membershipType;
        try {
            membershipType = MembershipType.valueOf(membershipTypeValue.toUpperCase());
        } catch (Exception e) {
            // Default value
            membershipType = MembershipType.INVITE;
        }
        configuration.setMembershipType(membershipType);
    }
    return configuration;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiUserConfiguration(org.xwiki.wiki.user.WikiUserConfiguration) UserScope(org.xwiki.wiki.user.UserScope) MembershipType(org.xwiki.wiki.user.MembershipType) XWikiException(com.xpn.xwiki.XWikiException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 19 with WikiUserManagerException

use of org.xwiki.wiki.user.WikiUserManagerException in project xwiki-platform by xwiki.

the class DefaultWikiUserConfigurationHelper method getDocument.

private XWikiDocument getDocument(String wikiId) throws WikiUserManagerException {
    try {
        XWikiContext context = xcontextProvider.get();
        XWiki xwiki = context.getWiki();
        DocumentReference reference = new DocumentReference(wikiId, CONFIGURATION_SPACE_NAME, CONFIGURATION_PAGE_NAME);
        return xwiki.getDocument(reference, context);
    } catch (XWikiException e) {
        throw new WikiUserManagerException(String.format("Fail to get the configuration document for wiki [%s].", wikiId));
    }
}
Also used : WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 20 with WikiUserManagerException

use of org.xwiki.wiki.user.WikiUserManagerException in project xwiki-platform by xwiki.

the class AddUsersStepTest method executeWhenException.

@Test
public void executeWhenException() throws Exception {
    WikiCreationRequest request = new WikiCreationRequest();
    request.setWikiId("wikiId");
    List<String> members = new ArrayList<>();
    request.setMembers(members);
    Exception exception = new WikiUserManagerException("Execption in WikiUserManager.");
    doThrow(exception).when(wikiUserManager).addMembers(anyCollection(), any());
    // Test
    WikiCreationException caughtException = null;
    try {
        mocker.getComponentUnderTest().execute(request);
    } catch (WikiCreationException e) {
        caughtException = e;
    }
    // Verify
    assertNotNull(caughtException);
    assertEquals("Failed to add members to the wiki [wikiId].", caughtException.getMessage());
    assertEquals(exception, caughtException.getCause());
}
Also used : WikiCreationException(org.xwiki.platform.wiki.creationjob.WikiCreationException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) WikiCreationRequest(org.xwiki.platform.wiki.creationjob.WikiCreationRequest) ArrayList(java.util.ArrayList) WikiCreationException(org.xwiki.platform.wiki.creationjob.WikiCreationException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) Test(org.junit.Test)

Aggregations

WikiUserManagerException (org.xwiki.wiki.user.WikiUserManagerException)27 Test (org.junit.Test)14 XWikiContext (com.xpn.xwiki.XWikiContext)9 AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)9 XWikiException (com.xpn.xwiki.XWikiException)8 BaseObject (com.xpn.xwiki.objects.BaseObject)6 DocumentReference (org.xwiki.model.reference.DocumentReference)6 MemberCandidacy (org.xwiki.wiki.user.MemberCandidacy)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 XWiki (com.xpn.xwiki.XWiki)4 MembershipType (org.xwiki.wiki.user.MembershipType)3 UserScope (org.xwiki.wiki.user.UserScope)3 ArrayList (java.util.ArrayList)2 WikiCreationException (org.xwiki.platform.wiki.creationjob.WikiCreationException)2 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)2 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)1 WikiCreationRequest (org.xwiki.platform.wiki.creationjob.WikiCreationRequest)1 WikiTemplateManagerException (org.xwiki.wiki.template.WikiTemplateManagerException)1 WikiUserConfiguration (org.xwiki.wiki.user.WikiUserConfiguration)1