]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ldap/ArgeoUserDetailsContextMapper.java
459d5e5fc834989b43081c3271c5db673ae5f5c0
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / ldap / ArgeoUserDetailsContextMapper.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.security.ldap;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
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;
33
34 /**
35 * Performs the mapping between LDAP and the user natures, using
36 * {@link UserNatureMapper}.
37 */
38 public class ArgeoUserDetailsContextMapper implements UserDetailsContextMapper {
39 // private final static Log log = LogFactory
40 // .getLog(ArgeoUserDetailsContextMapper.class);
41
42 private List<UserNatureMapper> userNatureMappers = new ArrayList<UserNatureMapper>();
43
44 public UserDetails mapUserFromContext(DirContextOperations ctx,
45 String username, GrantedAuthority[] authorities) {
46 byte[] arr = (byte[]) ctx.getAttributeSortedStringSet("userPassword")
47 .first();
48 String password = new String(arr);
49
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);
55 }
56
57 return new ArgeoUserDetails(username,
58 Collections.unmodifiableMap(userNatures), password, authorities);
59 }
60
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
72 }
73 }
74 }
75 }
76 }
77
78 public void setUserNatureMappers(List<UserNatureMapper> userNatureMappers) {
79 this.userNatureMappers = userNatureMappers;
80 }
81
82 }