Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

You should already have a functioning LDAP configuration in JEMH.

Script Field Processor Example

(info) since 3.3.46

Here is some JS that can can be used to show how a nominated user can be pulled from LDAP:

Code Block
//
// Username lookup example
//
var username = "Administrator";
var ldap = jemhUtils.getLdap();
var ldapUser = ldap.getUserDetails(username);

if (ldapUser)
{
// FYI see http://ppl-docs.s3-website-us-east-1.amazonaws.com/JEMH/3.3.12/com/javahollic/jira/emh/api/LDAPUser.html
print("ldap user found with username="+username+", email="+ldapUser.getEmail());
}
else
{
print("no ldap user found with username="+username);
}

Execution result

...

Get all user attributes:

Code Block
var filter = "(sAMAccountName=Administrator)";
var ldap = jemhUtils.getLdap();
var userAtts = ldap.getAllUserAttributes(filter);
var keySetIter = userAtts.entrySet().iterator();
while (keySetIter.hasNext())
{
  var anAtt = keySetIter.next();
  print("Attr: "+anAtt.getKey()+" == "+anAtt.getValue());
}

// showing how to get single value from result
print("primaryGroupID = "+userAtts.get("primaryGroupID").get(0));

Execution result:

...

Get specific user attributes:

Code Block
var filter = "(sAMAccountName=Administrator)";
var ldap = jemhUtils.getLdap();
var atts = [ "sAMAccountName", "mail", "primaryGroupID" ];
var userAtts = ldap.getLimitedUserAttributes(filter, atts);
var keySetIter = userAtts.entrySet().iterator();
while (keySetIter.hasNext())
{
  var anAtt = keySetIter.next();
  print("Attr: "+anAtt.getKey()+" == "+anAtt.getValue());
}

// showing how to get single value from result
print("primaryGroupID = "+userAtts.get("primaryGroupID").get(0));

Execution result:

...

Template Examples

Example: get default user data

The following script shows how you can access LDAP in notification templates, here for example, we are looking up the user with account name “Administrator“ (could be username in Jira). The default return type is LDAPUser which only has a limited set of attributes.

Code Block
#set ($username = "Administrator")
#set ($ldap = $jemhUtils.getLdap())
#set ($ldapUser = $ldap.getUserDetails($username))
#if ($!ldapUser)
## FYI see http://ppl-docs.s3-website-us-east-1.amazonaws.com/JEMH/3.3.12/com/javahollic/jira/emh/api/LDAPUser.html
ldap user found with username=$username, email=$ldapUser.getEmail()
#else
no ldap user found with username=$username
#end

In the JEMH TemplateSet ‘preview’ (available after setting the Preview Issue Key), the result is seen:

...

Example: get all attribute, extract value of one

For arbitrary LDAP attribute retrieval, you’ll need to use a raw LDAP filter and specify the attributes required. The result is a map of the attribute names to a List of possible values. The example below shows how to get the (typically) first value in the list with .get(0):

Code Block
#set($filter = "(sAMAccountName=Administrator)")
#set($userAtts = $ldap.getUserAttributes($filter, null))
#foreach ($anAttribute in $userAtts.keySet())
$anAttribute == $userAtts.get($anAttribute)
#end

## showing how to get single value from result
primaryGroupID = $userAtts.get("primaryGroupID").get(0)

Example output

...

Example to get nominated set of attributes

...

Code Block
<h3>Get Named Atts Only</h3>
#set($requiredAttributes = ["sAMAccountName","distinguishedName","mail", "memberOf"])
#set($namedAttResults = $jemhUtils.getLdap(1).getUserAttributes("(sAMAccountName=andy)", $requiredAttributes))
<p>Got $namedAttResults.size() attributes:</p>
<p>
<UL>
    <LI>mail = $namedAttResults.get("sAMAccountName").get(0)</LI>
    <LI>distinguishedName = $namedAttResults.get("distinguishedName").get(0)</LI>
    <LI>mail = $namedAttResults.get("mail").get(0)</LI>
    <LI>memberOf = $namedAttResults.get("memberOf").get(0)</LI>
</UL>
</p>

Example output

...

Performance

Currently, retrieved data is not cached, every invocation triggers a call to the remote server. If your notifications are in response to interactive user actions, their user interfaces experience could be degraded as the user ‘submit’ doesn’t complete until the notifications have been rendered and queued for delivery.

...