Search in sources :

Example 11 with DocumentModel

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

the class NuxeoDriveManagerImpl method addToLocallyEditedCollection.

@Override
public void addToLocallyEditedCollection(CoreSession session, DocumentModel doc) {
    // Add document to "Locally Edited" collection, creating if if not
    // exists
    CollectionManager cm = Framework.getService(CollectionManager.class);
    DocumentModel userCollections = cm.getUserDefaultCollections(doc, session);
    DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), LOCALLY_EDITED_COLLECTION_NAME);
    DocumentModel locallyEditedCollection = null;
    if (session.exists(locallyEditedCollectionRef)) {
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
        cm.addToCollection(locallyEditedCollection, doc, session);
    } else {
        cm.addToNewCollection(LOCALLY_EDITED_COLLECTION_NAME, "Documents locally edited with Nuxeo Drive", doc, session);
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
    }
    // Register "Locally Edited" collection as a synchronization root if not
    // already the case
    Set<IdRef> syncRootRefs = getSynchronizationRootReferences(session);
    if (!syncRootRefs.contains(new IdRef(locallyEditedCollection.getId()))) {
        registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    }
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CollectionManager(org.nuxeo.ecm.collections.api.CollectionManager) PathRef(org.nuxeo.ecm.core.api.PathRef) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 12 with DocumentModel

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

the class NuxeoDriveManagerImpl method computeCollectionSyncRootMemberIds.

@SuppressWarnings("unchecked")
protected Map<String, Set<String>> computeCollectionSyncRootMemberIds(Principal principal) {
    Map<String, Set<String>> collectionSyncRootMemberIds = new HashMap<String, Set<String>>();
    PageProviderService pageProviderService = Framework.getService(PageProviderService.class);
    RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
    for (String repositoryName : repositoryManager.getRepositoryNames()) {
        Set<String> collectionMemberIds = new HashSet<String>();
        try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
            Map<String, Serializable> props = new HashMap<String, Serializable>();
            props.put(CORE_SESSION_PROPERTY, (Serializable) session);
            PageProvider<DocumentModel> collectionPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(CollectionConstants.ALL_COLLECTIONS_PAGE_PROVIDER, null, null, 0L, props);
            List<DocumentModel> collections = collectionPageProvider.getCurrentPage();
            for (DocumentModel collection : collections) {
                if (isSynchronizationRoot(principal, collection)) {
                    PageProvider<DocumentModel> collectionMemberPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(CollectionConstants.COLLECTION_CONTENT_PAGE_PROVIDER, null, COLLECTION_CONTENT_PAGE_SIZE, 0L, props, collection.getId());
                    List<DocumentModel> collectionMembers = collectionMemberPageProvider.getCurrentPage();
                    for (DocumentModel collectionMember : collectionMembers) {
                        collectionMemberIds.add(collectionMember.getId());
                    }
                }
            }
            collectionSyncRootMemberIds.put(repositoryName, collectionMemberIds);
        }
    }
    return collectionSyncRootMemberIds;
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) PageProviderService(org.nuxeo.ecm.platform.query.api.PageProviderService) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) PageProvider(org.nuxeo.ecm.platform.query.api.PageProvider) RepositoryManager(org.nuxeo.ecm.core.api.repository.RepositoryManager) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 13 with DocumentModel

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

the class AuditChangeFinderTestSuite method testFolderishCollection2.

/**
 * <pre>
 * /folder1                 -> synchronization root && isMemberOf(collectionFolder)
 *   |-- collectionFolder
 * /collectionSyncRoot      -> synchronization root
 * /testDoc                 -> isMemberOf(collectionFolder, collectionSyncRoot)
 * </pre>
 */
