forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLdapEntry.java
More file actions
145 lines (124 loc) · 3.41 KB
/
LdapEntry.java
File metadata and controls
145 lines (124 loc) · 3.41 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package org.labkey.openldapsync.ldap;
import org.apache.directory.api.ldap.model.entry.Attribute;
import org.apache.directory.api.ldap.model.entry.Entry;
import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
import org.apache.directory.api.ldap.model.name.Dn;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.labkey.api.security.ValidEmail;
/**
* Created with IntelliJ IDEA.
* User: bimber
* Date: 1/21/13
* Time: 8:24 AM
*/
public class LdapEntry
{
private static final Logger _log = LogManager.getLogger(LdapEntry.class);
private Entry _entry;
protected LdapSettings _settings;
//testing purposes only
protected LdapEntry(LdapSettings settings)
{
_settings = settings;
}
public LdapEntry(Entry e, LdapSettings settings)
{
_entry = e;
_settings = settings;
}
public ValidEmail getValidEmail()
{
try
{
return new ValidEmail(getEmail());
}
catch (ValidEmail.InvalidEmailException e)
{
//ignore
}
return null;
}
public Dn getDn()
{
return _entry.getDn();
}
public String getEmail()
{
return getAttribute(_settings.getEmailMapping());
}
public boolean isEnabled()
{
if (!_settings.shouldReadUserAccountControl())
return true;
String a = getAttribute("userAccountControl");
if (a == null)
{
//only report this error for users.
if (_entry.hasObjectClass(_settings.getUserObjectClass()))
{
_log.info("Unable to determine if LDAP user is active, lacked userAccountControl attribute: " + getDisplayName() + " / " + getDn());
}
return true;
}
try
{
int value = Integer.parseInt(a);
return (value & 2) == 0;
}
catch (NumberFormatException e)
{
_log.error("Invalid value for userAccountControl: " + a + " for user: " + getEmail());
return false;
}
}
public String getDisplayName()
{
String a = getAttribute(_settings.getDisplayNameMapping());
if (a == null)
a = getAttribute("name");
return a;
}
public String getLastName()
{
return getAttribute(_settings.getLastNameMapping());
}
public String getFirstName()
{
return getAttribute(_settings.getFirstNameMapping());
}
public String getPhone()
{
return getAttribute(_settings.getPhoneMapping());
}
public String getUID()
{
return getAttribute(_settings.getUIDMapping());
}
public String getIM() throws LdapInvalidAttributeValueException
{
try
{
Attribute a = _entry.get(_settings.getIMMapping());
return a == null ? null : a.getString();
}
catch (LdapInvalidAttributeValueException e)
{
//not sure what's best here
}
return null;
}
protected String getAttribute(String alias)
{
try
{
Attribute a = _entry.get(alias);
return a == null ? null : a.getString();
}
catch (LdapInvalidAttributeValueException e)
{
_log.error("Invalid LDAP attribute for: " + alias, e);
}
return null;
}
}