Search in sources :

Example 1 with SynchronizationRoots

use of org.nuxeo.drive.service.SynchronizationRoots in project nuxeo-drive-server by nuxeo.

the class AuditChangeFinder method getFileSystemChanges.

protected List<FileSystemItemChange> getFileSystemChanges(CoreSession session, Set<IdRef> lastActiveRootRefs, SynchronizationRoots activeRoots, Set<String> collectionSyncRootMemberIds, long lowerBound, long upperBound, boolean integerBounds, int limit) throws TooManyChangesException {
    String principalName = session.getPrincipal().getName();
    List<FileSystemItemChange> changes = new ArrayList<FileSystemItemChange>();
    // Note: lastActiveRootRefs is not used: we could remove it from the
    // public API
    // and from the client as well but it might be useful to optimize future
    // alternative implementations FileSystemChangeFinder component so it
    // might
    // be better to leave it part of the public API as currently.
    // Find changes from the log under active roots or events that are
    // linked to the un-registration or deletion of formerly synchronized
    // roots
    List<LogEntry> entries = queryAuditEntries(session, activeRoots, collectionSyncRootMemberIds, lowerBound, upperBound, integerBounds, limit);
    // query with the actual active roots.
    for (LogEntry entry : entries) {
        if (NuxeoDriveEvents.EVENT_CATEGORY.equals(entry.getCategory())) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Detected sync root change for user '%s' in audit log:" + " invalidating the root cache and refetching the changes.", principalName));
            }
            NuxeoDriveManager driveManager = Framework.getService(NuxeoDriveManager.class);
            driveManager.invalidateSynchronizationRootsCache(principalName);
            driveManager.invalidateCollectionSyncRootMemberCache(principalName);
            Map<String, SynchronizationRoots> synchronizationRoots = driveManager.getSynchronizationRoots(session.getPrincipal());
            SynchronizationRoots updatedActiveRoots = synchronizationRoots.get(session.getRepositoryName());
            Set<String> updatedCollectionSyncRootMemberIds = driveManager.getCollectionSyncRootMemberIds(session.getPrincipal()).get(session.getRepositoryName());
            entries = queryAuditEntries(session, updatedActiveRoots, updatedCollectionSyncRootMemberIds, lowerBound, upperBound, integerBounds, limit);
            break;
        }
    }
    if (entries.size() >= limit) {
        throw new TooManyChangesException("Too many changes found in the audit logs.");
    }
    for (LogEntry entry : entries) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Handling log entry %s", entry));
        }
        FileSystemItemChange change = null;
        DocumentRef docRef = new IdRef(entry.getDocUUID());
        ExtendedInfo fsIdInfo = entry.getExtendedInfos().get("fileSystemItemId");
        if (fsIdInfo != null) {
            // been updated, we just know the FileSystemItem id and name.
            if (log.isDebugEnabled()) {
                log.debug(String.format("Found extended info in audit log entry: document has been deleted, moved," + " is an unregistered synchronization root or its security has been updated," + " we just know the FileSystemItem id and name."));
            }
            boolean isChangeSet = false;
            // current user still has access to the document.
            if (!"deleted".equals(entry.getEventId()) && session.exists(docRef)) {
                change = getFileSystemItemChange(session, docRef, entry, fsIdInfo.getValue(String.class));
                if (change != null) {
                    if (NuxeoDriveEvents.MOVED_EVENT.equals(entry.getEventId())) {
                        // virtual event.
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Document %s (%s) has been moved to another synchronzation root, not adding entry to the change summary.", entry.getDocPath(), docRef));
                        }
                        continue;
                    }
                    isChangeSet = true;
                }
            }
            if (!isChangeSet) {
                // FileSystemItem id and name to the FileSystemItemChange entry.
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Document %s (%s) doesn't exist or is not adaptable as a FileSystemItem, only providing the FileSystemItem id and name to the FileSystemItemChange entry.", entry.getDocPath(), docRef));
                }
                String fsId = fsIdInfo.getValue(String.class);
                String eventId;
                if (NuxeoDriveEvents.MOVED_EVENT.equals(entry.getEventId())) {
                    // Move to a non synchronization root
                    eventId = NuxeoDriveEvents.DELETED_EVENT;
                } else {
                    // Deletion, unregistration or security update
                    eventId = entry.getEventId();
                }
                change = new FileSystemItemChangeImpl(eventId, entry.getEventDate().getTime(), entry.getRepositoryId(), entry.getDocUUID(), fsId, null);
            }
            if (log.isDebugEnabled()) {
                log.debug(String.format("Adding FileSystemItemChange entry to the change summary: %s", change));
            }
            changes.add(change);
        } else {
            // unregistered synchronization root nor a security update denying access to the current user.
            if (log.isDebugEnabled()) {
                log.debug(String.format("No extended info found in audit log entry %s (%s): this is not a deleted document, a moved document," + " an unregistered synchronization root nor a security update denying access to the current user.", entry.getDocPath(), docRef));
            }
            if (!session.exists(docRef)) {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Document %s (%s) doesn't exist, not adding entry to the change summary.", entry.getDocPath(), docRef));
                }
                // try to propagate this event.
                continue;
            }
            // Let's try to adapt the document as a FileSystemItem to
            // provide it to the FileSystemItemChange entry.
            change = getFileSystemItemChange(session, docRef, entry, null);
            if (change == null) {
                // Non-adaptable documents are ignored
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Document %s (%s) is not adaptable as a FileSystemItem, not adding any entry to the change summary.", entry.getDocPath(), docRef));
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Adding FileSystemItemChange entry to the change summary: %s", change));
                }
                changes.add(change);
            }
        }
    }
    return changes;
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) ArrayList(java.util.ArrayList) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) ExtendedInfo(org.nuxeo.ecm.platform.audit.api.ExtendedInfo) FileSystemItemChange(org.nuxeo.drive.service.FileSystemItemChange) TooManyChangesException(org.nuxeo.drive.service.TooManyChangesException) LogEntry(org.nuxeo.ecm.platform.audit.api.LogEntry) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager)