@Test
public void testFolderishCollection2() throws Exception {
    DocumentModel collectionFolder;
    DocumentModel collectionSyncRoot;
    DocumentModel testDoc;
    List<FileSystemItemChange> changes;
    try {
        log.trace("testFolderishCollection2():" + "\nCreate a folder with the Collection facet (\"collectionFolder\") inside a folder (\"folder1\");" + "\nAdd \"folder1\" to the \"collectionFolder\" collection;" + "\nRegister \"folder1\" as a synchronization root;" + "\nCreate a collection \"collectionSyncRoot\" and register it as a synchronization root;" + "\nCreate a document \"testDoc\" and add it to both collections \"collectionFolder\" and \"collectionSyncRoot\".\n");
        collectionFolder = session.createDocumentModel("/folder1", "collectionFolder", "FolderishCollection");
        collectionFolder = session.createDocument(collectionFolder);
        collectionManager.addToCollection(collectionFolder, folder1, session);
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder1, session);
        collectionSyncRoot = collectionManager.createCollection(session, "collectionSyncRoot", null, "/");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collectionSyncRoot, session);
        testDoc = session.createDocumentModel("/", "testDoc", "File");
        testDoc.setPropertyValue("file:content", new StringBlob("The content of testDoc."));
        testDoc = session.createDocument(testDoc);
        collectionManager.addToCollection(collectionFolder, testDoc, session);
        collectionManager.addToCollection(collectionSyncRoot, testDoc, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 12 (among which 10 distinct) changes:
        // - addedToCollection for testDoc
        // - documentModified for collectionSyncRoot
        // - addedToCollection for testDoc
        // - documentModified for collectionFolder
        // - documentCreated for testDoc
        // - rootRegistered for collectionSyncRoot
        // - documentCreated for collectionSyncRoot
        // - rootRegistered for folder1
        // - addedToCollection for folder1
        // - documentModified for collectionFolder
        // - documentCreated for collectionFolder
        // - documentCreated for folder1
        changes = getChanges(session.getPrincipal());
        assertEquals(12, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionFolder.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionFolder.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(folder1.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with DocumentModel

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

the class AuditChangeFinderTestSuite method testFindSecurityChanges.

@Test
public void testFindSecurityChanges() throws Exception {
    List<FileSystemItemChange> changes;
    DocumentModel subFolder;
    try {
        // No sync roots
        changes = getChanges();
        assertTrue(changes.isEmpty());
        // Create a folder in a sync root
        subFolder = user1Session.createDocumentModel("/folder1", "subFolder", "Folder");
        subFolder = user1Session.createDocument(subFolder);
        // Sync roots for user1
        nuxeoDriveManager.registerSynchronizationRoot(user1Session.getPrincipal(), folder1, user1Session);
        nuxeoDriveManager.registerSynchronizationRoot(user1Session.getPrincipal(), folder2, user1Session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Get changes for user1
        changes = getChanges(user1Session.getPrincipal());
        // Folder creation and sync root registration events
        assertEquals(3, changes.size());
        // Permission changes: deny Read
        // Deny Read to user1 on a regular doc
        setPermissions(subFolder, new ACE(SecurityConstants.ADMINISTRATOR, SecurityConstants.EVERYTHING), ACE.BLOCK);
        // Deny Read to user1 on a sync root
        setPermissions(folder2, new ACE(SecurityConstants.ADMINISTRATOR, SecurityConstants.EVERYTHING), ACE.BLOCK);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        changes = getChanges(user1Session.getPrincipal());
        assertEquals(2, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(folder2.getId(), "securityUpdated", "test", "test#" + folder2.getId(), "folder2"));
        expectedChanges.add(new SimpleFileSystemItemChange(subFolder.getId(), "securityUpdated", "test", "test#" + subFolder.getId(), "subFolder"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        // Changed documents are not adaptable as a FileSystemItem since no Read permission
        for (FileSystemItemChange change : changes) {
            assertNull(change.getFileSystemItem());
        }
        // Permission changes: grant Read
        // Grant Read to user1 on a regular doc
        setPermissions(subFolder, new ACE("user1", SecurityConstants.READ));
        // Grant Read to user1 on a sync root
        setPermissions(folder2, new ACE("user1", SecurityConstants.READ));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    changes = getChanges(user1Session.getPrincipal());
    assertEquals(2, changes.size());
    Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
    expectedChanges.add(new SimpleFileSystemItemChange(folder2.getId(), "securityUpdated", "test", "defaultSyncRootFolderItemFactory#test#" + folder2.getId(), "folder2"));
    expectedChanges.add(new SimpleFileSystemItemChange(subFolder.getId(), "securityUpdated", "test", "defaultFileSystemItemFactory#test#" + subFolder.getId(), "subFolder"));
    assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
    // Changed documents are adaptable as a FileSystemItem since Read permission
    for (FileSystemItemChange change : changes) {
        assertNotNull(change.getFileSystemItem());
    }
}
Also used : ACE(org.nuxeo.ecm.core.api.security.ACE) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with DocumentModel

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

the class AuditChangeFinderTestSuite method testFolderishCollection1.

/**
 * <pre>
 * /folder1                 -> isMemberOf(collectionFolder2)
 *   |-- collectionFolder1
 *   |-- collectionFolder2
 * /collectionSyncRoot      -> synchronization root
 * /testDoc                 -> isMemberOf(collectionFolder1, collectionSyncRoot)
 * </pre>
 */
@Test
public void testFolderishCollection1() throws Exception {
    DocumentModel collectionSyncRoot;
    DocumentModel testDoc;
    List<FileSystemItemChange> changes;
    try {
        log.trace("testFolderishCollection1():" + "\nCreate a folder with the Collection facet (\"collectionFolder1\") inside a folder (\"folder1\");" + "\nCreate a folder with the Collection facet (\"collectionFolder2\") inside a folder (\"folder1\");" + "\nAdd \"folder1\" to the \"collectionFolder2\" collection;" + "\nCreate a collection \"collectionSyncRoot\" and register it as a synchronization root;" + "\nCreate a document \"testDoc\" and add it to both collections \"collectionFolder1\" and \"collectionSyncRoot\".\n");
        DocumentModel collectionFolder1 = session.createDocumentModel("/folder1", "collectionFolder", "FolderishCollection");
        collectionFolder1 = session.createDocument(collectionFolder1);
        DocumentModel collectionFolder2 = session.createDocumentModel("/folder1", "collectionFolder", "FolderishCollection");
        collectionFolder2 = session.createDocument(collectionFolder2);
        collectionManager.addToCollection(collectionFolder2, folder1, session);
        collectionSyncRoot = collectionManager.createCollection(session, "collectionSyncRoot", null, "/");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), collectionSyncRoot, session);
        testDoc = session.createDocumentModel("/", "testDoc", "File");
        testDoc.setPropertyValue("file:content", new StringBlob("The content of testDoc."));
        testDoc = session.createDocument(testDoc);
        collectionManager.addToCollection(collectionFolder1, testDoc, session);
        collectionManager.addToCollection(collectionSyncRoot, testDoc, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 6 (among which 5 distinct) changes:
        // - addedToCollection for testDoc
        // - documentModified for collectionSyncRoot
        // - addedToCollection for testDoc
        // - documentCreated for testDoc
        // - rootRegistered for collectionSyncRoot
        // - documentCreated for collectionSyncRoot
        changes = getChanges(session.getPrincipal());
        assertEquals(6, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(testDoc.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(collectionSyncRoot.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) 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