LDAP Query

Last modified by Robert on 2018/06/27 08:52

cogSnippet to make LDAP queries from XWiki
Developed by

Ludovic Dubost

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

This snipped allows to run LDAP queries on any LDAP directory (including the one configured for XWiki):

Tool to find information in LDAP Directory:


* Find a user:  
** cn=Ludovic Dubost
** cn=Dubost
* Find groups of user:
** member=CN=Ludovic DUBOST,O=xwiki.com

{{groovy}}
import org.xwiki.velocity.tools.EscapeTool;
import org.xwiki.contrib.ldap.XWikiLDAPConfig;
import org.xwiki.contrib.ldap.XWikiLDAPConnection;
import org.xwiki.contrib.ldap.XWikiLDAPSearchAttribute;
import com.novell.ldap.LDAPConnection;

def escapetool = new EscapeTool();
def server = "";
def binddn = "";
def bindpassword = "";
def query = "";

if (request.server) {
  server = request.server;
} else {
  server = xwiki.getXWikiPreference("ldap_server", "");
}
if (request.binddn) {
  binddn = request.binddn;
} else {
  binddn = xwiki.getXWikiPreference("ldap_bind_DN", "");
}
if (request.bindpassword) {
  bindpassword = request.bindpassword;
} else {
  bindpassword = xwiki.getXWikiPreference("ldap_bind_pass", "");
}
if (request.basedn) {
  basedn = request.basedn;
} else {
  basedn = xwiki.getXWikiPreference("ldap_base_DN", "");
}
if (request.query) {
  query = request.query;
} else {
  query = "cn=Ludovic Dubost";
}
println "Server: ${server}"

def squery = escapetool.xml(query)
def sserver = escapetool.xml(server)
def sbinddn = escapetool.xml(binddn)
def sbindpassword = escapetool.xml(bindpassword)
def sbasedn = escapetool.xml(basedn)

println """
{{html clean="false"}}
<form action="" method="POST">
Server: <input type="text" name="server" value="$sserver" size="20" /><br />
Bind DN: <input type="text" name="binddn" value="$sbinddn" size="80" /><br />
Bind Password: <input type="password" name="bindpassword" value="$sbindpassword" size="20" /><br />
Base DN: <input type="text" name="basedn" value="$sbasedn" size="80" /><br />
LDAP Query: <input type="text" name="query" value="$squery" size="80"/><br />

<input type="submit" value="Go" />
</form>
{{/html}}
"""


def connection = new LDAPConnection();

try {
 // connect
  connection.connect(server, 389)
  connection.bind(LDAPConnection.LDAP_V3, binddn, bindpassword.getBytes("UTF8"));
  String[] params = new String[2]
  params[0] = "dn"
  params[1] = "samAccountName"

 def results = connection.search(basedn, (int) 2, query, params, false);
 def count= results.getCount()
  println "${count} results"
 while (results.hasMore()) {
  try {
   def entry = results.next()
   def dn = entry.getDN()
    println "* ${dn}"
   def attrs = entry.getAttributeSet()
   for (attr in attrs) {
    try {
      println "** ${attr.getName()} ${attr.getStringValue()}"
     } catch(e2) {
      println "** Exception getting attribute"
     }
    }
    } catch(e3) {
      println "** Exception calling next"
    }
  }
} catch (e) {
   e.printStackTrace();
   println("Exception")
   println(e.getMessage())
   println(org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e));
}

{{/groovy}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Tags: ldap
Created by Ludovic Dubost on 2015/09/22 13:58
     

Get Connected