Example 2 with SynchronizationRoots

use of org.nuxeo.drive.service.SynchronizationRoots in project nuxeo-drive-server by nuxeo.

the class UserSyncRootParentFolderItem method getChildren.

@Override
public List<FileSystemItem> getChildren() {
    if (isUserWorkspaceSyncRoot) {
        return super.getChildren();
    } else {
        List<FileSystemItem> children = new ArrayList<FileSystemItem>();
        Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class).getSynchronizationRoots(principal);
        for (String repositoryName : syncRootsByRepo.keySet()) {
            try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
                Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs();
                Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
                while (syncRootRefsIt.hasNext()) {
                    IdRef idRef = syncRootRefsIt.next();
                    // See https://jira.nuxeo.com/browse/NXP-11146
                    if (!session.hasPermission(idRef, SecurityConstants.READ)) {
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("User %s has no READ access on synchronization root %s, not including it in children.", session.getPrincipal().getName(), idRef));
                        }
                        continue;
                    }
                    DocumentModel doc = session.getDocument(idRef);
                    // principal)
                    if (session.getPrincipal().getName().equals(doc.getPropertyValue("dc:creator"))) {
                        // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                        FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false, false);
                        if (child == null) {
                            if (log.isDebugEnabled()) {
                                log.debug(String.format("Synchronization root %s cannot be adapted as a FileSystemItem, maybe because user %s doesn't have the required permission on it (default required permission is ReadWrite). Not including it in children.", idRef, session.getPrincipal().getName()));
                            }
                            continue;
                        }
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Including synchronization root %s in children.", idRef));
                        }
                        children.add(child);
                    }
                }
            }
        }
        Collections.sort(children);
        return children;
    }
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 3 with SynchronizationRoots

use of org.nuxeo.drive.service.SynchronizationRoots in project nuxeo-drive-server by nuxeo.

the class UserWorkspaceSyncRootParentFolderItem method getChildren.

@Override
public List<FileSystemItem> getChildren() {
    List<FileSystemItem> children = new ArrayList<FileSystemItem>();
    Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class).getSynchronizationRoots(principal);
    for (String repositoryName : syncRootsByRepo.keySet()) {
        try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
            Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs();
            Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
            while (syncRootRefsIt.hasNext()) {
                IdRef idRef = syncRootRefsIt.next();
                // See https://jira.nuxeo.com/browse/NXP-11146
                if (!session.hasPermission(idRef, SecurityConstants.READ)) {
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("User %s has no READ access on synchronization root %s, not including it in children.", session.getPrincipal().getName(), idRef));
                    }
                    continue;
                }
                DocumentModel doc = session.getDocument(idRef);
                // registered as a synchronization root to avoid recursion
                if (!UserWorkspaceHelper.isUserWorkspace(doc)) {
                    // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                    FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false, false);
                    if (child == null) {
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Synchronization root %s cannot be adapted as a FileSystemItem, not including it in children.", idRef));
                        }
                        continue;
                    }
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Including synchronization root %s in children.", idRef));
                    }
                    children.add(child);
                }
            }
        }
    }
    Collections.sort(children);
    return children;
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 4 with SynchronizationRoots

