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.users.User;
24 import org.apache.commons.lang.StringUtils;
25 import org.springframework.stereotype.Service;
27 import javax.annotation.PostConstruct;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.directory.Attributes;
31 import javax.naming.directory.BasicAttributes;
32 import java.util.Date;
35 * @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" )
84 private UserConfiguration userConf;
87 public void initialize()
89 emailAttribute = userConf.getString( "ldap.config.mapper.attribute.email", emailAttribute );
90 fullNameAttribute = userConf.getString( "ldap.config.mapper.attribute.fullname", fullNameAttribute );
91 passwordAttribute = userConf.getString( "ldap.config.mapper.attribute.password", passwordAttribute );
92 userIdAttribute = userConf.getString( "ldap.config.mapper.attribute.user.id", userIdAttribute );
93 userBaseDn = userConf.getConcatenatedList( "ldap.config.mapper.attribute.user.base.dn",
94 userConf.getConcatenatedList( "ldap.config.base.dn", userBaseDn ) );
95 userObjectClass = userConf.getString( "ldap.config.mapper.attribute.user.object.class", userObjectClass );
96 userFilter = userConf.getString( "ldap.config.mapper.attribute.user.filter", userFilter );
97 maxResultCount = userConf.getInt( "ldap.config.max.result.count", maxResultCount );
100 public Attributes getCreationAttributes( User user, boolean encodePasswordIfChanged )
101 throws MappingException
103 Attributes userAttrs = new BasicAttributes();
105 boolean passwordSet = false;
107 if ( !passwordSet && ( user.getEncodedPassword() != null ) )
109 userAttrs.put( getPasswordAttribute(), user.getEncodedPassword() );
112 if ( !StringUtils.isEmpty( user.getFullName() ) )
114 userAttrs.put( getUserFullNameAttribute(), user.getFullName() );
117 if ( !StringUtils.isEmpty( user.getEmail() ) )
119 userAttrs.put( getEmailAddressAttribute(), user.getEmail() );
125 public String getEmailAddressAttribute()
127 return emailAttribute;
130 public String getUserFullNameAttribute()
132 return fullNameAttribute;
135 public String getPasswordAttribute()
137 return passwordAttribute;
140 public String[] getUserAttributeNames()
142 return new String[]{ emailAttribute, fullNameAttribute, passwordAttribute, userIdAttribute };
145 public int getMaxResultCount()
147 return maxResultCount;
150 public UserUpdate getUpdate( LdapUser user )
151 throws MappingException
154 Attributes addAttrs = new BasicAttributes();
156 Attributes modAttrs = new BasicAttributes();
158 if ( !StringUtils.isEmpty( user.getFullName() ) )
160 if ( user.getFullName() == null )
162 addAttrs.put( getUserFullNameAttribute(), user.getFullName() );
164 else if ( !user.getFullName().equals( user.getFullName() ) )
166 modAttrs.put( getUserFullNameAttribute(), user.getFullName() );
170 if ( !StringUtils.isEmpty( user.getEmail() ) )
172 if ( user.getEmail() == null )
174 addAttrs.put( getEmailAddressAttribute(), user.getEmail() );
176 else if ( !user.getEmail().equals( user.getEmail() ) )
178 modAttrs.put( getEmailAddressAttribute(), user.getEmail() );
185 public LdapUser getUser( Attributes attributes )
186 throws MappingException
188 String userIdAttribute = getUserIdAttribute();
189 String emailAddressAttribute = getEmailAddressAttribute();
190 String nameAttribute = getUserFullNameAttribute();
191 String passwordAttribute = getPasswordAttribute();
193 String userId = ( LdapUtils.getAttributeValue( attributes, userIdAttribute, "username" ) );
195 LdapUser user = new LdapUser( userId );
196 user.setOriginalAttributes( attributes );
198 user.setEmail( LdapUtils.getAttributeValue( attributes, emailAddressAttribute, "email address" ) );
199 user.setFullName( LdapUtils.getAttributeValue( attributes, nameAttribute, "name" ) );
201 String encodedPassword = LdapUtils.getAttributeValueFromByteArray( attributes, passwordAttribute, "password" );
203 // it seems to be a common convention for the password to come back prepended with the encoding type..
204 // however we deal with that via configuration right now so just smoke it.
205 if ( encodedPassword != null && encodedPassword.startsWith( "{" ) )
207 encodedPassword = encodedPassword.substring( encodedPassword.indexOf( "}" ) + 1 );
210 user.setEncodedPassword( encodedPassword );
212 // REDBACK-215: skip NPE
213 user.setLastPasswordChange( new Date() );
218 public String getUserIdAttribute()
220 return userIdAttribute;
223 public String getEmailAttribute()
225 return emailAttribute;
228 public void setEmailAttribute( String emailAttribute )
230 this.emailAttribute = emailAttribute;
233 public String getFullNameAttribute()
235 return fullNameAttribute;
238 public void setFullNameAttribute( String fullNameAttribute )
240 this.fullNameAttribute = fullNameAttribute;
243 public void setMaxResultCount( int maxResultCount )
245 this.maxResultCount = maxResultCount;
248 public String getUserBaseDn()
253 public void setUserBaseDn( String userBaseDn )
255 this.userBaseDn = userBaseDn;
258 public String getUserObjectClass()
260 return userObjectClass;
263 public String getUserFilter()
268 public void setUserFilter( String userFilter )
270 this.userFilter = userFilter;
273 public void setUserObjectClass( String userObjectClass )
275 this.userObjectClass = userObjectClass;
278 public void setPasswordAttribute( String passwordAttribute )
280 this.passwordAttribute = passwordAttribute;
283 public void setUserIdAttribute( String userIdAttribute )
285 this.userIdAttribute = userIdAttribute;
288 public LdapUser newUserInstance( String username, String fullName, String email )
290 return new LdapUser( username, fullName, email );
293 public LdapUser newTemplateUserInstance()
295 return new LdapUser();
298 public String[] getReturningAttributes()
300 return new String[]{ getUserIdAttribute(), getEmailAttribute(), getFullNameAttribute(),
301 getPasswordAttribute() };
304 public UserConfiguration getUserConf()
309 public void setUserConf( UserConfiguration userConf )
311 this.userConf = userConf;