Search in sources :

Example 21 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class GroupChangesTestSuite method testGroupChangesWithAncestorGroups.

/**
 * Tests changes on a descendant group of the given group that has access to a synchronization root.
 */
protected void testGroupChangesWithAncestorGroups(String ancestorGroup) throws Exception {
    List<FileSystemItemChange> changes;
    DocumentModel syncRoot;
    try {
        syncRoot = session.createDocument(session.createDocumentModel("/", "syncRoot", "Folder"));
        log.trace("Grant ReadWrite to " + ancestorGroup + " on syncRoot");
        setPermissions(syncRoot, new ACE(ancestorGroup, SecurityConstants.READ_WRITE));
        nuxeoDriveManager.registerSynchronizationRoot(userSession.getPrincipal(), syncRoot, userSession);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges(userSession.getPrincipal());
        // Sync root creation and registration events
        assertEquals(2, changes.size());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    testGroupChanges(syncRoot, "defaultSyncRootFolderItemFactory", "group1", true);
}
Also used : ACE(org.nuxeo.ecm.core.api.security.ACE) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 22 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class TestNuxeoDriveManager method testResetSyncRootsOnCopyDisabled.

@Test
@Deploy("org.nuxeo.drive.core:OSGI-INF/test-nuxeodrive-reset-sync-roots-on-copy-disabled-contrib.xml")
public void testResetSyncRootsOnCopyDisabled() {
    nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder_1_1, session);
    // Copy a sync root
    DocumentModel copy = session.copy(folder_1_1.getRef(), workspace_2.getRef(), null);
    txFeature.nextTransaction();
    assertTrue(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), copy));
    nuxeoDriveManager.invalidateSynchronizationRootsCache(session.getPrincipal().getName());
    // Copy a folder containing a sync root
    copy = session.copy(workspace_1.getRef(), workspace_2.getRef(), null);
    txFeature.nextTransaction();
    assertTrue(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), session.getDocument(new PathRef(copy.getPathAsString() + "/" + folder_1_1.getName()))));
}
Also used : PathRef(org.nuxeo.ecm.core.api.PathRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test) Deploy(org.nuxeo.runtime.test.runner.Deploy)

Example 23 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testFileItem.

