1 package org
.argeo
.security
.ldap
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Collections
;
7 import org
.argeo
.security
.ArgeoUser
;
8 import org
.argeo
.security
.UserNature
;
9 import org
.argeo
.security
.core
.ArgeoUserDetails
;
10 import org
.springframework
.ldap
.core
.DirContextAdapter
;
11 import org
.springframework
.ldap
.core
.DirContextOperations
;
12 import org
.springframework
.security
.GrantedAuthority
;
13 import org
.springframework
.security
.userdetails
.UserDetails
;
14 import org
.springframework
.security
.userdetails
.ldap
.UserDetailsContextMapper
;
16 public class ArgeoUserDetailsContextMapper
implements UserDetailsContextMapper
{
17 // private final static Log log = LogFactory
18 // .getLog(ArgeoUserDetailsContextMapper.class);
20 private List
<UserNatureMapper
> userNatureMappers
= new ArrayList
<UserNatureMapper
>();
22 public UserDetails
mapUserFromContext(DirContextOperations ctx
,
23 String username
, GrantedAuthority
[] authorities
) {
24 byte[] arr
= (byte[]) ctx
.getAttributeSortedStringSet("userPassword")
26 String password
= new String(arr
);
28 List
<UserNature
> userNatures
= new ArrayList
<UserNature
>();
29 for (UserNatureMapper userInfoMapper
: userNatureMappers
) {
30 UserNature userNature
= userInfoMapper
.mapUserInfoFromContext(ctx
);
31 if (userNature
!= null)
32 userNatures
.add(userNature
);
35 return new ArgeoUserDetails(username
, Collections
36 .unmodifiableList(userNatures
), password
, authorities
);
39 public void mapUserToContext(UserDetails user
, DirContextAdapter ctx
) {
40 ctx
.setAttributeValues("objectClass", new String
[] { "inetOrgPerson" });
41 ctx
.setAttributeValue("uid", user
.getUsername());
42 ctx
.setAttributeValue("userPassword", user
.getPassword());
43 if (user
instanceof ArgeoUser
) {
44 ArgeoUser argeoUser
= (ArgeoUser
) user
;
45 for (UserNature userNature
: argeoUser
.getUserNatures()) {
46 for (UserNatureMapper userInfoMapper
: userNatureMappers
) {
47 if (userInfoMapper
.supports(userNature
)) {
48 userInfoMapper
.mapUserInfoToContext(userNature
, ctx
);
49 break;// use the first mapper found and no others
56 public void setUserNatureMappers(List
<UserNatureMapper
> userNatureMappers
) {
57 this.userNatureMappers
= userNatureMappers
;