Search in sources :

Example 1 with WikiUserManagerException

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

the class DefaultWikiUserManager method getMembersGroupDocument.

private XWikiDocument getMembersGroupDocument(String wikiId) throws WikiUserManagerException {
    // Reference to the document
    DocumentReference memberGroupReference = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "XWikiAllGroup");
    // Get the document
    try {
        XWikiContext xcontext = xcontextProvider.get();
        XWiki xwiki = xcontext.getWiki();
        return xwiki.getDocument(memberGroupReference, xcontext);
    } catch (XWikiException e) {
        throw new WikiUserManagerException(String.format("Fail to load the member group document [%s].", memberGroupReference.toString()), e);
    }
}
Also used : WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 2 with WikiUserManagerException

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

the class DefaultWikiUserManager method addMemberObject.

private void addMemberObject(XWikiDocument groupDoc, String userId) throws WikiUserManagerException {
    try {
        XWikiContext xcontext = xcontextProvider.get();
        int objectNumber = groupDoc.createXObject(GROUPCLASS_REFERENCE, xcontext);
        BaseObject object = groupDoc.getXObject(GROUPCLASS_REFERENCE, objectNumber);
        object.set(GROUP_CLASS_MEMBER_FIELD, userId, xcontext);
    } catch (XWikiException e) {
        throw new WikiUserManagerException("Fail to add a member to the group", e);
    }
}
Also used : WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with WikiUserManagerException

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

the class DefaultWikiUserManager method getMembers.

@Override
public Collection<String> getMembers(String wikiId) throws WikiUserManagerException {
    List<String> members = new ArrayList<>();
    try {
        // Get the descriptor
        WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
        // Add the wiki owner
        members.add(descriptor.getOwnerId());
    } catch (WikiManagerException e) {
        throw new WikiUserManagerException(String.format("Failed to get the descriptor for [%s]", wikiId), e);
    }
    // Get the other members from the wiki AllGroup
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
    if (memberObjects != null) {
        for (BaseObject object : memberObjects) {
            if (object == null) {
                continue;
            }
            String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
            if (!member.isEmpty() && !members.contains(member)) {
                members.add(member);
            }
        }
    }
    return members;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) ArrayList(java.util.ArrayList) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 4 with WikiUserManagerException

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

the class DefaultWikiUserManager method invite.

@Override
public MemberCandidacy invite(String userId, String wikiId, String message) throws WikiUserManagerException {
    XWikiContext xcontext = xcontextProvider.get();
    // Create the candidacy
    MemberCandidacy candidacy = new MemberCandidacy(wikiId, userId, MemberCandidacy.CandidateType.INVITATION);
    candidacy.setUserComment(message);
    candidacy.setAdminId(documentReferenceSerializer.serialize(xcontext.getUserReference()));
    // Get the group document
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    // Add a candidacy object
    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.setStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN, candidacy.getAdminId());
        object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT, message);
        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 invitation object.", e);
    }
    // Save the document
    saveGroupDocument(groupDoc, String.format("[%s] is invited 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 5 with WikiUserManagerException

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

the class WikiUserManagerScriptService method leave.

/**
 * Leave a wiki.
 *
 * @param userId userId to remove from the wiki
 * @param wikiId id of the wiki
 * @return true if it succeed
 */
public boolean leave(String userId, String wikiId) {
    // Check if the current user is userId
    XWikiContext context = xcontextProvider.get();
    DocumentReference candidacyUser = documentReferenceResolver.resolve(userId);
    if (!context.getUserReference().equals(candidacyUser)) {
        setLastError(new WikiUserManagerException(String.format("User [%s] cannot call $services.wiki.user.leave()" + " with an other userId.", context.getUserReference())));
        return false;
    }
    // Leave the wiki
    try {
        wikiUserManager.leave(userId, wikiId);
    } catch (WikiUserManagerException e) {
        setLastError(e);
        return false;
    }
    return true;
}
Also used : WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference)

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