Search in sources :

Example 66 with XWikiContext

use of com.xpn.xwiki.XWikiContext 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 67 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class DefaultWikiUserManager method acceptRequest.

@Override
public void acceptRequest(MemberCandidacy request, String message, String privateComment) throws WikiUserManagerException {
    // Add the user to the members
    addMember(request.getUserId(), request.getWikiId());
    // Then, update the candidacy object
    XWikiContext xcontext = xcontextProvider.get();
    // Set the values
    request.setAdminId(documentReferenceSerializer.serialize(xcontext.getUserReference()));
    request.setAdminComment(message);
    request.setAdminPrivateComment(privateComment);
    request.setStatus(MemberCandidacy.Status.ACCEPTED);
    request.setDateOfClosure(new Date());
    // Get the group document
    XWikiDocument groupDoc = getMembersGroupDocument(request.getWikiId());
    // Get the candidacy object
    BaseObject object = groupDoc.getXObject(WikiCandidateMemberClassInitializer.REFERENCE, request.getId());
    // Set the new values
    object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN, request.getAdminId());
    object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT, request.getAdminComment());
    object.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT, request.getAdminPrivateComment());
    object.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE, request.getDateOfClosure());
    object.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS, request.getStatus().name().toLowerCase());
    // Save the document
    saveGroupDocument(groupDoc, String.format("Accept join request from user [%s]", request.getUserId()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) Date(java.util.Date) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 68 with XWikiContext

use of com.xpn.xwiki.XWikiContext 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 69 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class WikiUserFromXEMMigration method hibernateMigrate.

@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
    // Context, XWiki
    XWikiContext context = getXWikiContext();
    XWiki xwiki = context.getWiki();
    // Current wiki
    String currentWikiId = wikiDescriptorManager.getCurrentWikiId();
    // Get the old wiki descriptor
    DocumentReference oldWikiDescriptorReference = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, String.format("XWikiServer%s", StringUtils.capitalize(currentWikiId)));
    XWikiDocument oldWikiDescriptor = xwiki.getDocument(oldWikiDescriptorReference, context);
    // Try to get the old workspace object
    DocumentReference oldClassDocument = new DocumentReference(wikiDescriptorManager.getMainWikiId(), WORKSPACE_CLASS_SPACE, WORKSPACE_CLASS_PAGE);
    BaseObject oldObject = oldWikiDescriptor.getXObject(oldClassDocument);
    // Upgrade depending of the type
    if (oldObject != null || isWorkspaceTemplate(currentWikiId)) {
        // It's a workspace
        upgradeWorkspace(oldObject, currentWikiId, oldWikiDescriptor);
    } else {
        // It's a regular subwiki
        upgradeRegularSubwiki(currentWikiId);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 70 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class WikiUserFromXEMMigration method upgradeWorkspaceConfiguration.

/**
 * Convert the old WorkspaceManager.WorkspaceClass objects to the new configuration format.
 *
 * @param oldObject old workspace object
 * @param wikiId id of the wiki to upgrade
 * @param oldWikiDescriptor document that holds the old object
 * @throws DataMigrationException if problems occur
 * @throws XWikiException if problems occur
 */
private void upgradeWorkspaceConfiguration(BaseObject oldObject, String wikiId, XWikiDocument oldWikiDescriptor) throws DataMigrationException, XWikiException {
    // Context, XWiki
    XWikiContext context = getXWikiContext();
    XWiki xwiki = context.getWiki();
    // Create the new configuration
    WikiUserConfiguration configuration = new WikiUserConfiguration();
    // No local users
    configuration.setUserScope(UserScope.GLOBAL_ONLY);
    // Set the membershipType value
    if (oldObject != null) {
        // Get the membershipType value
        String membershipTypeValue = oldObject.getStringValue("membershipType");
        MembershipType membershipType;
        try {
            membershipType = MembershipType.valueOf(membershipTypeValue.toUpperCase());
        } catch (Exception e) {
            // Default value
            membershipType = MembershipType.INVITE;
        }
        configuration.setMembershipType(membershipType);
    } else {
        // If there is no workspace object, we put a default value.
        configuration.setMembershipType(MembershipType.INVITE);
    }
    // Save the new configuration
    saveConfiguration(configuration, wikiId);
}
Also used : WikiUserConfiguration(org.xwiki.wiki.user.WikiUserConfiguration) MembershipType(org.xwiki.wiki.user.MembershipType) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)564 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)203 XWikiException (com.xpn.xwiki.XWikiException)195 DocumentReference (org.xwiki.model.reference.DocumentReference)150 XWiki (com.xpn.xwiki.XWiki)106 BaseObject (com.xpn.xwiki.objects.BaseObject)104 Test (org.junit.Test)64 ArrayList (java.util.ArrayList)55 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)43 ExecutionContext (org.xwiki.context.ExecutionContext)43 Session (org.hibernate.Session)37 InitializationException (org.xwiki.component.phase.InitializationException)36 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)34 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)33 WikiReference (org.xwiki.model.reference.WikiReference)31 Before (org.junit.Before)29 EntityReference (org.xwiki.model.reference.EntityReference)28 QueryException (org.xwiki.query.QueryException)28 Execution (org.xwiki.context.Execution)27 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)24