@Test
public void testFileItem() throws Exception {
    // ------------------------------------------------------
    // FileItem#getDownloadURL
    // ------------------------------------------------------
    FileItem fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
    String downloadURL = fileItem.getDownloadURL();
    assertEquals("nxfile/test/" + file.getId() + "/blobholder:0/Joe.odt", downloadURL);
    // ------------------------------------------------------------
    // FileItem#getDigestAlgorithm
    // ------------------------------------------------------------
    assertEquals("MD5", fileItem.getDigestAlgorithm());
    FileItem noteItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(note);
    assertEquals("MD5", noteItem.getDigestAlgorithm());
    // ------------------------------------------------------------
    // FileItem#getDigest
    // ------------------------------------------------------------
    assertEquals(file.getAdapter(BlobHolder.class).getBlob().getDigest(), fileItem.getDigest());
    String noteDigest = FileSystemItemHelper.getMD5Digest(note.getAdapter(BlobHolder.class).getBlob());
    assertEquals(noteDigest, noteItem.getDigest());
    assertEquals(custom.getAdapter(BlobHolder.class).getBlob().getDigest(), ((FileItem) defaultFileSystemItemFactory.getFileSystemItem(custom)).getDigest());
    // ------------------------------------------------------------
    // FileItem#getCanUpdate
    // ------------------------------------------------------------
    // As Administrator
    assertTrue(fileItem.getCanUpdate());
    // As a user with READ permission
    DocumentModel rootDoc = session.getRootDocument();
    setPermission(rootDoc, "joe", SecurityConstants.READ, true);
    // Under Oracle, the READ ACL optims are not visible from the
    // joe session while the transaction has not been committed.
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertFalse(fileItem.getCanUpdate());
        // As a user with WRITE permission
        setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        assertTrue(fileItem.getCanUpdate());
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        // ------------------------------------------------------
        // FileItem#getBlob
        // ------------------------------------------------------
        Blob fileItemBlob = fileItem.getBlob();
        assertEquals("Joe.odt", fileItemBlob.getFilename());
        assertEquals("Content of Joe's file.", fileItemBlob.getString());
        // Check versioning
        assertVersion("0.0", file);
        // ------------------------------------------------------
        // FileItem#setBlob and versioning
        // ------------------------------------------------------
        Blob newBlob = new StringBlob("This is a new file.");
        newBlob.setFilename("New blob.txt");
        ensureJustModified(file, session);
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        Blob updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("New blob.txt", updatedBlob.getFilename());
        assertEquals("This is a new file.", updatedBlob.getString());
        // Check versioning => should not be versioned since same
        // contributor
        // and last modification was done before the versioning delay
        assertVersion("0.0", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        newBlob.setFilename("File name modified.txt");
        fileItem.setBlob(newBlob);
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.1", file);
        List<DocumentModel> fileVersions = session.getVersions(file.getRef());
        assertEquals(1, fileVersions.size());
        DocumentModel lastFileVersion = fileVersions.get(0);
        Blob versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("New blob.txt", versionedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        newBlob.setFilename("File name modified by Joe.txt");
        fileItem.setBlob(newBlob);
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File name modified by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.2", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(2, fileVersions.size());
        lastFileVersion = fileVersions.get(1);
        versionedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("File name modified.txt", versionedBlob.getFilename());
        // ------------------------------------------------------
        // DocumentBackedFileItem#rename and versioning
        // ------------------------------------------------------
        // Save document to trigger the DublinCoreListener and update
        // dc:lastContributor to "Administrator"
        // rename the file to enable dc listener (disable if not dirty)
        file.setPropertyValue("file:content/name", "newTitle");
        file = session.saveDocument(file);
        // Check versioning => should be versioned cause last contributor has changed
        assertVersion("0.3", file);
        // Switch back to Administrator as last contributor
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        ensureJustModified(file, session);
        fileItem.rename("Renamed file.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Check versioning => should not be versioned since same
        // contributor and last modification was done before the
        // versioning delay
        assertVersion("0.3", file);
        // Wait for versioning delay
        Thread.sleep(VERSIONING_DELAY);
        fileItem.rename("Renamed again.txt");
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since last
        // modification was done after the versioning delay
        assertVersion("0.4", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(4, fileVersions.size());
        lastFileVersion = fileVersions.get(3);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed file.txt", updatedBlob.getFilename());
        // Update file with another contributor
        file = joeSession.getDocument(file.getRef());
        fileItem = (FileItem) defaultFileSystemItemFactory.getFileSystemItem(file);
        fileItem.rename("File renamed by Joe.txt");
        // Re-fetch file with Administrator session
        file = session.getDocument(file.getRef());
        updatedBlob = (Blob) file.getPropertyValue("file:content");
        assertEquals("File renamed by Joe.txt", updatedBlob.getFilename());
        // Check versioning => should be versioned since updated by a
        // different contributor
        assertVersion("0.5", file);
        fileVersions = session.getVersions(file.getRef());
        assertEquals(5, fileVersions.size());
        lastFileVersion = fileVersions.get(4);
        updatedBlob = (Blob) lastFileVersion.getPropertyValue("file:content");
        assertEquals("Renamed again.txt", updatedBlob.getFilename());
    }
    resetPermissions(rootDoc, "joe");
}
Also used : FileItem(org.nuxeo.drive.adapter.FileItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 24 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testCollectionMembership.

@Test
public void testCollectionMembership() {
    DocumentModel doc = session.createDocumentModel(session.getRootDocument().getPathAsString(), "testDoc", "File");
    Blob blob = new StringBlob("Content of Joe's file.");
    blob.setFilename("Joe.odt");
    doc.setPropertyValue("file:content", (Serializable) blob);
    doc = session.createDocument(doc);
    log.trace("Try to adapt a document not member of any collection");
    try {
        defaultFileSystemItemFactory.getFileSystemItem(doc);
        fail("Trying to adapt doc as a FileSystemItem should throw a RootlessItemException");
    } catch (RootlessItemException e) {
        log.trace(e);
    }
    log.trace("Try to adapt a document member of a non sync root collection");
    DocumentModel nonSyncrootCollection = collectionManager.createCollection(session, "Non sync root collection", "", session.getRootDocument().getPathAsString());
    collectionManager.addToCollection(nonSyncrootCollection, doc, session);
    try {
        defaultFileSystemItemFactory.getFileSystemItem(doc);
        fail("Trying to adapt doc as a FileSystemItem should throw a RootlessItemException");
    } catch (RootlessItemException e) {
        log.trace(e);
    }
    log.trace("Adapt a document member of a non sync root colllection and a sync root collection");
    DocumentModel syncRootCollection = collectionManager.createCollection(session, "Sync root collection", "", session.getRootDocument().getPathAsString());
    nuxeoDriveManager.registerSynchronizationRoot(principal, syncRootCollection, session);
    collectionManager.addToCollection(syncRootCollection, doc, session);
    FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(doc);
    assertNotNull(fsItem);
    log.trace("Adapt a document member of a sync root collection only");
    collectionManager.removeFromCollection(nonSyncrootCollection, doc, session);
    assertEquals(fsItem, defaultFileSystemItemFactory.getFileSystemItem(doc));
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) Blob(org.nuxeo.ecm.core.api.Blob) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) RootlessItemException(org.nuxeo.drive.adapter.RootlessItemException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 25 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class DefaultFileSystemItemFactoryFixture method testLockedDocument.

@Test
public void testLockedDocument() {
    setPermission(syncRootFolder, "joe", SecurityConstants.READ_WRITE, true);
    setPermission(syncRootFolder, "jack", SecurityConstants.READ_WRITE, true);
    try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
        nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, joeSession);
        DocumentModel joeFile = joeSession.getDocument(file.getRef());
        log.trace("Check readonly flags on an unlocked document");
        FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(joeFile);
        assertTrue(fsItem.getCanRename());
        assertTrue(fsItem.getCanDelete());
        assertTrue(((FileItem) fsItem).getCanUpdate());
        assertNull(fsItem.getLockInfo());
        log.trace("Check readonly flags on an document locked by the current user");
        joeSession.setLock(joeFile.getRef());
        // Re-fetch document to clear lock info
        joeFile = joeSession.getDocument(file.getRef());
        fsItem = defaultFileSystemItemFactory.getFileSystemItem(joeFile);
        assertTrue(fsItem.getCanRename());
        assertTrue(fsItem.getCanDelete());
        assertTrue(((FileItem) fsItem).getCanUpdate());
        Lock lockInfo = fsItem.getLockInfo();
        assertNotNull(lockInfo);
        assertEquals("joe", lockInfo.getOwner());
        assertNotNull(lockInfo.getCreated());
        // Check that the lock info is not fetched for FileSystemItem
        // adaptation when calling getChildren or
        // scrollDescendants
        FileSystemItemFactory defaultSyncRootFolderItemFactory = ((FileSystemItemAdapterServiceImpl) fileSystemItemAdapterService).getFileSystemItemFactory("defaultSyncRootFolderItemFactory");
        FolderItem syncRootFolderItem = (FolderItem) defaultSyncRootFolderItemFactory.getFileSystemItem(syncRootFolder);
        List<FileSystemItem> children = syncRootFolderItem.getChildren();
        assertEquals(5, children.size());
        for (FileSystemItem child : children) {
            assertNull(child.getLockInfo());
        }
        children = syncRootFolderItem.scrollDescendants(null, 10, 1000);
        assertEquals(5, children.size());
        for (FileSystemItem child : children) {
            assertNull(child.getLockInfo());
        }
        try (CloseableCoreSession jackSession = coreFeature.openCoreSession("jack")) {
            nuxeoDriveManager.registerSynchronizationRoot(jackSession.getPrincipal(), syncRootFolder, jackSession);
            DocumentModel jackFile = jackSession.getDocument(file.getRef());
            log.trace("Check readonly flags for a non administrator on a document locked by another user");
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(jackFile);
            assertFalse(fsItem.getCanRename());
            assertFalse(fsItem.getCanDelete());
            assertFalse(((FileItem) fsItem).getCanUpdate());
            lockInfo = fsItem.getLockInfo();
            assertNotNull(lockInfo);
            assertEquals("joe", lockInfo.getOwner());
            assertNotNull(lockInfo.getCreated());
            log.trace("Check readonly flags for an administrator on a document locked by another user");
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
            assertTrue(fsItem.getCanRename());
            assertTrue(fsItem.getCanDelete());
            assertTrue(((FileItem) fsItem).getCanUpdate());
            lockInfo = fsItem.getLockInfo();
            assertNotNull(lockInfo);
            assertEquals("joe", lockInfo.getOwner());
            assertNotNull(lockInfo.getCreated());
            log.trace("Check readonly flags for a non administrator on an unlocked document");
            joeSession.removeLock(joeFile.getRef());
            // Re-fetch document to clear lock info
            jackFile = jackSession.getDocument(file.getRef());
            fsItem = defaultFileSystemItemFactory.getFileSystemItem(jackFile);
            assertTrue(fsItem.getCanRename());
            assertTrue(fsItem.getCanDelete());
            assertTrue(((FileItem) fsItem).getCanUpdate());
            assertNull(fsItem.getLockInfo());
        }
    }
    resetPermissions(syncRootFolder, "jack");
    resetPermissions(syncRootFolder, "joe");
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) DefaultFileSystemItemFactory(org.nuxeo.drive.service.impl.DefaultFileSystemItemFactory) FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) FolderItem(org.nuxeo.drive.adapter.FolderItem) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) FileSystemItemAdapterServiceImpl(org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Lock(org.nuxeo.ecm.core.api.Lock) Test(org.junit.Test)

Aggregations

DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)128 Test (org.junit.Test)58 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)26 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)25 FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)24 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)22 PathRef (org.nuxeo.ecm.core.api.PathRef)21 IdRef (org.nuxeo.ecm.core.api.IdRef)18 HashSet (java.util.HashSet)17 Blob (org.nuxeo.ecm.core.api.Blob)17 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)17 ArrayList (java.util.ArrayList)16 FolderItem (org.nuxeo.drive.adapter.FolderItem)14 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)11 Principal (java.security.Principal)10 ACE (org.nuxeo.ecm.core.api.security.ACE)10 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)9 FileItem (org.nuxeo.drive.adapter.FileItem)8 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)8 HashMap (java.util.HashMap)7