001: /*
002: * Copyright 2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package com.pentaho.security.ldap;
014:
015: import javax.naming.NamingException;
016:
017: import org.acegisecurity.GrantedAuthority;
018: import org.acegisecurity.ldap.LdapEntryMapper;
019: import org.acegisecurity.ldap.LdapUserSearch;
020: import org.acegisecurity.providers.ldap.LdapAuthoritiesPopulator;
021: import org.acegisecurity.userdetails.UserDetails;
022: import org.acegisecurity.userdetails.UserDetailsService;
023: import org.acegisecurity.userdetails.ldap.LdapUserDetails;
024: import org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl;
025: import org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.springframework.beans.factory.InitializingBean;
029: import org.springframework.util.Assert;
030:
031: import com.pentaho.messages.LdapUserDetailsServiceMessages;
032:
033: /**
034: * A <code>UserDetailsService</code> implementation that can communicate with
035: * an LDAP repository.
036: *
037: * <p>
038: * See <a href="http://forum.springframework.org/showthread.php?t=22154">this
039: * Spring forum thread</a> for some background.
040: * </p>
041: *
042: * @author mlowery
043: */
044: public class LdapUserDetailsService implements UserDetailsService,
045: InitializingBean {
046: private static final Log logger = LogFactory
047: .getLog(LdapUserDetailsService.class);
048:
049: private LdapUserSearch userSearch;
050:
051: private LdapAuthoritiesPopulator populator;
052:
053: private LdapUserDetailsMapper userDetailsMapper = new LdapUserDetailsMapper();
054:
055: protected LdapEntryMapper getUserDetailsMapper() {
056: return userDetailsMapper;
057: }
058:
059: public void setUserDetailsMapper(
060: final LdapUserDetailsMapper userDetailsMapper) {
061: this .userDetailsMapper = userDetailsMapper;
062: }
063:
064: /**
065: * Unfortunately, this method copies code from
066: * <code>AbstractLdapAuthenticator</code>,
067: * <code>LdapAuthenticationProvider</code>, and <code>LdapTemplate</code>.
068: */
069: public UserDetails loadUserByUsername(final String username) {
070: LdapUserDetails ldapUser = userSearch.searchForUser(username);
071: LdapUserDetailsImpl.Essence user = null;
072: try {
073: user = (LdapUserDetailsImpl.Essence) userDetailsMapper
074: .mapAttributes(ldapUser.getDn(), ldapUser
075: .getAttributes());
076: } catch (NamingException e) {
077: if (logger.isErrorEnabled()) {
078: logger
079: .error(
080: LdapUserDetailsServiceMessages
081: .getString("LdapUserDetailsService.ERROR_0001_NAMING_EXCEPTION"), e); //$NON-NLS-1$
082: }
083: }
084:
085: user.setUsername(username);
086:
087: GrantedAuthority[] extraAuthorities = populator
088: .getGrantedAuthorities(ldapUser);
089:
090: for (int i = 0; i < extraAuthorities.length; i++) {
091: user.addAuthority(extraAuthorities[i]);
092: }
093:
094: return user.createUserDetails();
095: }
096:
097: public void setPopulator(final LdapAuthoritiesPopulator populator) {
098: this .populator = populator;
099: }
100:
101: public void setUserSearch(final LdapUserSearch userSearch) {
102: this .userSearch = userSearch;
103: }
104:
105: public void afterPropertiesSet() throws Exception {
106: Assert
107: .notNull(
108: userSearch,
109: LdapUserDetailsServiceMessages
110: .getString("LdapUserDetailsService.ERROR_0002_USERSEARCH_NOT_SPECIFIED")); //$NON-NLS-1$
111: Assert
112: .notNull(
113: populator,
114: LdapUserDetailsServiceMessages
115: .getString("LdapUserDetailsService.ERROR_0003_POPULATOR_NOT_SPECIFIED")); //$NON-NLS-1$
116: Assert
117: .notNull(
118: userDetailsMapper,
119: LdapUserDetailsServiceMessages
120: .getString("LdapUserDetailsService.ERROR_0004_USERDETAILSMAPPER_NOT_SPECIFIED")); //$NON-NLS-1$
121: }
122: }
|