Search in sources :

Example 11 with PathRef

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

the class TestGetChangeSummaryMultiRepo method cleanUp.

@After
public void cleanUp() throws Exception {
    // Reset 'other' repository
    otherSession.removeChildren(new PathRef("/"));
    // Close session bound to the 'other' repository
    otherSession.close();
    otherSession = null;
}
Also used : PathRef(org.nuxeo.ecm.core.api.PathRef) After(org.junit.After)

Example 12 with PathRef

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

the class NuxeoDriveSetupIntegrationTests method createTestWorkspace.

protected void createTestWorkspace(String[] testUserNames) {
    // Create test workspace
    String testWorkspaceParentPath = NuxeoDriveIntegrationTestsHelper.getDefaultDomainPath(session) + "/" + NuxeoDriveIntegrationTestsHelper.TEST_WORKSPACE_PARENT_NAME;
    DocumentModel testWorkspace = session.createDocumentModel(testWorkspaceParentPath, TEST_WORKSPACE_NAME, "Workspace");
    testWorkspace.setPropertyValue("dc:title", TEST_WORKSPACE_TITLE);
    session.createDocument(testWorkspace);
    // Grant the given permission to the test users on the test workspace
    String testWorkspacePath = testWorkspaceParentPath + "/" + TEST_WORKSPACE_NAME;
    DocumentRef testWorkspaceDocRef = new PathRef(testWorkspacePath);
    ACP acp = session.getACP(testWorkspaceDocRef);
    ACL localACL = acp.getOrCreateACL(ACL.LOCAL_ACL);
    for (String testUserName : testUserNames) {
        localACL.add(new ACE(testUserName, permission, true));
    }
    session.setACP(testWorkspaceDocRef, acp, false);
}
Also used : ACE(org.nuxeo.ecm.core.api.security.ACE) DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) PathRef(org.nuxeo.ecm.core.api.PathRef) ACL(org.nuxeo.ecm.core.api.security.ACL) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) ACP(org.nuxeo.ecm.core.api.security.ACP)

Example 13 with PathRef

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

the class NuxeoDriveIntegrationTestsHelper method cleanUp.

public static void cleanUp(CoreSession session) {
    // Delete test users and their personal workspace if exist
    UserManager userManager = Framework.getService(UserManager.class);
    DocumentModelList testUsers = userManager.searchUsers(TEST_USER_NAME_PREFIX);
    for (DocumentModel testUser : testUsers) {
        String testUserName = (String) testUser.getPropertyValue(userManager.getUserSchemaName() + ":" + userManager.getUserIdField());
        if (userManager.getPrincipal(testUserName) != null) {
            userManager.deleteUser(testUserName);
        }
        String testUserWorkspaceName = IdUtils.generateId(testUserName, "-", false, 30);
        String testUserWorkspacePath = getDefaultDomainPath(session) + "/" + USER_WORKSPACE_PARENT_NAME + "/" + testUserWorkspaceName;
        DocumentRef testUserWorkspaceRef = new PathRef(testUserWorkspacePath);
        if (session.exists(testUserWorkspaceRef)) {
            session.removeDocument(testUserWorkspaceRef);
            session.save();
        }
    }
    // Delete test workspace if exists
    String testWorkspacePath = getDefaultDomainPath(session) + "/" + TEST_WORKSPACE_PARENT_NAME + "/" + TEST_WORKSPACE_NAME;
    DocumentRef testWorkspaceDocRef = new PathRef(testWorkspacePath);
    if (session.exists(testWorkspaceDocRef)) {
        session.removeDocument(testWorkspaceDocRef);
        session.save();
    }
    // Invalidate user profile cache
    Framework.getService(UserProfileService.class).clearCache();
}
Also used : DocumentModelList(org.nuxeo.ecm.core.api.DocumentModelList) DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) PathRef(org.nuxeo.ecm.core.api.PathRef) UserManager(org.nuxeo.ecm.platform.usermanager.UserManager) UserProfileService(org.nuxeo.ecm.user.center.profile.UserProfileService) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 14 with PathRef

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

the class NuxeoDriveManagerImpl method registerSynchronizationRoot.

