Building an LDAP CFC


Because we like to keep all data access logic in ColdFusion within CFCs, it makes sense to encapsulate all of our LDAP interactions into an LDAP CFC. Each of the previous <cfldap> calls from this chapter were used to create this CFC.

Listing 22.9. LDAP.cfcCreating an LDAP CFC

[View full width]

 <cfcomponent>     <cfscript>         init();     </cfscript>     <cffunction name="init">         <cfset serverAddr = "ldap.tapper.net">         <cfset username="jtapper">         <cfset password="lalala">         <cfset organization="Tapper.net Consulting">         <cfset ou="Tech">     </cffunction>     <cffunction name="getUsers" returnType="query" access="public">         <cfargument name="filter" type="string" default="*">         <cfargument name="start" type="string" required="true">         <cfargument name="filterType" type="string"             hint="AD for ActiveDirectory, LDAP for ADSI or LDAP">         <Cfset var getEmail="">         <cfif filterType eq "LDAP">             <cfset getEmail = getUsersByUID(filter,start)>         <cfelseif filterType eq "AD">             <cfset getEmail = getUsersByCN(filter,start)>         </cfif>         <cfreturn getEmail>     </cffunction>     <cffunction name="getUsersByUID" returnType="query" access="private">         <cfargument name="filter" type="string" default="*">         <cfargument name="start" type="string" required="true">         <Cfset var getEmail="">         <CFLDAP              ACTION="QUERY"              NAME="GetEmail"              SERVER="#serverAddr#"              USERNAME="cn=#username#"              PASSWORD="#password#"              SCOPE="SUBTREE"              ATTRIBUTES="dn,cn,mail,objectclass"              START="#attributes.start#"              FILTER="(uid=#arguments.filter#)">         <cfreturn getEmail>     </cffunction>     <cffunction name="getUsersByCN" returnType="query" access="private">         <cfargument name="filter" type="string" default="*">         <Cfset var getEmail="">         <CFLDAP              ACTION="QUERY"              NAME="GetEmail"              SERVER="#serverAddr#"              USERNAME="cn=#username#"              PASSWORD="#password#"              SCOPE="SUBTREE"              ATTRIBUTES="dn,cn,mail,objectclass"              START="#attributes.start#"              FILTER="(cn=#arguments.filter#)">         <cfreturn getEmail>     </cffunction>     <cffunction name="getNameValuePairs" returnType="query">         <cfargument name="start" type="string" required="true">         <cfargument name="filterType" type="string"             hint="AD for ActiveDirectory, LDAP for ADSI or LDAP"             default="LDAP">         <cfset var getNamesAndValues="">         <cfif filterType eq "LDAP">           <cfset getNamesAndValues=getNameValuePairsByUID(arguments.start)>         <cfelseif filterType eq "AD">           <cfset getNamesAndValues=getNameValuePairsByCN(arguments.start)>         </cfif>         <cfreturn getNamesAndValues>     </cffunction>     <cffunction         name="getNameValuePairsByUID" returnType="query" access="private">         <cfset var GetNamesAndValues="">         <CFLDAP ACTION="QUERY"              NAME="GetNamesAndValues"              SERVER="#serverAddr#"              USERNAME="cn=#username#"              PASSWORD="#password#"              SCOPE="SUBTREE"              ATTRIBUTES="*"              START="#attributes.start#"              FILTER="(uid=*)">         <cfreturn getNamesAndValues>     </cffunction>     <cffunction         name="getNameValuePairsByCN" returnType="query" access="private">         <cfset var GetNamesAndValues="">         <CFLDAP ACTION="QUERY"              NAME="GetNamesAndValues"              SERVER="#serverAddr#"              USERNAME="cn=#username#"              PASSWORD="#password#"              SCOPE="SUBTREE"              ATTRIBUTES="*"              START="#attributes.start#"              FILTER="(cn=*)">         <cfreturn getNamesAndValues>     </cffunction>     <cffunction name="addUser" returnType="Boolean">         <cfargument name="cn" type="string" required="true">         <cfargument name="sn" type="string" required="true">         <cfargument name="email" type="string" required="true">         <cfargument name="uid" type="string" required="true">         <cfargument name="objectClass" type="string" required="true">         <cfargument name="dn" type="string"             default="cn=#arguments.uid#, cn=Recipients, ou=#ou#, o=#organization#">         <cftry>             <CFLDAP ACTION="ADD"                  SERVER="#serverAddr#"                  USERNAME="cn=#username#"                  PASSWORD="#password#"                  ATTRIBUTES="objectclass=#arguments.objectClass#;                      cn=#arguments.cn#;                      sn=#arguments.sn#;                      mail=#arguments.email#;                      ou=#ou#"                  DN="#arguments.dn#">             <cfcatch>                 <cfreturn false>             </cfcatch>         </cftry>         <cfreturn true>     </cffunction>     <cffunction name="modifyUserSingleField" returnType="Boolean">         <cfargument name="dn" type="string" required="true">         <cfargument name="modifyField" type="string" required="true">         <cfargument name="modifyValue" type="string" required="true">         <cfargument name="modifyType" type="string" default="REPLACE">         <cftry>             <CFLDAP ACTION="MODIFY"                MODIFYTYPE="#arguments.modifyType#"                SERVER="#serverAddr#"                USERNAME="cn=#username#"                PASSWORD="#password#"                ATTRIBUTES="#arguments.modifyField#=#arguments.modifyValue#"                DN="#arguments.DN#">              <cfcatch>                 <cfreturn false>             </cfcatch>         </cftry>         <cfreturn true>     </cffunction>     <cffunction name="modifyUserMultiField" returnType="Boolean">         <cfargument name="dn" type="string" required="true">         <cfargument name="modifyFieldArray" type="Array" required="true">         <cfargument name="modifyValueArray" type="Array" required="true">         <cfargument name="modifyType" type="string" default="REPLACE">         <cfset var attributeString = "">         <cftry>             <cfloop from="1" to="#arrayLen(modifyFieldArray)#" index="i">                 <cfset attributeString = attributeString &  modifyFieldArray[i]&"="&modifyValueArray[i]&"; ">             </cfloop>             <CFLDAP ACTION="MODIFY"                  SERVER="#serverAddr#"                  MODIFYTYPE="#arguments.modifyType#"                  USERNAME="cn=#username#"                  PASSWORD="#password#"                  ATTRIBUTES="#attributeString#"                  DN="#arguments.DN#">             <cfcatch>                 <cfreturn false>             </cfcatch>         </cftry>         <cfreturn true>     </cffunction>     <cffunction name="deleteUser" returnType="Boolean">         <cfargument name="dn" type="string" required="true">         <cftry>             <CFLDAP ACTION="DELETE"                 SERVER="#serverAddr#"                 USERNAME="cn=#username#"                 PASSWORD="#password#"                 DN="#attributes.dn#">             <cfcatch>                 <cfreturn false>             </cfcatch>         </cftry>         <cfreturn true>     </cffunction>     <cffunction name="modifyDN" returnType="Boolean">         <cfargument name="newDN" type="string" required="true">         <cfargument name="oldDN" type="string" required="true">         <cftry>             <CFLDAP ACTION="MODIFYDN"                 SERVER="#serverAddr#"                 USERNAME="cn=#username#"                 PASSWORD="#password#"                 ATTRIBUTES="#attributes.newDN#"                 DN="#attributes.oldDN#">             <cfcatch>                 <cfreturn false>             </cfcatch>         </cftry>         <cfreturn true>     </cffunction> </cfcomponent> 

All of the methods of this CFC directly mirror the code used throughout this chapter. One key difference is that the queries are split, built to either filter based on UID (used by most standard LDAP servers) or by CN, which is more likely to be used by an Active Directory.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net