When you add an entry to an LDAP directory, you specify the DN, all the required attributes, including the entry's object class, and any optional attributes. The following example builds a form that adds an entry to an LDAP directory.
<!--- set the LDAP server ID, user name, and password as variables here so they can be changed in only one place ---> <cfset myServer="ldap.myco.com"> <cfset myUserName="cn=Directory Manager"> <cfset myPassword="password"> <!--- Initialize the values used in form fields to empty strings ---> <cfparam name="fullNameValue" default=""> <cfparam name="surnameValue" default=""> <cfparam name="emailValue" default=""> <cfparam name="phoneValue" default=""> <cfparam name="uidValue" default=""> <!---When the form is submitted, add the LDAP entry ---> <cfif isdefined("Form.action") AND Trim(Form.uid) IS NOT ""> <cfif Form.action is "add"> <cfif Trim(Form.fullName) is "" OR Trim(Form.surname) is "" OR Trim(Form.email) is "" OR Trim(Form.phone) is ""> <h2>You must enter a value in every field.</h2> <cfset fullNameValue=Form.fullName> <cfset surnameValue=Form.surname> <cfset emailValue=Form.email> <cfset phoneValue=Form.phone> <cfset uidValue=Form.uid> <cfelse> <cfset attributelist="objectclass=top, person, organizationalperson, inetOrgPerson; cn=#Trim(Form.fullName)#; sn=#Trim(Form.surname)#; mail=#Trim(Form.email)#; telephonenumber=#Trim(Form.phone)#; ou=Human Resources; uid=#Trim(Form.uid)#"> <cfldap action="add" attributes="#attributeList#" dn="uid=#Trim(Form.uid)#, ou=People, o=Airius.com" server=#myServer# username=#myUserName# password=#myPassword#> <cfoutput><h3>Entry for User ID #Form.uid# has been added</h3> </cfoutput> </cfif> </cfif> </cfif> <html> <head> <title>Update LDAP Form</title> </head> <body> <h2>Manage LDAP Entries</h2> <cfform action="update_ldap.cfm" method="post"> <table> <tr><td>Full Name:</td> <td><cfinput type="Text" name="fullName" value=#fullNameValue# size="20" maxlength="30" tabindex="1"></td> </tr> <tr><td>Surname:</td> <td><cfinput type="Text" name="surname" Value= "#surnameValue#" size="20" maxlength="20" tabindex="2"></td> </tr> <tr> <td>E-mail Address:</td> <td><cfinput type="Text" name="email" value="#emailValue#" size="20" maxlength="20" tabindex="3"></td> </tr> <tr> <td>Telephone Number:</td> <td><cfinput type="Text" name="phone" value="#phoneValue#" size="20" maxlength="20" tabindex="4"></td> </tr> <tr> <td>User ID:</td> <td><cfinput type="Text" name="uid" value="#uidValue#" size="20" maxlength="20" tabindex="5"></td> </tr> <tr> <td colspan="2"> <input type="Submit" name="action" value="Add" tabindex="8"></td> </tr> </table> <br> *All fields are required for Add<br> </cfform> <!---Output the user list ---> <h2>User List for the Human Resources Department</h2> <cfldap name="GetList" server=#myServer# action="query" attributes="cn,sn,mail,telephonenumber,uid" start="o=Airius.com" scope="subtree" filter="ou=Human Resources" sort="sn,cn" sortControl="asc, nocase"> <table border="1"> <tr> <th>Full Name</th> <th>Surname</th> <th>Mail</th> <th>Phone</th> <th>UID</th> </tr> <cfoutput query="GetList"> <tr> <td>#GetList.cn#</td> <td>#GetList.sn#</td> <td>#GetList.mail#</td> <td>#GetList.telephonenumber#</td> <td>#GetList.uid#</td> </tr> </cfoutput> </table> </body> </html>
The following table describes the code:
Code | Description |
---|---|
<cfset myServer="ldap.myco.com"> <cfset myUserName="cn=Directory |
Initializes the LDAP connection information variables. Uses variables for all connection information so that any changes have to be made in only one place. |
<cfparam name="fullNameValue" |
Sets the default values of empty strings for the form field value variables. The data entry form uses |
<cfif isdefined("Form.action") AND Trim(Form.uid) IS NOT ""> |
Ensures that the user entered a User ID in the form. |
<cfif Form.action is "add"> |
If the user clicks Add, processes the code that follows. |
<cfif Trim(Form.fullName) is "" |
If any field in the submitted form is blank, display a message and set the other form fields to display data that the user submitted. |
<cfelse> <cfset attributelist= |
If the user entered data in all fields, sets the attributelist variable to specify the entry's attributes, including the object class and the organizational unit (in this case, Human Resources). The |
<cfldap action="add" attributes="#attributeList#" dn="uid=#Trim(Form.uid)#, |
Adds the new entry to the directory. |
<cfform action="update_ldap.cfm" method="post"> <table> <tr><td>Full Name:</td> <td><cfinput type="Text" name="fullName" value=#fullNameValue# size="20" maxlength="30" tabindex="1"></td> </tr> . . . <tr><td colspan="2"> <input type="Submit" name="action" value="Add" tabindex="6"></td> </tr> </table> <br> *All fields are required for Add<br> </cfform> |
Outputs the data entry form, formatted as a table. Each |
<cfldap name="GetList" server=#myServer# action="query" attributes="cn,sn,mail, |
Queries the directory and gets the common name, surname, e-mail address, telephone number, and user ID from the matching entries. Searches the subtree from the entry with the DN of o=Airius.com, and selects all entries in which the organizational unit is Human Resources. Sorts the results by surname and then common name (to sort by last name, then first). Sorts in default ascending order that is not case-sensitive. |
<table border="1"> <tr> <th>Full Name</th> <th>Surname</th> <th>Mail</th> <th>Phone</th> <th>UID</th> </tr> <cfoutput query="GetList"> <tr> <td>#GetList.cn#</td> <td>#GetList.sn#</td> <td>#GetList.mail#</td> <td>#GetList.telephonenumber#</td> <td>#GetList.uid#</td> </tr> </cfoutput> </table> </body> </html> |
Display the query results in a table. |