Purpose
Given a userId, this method will return the name/value HashMap containing the user profile information.
Applies To
LDAP
Signature
public HashMap ldapIdSearch(String userId)
Notes
This single HashMap should be in the same format as the name/value HashMaps returned by ldapSearch(). This will only be called if ldapOverride() returns true.
Example
This shows how when provided with a userId, information about that user is returned such as their name, company information, and any pre-defined user defined fields. Please note that this search stops when 1 match is found as there will only be 1 match per unique userID.
public HashMap ldapIdSearch (String userId) {
HashMap nameVals1 = new HashMap();
DirContext ctx = null;
NamingEnumeration results = null;
try {
String filter = "(" + Z.lu.LDAP_PRIMARYKEY + "=" + userId + ")";
String[] upsertFields = {Z.lu.LDAP_COMMONNAME,
Z.lu.LDAP_GIVENNAME,
Z.lu.LDAP_SURNAME,
Z.lu.LDAP_EMAIL,
Z.lu.LDAP_STREET,
Z.lu.LDAP_PHONE,
Z.lu.LDAP_POSTALCODE,
Z.lu.LDAP_STATE,
Z.lu.LDAP_CITY,
Z.lu.LDAP_COUNTRY,
Z.lu.LDAP_FAX,
Z.lu.LDAP_TITLE};
// Specify the scope of the search
// subtree: starts at the base entry and searches
// everything below it, including the base entry
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Perform the actual search
String dn = Z.lu.getInitMgrDn();
String password = Z.lu.getInitPswd();
ctx = this.getContext(dn, password);
results = ctx.search(Z.lu.getInitSearchBase(), filter, constraints);
// Now Stepping through the search results
int count = 0;
while (results != null && results.hasMore() && count 0) {
tmp.append(", ");
}
tmp.append(((String) nEnum.next()).trim());
if (TextManager.isStringInvisible(tmp.toString()) ) {
tmp = new StringBuffer(LdapUtil.NA);
}
nameVals1.put(upsertFields[i], tmp.toString());
count++;
// If the count was 0 then no results were returned.
// Send back a null HashMap otherwise Search ldap will assume
// a user was found and attempt to insert a null record.
if (count == 0) { nameVals1 = null; }
} catch(Exception e) {
Z.log.writeToLog(Z.log.ERROR, "UC: ldapIdSearch Exception: " + e);
ErrorWriter.write(e, ErrorWriter.LOGERR);
} finally {
try {
if (results != null) { results.close(); }
if (ctx != null) { ctx.close(); }
} catch (Exception e) {
Z.log.writeToLog(Z.log.ERROR, "UC: ldapIdSearch Exception: " +
"Could not close context or results - " + e);
ErrorWriter.write(e, ErrorWriter.LOGERR);
}
}
return nameVals1;
}