Search in sources :

Example 71 with XWikiContext

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

the class WikiUserFromXEMMigrationTest method setUp.

@Before
public void setUp() throws Exception {
    wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
    wikiUserConfigurationHelper = mocker.getInstance(WikiUserConfigurationHelper.class);
    execution = mock(Execution.class);
    mocker.registerComponent(Execution.class, execution);
    xcontext = mock(XWikiContext.class);
    xwiki = mock(XWiki.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(execution.getContext()).thenReturn(executionContext);
    when(executionContext.getProperty("xwikicontext")).thenReturn(xcontext);
    when(xcontext.getWiki()).thenReturn(xwiki);
    when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki");
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Before(org.junit.Before)

Example 72 with XWikiContext

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

the class WikiUserManagerScriptService method canSeeCandidacy.

private boolean canSeeCandidacy(MemberCandidacy candidacy) {
    XWikiContext context = xcontextProvider.get();
    // Test if the user is concerned by the candidacy...
    DocumentReference candidacyUser = documentReferenceResolver.resolve(candidacy.getUserId());
    if (context.getUserReference().equals(candidacyUser)) {
        // Hide the admin private comment
        candidacy.setAdminPrivateComment(null);
        return true;
    }
    // Otherwise the user must be an admin.
    return authorizationManager.hasAccess(Right.ADMIN, context.getUserReference(), new WikiReference(candidacy.getWikiId()));
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 73 with XWikiContext

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

Example 74 with XWikiContext

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

the class WikiUserManagerScriptService method join.

/**
 * Join a wiki.
 *
 * @param userId userId to add to the wiki
 * @param wikiId id of the wiki
 * @return true if it succeed
 */
public boolean join(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.join() with an other userId.", context.getUserReference())));
        return false;
    }
    try {
        wikiUserManager.join(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)

Example 75 with XWikiContext

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

the class DefaultDocumentRestorerFromAttachedXAR method restoreDocumentFromAttachedXAR.

@Override
public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName, List<DocumentReference> documentsToRestore) throws XWikiException {
    XWikiContext xcontext = xcontextProvider.get();
    XWiki xwiki = xcontext.getWiki();
    File tempZipFile = null;
    try {
        tempZipFile = getTemporaryZipFile(docReference, attachmentName);
        if (tempZipFile == null) {
            return;
        }
        ZipFile zipFile = new ZipFile(tempZipFile);
        // We look for each document to restore if there is a corresponding zipEntry.
        Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator();
        while (itDocumentsToRestore.hasNext()) {
            DocumentReference docRef = itDocumentsToRestore.next();
            // Compute what should be the filename of the document to restore
            String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(), docRef.getName());
            // Get the corresponding zip Entry
            ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore);
            if (zipEntry != null) {
                // Restore the document
                XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext);
                docToRestore.fromXML(zipFile.getInputStream(zipEntry));
                xwiki.saveDocument(docToRestore, xcontext);
                // We have restored this document
                itDocumentsToRestore.remove();
            }
        }
        zipFile.close();
    } catch (IOException e) {
        logger.error("Error during the decompression of [{}].", attachmentName, e);
    } finally {
        // Delete the temporary zip file
        if (tempZipFile != null) {
            tempZipFile.delete();
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference)

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