Search in sources :

Example 21 with MemberCandidacy

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

the class DefaultWikiUserManager method askToJoin.

@Override
public MemberCandidacy askToJoin(String userId, String wikiId, String message) throws WikiUserManagerException {
    MemberCandidacy candidacy = new MemberCandidacy(wikiId, userId, MemberCandidacy.CandidateType.REQUEST);
    candidacy.setUserComment(message);
    // Get the group document
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    // Add a candidacy object
    XWikiContext xcontext = xcontextProvider.get();
    try {
        int objectNumber = groupDoc.createXObject(WikiCandidateMemberClassInitializer.REFERENCE, xcontext);
        candidacy.setId(objectNumber);
        BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, objectNumber);
        object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_USER, candidacy.getUserId());
        object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT, candidacy.getUserComment());
        object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS, candidacy.getStatus().name().toLowerCase());
        object.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION, candidacy.getDateOfCreation());
        object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_TYPE, candidacy.getType().name().toLowerCase());
    } catch (XWikiException e) {
        throw new WikiUserManagerException("Failed to create a new join request.", e);
    }
    // Save the document
    saveGroupDocument(groupDoc, String.format("[%s] asks to join the wiki.", userId));
    return candidacy;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) MemberCandidacy(org.xwiki.wiki.user.MemberCandidacy) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 22 with MemberCandidacy

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

the class WikiUserManagerScriptService method getCandidacy.

/**
 * Get the specified candidacy.
 *
 * @param wikiId Id of the wiki concerned by the candidacy
 * @param candidacyId id of the candidacy
 * @return the candidacy or null if problems occur
 */
public MemberCandidacy getCandidacy(String wikiId, int candidacyId) {
    // Get the candidacy
    MemberCandidacy candidacy = null;
    try {
        candidacy = wikiUserManager.getCandidacy(wikiId, candidacyId);
        // Check the rights
        if (!canSeeCandidacy(candidacy)) {
            setLastError(new WikiUserManagerScriptServiceException("You are not allowed to see this candidacy."));
            candidacy = null;
        }
    } catch (WikiUserManagerException e) {
        setLastError(e);
    }
    return candidacy;
}
Also used : MemberCandidacy(org.xwiki.wiki.user.MemberCandidacy) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException)

Example 23 with MemberCandidacy

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

the class WikiUserManagerScriptServiceTest method getAllRequests.

@Test
public void getAllRequests() throws Exception {
    ArrayList<MemberCandidacy> candidacies = new ArrayList<MemberCandidacy>();
    // the first candidacy concerns the current user
    candidacies.add(new MemberCandidacy("subwiki", "mainWiki:XWiki.User", MemberCandidacy.CandidateType.REQUEST));
    // not the second
    candidacies.add(new MemberCandidacy("subwiki", "mainWiki:XWiki.OtherUser", MemberCandidacy.CandidateType.REQUEST));
    candidacies.get(0).setAdminPrivateComment("private message");
    // We do not have admin rights
    when(authorizationManager.hasAccess(eq(Right.ADMIN), eq(userDocRef), eq(new WikiReference("subwiki")))).thenReturn(false);
    when(wikiUserManager.getAllRequests("subwiki")).thenReturn(candidacies);
    // Test
    Collection<MemberCandidacy> result = mocker.getComponentUnderTest().getAllRequests("subwiki");
    // the result must have been filtered
    assertEquals(1, result.size());
    assertTrue(result.contains(candidacies.get(0)));
    assertFalse(result.contains(candidacies.get(1)));
    // The private message from the candidacy must be removed
    assertNull(((MemberCandidacy) result.toArray()[0]).getAdminPrivateComment());
}
Also used : MemberCandidacy(org.xwiki.wiki.user.MemberCandidacy) ArrayList(java.util.ArrayList) WikiReference(org.xwiki.model.reference.WikiReference) Test(org.junit.Test)

Example 24 with MemberCandidacy

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

the class WikiUserManagerScriptServiceTest method refuseRequestWhenUserHasNoAdminRight.

@Test
public void refuseRequestWhenUserHasNoAdminRight() throws Exception {
    MemberCandidacy candidacy = new MemberCandidacy("subwiki", "mainWiki:XWiki.OtherUser", MemberCandidacy.CandidateType.INVITATION);
    // Mocks
    Exception expectedException = currentUserHasNotAdminRight();
    // Test
    Boolean result = mocker.getComponentUnderTest().refuseRequest(candidacy, "message", "comment");
    // Asserts
    assertFalse(result);
    assertEquals(expectedException, mocker.getComponentUnderTest().getLastError());
    verifyZeroInteractions(wikiUserManager);
}
Also used : MemberCandidacy(org.xwiki.wiki.user.MemberCandidacy) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) Test(org.junit.Test)

Example 25 with MemberCandidacy

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

the class WikiUserManagerScriptServiceTest method invite.

@Test
public void invite() throws Exception {
    // Mocks
    when(wikiUserManager.invite(any(), any(), any())).thenReturn(new MemberCandidacy());
    // Test
    MemberCandidacy result = mocker.getComponentUnderTest().invite("someUser", "subwiki", "someMessage");
    // Asserts
    assertNotNull(result);
}
Also used : MemberCandidacy(org.xwiki.wiki.user.MemberCandidacy) Test(org.junit.Test)

Aggregations

MemberCandidacy (org.xwiki.wiki.user.MemberCandidacy)34 Test (org.junit.Test)29 WikiUserManagerException (org.xwiki.wiki.user.WikiUserManagerException)15 AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)12 WikiReference (org.xwiki.model.reference.WikiReference)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 BaseObject (com.xpn.xwiki.objects.BaseObject)3 ArrayList (java.util.ArrayList)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2