Sunday, October 12, 2008    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Nov 2006 >>
S M T W T F S
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

Search

Categories
 • Acrobat (2) [RSS]
 • Adobe (68) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (25) [RSS]
 • AIR (134) [RSS]
 • Appearances (122) [RSS]
 • Books (69) [RSS]
 • CFEclipse (14) [RSS]
 • ColdFusion (1154) [RSS]
 • Data Services (13) [RSS]
 • Fish Tank (2) [RSS]
 • Flash (106) [RSS]
 • Flex (372) [RSS]
 • Home Automation (3) [RSS]
 • Jobs (96) [RSS]
 • JRun (13) [RSS]
 • Labs (27) [RSS]
 • LiveCycle (22) [RSS]
 • MAX (160) [RSS]
 • Regular Expressions (13) [RSS]
 • RIA (11) [RSS]
 • SQL (38) [RSS]
 • Stuff (505) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (137) [RSS]
 • Wireless (99) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Day : November 21, 2006 / Main
November 21, 2006

LDAP Filter Building UDF

I am working on a project that needs to do LDAP lookups, and lots of them. Some of my searches are for a single name, others are for multiple, and some allow partial matches (begin with passed string) while others don't.

A simple LDAP test for equality filter might look like this: uid=jsmith. Searching for multiple users (an OR search, so that any of them match) requires a syntax like this: (|(uid=jsmith)(uid=jdoe)(uid=bjones)). And then to allow wildcard searching you'd need to add a * after the name (or each name in the OR example).

To make all of this simpler I wrote this little UDF. You pass it the attribute to search on, the text to search for (lists are allowed), and whether or not wildcard searches are allowed. The string that is returned can be passed to <CFLDAP> as is.

<!--- Build LDAP filter, handle lists (treat as or search) --->
<cffunction name="BuildLDAPFilter" returntype="string" output="false">
   <cfargument name="ldapField" type="string" required="yes">
   <cfargument name="searchText" type="string" required="yes">
   <cfargument name="wildcard" type="boolean" required="no" default="no">

   <cfset var result="">
   <cfset var i="">
   <cfset var suffix="">
   
   <!--- If wildcard, set suffix --->
   <cfif ARGUMENTS.wildcard>
      <cfset suffix="*">
   </cfif>

   <!--- Is this single or list? --->
   <cfif ListLen(ARGUMENTS.searchText) IS 1>
      <!--- Single, use as is --->
      <cfset result="#ARGUMENTS.ldapField#=#ARGUMENTS.searchText##suffix#">
   <cfelse>
      <!--- Multiple, start filter string --->
      <cfset result="(|">
      <!--- Loop through fields --->
      <cfloop list="#ARGUMENTS.searchText#" index="i">
         <!--- Append field --->
         <cfset result=result&"(#ARGUMENTS.ldapField#=#i##suffix#)">
      </cfloop>
      <!--- Wrap up filter string --->
      <cfset result=result&")">
   </cfif>

   <cfreturn result>
</cffunction>

TrackBacks
There are no trackbacks for this entry.

No trackback URL. Trackbacks are only allowed via interactive form.

Comments
I can't believe you have time to actually "work" ;-)
# Posted By Michael White | 11/22/06 3:09 PM

  © Copyright 1997-2008 Ben Forta, All Rights Reserved