Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Get members of an LDAP group

Created 1 year ago, Updated 11 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.onresolve.scriptrunner.ldap.LdapUtil
import org.springframework.LdapDataEntry
import org.springframework.ldap.core.support.AbstractContextMapper
import org.springframework.ldap.query.LdapQueryBuilder
import org.springframework.ldap.query.SearchScope

// Full 'dn' for the group you want to get members from
final groupDn = 'cn=testing_group,ou=Product Testing,dc=example,dc=org'

// LDAP HardcodedFilter that looks for inetOrgPerson objects with the provided memberOf Attribute
final ldapQueryFilter = "(&(objectClass=inetOrgPerson)(memberOf=${groupDn}))"

final resourcePoolName = 'testLdapResource'

LdapUtil.withTemplate(resourcePoolName) { ldap ->
    // Create the LDAP query
    def query = LdapQueryBuilder.query()
        .searchScope(SearchScope.SUBTREE)
        .filter(ldapQueryFilter)

    // Run the search, get the 'cn' attribute for each entry found and return a `List` of `cn` values
    ldap.search(query, {
        LdapDataEntry entry ->
            entry.attributes.get('cn').get()
    } as AbstractContextMapper<Object>)
}
Having an issue with this script?
Report it here