use of org.nuxeo.drive.service.SynchronizationRoots in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveGetRootsOperation method run.

@OperationMethod
public DocumentModelList run() {
    // By default get synchronization roots from all repositories, except if
    // a specific repository name is passed as a request header
    boolean allRepositories = true;
    HttpServletRequest request = (HttpServletRequest) ctx.get("request");
    if (request != null) {
        String respositoryName = request.getHeader(RenderingContext.REPOSITORY_NAME_REQUEST_HEADER);
        if (!StringUtils.isEmpty(respositoryName)) {
            allRepositories = false;
        }
    }
    NuxeoDriveManager driveManager = Framework.getService(NuxeoDriveManager.class);
    Map<String, SynchronizationRoots> roots = driveManager.getSynchronizationRoots(ctx.getPrincipal());
    DocumentModelList rootDocumentModels = new DocumentModelListImpl();
    for (Map.Entry<String, SynchronizationRoots> rootsEntry : roots.entrySet()) {
        if (session.getRepositoryName().equals(rootsEntry.getKey())) {
            Set<IdRef> references = rootsEntry.getValue().getRefs();
            rootDocumentModels.addAll(session.getDocuments(references.toArray(new DocumentRef[references.size()])));
        } else {
            if (allRepositories) {
                // XXX: do we really need to implement this now?
                throw new RuntimeException("Multi repo roots not yet implemented");
            }
        }
    }
    return rootDocumentModels;
}
Also used : DocumentModelListImpl(org.nuxeo.ecm.core.api.impl.DocumentModelListImpl) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) HttpServletRequest(javax.servlet.http.HttpServletRequest) DocumentModelList(org.nuxeo.ecm.core.api.DocumentModelList) Map(java.util.Map) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) OperationMethod(org.nuxeo.ecm.automation.core.annotations.OperationMethod)

Example 5 with SynchronizationRoots

use of org.nuxeo.drive.service.SynchronizationRoots in project nuxeo-drive-server by nuxeo.

the class SharedSyncRootParentFolderItem method getChildren.

@Override
public List<FileSystemItem> getChildren() {
    List<FileSystemItem> children = new ArrayList<FileSystemItem>();
    Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class).getSynchronizationRoots(principal);
    for (String repositoryName : syncRootsByRepo.keySet()) {
        try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
            Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs();
            Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
            while (syncRootRefsIt.hasNext()) {
                IdRef idRef = syncRootRefsIt.next();
                // See https://jira.nuxeo.com/browse/NXP-11146
                if (!session.hasPermission(idRef, SecurityConstants.READ)) {
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("User %s has no READ access on synchronization root %s, not including it in children.", session.getPrincipal().getName(), idRef));
                    }
                    continue;
                }
                DocumentModel doc = session.getDocument(idRef);
                // principal)
                if (!session.getPrincipal().getName().equals(doc.getPropertyValue("dc:creator"))) {
                    // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                    FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false, false);
                    if (child == null) {
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Synchronization root %s cannot be adapted as a FileSystemItem, maybe because user %s doesn't have the required permission on it (default required permission is ReadWrite). Not including it in children.", idRef, session.getPrincipal().getName()));
                        }
                        continue;
                    }
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Including synchronization root %s in children.", idRef));
                    }
                    children.add(child);
                }
            }
        }
    }
    Collections.sort(children);
    return children;
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Aggregations

SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)11 IdRef (org.nuxeo.ecm.core.api.IdRef)8 ArrayList (java.util.ArrayList)7 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)6 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)6 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)6 HashMap (java.util.HashMap)4 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)4 Map (java.util.Map)3 LinkedHashSet (java.util.LinkedHashSet)2 FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)2 TooManyChangesException (org.nuxeo.drive.service.TooManyChangesException)2 Serializable (java.io.Serializable)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Path (org.nuxeo.common.utils.Path)1 FileSystemChangeSummary (org.nuxeo.drive.service.FileSystemChangeSummary)1