Search in sources :

Example 16 with XWikiDeletedDocument

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

the class DeletedDocumentRevisionProvider method getRevision.

private XWikiDocument getRevision(String revision) throws XWikiException {
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDeletedDocument deletedDocument = xcontext.getWiki().getDeletedDocument(Long.valueOf(revision), xcontext);
    return deletedDocument != null ? deletedDocument.restoreDocument(xcontext) : null;
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument)

Example 17 with XWikiDeletedDocument

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

the class DefaultModelBridge method canRestoreDeletedDocument.

@Override
public boolean canRestoreDeletedDocument(long deletedDocumentId, DocumentReference userReference) {
    boolean result = false;
    XWikiContext context = this.xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    // Remember the context user.
    DocumentReference currentUserReference = context.getUserReference();
    try {
        XWikiDeletedDocument deletedDocument = xwiki.getRecycleBinStore().getDeletedDocument(deletedDocumentId, context, true);
        // Reuse the DeletedDocument API to check rights.
        DeletedDocument deletedDocumentApi = new DeletedDocument(deletedDocument, context);
        // Note: DeletedDocument API works with the current context user.
        context.setUserReference(userReference);
        result = deletedDocumentApi.canUndelete();
    } catch (Exception e) {
        logger.error("Failed to check restore rights on deleted document [{}] for user [{}]", deletedDocumentId, userReference, e);
    } finally {
        // Restore the context user;
        context.setUserReference(currentUserReference);
    }
    return result;
}
Also used : DeletedDocument(com.xpn.xwiki.api.DeletedDocument) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 18 with XWikiDeletedDocument

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

the class DefaultModelBridge method getDeletedDocumentIds.

@Override
public List<Long> getDeletedDocumentIds(String batchId) {
    XWikiContext context = this.xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    List<Long> result = new ArrayList<>();
    try {
        XWikiDeletedDocument[] deletedDocuments = xwiki.getRecycleBinStore().getAllDeletedDocuments(batchId, false, context, true);
        for (XWikiDeletedDocument deletedDocument : deletedDocuments) {
            result.add(deletedDocument.getId());
        }
    } catch (Exception e) {
        logger.error("Failed to get deleted document IDs for batch [{}]", batchId);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiException(com.xpn.xwiki.XWikiException)

Example 19 with XWikiDeletedDocument

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

the class DefaultModelBridgeTest method canRestoreDeletedDocument.

@Test
public void canRestoreDeletedDocument() throws Exception {
    long deletedDocumentId = 42;
    String deletedDocumentFullName = "Space.DeletedDocument";
    DocumentReference userReferenceToCheck = new DocumentReference("wiki", "Space", "User");
    DocumentReference currentUserReference = new DocumentReference("wiki", "Space", "CurrentUser");
    when(xcontext.getUserReference()).thenReturn(currentUserReference);
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getFullName()).thenReturn(deletedDocumentFullName);
    XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
    when(recycleBin.getDeletedDocument(deletedDocumentId, xcontext, true)).thenReturn(deletedDocument);
    when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
    XWikiRightService rightService = mock(XWikiRightService.class);
    when(xwiki.getRightService()).thenReturn(rightService);
    when(rightService.hasAccessLevel(any(), any(), any(), any())).thenReturn(true);
    assertTrue(mocker.getComponentUnderTest().canRestoreDeletedDocument(deletedDocumentId, userReferenceToCheck));
    // Verify that the rights were checked with the specified user as context user.
    verify(xcontext).setUserReference(userReferenceToCheck);
    // Verify that the context user was restored. Note: We don`t know the order here, but maybe we don`t care that
    // much.
    verify(xcontext).setUserReference(currentUserReference);
}
Also used : XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 20 with XWikiDeletedDocument

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

the class DefaultModelBridgeTest method restoreDeletedDocumentNoRights.

@Test
public void restoreDeletedDocumentNoRights() throws Exception {
    long deletedDocumentId = 42;
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    String deletedDocumentFullName = "space.page";
    XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
    when(deletedDocument.getDocumentReference()).thenReturn(documentReference);
    when(deletedDocument.getId()).thenReturn(deletedDocumentId);
    when(deletedDocument.getFullName()).thenReturn(deletedDocumentFullName);
    when(xwiki.getDeletedDocument(deletedDocumentId, xcontext)).thenReturn(deletedDocument);
    when(xwiki.exists(documentReference, xcontext)).thenReturn(false);
    XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
    when(recycleBin.getDeletedDocument(deletedDocumentId, xcontext, true)).thenReturn(deletedDocument);
    when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
    // No rights.
    XWikiRightService rightService = mock(XWikiRightService.class);
    when(xwiki.getRightService()).thenReturn(rightService);
    when(rightService.hasAccessLevel(any(), any(), any(), any())).thenReturn(false);
    assertFalse(mocker.getComponentUnderTest().restoreDeletedDocument(deletedDocumentId, true));
    verify(mocker.getMockedLogger()).error("You are not allowed to restore document [{}] with ID [{}]", documentReference, deletedDocumentId);
    verify(xwiki, never()).restoreFromRecycleBin(any(), any(), any());
}
Also used : XWikiRecycleBinStoreInterface(com.xpn.xwiki.store.XWikiRecycleBinStoreInterface) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) 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