|
Allow the User to Change Attributes in the Directory
Create a page that will display text boxes for associated attributes in the Directory. The user should be allowed to enter the necessary attributes and submit them. On submit, the attributes should be written into the Directory.
- Create an HTML page with text fields to modify each of the attributes for a user object in eDirectory.
- I used the same HTML file from the previous exercise. Here is the code from my file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Create a New User Account</title> </head>
<body bgcolor = "ffffde">
<h1>Enter in the following information to modify the attributes</h1>
<form method = "post" action = "http://edu-qc.provo.novell.com:8080/servlet/create">
<table cellpadding = "5" cellspacing = "5"> <tr> <td> <p>User Name:</p> </td> <td> <input type = "text" size = "50" name = "username" /> </td> </tr> <tr> <td> <p>First Name:</p> </td> <td> <input type = "text" size = "50" name = "firstname" /> </td> </tr> <tr> <td> <p>Last Name:</p> </td> <td> <input type = "text" size = "50" name = "lastname" /> </td> </tr> <tr> <td> <p>Email Address:</p> </td> <td> <input type = "text" size = "50" name = "email" /> </td> </tr> <tr> <td> <p>Password:</p> </td> <td> <input type = "password" size = "50" name = "password" /> </td> </tr> </table> <p> <input type = "submit" value = "Authenticate" /> <input type = "reset" value = "Clear Fields" /> </p> </form> </body> </html>
- Create a servlet to receive the attributes from the HTML page and change them in eDirectory.
- Create an LdapConnection object.
- Create an AuthenticateLdap object and log in as admin into the eDirectory tree.
- Assign the LdapConnection to the LdapConnection of the AuthenticateLdap bean.
- Instantiate a ModifyLdapEntry bean.
- Create a string and concatenate the username with the user's context.
- Set the name of the ModifyLdapEntry bean.
- Replace the other attributes that the user specified with the following lines.
mle.replaceAttribute("givenName", request.getParameter("firstname")); mle.replaceAttribute("sn", request.getParameter("lastname")); mle.replaceAttribute("mail", request.getParameter("email")); mle.replaceAttribute("fullName", request.getParameter("firstname") + " " + request.getParameter("lastname"));
- Execute the bean and let the user know that the modifications where made successfully.
- Instantiate a ChangePasswordLdapEntry bean.
- Set the user name with the user name provided from the HTML page.
- Set the password from the HTML page.
- Execute the bean and let the user know if the modifications were made successfully.
Here is the code from the modify servlet.
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import com.novell.ecb.*; import com.novell.ecb.ldap.*; public class modify extends HttpServlet { LdapConnection connection = null; LdapConnection connection2 = null; private static final String CONTENT_TYPE = "text/html"; public void init() throws ServletException { } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>modify</title></head>"); out.println("<body bgcolor=\"#ffc800\">"); //I'll first authenticate as admin AuthenticateLdap bean = new AuthenticateLdap(); bean.setURL("ldap://edu-qc.provo.novell.com"); bean.setDN("cn=admin, o=novell"); bean.setPassword("password"); try { bean.execute(); } catch(Exception e) { out.println("<h1>Connection to the Directory failed</h1>"); e.printStackTrace(out); } connection = bean.getLdapConnection(); connection2 = bean.getLdapConnection(); ModifyLdapEntry mle = new ModifyLdapEntry(connection); String s = "cn=" + request.getParameter("username") + ", o=novell"; mle.setName(s); mle.replaceAttribute("givenName", request.getParameter("firstname")); mle.replaceAttribute("sn", request.getParameter("lastname")); mle.replaceAttribute("mail", request.getParameter("email")); mle.replaceAttribute("fullName", request.getParameter("firstname") + " " + request.getParameter("lastname")); try { mle.execute(); out.println("<h1>User: " + request.getParameter("username") + " modified successfully</h1>"); } catch(Exception e) { e.printStackTrace(out); } SetPasswordLdapEntry sple = new SetPasswordLdapEntry(connection2); sple.setName(s); sple.setPassword(request.getParameter("password")); try { sple.execute(); out.println("<h1>Password for user: " + request.getParameter("username") + " changed successfully</h1>"); } catch(Exception e) { e.printStackTrace(out); } out.println("</body></html>"); } //Clean up resources public void destroy() { } }
|