forked from aayushgoyal1/rest-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLdapAttributesMapper.java
78 lines (61 loc) · 3.28 KB
/
LdapAttributesMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@Configuration
public class LdapAttributesMapper implements AttributesMapper<Object> {
public Object mapFromAttributes(Attributes attrs) throws NamingException {
System.out.println("attrs: "+ attrs.toString());
String preferredFirstName = null;
Attribute preferredFirstNameAttr = (Attribute)attrs.get("preferredFirstName");
if (preferredFirstNameAttr != null) preferredFirstName = (String)preferredFirstNameAttr.get();
String hrFirstName = null;
Attribute hrFirstNameAttr = (Attribute)attrs.get("hrFirstName");
if (hrFirstNameAttr != null) hrFirstName = (String)hrFirstNameAttr.get();
String givenName = null;
Attribute givenNameAttr = (Attribute)attrs.get("givenName");
if (givenNameAttr != null) givenName = (String)givenNameAttr.get(0);
String email = null;
Attribute emailAttr = (Attribute)attrs.get("preferredIdentity");
if (emailAttr != null) email = (String)emailAttr.get();
SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
LdapUser user = new LdapUser(email, email, Arrays.asList(new GrantedAuthority[] {authority}));
user.setEmail(email);
String firstName = null;
if (preferredFirstName != null) firstName = preferredFirstName;
else if (hrFirstName != null) firstName = hrFirstName;
else if (givenName != null) firstName = givenName;
else firstName = "Not found";
user.setFirstName(firstName);
String preferredLastName = null;
Attribute preferredLastNameAttr = (Attribute)attrs.get("preferredLastName");
if (preferredLastNameAttr != null) preferredLastName = (String)preferredLastNameAttr.get();
String hrLastName = null;
Attribute hrLastNameAttr = (Attribute)attrs.get("hrLastName");
if (hrLastNameAttr != null) hrLastName = (String)hrLastNameAttr.get();
String surName = null;
Attribute surNameAttr = (Attribute)attrs.get("sn");
if (surNameAttr != null) surName = (String)surNameAttr.get(0);
String lastName = null;
if (preferredLastName != null) lastName = preferredLastName;
else if (hrLastName != null) lastName = hrLastName;
else if (surName != null) lastName = surName;
else lastName = "Not found";
user.setLastName(lastName);
String uid = null;
Attribute uidAttr = (Attribute)attrs.get("uid");
if (uidAttr != null) uid = (String)uidAttr.get();
user.setUid(uid);
String phone = null;
Attribute phoneAttr = (Attribute)attrs.get("telephoneNumber");
if (phoneAttr != null) phone = (String)phoneAttr.get();
user.setPhoneNumber(phone);
}
}