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 {
    userLookupHelper.createUser("jsmith", "cn=jsmith,dc=example,dc=org", "secret", "John", "Smith");
}
Return TypeMethodDescriptionArguments
UserLookupHelperUserLookupHelper(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
Booleaninit()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.
BooleanisReady()Tests the ldap connection. Returns true if test connection succeeded, otherwise returns false
UsercreateUser(username, dn, password, firstName, lastName)Create user with the provided information.string, string, string, string, string
UsergetUser(username)Retrieve the user who is associated with the provided username.string
UsergetUserByNativeId(nativeId)Retrieve the user who is associated with the provided native ID.string
BooleandeleteUser(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 TypeMethodDescriptionArguments
BooleanhasError()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.
stringgetError()Retrieve the error returned
stringgetErrMessage()The same as getError(). This method is provided for ISAV compatibility.
Booleanauthenticate(password)Attempts to authenticate the user with the specified passwordstring
stringgetId()Get the users ID
stringgetNativeId()Get the users native ID.
string[]getAttributeNames()Get the users attribute names
stringgetAttribute(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
BooleanattributeExists(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 TypeMethodDescriptionArguments
LdapAttributeUtilLdapAttributeUtil(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
LdapAttributeResultinit()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.
BooleanisReady()Tests the ldap connection. Returns true if test connection succeeded, otherwise returns false
LdapAttributeResultaddAttributeValue(dn, attributeName, attributeValue)This interface gives the user ability to add the attribute from the LDAP.string, string, string
LdapAttributeResultcreateSubContext(dn, attributes)This method is to give the user ability to create the sub contextstring, JS object
LdapAttributeResultgetAttributeValue(dn, attributeNames)This interface gives the user ability to get the attribute from LDAPstring, string[]
LdapAttributeResultremoveAttribute(dn, attributeName, attributeValue)This interface gives the user ability to remove the attribute value from LDAPstring, string, string
LdapAttributeResultsetAttributeValue(dn, attributeName, attributeValue)This interface gives the user ability to update the attribute value from the LDAPstring, string, string
LdapAttributeResultsearch(dn, filter)This method gives the user ability to search LDAP for the specified filter in the specific dnstring, string

4. LdapAttributeResult

This result is the Operation result for the native LDAP Attribute Utility.

Return TypeMethodDescriptionArguments
BooleanhasError()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.
BooleanisSuccessful()Returns inverted value of hasError(). For example, if hasError() returns true, isSuccessful() returns false and vice versa. This method is provided for ISAV compatibility.
stringgetError()Retrieve the error returned
stringgetNamingException()The same as getError(). This method is provided for ISVA compatibility.
AttributesgetAttributes()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 TypeMethodDescriptionArguments
BooleanisCaseIgnored()Mockup function for ISVA compatibility, always returns false.
intsize()Retrieves the number of attributes in the attribute set.
Attributeget(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 TypeMethodDescriptionArguments
Booleancontains(attrVal)Determines whether a value is in the attributestring
BooleanisOrdered()Mockup function for ISVA compatibility, always returns true.
intsize()Retrieves the number of values in this attribute.
StringgetID()Retrieves the ID of this attribute.
Stringget(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 TypeMethodDescriptionArguments
AttributesgetAttributes()Retrieves the attributes in this search result.
StringgetName()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 TypeMethodDescriptionArguments
BooleanhasMore()Determines whether any more elements exist in the enumeration.
Tnext()Retrieves the next element in the enumeration. Returned object type depends on the method that creates the NamingEnumeration instance.