LDAP Connection
Adding an LDAP resource allows you to query your LDAP servers in a similar way to database connections.
Use an LDAP resource to:
- Validate that a username provided in a custom field is a member of an LDAP group.
- Write a REST endpoint to list office addresses.
- In a Leavers workflow, use a post function to mark a user as having left the company.
To set up an LDAP connection, and make the connection available to scripts:
Use LDAP Resources in Scripts
Having set up an LDAP connection, you can use it in a script as follows:
This example uses the LDAP connection with the Pool Namecorporate.
import com.onresolve.scriptrunner.ldap.LdapUtil
import org.springframework.ldap.core.AttributesMapper
import javax.naming.directory.SearchControls
def cnList = LdapUtil.withTemplate('corporate') { template ->
template.search("", "(sn=Smi*)", SearchControls.SUBTREE_SCOPE, { attributes ->
attributes.get('cn').get()
} as AttributesMapper<String>)
}
// cnList now contains the list of common names of users whose surnames begin with "Smi"...LdapUtil.withTemplate takes two arguments:
- The name of the connection as defined by you in the Pool Name parameter when adding the connection (in this example corporate),
- A closure. The closure receives a
org.springframework.ldap.core.LdapOperationsobject as an argument.
See spring ldap for more information on querying. Where the documentation refers to an LdapTemplate, this is equivalent to the above-mentioned LdapOperations.