LDAPClient Utility
LDAP utility
Use this utility to access external LDAP.
1. UserLookupHelper
This helper object allows managing LDAP user based on a particular LDAP server connection configuration.
Example of UserLookupHelper
usage:
var userLookupHelper = new UserLookupHelper("ldap_staging");
var user = userLookupHelper.getUser("jsmith");
if (user.authenticate("secret")) {
var arr = userLookupHelper.search("familyName", "Smith", 100);
for (const nativeId of arr) {
var otherUser = userLookupHelper.getUserByNativeId(nativeId);
if (otherUser.getNativeId() != user.getNativeId()) {
// do something
}
}
} else {
IDMappingExtUtils.traceString("Authentication failed:"+user.getErrMessage());
userLookupHelper.createUser("jsmith", "cn=jsmith,dc=example,dc=org", "secret", "John", "Smith");
}
Return Type | Method | Description | Arguments |
---|---|---|---|
UserLookupHelper | UserLookupHelper(cfgName) | Constructor of UserLookupHelper class. cfgname is the unique name of the ldap configuration that is specified in ldapcfg.yml . If cfgName is null, default configuration name ldap is used. | string |
Boolean | init() | Tests the ldap connection only, does not perform ldap initialization. Returns true if test connection succeeded, otherwise returns false . Refer to UserLookupHelper Constructor to connect different ldap. | |
Boolean | isReady() | Tests the ldap connection. Returns true if test connection succeeded, otherwise returns false | |
User | createUser(username, dn, password, firstName, lastName) | Create user with the provided information. | string, string, string, string, string |
User | getUser(username) | Retrieve the user who is associated with the provided username. | string |
User | getUserByNativeId(nativeId) | Retrieve the user who is associated with the provided native ID. | string |
Boolean | deleteUser(username) | Delete the user who is associated with the provided username. | string |
string[] | search(searchAttr, attrPattern, maxReturned) | Search users based on the specified attribute pattern. Returns match the array of native IDs. | string, string, integer |
2. User
This object represents a User that is returned by UserLookupHelper.
Return Type | Method | Description | Arguments |
---|---|---|---|
Boolean | hasError() | Check whether the processing detected any errors. This method is always called first to check whether an error exists whenever a new User object is return from a UserLookupHelper method. | |
string | getError() | Retrieve the error returned | |
string | getErrMessage() | The same as getError(). This method is provided for ISAV compatibility. | |
Boolean | authenticate(password) | Attempts to authenticate the user with the specified password | string |
string | getId() | Get the users ID | |
string | getNativeId() | Get the users native ID. | |
string[] | getAttributeNames() | Get the users attribute names | |
string | getAttribute(attrName) | Fetch a single attribute value. If this attribute is a multivalued attribute, the first value is returned. Null, if the attribute wasn't found. | string |
string[] | getAttributes(attrName) | Fetch all values for an attribute. Null if the attribute wasn't found. | string |
Boolean | attributeExists(attrName) | Check whether a user has an attribute. Returns true if the user has the attribute, false if does not. | string |
3. LdapAttributeUtil
This utility is the native LDAP Attribute Utility.
Example of LdapAttributeUtil
usage:
var attrUtil = new LdapAttributeUtil("ldap_staging");
// createSubContext
let result = attrUtil.createSubContext("ou=newOu", {"objectClass": ["top", "organizationalUnit"]});
if (result.hasError()) {
let error = result.getError();
// handle error...
}
// addAttributeValue
result = attrUtil.addAttributeValue("ou=newOu", "description", "description_01");
if (result.hasError()) {
let error = result.getError();
// handle error...
}
result = attrUtil.addAttributeValue("ou=newOu", "description", "description_02");
if (result.hasError()) {
let error = result.getError();
// handle error...
}
// getAttributeValue
result = attrUtil.getAttributeValue("ou=newOu", ["objectClass", "description"]);
if (result.hasError()) {
let error = result.getError();
// handle error...
} else {
let attributes = result.getAttributes();
// do something...
let attributesSize = attributes.size();
// do something...
// loop through attributes using attribute name enumeration
let attrNameItr = attributes.getIDs();
while (attrNameItr.hasMore()) {
let attrName = attrNameItr.next();
let attribute = attributes.get(attrName);
// do something...
}
// loop through attributes using attribute enumeration
let attrItr = attributes.getAll();
while (attrItr.hasMore()) {
let attribute = attrItr.next();
let attrSize = attribute.size();
let attrName = attribute.getID();
// do something...
// loop through attribute values using attribute value enumeration
let attrValItr = attribute.getAll();
while (attrValItr.hasMore()) {
let attrValue = attrValItr.next();
// do something...
}
}
}
// removeAttribute
result = attrUtil.removeAttribute("ou=newOu", "description", "description_02");
if (result.hasError()) {
let error = result.getError();
// handle error...
}
// setAttributeValue
result = attrUtil.setAttributeValue("ou=newOu", "description", "new_description_01");
if (result.hasError()) {
let error = result.getError();
// handle error...
}
// search
result = attrUtil.search("", "(objectclass=*)");
if (result.hasError()) {
let error = result.getError();
// handle error...
} else {
// loop through searchResult using search result enumeration
let searchResultItr = result.getNamingEnumeration();
while (searchResultItr.hasMore()) {
let searchResult = searchResultItr.next();
let name = searchResult.getName(); // dn
let attributes = searchResult.getAttributes(); // Attributes
// do something...
// refer to getAttributeValue section for iterating through Attributes
}
}
Return Type | Method | Description | Arguments |
---|---|---|---|
LdapAttributeUtil | LdapAttributeUtil(cfgName) | Constructor of LdapAttributeUtil class. cfgname is the unique name of the ldap configuration that is specified in ldapcfg.yml . If cfgName is null, default configuration name ldap is used. | string |
LdapAttributeResult | init() | Tests the ldap connection only, does not perform ldap initialization. Use hasError or isSuccessful on the returned LdapAttributeResult instance to check whether the test connection succeeded. Use the LdapAttributeUtil constructor to a connect different LDAP. | |
Boolean | isReady() | Tests the ldap connection. Returns true if test connection succeeded, otherwise returns false | |
LdapAttributeResult | addAttributeValue(dn, attributeName, attributeValue) | This interface gives the user ability to add the attribute from the LDAP. | string, string, string |
LdapAttributeResult | createSubContext(dn, attributes) | This method is to give the user ability to create the sub context | string, JS object |
LdapAttributeResult | getAttributeValue(dn, attributeNames) | This interface gives the user ability to get the attribute from LDAP | string, string[] |
LdapAttributeResult | removeAttribute(dn, attributeName, attributeValue) | This interface gives the user ability to remove the attribute value from LDAP | string, string, string |
LdapAttributeResult | setAttributeValue(dn, attributeName, attributeValue) | This interface gives the user ability to update the attribute value from the LDAP | string, string, string |
LdapAttributeResult | search(dn, filter) | This method gives the user ability to search LDAP for the specified filter in the specific dn | string, string |
4. LdapAttributeResult
This result is the Operation result for the native LDAP Attribute Utility.
Return Type | Method | Description | Arguments |
---|---|---|---|
Boolean | hasError() | Check whether the processing detected any error. This method is always called first to check whether an error exists whenever a new LdapAttributeResult object is return from a LdapAttributeUtil method. | |
Boolean | isSuccessful() | Returns inverted value of hasError(). For example, if hasError() returns true, isSuccessful() returns false and vice versa. This method is provided for ISAV compatibility. | |
string | getError() | Retrieve the error returned | |
string | getNamingException() | The same as getError(). This method is provided for ISVA compatibility. | |
Attributes | getAttributes() | Get the Attributes returned by LdapAttributeUtil.getAttributeValue() method. This ONLY works for LdapAttributeResult object return by LdapAttributeUtil.getAttributeValue() method | |
NamingEnumeration<SearchResult> | getNamingEnumeration() | Get the SearchResult Enumeration returned by LdapAttributeUtil.search() method. This ONLY works for LdapAttributeResult object return by LdapAttributeUtil.search() method |
5. Attributes
This example represents the LDAP attributes.
Return Type | Method | Description | Arguments |
---|---|---|---|
Boolean | isCaseIgnored() | Mockup function for ISVA compatibility, always returns false. | |
int | size() | Retrieves the number of attributes in the attribute set. | |
Attribute | get(attrID) | Retrieves the attribute with the specified attribute ID from the attribute set. | string |
NamingEnumeration<String> | getIDs() | Retrieves an enumeration of the IDs of the attributes in the attribute set. | |
NamingEnumeration<Attribute> | getAll() | Retrieves an enumeration of the attributes in the attribute set. |
6. Attribute
This example represents the LDAP attribute.
Return Type | Method | Description | Arguments |
---|---|---|---|
Boolean | contains(attrVal) | Determines whether a value is in the attribute | string |
Boolean | isOrdered() | Mockup function for ISVA compatibility, always returns true. | |
int | size() | Retrieves the number of values in this attribute. | |
String | getID() | Retrieves the ID of this attribute. | |
String | get(idx) | Retrieves the attribute value from the list of attribute values. Returns first attribute value if idx is not supplied. | int |
NamingEnumeration<String> | getAll() | Retrieves an enumeration of the attribute's values. |
7. SearchResult
This example represents an item in the NamingEnumeration
returned as a result of the LdapAttributeUtil.search()
methods.
Return Type | Method | Description | Arguments |
---|---|---|---|
Attributes | getAttributes() | Retrieves the attributes in this search result. | |
String | getName() | Retrieves the name of this search result (dn). |
8. NamingEnumeration
This class is for enumerating lists that are returned by methods in the LdapAttributeUtil
related classes
Return Type | Method | Description | Arguments |
---|---|---|---|
Boolean | hasMore() | Determines whether any more elements exist in the enumeration. | |
T | next() | Retrieves the next element in the enumeration. Returned object type depends on the method that creates the NamingEnumeration instance. |
Updated 3 months ago