001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.usermanager; 021 022import java.util.HashMap; 023import java.util.LinkedList; 024import java.util.List; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.NuxeoException; 029import org.nuxeo.ecm.core.api.security.AdministratorGroupsProvider; 030import org.nuxeo.ecm.directory.DirectoryException; 031import org.nuxeo.ecm.directory.api.DirectoryService; 032import org.nuxeo.ecm.platform.usermanager.UserManager.MatchType; 033import org.nuxeo.runtime.api.Framework; 034import org.nuxeo.runtime.api.login.Authenticator; 035import org.nuxeo.runtime.model.ComponentContext; 036import org.nuxeo.runtime.model.ComponentInstance; 037import org.nuxeo.runtime.model.ComponentName; 038import org.nuxeo.runtime.model.DefaultComponent; 039import org.nuxeo.runtime.services.event.EventService; 040 041public class UserService extends DefaultComponent { 042 043 public static final ComponentName NAME = new ComponentName(UserService.class.getName()); 044 045 private static final Log log = LogFactory.getLog(UserService.class); 046 047 private final List<UserManagerDescriptor> descriptors = new LinkedList<UserManagerDescriptor>(); 048 049 private UserManager userManager; 050 051 public UserManager getUserManager() { 052 if (userManager == null) { 053 recomputeUserManager(false); 054 EventService eventService = Framework.getLocalService(EventService.class); 055 eventService.addListener(UserManagerImpl.USERMANAGER_TOPIC, userManager); 056 } 057 return userManager; 058 } 059 060 protected void recomputeUserManager(boolean lazy) { 061 if (lazy && userManager == null) { 062 return; 063 } 064 UserManagerDescriptor merged = new UserManagerDescriptor(); 065 merged.userListingMode = "search_only"; 066 // BBB backward compatibility defaults 067 merged.userDirectoryName = "userDirectory"; 068 merged.userEmailField = "email"; 069 070 merged.userSearchFields = new HashMap<String, MatchType>(); 071 merged.userSearchFields.put("username", MatchType.SUBSTRING); 072 merged.userSearchFields.put("firstName", MatchType.SUBSTRING); 073 merged.userSearchFields.put("lastName", MatchType.SUBSTRING); 074 075 merged.groupDirectoryName = "groupDirectory"; 076 merged.groupLabelField = "grouplabel"; 077 merged.groupMembersField = "members"; 078 merged.groupSubGroupsField = "subGroups"; 079 merged.groupParentGroupsField = "parentGroups"; 080 081 merged.groupSearchFields = new HashMap<String, MatchType>(); 082 merged.groupSearchFields.put("groupname", MatchType.SUBSTRING); 083 merged.groupSearchFields.put("grouplabel", MatchType.SUBSTRING); 084 085 for (UserManagerDescriptor descriptor : descriptors) { 086 merged.merge(descriptor); 087 } 088 Class<?> klass = merged.userManagerClass; 089 if (userManager == null) { 090 if (descriptors.isEmpty()) { 091 throw new NuxeoException("No contributions registered for the userManager"); 092 } 093 if (klass == null) { 094 throw new NuxeoException("No class specified for the userManager"); 095 } 096 } 097 if (klass != null) { 098 try { 099 userManager = (UserManager) klass.newInstance(); 100 } catch (ReflectiveOperationException e) { 101 throw new NuxeoException(e); 102 } 103 } 104 userManager.setConfiguration(merged); 105 } 106 107 @Override 108 public <T> T getAdapter(Class<T> adapter) { 109 if (Authenticator.class == adapter || UserManager.class == adapter 110 || AdministratorGroupsProvider.class == adapter) { 111 return adapter.cast(getUserManager()); 112 } 113 return null; 114 } 115 116 @Override 117 public void activate(ComponentContext context) { 118 log.info("UserService activated"); 119 } 120 121 @Override 122 public void deactivate(ComponentContext context) { 123 log.info("UserService deactivated"); 124 if (userManager != null) { 125 EventService eventService = Framework.getLocalService(EventService.class); 126 if (eventService != null) { 127 eventService.removeListener(UserManagerImpl.USERMANAGER_TOPIC, userManager); 128 } 129 } 130 } 131 132 @Override 133 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 134 descriptors.add((UserManagerDescriptor) contribution); 135 recomputeUserManager(true); 136 } 137 138 @Override 139 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 140 descriptors.remove(contribution); 141 if (Framework.getService(DirectoryService.class) != null) { 142 try { 143 recomputeUserManager(true); 144 } catch (DirectoryException e) { 145 log.debug(e); // at shutdown we may not have a userDirectory anymore 146 } 147 } 148 } 149 150}