Search in sources :

Example 1 with UnrestrictedSessionRunner

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

the class NuxeoDriveManagerImpl method unregisterSynchronizationRoot.

@Override
public void unregisterSynchronizationRoot(Principal principal, final DocumentModel rootContainer, CoreSession session) {
    final String userName = principal.getName();
    if (log.isDebugEnabled()) {
        log.debug(String.format("Unregistering synchronization root %s for %s", rootContainer, userName));
    }
    checkCanUpdateSynchronizationRoot(rootContainer, session);
    UnrestrictedSessionRunner runner = new UnrestrictedSessionRunner(session) {

        @Override
        public void run() {
            if (!rootContainer.hasFacet(NUXEO_DRIVE_FACET)) {
                rootContainer.addFacet(NUXEO_DRIVE_FACET);
            }
            fireEvent(rootContainer, session, NuxeoDriveEvents.ABOUT_TO_UNREGISTER_ROOT, userName);
            @SuppressWarnings("unchecked") List<Map<String, Object>> subscriptions = (List<Map<String, Object>>) rootContainer.getPropertyValue(DRIVE_SUBSCRIPTIONS_PROPERTY);
            for (Map<String, Object> subscription : subscriptions) {
                if (userName.equals(subscription.get("username"))) {
                    subscription.put("enabled", Boolean.FALSE);
                    subscription.put("lastChangeDate", Calendar.getInstance(UTC));
                    break;
                }
            }
            rootContainer.setPropertyValue(DRIVE_SUBSCRIPTIONS_PROPERTY, (Serializable) subscriptions);
            rootContainer.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, true);
            rootContainer.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, true);
            rootContainer.putContextData(CoreSession.SOURCE, "drive");
            session.saveDocument(rootContainer);
            rootContainer.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, false);
            rootContainer.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, false);
            fireEvent(rootContainer, session, NuxeoDriveEvents.ROOT_UNREGISTERED, userName);
            session.save();
        }
    };
    runner.runUnrestricted();
    invalidateSynchronizationRootsCache(userName);
    invalidateCollectionSyncRootMemberCache(userName);
}
Also used : UnrestrictedSessionRunner(org.nuxeo.ecm.core.api.UnrestrictedSessionRunner) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with UnrestrictedSessionRunner

use of org.nuxeo.ecm.core.api.UnrestrictedSessionRunner 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)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 UnrestrictedSessionRunner (org.nuxeo.ecm.core.api.UnrestrictedSessionRunner)2 Path (org.nuxeo.common.utils.Path)1 SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)1 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)1 PathRef (org.nuxeo.ecm.core.api.PathRef)1