Example script code in Groovy to read attributes from persons of the LDAP in Jira Server or Datacenter by MrAddon

@GrabResolver(name='apache-snapshot', root='http://repository.apache.org/snapshots/')
@Grab(group='org.apache.directory', module='groovyldap', version='0.1-SNAPSHOT')
import javax.naming.Context;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.LdapName;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import org.apache.directory.groovyldap.*
 
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "Connecting EXAMPLE LDAP..."
def ldap = LDAP.newInstance('ldap://ldap.example.org:port', 'CN=Raul Pelaez,OU=LDAPUsers,OU=Users_Processes,OU=USER,DC=example,DC=org', 'xxxx')
log.debug "Connection completed"
//log.debug "Verifying if Raul Pelaez exists in the 9th dimension...."
//def exists = ldap.exists('CN=Raul Pelaez,OU=MrAddon,OU=External,OU=BCN,OU=USER,DC=example,DC=org')
//if (exists) {
//log.debug "Yes... he exists!"
log.debug "Searching users"
//def results = ldap.search('(objectClass=user)', 'DC=example,DC=org', SearchScope.SUB )
def results = ldap.search('(sAMAccountName=raul.pelaez)', 'DC=example,DC=org', SearchScope.SUB )
log.debug "# Entries found: ${results.size}"
def dn = results[0].getNameInNamespace();
Attributes attrs = results[0].getAttributes("");
NamingEnumeration<? extends Attribute> en = attrs.getAll();
Map<String, Object> map = new LinkedHashMap<>();
map.put("dn", dn);
while (en.hasMore()) {
	Attribute attr = en.next();
    String key = attr.getID();
    map.put(key, attr.get(0).toString());
    log.debug attr.get(0).toString()
}
Posted by:.

2 replies on “How to read attributes from LDAP in Jira using Groovy

  1. Hi Mr Addon,
    Can get personel pictures from LDAP with this way? Can we use this pictures for jira and confluence user avatars?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s