2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.security
.ldap
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Collections
;
21 import java
.util
.HashMap
;
22 import java
.util
.List
;
25 import org
.argeo
.security
.ArgeoUser
;
26 import org
.argeo
.security
.UserNature
;
27 import org
.argeo
.security
.core
.ArgeoUserDetails
;
28 import org
.springframework
.ldap
.core
.DirContextAdapter
;
29 import org
.springframework
.ldap
.core
.DirContextOperations
;
30 import org
.springframework
.security
.GrantedAuthority
;
31 import org
.springframework
.security
.userdetails
.UserDetails
;
32 import org
.springframework
.security
.userdetails
.ldap
.UserDetailsContextMapper
;
35 * Performs the mapping between LDAP and the user natures, using
36 * {@link UserNatureMapper}.
38 public class ArgeoUserDetailsContextMapper
implements UserDetailsContextMapper
{
39 // private final static Log log = LogFactory
40 // .getLog(ArgeoUserDetailsContextMapper.class);
42 private List
<UserNatureMapper
> userNatureMappers
= new ArrayList
<UserNatureMapper
>();
44 public UserDetails
mapUserFromContext(DirContextOperations ctx
,
45 String username
, GrantedAuthority
[] authorities
) {
46 byte[] arr
= (byte[]) ctx
.getAttributeSortedStringSet("userPassword")
48 String password
= new String(arr
);
50 Map
<String
, UserNature
> userNatures
= new HashMap
<String
, UserNature
>();
51 for (UserNatureMapper userInfoMapper
: userNatureMappers
) {
52 UserNature userNature
= userInfoMapper
.mapUserInfoFromContext(ctx
);
53 if (userNature
!= null)
54 userNatures
.put(userInfoMapper
.getName(), userNature
);
57 return new ArgeoUserDetails(username
,
58 Collections
.unmodifiableMap(userNatures
), password
, authorities
);
61 public void mapUserToContext(UserDetails user
, DirContextAdapter ctx
) {
62 ctx
.setAttributeValues("objectClass", new String
[] { "inetOrgPerson" });
63 ctx
.setAttributeValue("uid", user
.getUsername());
64 ctx
.setAttributeValue("userPassword", user
.getPassword());
65 if (user
instanceof ArgeoUser
) {
66 ArgeoUser argeoUser
= (ArgeoUser
) user
;
67 for (UserNature userNature
: argeoUser
.getUserNatures().values()) {
68 for (UserNatureMapper userInfoMapper
: userNatureMappers
) {
69 if (userInfoMapper
.supports(userNature
)) {
70 userInfoMapper
.mapUserInfoToContext(userNature
, ctx
);
71 break;// use the first mapper found and no others
78 public void setUserNatureMappers(List
<UserNatureMapper
> userNatureMappers
) {
79 this.userNatureMappers
= userNatureMappers
;