001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.hierarchy.userworkspace.adapter;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import org.apache.logging.log4j.LogManager;
029import org.apache.logging.log4j.Logger;
030import org.nuxeo.drive.adapter.FileSystemItem;
031import org.nuxeo.drive.adapter.FolderItem;
032import org.nuxeo.drive.adapter.impl.AbstractVirtualFolderItem;
033import org.nuxeo.drive.service.NuxeoDriveManager;
034import org.nuxeo.drive.service.SynchronizationRoots;
035import org.nuxeo.ecm.core.api.CloseableCoreSession;
036import org.nuxeo.ecm.core.api.CoreInstance;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.IdRef;
039import org.nuxeo.ecm.core.api.NuxeoPrincipal;
040import org.nuxeo.ecm.core.api.security.SecurityConstants;
041import org.nuxeo.runtime.api.Framework;
042
043/**
044 * User workspace based implementation of the synchronization root parent {@link FolderItem}.
045 *
046 * @author Antoine Taillefer
047 */
048public class UserWorkspaceSyncRootParentFolderItem extends AbstractVirtualFolderItem {
049
050    private static final Logger log = LogManager.getLogger(UserWorkspaceSyncRootParentFolderItem.class);
051
052    public UserWorkspaceSyncRootParentFolderItem(String factoryName, NuxeoPrincipal principal, String parentId,
053            String parentPath, String folderName) {
054        super(factoryName, principal, parentId, parentPath, folderName);
055    }
056
057    protected UserWorkspaceSyncRootParentFolderItem() {
058        // Needed for JSON deserialization
059    }
060
061    @Override
062    public List<FileSystemItem> getChildren() {
063
064        List<FileSystemItem> children = new ArrayList<>();
065        Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class)
066                                                                     .getSynchronizationRoots(principal);
067        for (Map.Entry<String, SynchronizationRoots> entry : syncRootsByRepo.entrySet()) {
068            try (CloseableCoreSession session = CoreInstance.openCoreSession(entry.getKey(), principal)) {
069                Set<IdRef> syncRootRefs = entry.getValue().getRefs();
070                Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
071                while (syncRootRefsIt.hasNext()) {
072                    IdRef idRef = syncRootRefsIt.next();
073                    // TODO: ensure sync roots cache is up-to-date if ACL
074                    // change, for now need to check permission
075                    // See https://jira.nuxeo.com/browse/NXP-11146
076                    if (!session.hasPermission(idRef, SecurityConstants.READ)) {
077                        log.debug(
078                                "User {} has no READ access on synchronization root {}, not including it in children.",
079                                session::getPrincipal, () -> idRef);
080                        continue;
081                    }
082                    DocumentModel doc = session.getDocument(idRef);
083                    // Don't include user workspace (ie.top level folder) if
084                    // registered as a synchronization root to avoid recursion
085                    if (!UserWorkspaceHelper.isUserWorkspace(doc)) {
086                        // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
087                        FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false,
088                                false, false);
089                        if (child == null) {
090                            log.debug(
091                                    "Synchronization root {} cannot be adapted as a FileSystemItem, not including it in children.",
092                                    idRef);
093                            continue;
094                        }
095                        log.debug("Including synchronization root {} in children.", idRef);
096                        children.add(child);
097                    }
098                }
099            }
100        }
101        Collections.sort(children);
102        return children;
103    }
104
105}