kumsa29

Identity Suite : VApp : 14.2 : JavaScript LDAP plugin

Blog Post created by kumsa29 Employee on May 22, 2018

Plugin Code

-------------------------------------------------------------

function LDAPSearch(query, attributes, baseDN) {
// Params:
// query LDAP filter - ex: '(cn=IAM_*)'
// attributes Comma-separeted list of attributes to retrieve - ex: 'cn,description,owner'
// baseDN base DN used for the search - ex: 'o=acme, c=br'

// Allows the usage of 'importPackage' with JRE8
try{
load("nashorn:mozilla_compat.js");
}
catch(e){}

// Import LDAP Packages
importPackage(Packages.java.util.Hashtable);
importPackage(Packages.java.util.HashMap);
importPackage(Packages.javax.naming.Context);
importPackage(Packages.javax.naming.NamingEnumeration);
importPackage(Packages.javax.naming.NamingException);
importPackage(Packages.javax.naming.directory.DirContext);
importPackage(Packages.javax.naming.directory.InitialDirContext);
importPackage(Packages.javax.naming.directory.SearchControls);

 

// Connexion and Search Control Variables
var host = "<enter IDM User Store server name>";  // i.e. "127.0.0.1"
var port = "< enter IDM User Store server port>"       //  i.e. "19289"
var bindDN =  "< enter IDM User Store user bind DN>"     //  i.e. "cn=dsaadmin,ou=im,ou=ca,o=com";
var pwd = "< enter IDM User Store user password>";
var cLimit = 100;
var tLimit = 60 * 1000;

// Formats the attribute list into a String Array
var attr = attributes.split(",");

// Connection info
var env = new java.util.Hashtable(11);
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(javax.naming.Context.PROVIDER_URL, "ldap://" + host + ":" + port);
env.put(javax.naming.Context.SECURITY_PRINCIPAL, bindDN);
env.put(javax.naming.Context.SECURITY_CREDENTIALS, pwd);

// Start the context and launches the query
try {
var ctx = new javax.naming.directory.InitialDirContext(env);
var ctls = new javax.naming.directory.SearchControls();
ctls.setSearchScope(javax.naming.directory.SearchControls.SUBTREE_SCOPE);
ctls.setReturningAttributes(attr);
ctls.setCountLimit(cLimit);
ctls.setTimeLimit(tLimit);

var result = ctx.search(baseDN, query, ctls);

ctx.close();

var retvalMap = new java.util.HashMap();

// Iterates through results
while(result.hasMoreElements()){
var sr = result.next();

// gets the DN
var id = sr.getNameInNamespace();
var atbs = sr.getAttributes().getAll();

// HashMap used to store the result attributes as an independent object
var retAtbs = new java.util.HashMap();

// Iterates through all attributes
while (atbs.hasMoreElements()){
var atb = atbs.next();

// For multi-valued attribute content
if(atb.size() > 1) {
var multi = atb.getAll();

// HashMap used to store the result attribute's values as an independent object
var retMulti = new java.util.HashMap();
// Counter used as Key in the key/value pair of the HashMaps
var i = 0;

// Iterates through all values
while(multi.hasMoreElements()){
retMulti.put(i, multi.next());
i++;
}

retAtbs.put(atb.getID(), retMulti);
}
// For single-valued attribute, just get the content
else {
retAtbs.put(atb.getID(), atb.get());
}
}
// Sets the return as a key/Object containing the attributes
retvalMap.put(id, retAtbs);
}


return retvalMap;

}
catch (e) {
return 'Error: ' + e.toString();
}

}

 

--------------------------------------------------------------------------------------------------

Handler Code

--------------------------------------------------------------------------------------------------------

 

var usersLdapFilter = "(imManagerId=*" + searchMgrId + "*)";
var usersAttributes = "uid,cn,sn,mail,telephoneNumber,title,imManagerId";
var usersBaseDN = "ou=people,ou=im,ou=ca,o=com";

 

return api.server(['LDAPSearch', usersLdapFilter, usersAttributes, usersBaseDN]).then(

 

function(success) {

 

// First of all, get your return object
console.log(success);
var result = success.returnValue;
//console.log(result);

// Iterate through every entry in the result
for (var dn in result){


// To use the entry's DN, just refer to the "dn" variable
var entry_dn = dn;
// Creates the object containing all attributes for that particular DN
var attributes = result[dn];
// To get one particular attribute value use the format attributes['attribute name']
//console.log(attributes['uid'] + " ; " + attributes['title'] + "; " + attributes['imManagerId']);
var csvRecord = attributes['uid'] + "," + attributes['cn'] + "," + attributes['sn'] + ","
+ attributes['mail'] + "," + attributes['telephoneNumber'] + ","
+ attributes['title'] + "," + attributes['imManagerId'];
api.getProp('refcsv').values.push(csvRecord);
}


console.log("CSV Data..............");
console.log(api.getProp('refcsv'));
return true;


},

 

function(error) {
// Do some eventual error handling like a message display
api.prompt("MSGXX- Error" );
return false;
}

);

Outcomes