001/*
002 * (C) Copyright 2006-2018 Nuxeo (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.lang3.StringUtils;
023import org.nuxeo.common.utils.Path;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.multi.tenant.MultiTenantHelper;
026import org.nuxeo.ecm.multi.tenant.MultiTenantService;
027import org.nuxeo.ecm.platform.userworkspace.constants.UserWorkspaceConstants;
028import org.nuxeo.ecm.platform.userworkspace.core.service.DefaultUserWorkspaceServiceImpl;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Multi tenant aware implementation of the {@link org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService}.
033 * <p>
034 * If there is a current tenant, the UserWorkspaceRoot is stored inside the tenant, otherwise it uses the default
035 * behavior of {@link DefaultUserWorkspaceServiceImpl}.
036 *
037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
038 * @since 5.6
039 */
040public class MultiTenantUserWorkspaceService extends DefaultUserWorkspaceServiceImpl {
041
042    private static final long serialVersionUID = 1L;
043
044    protected String getTenantId(CoreSession userCoreSession, String userName) {
045        String tenantId = null;
046        if (userName == null) {
047            userName = userCoreSession.getPrincipal().getName();
048        }
049        MultiTenantService multiTenantService = Framework.getService(MultiTenantService.class);
050        if (multiTenantService.isTenantIsolationEnabled(userCoreSession)) {
051            tenantId = MultiTenantHelper.getTenantId(userName);
052        }
053        return tenantId;
054    }
055
056    @Override
057    protected String computePathUserWorkspaceRoot(CoreSession userCoreSession, String userName) {
058        String tenantId = getTenantId(userCoreSession, userName);
059        if (StringUtils.isBlank(tenantId)) {
060            // default behavior
061            return super.computePathUserWorkspaceRoot(userCoreSession, userName);
062        } else {
063            // tenant specific behavior
064            return computePathUserWorkspaceRootForTenant(userCoreSession, tenantId);
065        }
066    }
067
068    protected String computePathUserWorkspaceRootForTenant(CoreSession session, String tenantId) {
069        String tenantDocumentPath = MultiTenantHelper.getTenantDocumentPath(session, tenantId);
070        Path path = new Path(tenantDocumentPath);
071        path = path.append(UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT);
072        return path.toString();
073    }
074
075}