Search in sources :

Example 1 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class DocumentEventConverter method unserializeDeletdDocument.

private XWikiDocument unserializeDeletdDocument(Serializable remoteData, XWikiContext xcontext) throws XWikiException {
    Map<String, Serializable> remoteDataMap = (Map<String, Serializable>) remoteData;
    DocumentReference docReference = (DocumentReference) remoteDataMap.get(DOC_NAME);
    XWikiDocument doc = new XWikiDocument(docReference);
    XWikiDocument origDoc = new XWikiDocument(docReference);
    // We have to get deleted document from the trash (hoping it is in the trash...)
    XWiki xwiki = xcontext.getWiki();
    XWikiRecycleBinStoreInterface store = xwiki.getRecycleBinStore();
    XWikiDeletedDocument[] deletedDocuments = store.getAllDeletedDocuments(origDoc, xcontext, true);
    if (deletedDocuments != null && deletedDocuments.length > 0) {
        long index = deletedDocuments[0].getId();
        origDoc = store.restoreFromRecycleBin(index, xcontext, true);
    }
    doc.setOriginalDocument(origDoc);
    return doc;
}
Also used : Serializable(java.io.Serializable) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWiki(com.xpn.xwiki.XWiki) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class DeleteAction method deleteFromRecycleBin.

private void deleteFromRecycleBin(long index, XWikiContext context) throws XWikiException {
    XWiki xwiki = context.getWiki();
    XWikiResponse response = context.getResponse();
    XWikiDocument doc = context.getDoc();
    XWikiDeletedDocument dd = xwiki.getRecycleBinStore().getDeletedDocument(index, context, true);
    // don't try to delete it and instead redirect to the view page.
    if (dd != null) {
        DeletedDocument ddapi = new DeletedDocument(dd, context);
        if (!ddapi.canDelete()) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, "You are not allowed to delete a document from the trash " + "immediately after it has been deleted from the wiki");
        }
        if (!dd.getFullName().equals(doc.getFullName())) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, "The specified trash entry does not match the current document");
        }
        xwiki.getRecycleBinStore().deleteFromRecycleBin(index, context, true);
    }
    sendRedirect(response, Utils.getRedirect("view", context));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) DeletedDocument(com.xpn.xwiki.api.DeletedDocument) XWiki(com.xpn.xwiki.XWiki) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class UndeleteActionTest method missingCSRFToken.

@Test
public void missingCSRFToken() throws Exception {
    // Valid Deleted document ID.
    long id = 13;
    when(request.getParameter("id")).thenReturn(String.valueOf(id));
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
    // Invalid CSRF token.
    CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
    when(csrfToken.isTokenValid(null)).thenReturn(false);
    assertFalse(undeleteAction.action(context));
    // Verify that the resubmission URL was retrieved to be used in the redirect.
    verify(csrfToken).getResubmissionURL();
}
Also used : CSRFToken(org.xwiki.csrf.CSRFToken) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiContext(com.xpn.xwiki.XWikiContext) Test(org.junit.Test)

Example 4 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class UndeleteActionTest method restoreSingleDocument.

/**
 * Launches a RestoreJob with the current deleted document ID.
 */
@Test
public void restoreSingleDocument() throws Exception {
    CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
    when(csrfToken.isTokenValid(null)).thenReturn(true);
    long id = 13;
    when(request.getParameter("id")).thenReturn(String.valueOf(id));
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getLocale()).thenReturn(Locale.ROOT);
    when(deletedDocument.getId()).thenReturn(id);
    when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
    when(rightsService.hasAccessLevel(any(), any(), any(), any())).thenReturn(true);
    assertFalse(undeleteAction.action(context));
    verify(refactoringScriptService).createRestoreRequest(Arrays.asList(id));
    verify(jobExecutor).execute(RefactoringJobs.RESTORE, jobRequest);
    verify(job).join();
}
Also used : CSRFToken(org.xwiki.csrf.CSRFToken) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiContext(com.xpn.xwiki.XWikiContext) Test(org.junit.Test)

Example 5 with XWikiDeletedDocument

use of com.xpn.xwiki.doc.XWikiDeletedDocument in project xwiki-platform by xwiki.

the class UndeleteActionTest method notAllowedToRestoreBatch.

/**
 * When trying to restore, rights are checked on the current deleted document, regardless if single or batch
 * restore.
 */
@Test
public void notAllowedToRestoreBatch() throws Exception {
    CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
    when(csrfToken.isTokenValid(null)).thenReturn(true);
    long id = 13;
    String batchId = "abc123";
    when(request.getParameter("id")).thenReturn(String.valueOf(id));
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getLocale()).thenReturn(Locale.ROOT);
    when(deletedDocument.getId()).thenReturn(id);
    when(deletedDocument.getBatchId()).thenReturn(batchId);
    when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
    // Go through the screen showing the option to include the batch and displaying its contents.
    when(request.getParameter("showBatch")).thenReturn("true");
    // Option to include the entire batch when restoring is enabled.
    when(request.getParameter("includeBatch")).thenReturn("true");
    // Confirmation button pressed.
    when(request.getParameter("confirm")).thenReturn("true");
    // No rights to restore the page when checking from the Action. The job will check individual rights.
    when(rightsService.hasAccessLevel(any(), any(), any(), any())).thenReturn(false);
    assertTrue(undeleteAction.action(context));
    // Render the "accessdenied" template.
    assertEquals("accessdenied", undeleteAction.render(context));
    // Just make sure we don`t go any further.
    verify(refactoringScriptService, never()).createRestoreRequest(batchId);
}
Also used : CSRFToken(org.xwiki.csrf.CSRFToken) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiContext(com.xpn.xwiki.XWikiContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

XWikiDeletedDocument (com.xpn.xwiki.doc.XWikiDeletedDocument)24 Test (org.junit.Test)13 XWikiContext (com.xpn.xwiki.XWikiContext)10 DocumentReference (org.xwiki.model.reference.DocumentReference)8 XWiki (com.xpn.xwiki.XWiki)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 XWikiException (com.xpn.xwiki.XWikiException)5 CSRFToken (org.xwiki.csrf.CSRFToken)5 DeletedDocument (com.xpn.xwiki.api.DeletedDocument)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 XWikiRecycleBinStoreInterface (com.xpn.xwiki.store.XWikiRecycleBinStoreInterface)4 XWikiRightService (com.xpn.xwiki.user.api.XWikiRightService)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 XWikiDeletedDocumentContent (com.xpn.xwiki.doc.XWikiDeletedDocumentContent)1 XWikiHibernateDeletedDocumentContent (com.xpn.xwiki.internal.store.hibernate.XWikiHibernateDeletedDocumentContent)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Map (java.util.Map)1 HasToString (org.hamcrest.object.HasToString)1