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()
}
.'s avatar
Posted by:.

4 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

  2. Hi Mr Addon,

    i get this error msg

    Message:

    groovy.lang.MissingPropertyException: No such property: LDAP for class: script_dffc7750134f837403bc984f487160e9

    Stack:

    org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:65)
    org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51)
    org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:309)
    script_dffc7750134f837403bc984f487160e9.run(script_dffc7750134f837403bc984f487160e9.groovy:29)

    Could you pls help me how to fix this issue?

    Thanks

    Like

Leave a reply to Nihat Çetinkaya Cancel reply