001/*
002 * (C) Copyright 2006-2012 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 *     Thomas Roger <troger@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.multi.tenant.userworkspace;
021
022import org.apache.commons.lang.StringUtils;
023import org.nuxeo.common.utils.Path;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.multi.tenant.MultiTenantHelper;
027import org.nuxeo.ecm.multi.tenant.MultiTenantService;
028import org.nuxeo.ecm.platform.userworkspace.constants.UserWorkspaceConstants;
029import org.nuxeo.ecm.platform.userworkspace.core.service.DefaultUserWorkspaceServiceImpl;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Multi tenant aware implementation of the {@link org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService}.
034 * <p>
035 * If there is a current tenant, the UserWorkspaceRoot is stored inside the tenant, otherwise it uses the default
036 * behavior of {@link DefaultUserWorkspaceServiceImpl}.
037 * 
038 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
039 * @since 5.6
040 */
041public class MultiTenantUserWorkspaceService extends DefaultUserWorkspaceServiceImpl {
042
043    private static final long serialVersionUID = 1L;
044
045    protected String getTenantId(CoreSession userCoreSession, String userName) {
046        String tenantId = null;
047        if (userName == null) {
048            userName = userCoreSession.getPrincipal().getName();
049        }
050        MultiTenantService multiTenantService = Framework.getLocalService(MultiTenantService.class);
051        if (multiTenantService.isTenantIsolationEnabled(userCoreSession)) {
052            tenantId = MultiTenantHelper.getTenantId(userName);
053        }
054        return tenantId;
055    }
056
057    @Override
058    protected String computePathUserWorkspaceRoot(CoreSession userCoreSession, String userName,
059            DocumentModel currentDocument) {
060
061        String tenantId = getTenantId(userCoreSession, userName);
062        if (StringUtils.isBlank(tenantId)) {
063            // default behavior
064            return super.computePathUserWorkspaceRoot(userCoreSession, userName, currentDocument);
065        } else {
066            // tenant specific behavior
067            return computePathUserWorkspaceRootForTenant(userCoreSession, tenantId);
068        }
069    }
070
071    protected String computePathUserWorkspaceRootForTenant(CoreSession session, String tenantId) {
072        String tenantDocumentPath = MultiTenantHelper.getTenantDocumentPath(session, tenantId);
073        Path path = new Path(tenantDocumentPath);
074        path = path.append(UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT);
075        return path.toString();
076    }
077
078    /**
079     * Overridden to compute the right user workspace path for an user which is not the current user in the
080     * {@code userCoreSession}.
081     */
082    @Override
083    protected String computePathForUserWorkspace(CoreSession userCoreSession, String userName,
084            DocumentModel currentDocument) {
085
086        String tenantId = getTenantId(userCoreSession, userName);
087        if (StringUtils.isBlank(tenantId)) {
088            // default behavior
089            return super.computePathForUserWorkspace(userCoreSession, userName, currentDocument);
090        } else {
091            // multi-tenant specific
092            return computePathForUserWorkspaceForTenant(userCoreSession, tenantId, userName);
093        }
094    }
095
096    protected String computePathForUserWorkspaceForTenant(CoreSession session, String tenantId, String userName)
097            {
098        String rootPath = computePathUserWorkspaceRootForTenant(session, tenantId);
099        Path path = new Path(rootPath);
100        path = path.append(getUserWorkspaceNameForUser(userName));
101        return path.toString();
102    }
103
104}