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;
021
022import static org.jboss.seam.ScopeType.STATELESS;
023import static org.jboss.seam.annotations.Install.FRAMEWORK;
024import static org.nuxeo.ecm.multi.tenant.Constants.TENANT_ID_PROPERTY;
025
026import java.io.Serializable;
027import java.util.List;
028
029import org.apache.commons.lang.StringUtils;
030import org.jboss.seam.annotations.In;
031import org.jboss.seam.annotations.Install;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Scope;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.NuxeoPrincipal;
037import org.nuxeo.ecm.directory.api.DirectoryService;
038import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
039import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
040import org.nuxeo.ecm.webapp.directory.DirectoryUIActionsBean;
041import org.nuxeo.runtime.api.Framework;
042
043import javax.faces.application.FacesMessage;
044import javax.faces.component.UIComponent;
045import javax.faces.context.FacesContext;
046import javax.faces.validator.ValidatorException;
047
048/**
049 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
050 * @since 5.6
051 */
052@Name("multiTenantActions")
053@Scope(STATELESS)
054@Install(precedence = FRAMEWORK)
055public class MultiTenantActions implements Serializable {
056
057    private static final long serialVersionUID = 1L;
058
059    public static final String TENANT_ADMINISTRATORS_VALIDATION_ERROR = "label.tenant.administrators.validation.error";
060
061    @In(create = true)
062    protected transient CoreSession documentManager;
063
064    @In(create = true)
065    protected NavigationContext navigationContext;
066
067    @In(create = true)
068    protected DirectoryUIActionsBean directoryUIActions;
069
070    public List<DocumentModel> getTenants() {
071        MultiTenantService multiTenantService = Framework.getService(MultiTenantService.class);
072        return multiTenantService.getTenants();
073    }
074
075    public boolean isTenantIsolationEnabled() {
076        MultiTenantService multiTenantService = Framework.getService(MultiTenantService.class);
077        return multiTenantService.isTenantIsolationEnabled(documentManager);
078    }
079
080    public void enableTenantIsolation() {
081        MultiTenantService multiTenantService = Framework.getService(MultiTenantService.class);
082        multiTenantService.enableTenantIsolation(documentManager);
083    }
084
085    public void disableTenantIsolation() {
086        MultiTenantService multiTenantService = Framework.getService(MultiTenantService.class);
087        multiTenantService.disableTenantIsolation(documentManager);
088    }
089
090    public boolean isReadOnlyDirectory(String directoryName) {
091        MultiTenantService multiTenantService = Framework.getService(MultiTenantService.class);
092        if (multiTenantService.isTenantIsolationEnabled(documentManager)) {
093            if (multiTenantService.isTenantAdministrator(documentManager.getPrincipal())) {
094                DirectoryService directoryService = Framework.getService(DirectoryService.class);
095                return !directoryService.getDirectory(directoryName).isMultiTenant();
096            }
097        }
098        return directoryUIActions.isReadOnly(directoryName);
099    }
100
101    @SuppressWarnings("unchecked")
102    public void validateTenantAdministrators(FacesContext context, UIComponent component, Object value)
103            {
104        DocumentModel currentDocument = navigationContext.getCurrentDocument();
105        String currentDocumentTenantId = (String) currentDocument.getPropertyValue(TENANT_ID_PROPERTY);
106        NuxeoPrincipal currentUser = (NuxeoPrincipal) documentManager.getPrincipal();
107        String currentUserTenantId = currentUser.getTenantId();
108        if (!StringUtils.isBlank(currentDocumentTenantId) && !StringUtils.isBlank(currentUserTenantId)
109                && currentUserTenantId.equals(currentDocumentTenantId)) {
110            String administratorGroup = MultiTenantHelper.computeTenantAdministratorsGroup(currentDocumentTenantId);
111            if (currentUser.isMemberOf(administratorGroup)) {
112                List<String> users = (List<String>) value;
113                if (!users.contains(currentUser.getName())) {
114                    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, ComponentUtils.translate(
115                            context, TENANT_ADMINISTRATORS_VALIDATION_ERROR), null);
116                    throw new ValidatorException(message);
117                }
118            }
119        }
120    }
121
122}