001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.usermanager;
023
024import java.util.HashMap;
025import java.util.LinkedList;
026import java.util.List;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.security.AdministratorGroupsProvider;
032import org.nuxeo.ecm.directory.DirectoryException;
033import org.nuxeo.ecm.directory.api.DirectoryService;
034import org.nuxeo.ecm.platform.usermanager.UserManager.MatchType;
035import org.nuxeo.runtime.api.Framework;
036import org.nuxeo.runtime.api.login.Authenticator;
037import org.nuxeo.runtime.model.ComponentContext;
038import org.nuxeo.runtime.model.ComponentInstance;
039import org.nuxeo.runtime.model.ComponentName;
040import org.nuxeo.runtime.model.DefaultComponent;
041import org.nuxeo.runtime.services.event.EventService;
042
043public class UserService extends DefaultComponent {
044
045    public static final ComponentName NAME = new ComponentName(UserService.class.getName());
046
047    private static final Log log = LogFactory.getLog(UserService.class);
048
049    private final List<UserManagerDescriptor> descriptors = new LinkedList<UserManagerDescriptor>();
050
051    private UserManager userManager;
052
053    public UserManager getUserManager() {
054        if (userManager == null) {
055            recomputeUserManager(false);
056            EventService eventService = Framework.getLocalService(EventService.class);
057            eventService.addListener(UserManagerImpl.USERMANAGER_TOPIC, userManager);
058        }
059        return userManager;
060    }
061
062    protected void recomputeUserManager(boolean lazy) {
063        if (lazy && userManager == null) {
064            return;
065        }
066        UserManagerDescriptor merged = new UserManagerDescriptor();
067        merged.userListingMode = "search_only";
068        // BBB backward compatibility defaults
069        merged.userDirectoryName = "userDirectory";
070        merged.userEmailField = "email";
071
072        merged.userSearchFields = new HashMap<String, MatchType>();
073        merged.userSearchFields.put("username", MatchType.SUBSTRING);
074        merged.userSearchFields.put("firstName", MatchType.SUBSTRING);
075        merged.userSearchFields.put("lastName", MatchType.SUBSTRING);
076
077        merged.groupDirectoryName = "groupDirectory";
078        merged.groupLabelField = "grouplabel";
079        merged.groupMembersField = "members";
080        merged.groupSubGroupsField = "subGroups";
081        merged.groupParentGroupsField = "parentGroups";
082
083        merged.groupSearchFields = new HashMap<String, MatchType>();
084        merged.groupSearchFields.put("groupname", MatchType.SUBSTRING);
085        merged.groupSearchFields.put("grouplabel", MatchType.SUBSTRING);
086
087        for (UserManagerDescriptor descriptor : descriptors) {
088            merged.merge(descriptor);
089        }
090        Class<?> klass = merged.userManagerClass;
091        if (userManager == null) {
092            if (descriptors.isEmpty()) {
093                throw new NuxeoException("No contributions registered for the userManager");
094            }
095            if (klass == null) {
096                throw new NuxeoException("No class specified for the userManager");
097            }
098        }
099        if (klass != null) {
100            try {
101                userManager = (UserManager) klass.newInstance();
102            } catch (ReflectiveOperationException e) {
103                throw new NuxeoException(e);
104            }
105        }
106        userManager.setConfiguration(merged);
107    }
108
109    @Override
110    public <T> T getAdapter(Class<T> adapter) {
111        if (Authenticator.class == adapter || UserManager.class == adapter
112                || AdministratorGroupsProvider.class == adapter) {
113            return adapter.cast(getUserManager());
114        }
115        return null;
116    }
117
118    @Override
119    public void activate(ComponentContext context) {
120        log.info("UserService activated");
121    }
122
123    @Override
124    public void deactivate(ComponentContext context) {
125        log.info("UserService deactivated");
126        if (userManager != null) {
127            EventService eventService = Framework.getLocalService(EventService.class);
128            if (eventService != null) {
129                eventService.removeListener(UserManagerImpl.USERMANAGER_TOPIC, userManager);
130            }
131        }
132    }
133
134    @Override
135    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
136        descriptors.add((UserManagerDescriptor) contribution);
137        recomputeUserManager(true);
138    }
139
140    @Override
141    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
142        descriptors.remove(contribution);
143        if (Framework.getService(DirectoryService.class) != null) {
144            try {
145                recomputeUserManager(true);
146            } catch (DirectoryException e) {
147                log.debug(e); // at shutdown we may not have a userDirectory anymore
148            }
149        }
150    }
151
152}