@Override
public void registerSynchronizationRoot(Principal principal, final DocumentModel newRootContainer, CoreSession session) {
    final String userName = principal.getName();
    if (log.isDebugEnabled()) {
        log.debug(String.format("Registering synchronization root %s for %s", newRootContainer, userName));
    }
    // If new root is child of a sync root, ignore registration, except for
    // the 'Locally Edited' collection: it is under the personal workspace
    // and we want to allow both the personal workspace and the 'Locally
    // Edited' collection to be registered as sync roots
    Map<String, SynchronizationRoots> syncRoots = getSynchronizationRoots(principal);
    SynchronizationRoots synchronizationRoots = syncRoots.get(session.getRepositoryName());
    if (!NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME.equals(newRootContainer.getName())) {
        for (String syncRootPath : synchronizationRoots.getPaths()) {
            String syncRootPrefixedPath = syncRootPath + "/";
            if (newRootContainer.getPathAsString().startsWith(syncRootPrefixedPath)) {
                // the only exception is when the right inheritance is
                // blocked
                // in the hierarchy
                boolean rightInheritanceBlockedInTheHierarchy = false;
                // should get only parents up to the sync root
                Path parentPath = newRootContainer.getPath().removeLastSegments(1);
                while (!"/".equals(parentPath.toString())) {
                    String parentPathAsString = parentPath.toString() + "/";
                    if (!parentPathAsString.startsWith(syncRootPrefixedPath)) {
                        break;
                    }
                    PathRef parentRef = new PathRef(parentPathAsString);
                    if (!session.hasPermission(principal, parentRef, SecurityConstants.READ)) {
                        rightInheritanceBlockedInTheHierarchy = true;
                        break;
                    }
                    parentPath = parentPath.removeLastSegments(1);
                }
                if (!rightInheritanceBlockedInTheHierarchy) {
                    return;
                }
            }
        }
    }
    checkCanUpdateSynchronizationRoot(newRootContainer, session);
    // Unregister any sub-folder of the new root, except for the 'Locally
    // Edited' collection
    String newRootPrefixedPath = newRootContainer.getPathAsString() + "/";
    for (String existingRootPath : synchronizationRoots.getPaths()) {
        if (!existingRootPath.endsWith(NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME)) {
            if (existingRootPath.startsWith(newRootPrefixedPath)) {
                // Unregister the nested root sub-folder first
                PathRef ref = new PathRef(existingRootPath);
                if (session.exists(ref)) {
                    DocumentModel subFolder = session.getDocument(ref);
                    unregisterSynchronizationRoot(principal, subFolder, session);
                }
            }
        }
    }
    UnrestrictedSessionRunner runner = new UnrestrictedSessionRunner(session) {

        @Override
        public void run() {
            if (!newRootContainer.hasFacet(NUXEO_DRIVE_FACET)) {
                newRootContainer.addFacet(NUXEO_DRIVE_FACET);
            }
            fireEvent(newRootContainer, session, NuxeoDriveEvents.ABOUT_TO_REGISTER_ROOT, userName);
            @SuppressWarnings("unchecked") List<Map<String, Object>> subscriptions = (List<Map<String, Object>>) newRootContainer.getPropertyValue(DRIVE_SUBSCRIPTIONS_PROPERTY);
            boolean updated = false;
            for (Map<String, Object> subscription : subscriptions) {
                if (userName.equals(subscription.get("username"))) {
                    subscription.put("enabled", Boolean.TRUE);
                    subscription.put("lastChangeDate", Calendar.getInstance(UTC));
                    updated = true;
                    break;
                }
            }
            if (!updated) {
                Map<String, Object> subscription = new HashMap<String, Object>();
                subscription.put("username", userName);
                subscription.put("enabled", Boolean.TRUE);
                subscription.put("lastChangeDate", Calendar.getInstance(UTC));
                subscriptions.add(subscription);
            }
            newRootContainer.setPropertyValue(DRIVE_SUBSCRIPTIONS_PROPERTY, (Serializable) subscriptions);
            newRootContainer.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, true);
            newRootContainer.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, true);
            newRootContainer.putContextData(CoreSession.SOURCE, "drive");
            DocumentModel savedNewRootContainer = session.saveDocument(newRootContainer);
            newRootContainer.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, false);
            newRootContainer.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, false);
            fireEvent(savedNewRootContainer, session, NuxeoDriveEvents.ROOT_REGISTERED, userName);
            session.save();
        }
    };
    runner.runUnrestricted();
    invalidateSynchronizationRootsCache(userName);
    invalidateCollectionSyncRootMemberCache(userName);
}
Also used : Path(org.nuxeo.common.utils.Path) PathRef(org.nuxeo.ecm.core.api.PathRef) HashMap(java.util.HashMap) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) UnrestrictedSessionRunner(org.nuxeo.ecm.core.api.UnrestrictedSessionRunner) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with PathRef

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

the class AuditChangeFinderTestSuite method testCollectionEvents.

