001/*
002 * (C) Copyright 2011 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 *     bjalon
018 */
019package org.nuxeo.ecm.platform.usermanager;
020
021import static org.nuxeo.ecm.directory.localconfiguration.DirectoryConfigurationConstants.DIRECTORY_CONFIGURATION_FACET;
022
023import java.io.Serializable;
024import java.util.Map;
025import java.util.Set;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
032import org.nuxeo.ecm.directory.localconfiguration.DirectoryConfiguration;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author bjalon
037 */
038public class DefaultUserMultiTenantManagement implements UserMultiTenantManagement {
039
040    protected static final Log log = LogFactory.getLog(DefaultUserMultiTenantManagement.class);
041
042    protected static final String SUFFIX_SEPARATOR = "-";
043
044    protected String getDirectorySuffix(DocumentModel documentContext) {
045        LocalConfigurationService localConfigurationService = Framework.getService(LocalConfigurationService.class);
046        DirectoryConfiguration configuration = localConfigurationService.getConfiguration(DirectoryConfiguration.class,
047                DIRECTORY_CONFIGURATION_FACET, documentContext);
048        if (configuration != null && configuration.getDirectorySuffix() != null) {
049            return SUFFIX_SEPARATOR + configuration.getDirectorySuffix();
050        }
051        return null;
052    }
053
054    @Override
055    public void queryTransformer(UserManager um, Map<String, Serializable> filter, Set<String> fulltext,
056            DocumentModel context) {
057        String groupId = um.getGroupIdField();
058        if (filter == null || fulltext == null) {
059            throw new NuxeoException("Filter and Fulltext must be not null");
060        }
061
062        if (getDirectorySuffix(context) == null) {
063            log.debug("Directory Local Configuration is null, don't need to filter");
064            return;
065        }
066
067        String groupIdSuffix = getDirectorySuffix(context);
068
069        if (!filter.containsKey(groupId)) {
070            log.debug("no filter on group id, need to filter with the directory local " + "configuration suffix : "
071                    + groupId + " = %" + groupIdSuffix);
072            filter.put(groupId, "%" + groupIdSuffix);
073            fulltext.add(groupId);
074            return;
075        }
076
077        if (!(filter.get(groupId) instanceof String)) {
078            throw new UnsupportedOperationException("Filter value on " + "group id is not a string : "
079                    + filter.get(groupId));
080        }
081
082        String filterIdValue = (String) filter.get(um.getGroupIdField());
083        filter.put(groupId, filterIdValue + groupIdSuffix);
084    }
085
086    @Override
087    public DocumentModel groupTransformer(UserManager um, DocumentModel group, DocumentModel context)
088            {
089        if (context == null) {
090            return group;
091        }
092        String groupIdValue = group.getPropertyValue(um.getGroupIdField()) + getDirectorySuffix(context);
093        group.setPropertyValue(um.getGroupIdField(), groupIdValue);
094        return group;
095    }
096
097    @Override
098    public String groupnameTranformer(UserManager um, String groupname, DocumentModel context) {
099        String suffix = getDirectorySuffix(context);
100        if (suffix != null) {
101            groupname += suffix;
102        }
103        return groupname;
104    }
105}