1 package org.apache.archiva.redback.common.ldap;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.redback.configuration.UserConfiguration;
23 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
24 import org.apache.archiva.redback.users.User;
25 import org.apache.commons.lang.StringUtils;
26 import org.springframework.stereotype.Service;
28 import javax.annotation.PostConstruct;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.directory.Attributes;
32 import javax.naming.directory.BasicAttributes;
33 import java.util.Date;
36 * @author <a href="jesse@codehaus.org"> jesse
38 @Service("userMapper#ldap")
39 public class LdapUserMapper
45 String emailAttribute = "mail";
50 String fullNameAttribute = "givenName";
55 String passwordAttribute = "userPassword";
60 String userIdAttribute = "cn";
70 String userObjectClass = "inetOrgPerson";
80 int maxResultCount = 0;
83 @Named(value = "userConfiguration#default")
84 private UserConfiguration userConf;
87 public void initialize()
89 emailAttribute = userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_EMAIL, emailAttribute );
91 userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_FULLNAME, fullNameAttribute );
93 userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_PASSWORD, passwordAttribute );
94 userIdAttribute = userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_ID, userIdAttribute );
95 userBaseDn = userConf.getConcatenatedList( "ldap.config.mapper.attribute.user.base.dn",
96 userConf.getConcatenatedList( "ldap.config.base.dn", userBaseDn ) );
98 userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_OBJECT_CLASS, userObjectClass );
99 userFilter = userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_FILTER, userFilter );
100 maxResultCount = userConf.getInt( UserConfigurationKeys.LDAP_MAX_RESULT_COUNT, maxResultCount );
103 public Attributes getCreationAttributes( User user, boolean encodePasswordIfChanged )
104 throws MappingException
106 Attributes userAttrs = new BasicAttributes();
108 boolean passwordSet = false;
110 if ( !passwordSet && ( user.getEncodedPassword() != null ) )
112 userAttrs.put( getPasswordAttribute(), user.getEncodedPassword() );
115 if ( !StringUtils.isEmpty( user.getFullName() ) )
117 userAttrs.put( getUserFullNameAttribute(), user.getFullName() );
120 if ( !StringUtils.isEmpty( user.getEmail() ) )
122 userAttrs.put( getEmailAddressAttribute(), user.getEmail() );
128 public String getEmailAddressAttribute()
130 return emailAttribute;
133 public String getUserFullNameAttribute()
135 return fullNameAttribute;
138 public String getPasswordAttribute()
140 return passwordAttribute;
143 public String[] getUserAttributeNames()
145 return new String[]{ emailAttribute, fullNameAttribute, passwordAttribute, userIdAttribute };
148 public int getMaxResultCount()
150 return maxResultCount;
153 public UserUpdate getUpdate( LdapUser user )
154 throws MappingException
157 Attributes addAttrs = new BasicAttributes();
159 Attributes modAttrs = new BasicAttributes();
161 if ( !StringUtils.isEmpty( user.getFullName() ) )
163 if ( user.getFullName() == null )
165 addAttrs.put( getUserFullNameAttribute(), user.getFullName() );
167 else if ( !user.getFullName().equals( user.getFullName() ) )
169 modAttrs.put( getUserFullNameAttribute(), user.getFullName() );
173 if ( !StringUtils.isEmpty( user.getEmail() ) )
175 if ( user.getEmail() == null )
177 addAttrs.put( getEmailAddressAttribute(), user.getEmail() );
179 else if ( !user.getEmail().equals( user.getEmail() ) )
181 modAttrs.put( getEmailAddressAttribute(), user.getEmail() );
188 public LdapUser getUser( Attributes attributes )
189 throws MappingException
191 String userIdAttribute = getUserIdAttribute();
192 String emailAddressAttribute = getEmailAddressAttribute();
193 String nameAttribute = getUserFullNameAttribute();
194 String passwordAttribute = getPasswordAttribute();
196 String userId = ( LdapUtils.getAttributeValue( attributes, userIdAttribute, "username" ) );
198 LdapUser user = new LdapUser( userId );
199 user.setOriginalAttributes( attributes );
201 user.setEmail( LdapUtils.getAttributeValue( attributes, emailAddressAttribute, "email address" ) );
202 user.setFullName( LdapUtils.getAttributeValue( attributes, nameAttribute, "name" ) );
204 String encodedPassword = LdapUtils.getAttributeValueFromByteArray( attributes, passwordAttribute, "password" );
206 // it seems to be a common convention for the password to come back prepended with the encoding type..
207 // however we deal with that via configuration right now so just smoke it.
208 if ( encodedPassword != null && encodedPassword.startsWith( "{" ) )
210 encodedPassword = encodedPassword.substring( encodedPassword.indexOf( "}" ) + 1 );
213 user.setEncodedPassword( encodedPassword );
215 // REDBACK-215: skip NPE
216 user.setLastPasswordChange( new Date() );
221 public String getUserIdAttribute()
223 return userIdAttribute;
226 public String getEmailAttribute()
228 return emailAttribute;
231 public void setEmailAttribute( String emailAttribute )
233 this.emailAttribute = emailAttribute;
236 public String getFullNameAttribute()
238 return fullNameAttribute;
241 public void setFullNameAttribute( String fullNameAttribute )
243 this.fullNameAttribute = fullNameAttribute;
246 public void setMaxResultCount( int maxResultCount )
248 this.maxResultCount = maxResultCount;
251 public String getUserBaseDn()
256 public void setUserBaseDn( String userBaseDn )
258 this.userBaseDn = userBaseDn;
261 public String getUserObjectClass()
263 return userObjectClass;
266 public String getUserFilter()
271 public void setUserFilter( String userFilter )
273 this.userFilter = userFilter;
276 public void setUserObjectClass( String userObjectClass )
278 this.userObjectClass = userObjectClass;
281 public void setPasswordAttribute( String passwordAttribute )
283 this.passwordAttribute = passwordAttribute;
286 public void setUserIdAttribute( String userIdAttribute )
288 this.userIdAttribute = userIdAttribute;
291 public LdapUser newUserInstance( String username, String fullName, String email )
293 return new LdapUser( username, fullName, email );
296 public LdapUser newTemplateUserInstance()
298 return new LdapUser();
301 public String[] getReturningAttributes()
303 return new String[]{ getUserIdAttribute(), getEmailAttribute(), getFullNameAttribute(),
304 getPasswordAttribute() };
307 public UserConfiguration getUserConf()
312 public void setUserConf( UserConfiguration userConf )
314 this.userConf = userConf;