@Test
public void testCollectionEvents() throws Exception {
    DocumentModel doc1;
    DocumentModel doc2;
    DocumentModel doc3;
    List<FileSystemItemChange> changes;
    DocumentModel locallyEditedCollection;
    try {
        log.trace("Create 2 test docs and them to the 'Locally Edited' collection");
        doc1 = session.createDocumentModel(folder1.getPathAsString(), "doc1", "File");
        doc1.setPropertyValue("file:content", new StringBlob("File content."));
        doc1 = session.createDocument(doc1);
        doc2 = session.createDocumentModel(folder1.getPathAsString(), "doc2", "File");
        doc2.setPropertyValue("file:content", new StringBlob("File content."));
        doc2 = session.createDocument(doc2);
        nuxeoDriveManager.addToLocallyEditedCollection(session, doc1);
        nuxeoDriveManager.addToLocallyEditedCollection(session, doc2);
        DocumentModel userCollections = collectionManager.getUserDefaultCollections(folder1, session);
        DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME);
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
        // Re-fetch documents to get rid of the disabled events in context
        // data
        doc1 = session.getDocument(doc1.getRef());
        doc2 = session.getDocument(doc2.getRef());
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 8 (among which 7 distinct) changes:
        // - addedToCollection for doc2
        // - documentModified for 'Locally Edited' collection (2 occurrences)
        // - rootRegistered for 'Locally Edited' collection
        // - addedToCollection for doc1
        // - documentCreated for 'Locally Edited' collection
        // - documentCreated for doc2
        // - documentCreated for doc1
        changes = getChanges(session.getPrincipal());
        assertEquals(8, changes.size());
        Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
        expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "rootRegistered"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "documentCreated"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        log.trace("Update doc1 member of the 'Locally Edited' collection");
        doc1.setPropertyValue("file:content", new StringBlob("Updated file content."));
        session.saveDocument(doc1);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 1 change: documentModified for doc1
        changes = getChanges(session.getPrincipal());
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(doc1.getId(), "documentModified"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Remove doc1 from the 'Locally Edited' collection, delete doc2 and add doc 3 to the collection");
        collectionManager.removeFromCollection(locallyEditedCollection, doc1, session);
        doc2.followTransition(LifeCycleConstants.DELETE_TRANSITION);
        doc3 = session.createDocumentModel(folder1.getPathAsString(), "doc3", "File");
        doc3.setPropertyValue("file:content", new StringBlob("File content."));
        doc3 = session.createDocument(doc3);
        collectionManager.addToCollection(locallyEditedCollection, doc3, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 6 (among which 5 distinct) changes:
        // - addedToCollection for doc3
        // - documentModified for 'Locally Edited' collection (2 occurrences)
        // - documentCreated for doc3
        // - deleted for doc2
        // - deleted for doc1
        changes = getChanges(session.getPrincipal());
        assertEquals(6, changes.size());
        List<SimpleFileSystemItemChange> expectedChanges = new ArrayList<>();
        expectedChanges.add(new SimpleFileSystemItemChange(doc3.getId(), "addedToCollection"));
        expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentModified"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc3.getId(), "documentCreated"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "deleted"));
        expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "deleted"));
        assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
        log.trace("Unregister the 'Locally Edited' collection as a sync root");
        nuxeoDriveManager.unregisterSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 1 change: deleted for 'Locally Edited' collection
        changes = getChanges(session.getPrincipal());
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "deleted"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Register the 'Locally Edited' collection back as a sync root");
        nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 1 change: rootRegistered for 'Locally Edited'
        // collection
        changes = getChanges(session.getPrincipal());
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "rootRegistered"), toSimpleFileSystemItemChange(changes.get(0)));
        log.trace("Delete the 'Locally Edited' collection");
        locallyEditedCollection.followTransition(LifeCycleConstants.DELETE_TRANSITION);
    } finally {
        commitAndWaitForAsyncCompletion();
    }
    try {
        // Expecting 1 change: deleted for 'Locally Edited' collection
        changes = getChanges(session.getPrincipal());
        assertEquals(1, changes.size());
        assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "deleted"), toSimpleFileSystemItemChange(changes.get(0)));
    } finally {
        commitAndWaitForAsyncCompletion();
    }
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) PathRef(org.nuxeo.ecm.core.api.PathRef) StringBlob(org.nuxeo.ecm.core.api.impl.blob.StringBlob) ArrayList(java.util.ArrayList) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PathRef (org.nuxeo.ecm.core.api.PathRef)26 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)21 Test (org.junit.Test)14 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)8 Path (org.nuxeo.common.utils.Path)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 Blob (org.nuxeo.ecm.core.api.Blob)4 ArrayList (java.util.ArrayList)3 Blob (org.nuxeo.ecm.automation.client.model.Blob)3 IdRef (org.nuxeo.ecm.core.api.IdRef)3 ACE (org.nuxeo.ecm.core.api.security.ACE)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 StringBlob (org.nuxeo.ecm.automation.client.model.StringBlob)2 CollectionManager (org.nuxeo.ecm.collections.api.CollectionManager)2 CoreSession (org.nuxeo.ecm.core.api.CoreSession)2 DocumentModelList (org.nuxeo.ecm.core.api.DocumentModelList)2 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)2 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)2 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)2