001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.multi.tenant.userworkspace;
019
020import org.apache.commons.lang.StringUtils;
021import org.nuxeo.common.utils.Path;
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.multi.tenant.MultiTenantHelper;
025import org.nuxeo.ecm.multi.tenant.MultiTenantService;
026import org.nuxeo.ecm.platform.userworkspace.constants.UserWorkspaceConstants;
027import org.nuxeo.ecm.platform.userworkspace.core.service.DefaultUserWorkspaceServiceImpl;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * Multi tenant aware implementation of the {@link org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService}.
032 * <p>
033 * If there is a current tenant, the UserWorkspaceRoot is stored inside the tenant, otherwise it uses the default
034 * behavior of {@link DefaultUserWorkspaceServiceImpl}.
035 * 
036 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
037 * @since 5.6
038 */
039public class MultiTenantUserWorkspaceService extends DefaultUserWorkspaceServiceImpl {
040
041    private static final long serialVersionUID = 1L;
042
043    protected String getTenantId(CoreSession userCoreSession, String userName) {
044        String tenantId = null;
045        if (userName == null) {
046            userName = userCoreSession.getPrincipal().getName();
047        }
048        MultiTenantService multiTenantService = Framework.getLocalService(MultiTenantService.class);
049        if (multiTenantService.isTenantIsolationEnabled(userCoreSession)) {
050            tenantId = MultiTenantHelper.getTenantId(userName);
051        }
052        return tenantId;
053    }
054
055    @Override
056    protected String computePathUserWorkspaceRoot(CoreSession userCoreSession, String userName,
057            DocumentModel currentDocument) {
058
059        String tenantId = getTenantId(userCoreSession, userName);
060        if (StringUtils.isBlank(tenantId)) {
061            // default behavior
062            return super.computePathUserWorkspaceRoot(userCoreSession, userName, currentDocument);
063        } else {
064            // tenant specific behavior
065            return computePathUserWorkspaceRootForTenant(userCoreSession, tenantId);
066        }
067    }
068
069    protected String computePathUserWorkspaceRootForTenant(CoreSession session, String tenantId) {
070        String tenantDocumentPath = MultiTenantHelper.getTenantDocumentPath(session, tenantId);
071        Path path = new Path(tenantDocumentPath);
072        path = path.append(UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT);
073        return path.toString();
074    }
075
076    /**
077     * Overridden to compute the right user workspace path for an user which is not the current user in the
078     * {@code userCoreSession}.
079     */
080    @Override
081    protected String computePathForUserWorkspace(CoreSession userCoreSession, String userName,
082            DocumentModel currentDocument) {
083
084        String tenantId = getTenantId(userCoreSession, userName);
085        if (StringUtils.isBlank(tenantId)) {
086            // default behavior
087            return super.computePathForUserWorkspace(userCoreSession, userName, currentDocument);
088        } else {
089            // multi-tenant specific
090            return computePathForUserWorkspaceForTenant(userCoreSession, tenantId, userName);
091        }
092    }
093
094    protected String computePathForUserWorkspaceForTenant(CoreSession session, String tenantId, String userName)
095            {
096        String rootPath = computePathUserWorkspaceRootForTenant(session, tenantId);
097        Path path = new Path(rootPath);
098        path = path.append(getUserWorkspaceNameForUser(userName));
099        return path.toString();
100    }
101
102}