KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > ldap > UserLDAP


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /**
66  *
67  * Copyright 2000, 2001 Jcorporate Ltd.
68  */

69 package com.jcorporate.expresso.ext.ldap;
70
71 import com.jcorporate.expresso.core.db.DBConnection;
72 import com.jcorporate.expresso.core.db.DBException;
73 import com.jcorporate.expresso.core.misc.ConfigManager;
74 import com.jcorporate.expresso.core.misc.ConfigurationException;
75 import com.jcorporate.expresso.core.misc.EMailSender;
76 import com.jcorporate.expresso.core.misc.StringUtil;
77 import com.jcorporate.expresso.core.misc.Base64;
78 import com.jcorporate.expresso.core.security.UserInfo;
79 import com.jcorporate.expresso.core.security.CryptoManager;
80 import com.jcorporate.expresso.services.dbobj.DefaultUserInfo;
81 import com.jcorporate.expresso.services.dbobj.Setup;
82 import org.apache.log4j.Logger;
83
84 import javax.naming.AuthenticationException JavaDoc;
85 import javax.naming.Context JavaDoc;
86 import javax.naming.NamingEnumeration JavaDoc;
87 import javax.naming.NamingException JavaDoc;
88 import javax.naming.directory.Attribute JavaDoc;
89 import javax.naming.directory.Attributes JavaDoc;
90 import javax.naming.directory.DirContext JavaDoc;
91 import javax.naming.directory.InitialDirContext JavaDoc;
92 import javax.naming.directory.SearchControls JavaDoc;
93 import javax.naming.directory.SearchResult JavaDoc;
94 import java.util.Date JavaDoc;
95 import java.util.Enumeration JavaDoc;
96 import java.util.Hashtable JavaDoc;
97 import java.util.Vector JavaDoc;
98
99
100 /**
101  * UserLDAP is an LDAP-aware implementation of the UserInfo object - this allows user
102  * information to be retrieved from an LDAP server, as opposed to simply retrieved
103  * from the database. In order for this implementation to work, the following
104  * properties must be set in the properties file for the specific db/context to be
105  * user with LDAP:
106  * ldapServer: the URL to the LDAP context via JNDI (e.g. ldap://localhost:389)
107  * ldapUser: The username to use to access the LDAP server (cn=Directory Manager)
108  * ldapPassword: The password to use when accessing the LDAP server (e.g. pass)
109  * ldapRoot: The root for the entries we look up (e.g. o=javacorp.com)
110  * <p/>
111  * Creation date: (9/18/00 1:55:15 PM)
112  *
113  * @author Michael Nash
114  */

115 public class UserLDAP
116         implements UserInfo {
117     private String JavaDoc dbName = "default";
118
119     /* Hashtable containing the environment for the JNDI call */
120     private Hashtable JavaDoc env = null;
121     private DirContext JavaDoc ctx = null;
122     private String JavaDoc thisClass = UserLDAP.class.getName() + ".";
123     private String JavaDoc userName = null;
124     private String JavaDoc email = null;
125     private String JavaDoc lastName = null;
126     private String JavaDoc firstName = null;
127     private String JavaDoc descrip = null;
128     private String JavaDoc ldapRoot = null;
129     private String JavaDoc name = null;
130
131     private static Logger log = Logger.getLogger(UserLDAP.class);
132
133     /**
134      * UserLDAP constructor comment.
135      */

136     public UserLDAP() {
137         super();
138     } /* UserLDAP() */
139
140     /**
141      * @throws DBException
142      */

143     public void add()
144             throws DBException {
145         String JavaDoc myName = thisClass + "add()";
146         throw new DBException(myName + ":Add not yet implemented");
147     } /* add() */
148
149
150     /**
151      * Deletes the User (Not implemented)
152      */

153     public void delete()
154             throws DBException {
155         String JavaDoc myName = thisClass + "delete()";
156         throw new java.lang.UnsupportedOperationException JavaDoc(myName + ":Delete not yet implemented");
157     } /* delete() */
158
159
160     /**
161      * Find the LDAP user
162      *
163      * @return true if found
164      */

165     public boolean find()
166             throws DBException {
167         try {
168             retrieve();
169         } catch (DBException de) {
170             return false;
171         }
172
173         return true;
174     } /* find() */
175
176
177     public String JavaDoc getAccountStatus()
178             throws DBException {
179         return "A";
180     }
181
182     public Vector JavaDoc getAllUsers()
183             throws DBException {
184         return null;
185     }
186
187     public String JavaDoc getCreateDate()
188             throws DBException {
189         return null;
190     }
191
192     /**
193      * Most often used for testing to peak into things.
194      *
195      * @return the Directory Context
196      */

197     public DirContext JavaDoc getContext() {
198         return ctx;
199     }
200
201     public String JavaDoc getEmail()
202             throws DBException {
203         return email;
204     }
205
206     /**
207      * Here we generate an authorization code that would be hard for someone to guess.
208      * The idea is that the person has to check the email sent to them to get this
209      * number, and then click on the specially encoded URL to ensure that he/she
210      * actually is checking the email account used at registration.
211      * <p/>
212      * The little trick of getting the time in milliseconds that the person registered,
213      * multiplying by some constant, and then rounding, is extremely weak. We need a
214      * better method of generating a unique code that will "play nice" in a query
215      * string.
216      * <p/>
217      * Creation date: (8/8/00 3:00:41 PM)
218      * author Adam Rossi, PlatinumSolutions, Inc.
219      *
220      * @return java.lang.String
221      */

222     public String JavaDoc getEmailAuthCode()
223             throws DBException {
224         Date JavaDoc createDate = new Date JavaDoc();
225         long dateLong = createDate.getTime();
226         long emailAuthCode = Math.round(dateLong * 1.71);
227
228         return Long.toString(emailAuthCode);
229     } /* getEmailAuthCode() */
230
231
232     public String JavaDoc getEmailValCode()
233             throws DBException {
234         return getField("EmailValCode");
235     }
236
237     /**
238      * getField method comment.
239      *
240      * @param fieldName The field name to retrieve
241      * @return The field string
242      */

243     public String JavaDoc getField(String JavaDoc fieldName)
244             throws DBException {
245         String JavaDoc myName = thisClass + "getField(String)";
246
247         if (fieldName.equals("UserName")) {
248             return userName;
249         } else if (fieldName.equals("EMail")) {
250             return email;
251         } else if (fieldName.equals("Descrip")) {
252             return descrip;
253         } else if (fieldName.equals("FirstName")) {
254             return firstName;
255         } else if (fieldName.equals("LastName")) {
256             return lastName;
257         } else if (fieldName.equals("AccountStatus")) {
258             return "A";
259         } else if (fieldName.equals("CreateDate")) {
260             return null;
261         }
262
263         throw new DBException(myName + ":No such field as '" + fieldName +
264                 "'");
265     } /* getField(String) */
266
267
268     /**
269      * @return java.util.Enumeration
270      * @throws DBException upon Error
271      */

272     public Enumeration JavaDoc getFieldList()
273             throws DBException {
274         Vector JavaDoc v = new Vector JavaDoc();
275         v.addElement("UserName");
276         v.addElement("EMail");
277         v.addElement("Descrip");
278         v.addElement("FirstName");
279         v.addElement("LastName");
280         v.addElement("AccountStatus");
281         v.addElement("CreateDate");
282
283         return v.elements();
284     } /* getFieldList() */
285
286
287     /**
288      * Get the groups associated with this User
289      *
290      * @return The group list
291      */

292     public java.util.Vector JavaDoc getGroups()
293             throws DBException {
294         SearchControls JavaDoc constraints = new SearchControls JavaDoc();
295         constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
296
297         Vector JavaDoc v = new Vector JavaDoc();
298
299         try {
300             setDBName("default");
301
302             NamingEnumeration JavaDoc results = ctx.search("o=" + ldapRoot,
303                     "(uniquemember=" + name +
304                     ", o=" + ldapRoot + ")",
305                     constraints);
306
307             if (results == null) {
308                 return v;
309             }
310             while (results.hasMore()) {
311                 SearchResult JavaDoc sr = (SearchResult JavaDoc) results.next();
312                 Attributes JavaDoc a = sr.getAttributes();
313                 Attribute JavaDoc attr = a.get("cn");
314
315                 if (attr != null) {
316                     NamingEnumeration JavaDoc vals = attr.getAll();
317
318                     if (vals.hasMoreElements()) {
319                         v.addElement(vals.nextElement());
320                     }
321                 }
322             }
323         } catch (NamingException JavaDoc ne) {
324             throw new DBException(ne);
325         }
326
327         return v;
328     } /* getGroups() */
329
330
331     public String JavaDoc getLoginName()
332             throws DBException {
333         return userName;
334     }
335
336     public String JavaDoc getPassword()
337             throws DBException {
338         return null;
339     }
340
341     public boolean getRegComplete()
342             throws DBException {
343
344         // Always return true for now...
345
return true;
346     }
347
348     public String JavaDoc getRegistrationDomain()
349             throws DBException {
350         return "default";
351     }
352
353     public String JavaDoc getRoot() {
354         return ldapRoot;
355     }
356
357     public int getUid()
358             throws DBException {
359         return 0;
360     }
361
362     public String JavaDoc getUpdateDate()
363             throws DBException {
364         return null;
365     }
366
367     public String JavaDoc getUserName()
368             throws DBException {
369         return firstName + " " + lastName;
370     }
371
372     /**
373      * Send this user a notification via e-mail.
374      *
375      * @param subject Subject of the e-mail
376      * @param message Message to send in body of e-mail
377      * @throws DBException If the mail message cannot be sent
378      */

379     public void notify(String JavaDoc subject, String JavaDoc message)
380             throws DBException {
381         String JavaDoc myName = (thisClass + "notify(String, String)");
382         log.info("Notifying user " + getField("UserName") + " of " + subject);
383
384         String JavaDoc sendToUser = getField("EMail");
385
386         try {
387             EMailSender ems = new EMailSender();
388             ems.setDBName("default");
389             ems.send(sendToUser, subject, message);
390         } catch (Exception JavaDoc e) {
391             throw new DBException(myName +
392                     ":Uncaught exception sending e-mail", e);
393         }
394     } /* notify(String, String) */
395
396
397     /**
398      * Check if the given number is in the range of letters and
399      * digits that we want to use for generating a password
400      * Previously in com.jcorporate.expresso.ext.servlet.RegisterUser.java
401      *
402      * @param x the double random value
403      * @return true if the number is a printable character
404      */

405     private boolean okNumber(double x) {
406         int oneNumber = new Double JavaDoc(x).intValue();
407
408         if ((oneNumber >= 65) && (oneNumber <= 90)) {
409             return true;
410         }
411         if ((oneNumber >= 48) && (oneNumber <= 57)) {
412             return true;
413         }
414         if ((oneNumber >= 97) && (oneNumber <= 122)) {
415             return true;
416         }
417
418         return false;
419     } /* okNumber(double) */
420
421     /**
422      * Checks if the supplied password equals the one on file
423      *
424      * @param tryPassword The password to check
425      * @return true if the tryPassword equals the one on file
426      */

427     public boolean passwordEquals(String JavaDoc tryPassword)
428             throws DBException {
429         String JavaDoc myName = thisClass + "getField(String)";
430         Hashtable JavaDoc env2 = new Hashtable JavaDoc();
431         env2.put(Context.INITIAL_CONTEXT_FACTORY,
432                 "com.sun.jndi.ldap.LdapCtxFactory");
433
434         String JavaDoc ldapServer = "";
435
436         try {
437             ldapServer = StringUtil.notNull(ConfigManager.getContext(DBConnection.DEFAULT_DB_CONTEXT_NAME)
438                     .getLdap().getServer());
439         } catch (Exception JavaDoc e) {
440             throw new DBException(myName +
441                     ":Must specify ldapServer property in properties file");
442         }
443
444         if (ldapServer.equals("")) {
445             throw new DBException(myName +
446                     ":Must specify ldapServer property in properties file");
447         }
448
449         env2.put(Context.PROVIDER_URL, ldapServer);
450         env2.put(Context.SECURITY_AUTHENTICATION, "simple");
451
452         if (userName.equals("")) {
453             throw new DBException(myName +
454                     ":Must set UserName field before checking password");
455         }
456
457         env2.put(Context.SECURITY_PRINCIPAL, name + ", o=" + ldapRoot);
458
459         if (StringUtil.notNull(tryPassword).equals("")) {
460             throw new DBException(myName + ":Must specify non-blank password");
461         }
462
463         env2.put(Context.SECURITY_CREDENTIALS, tryPassword);
464
465         try {
466             new InitialDirContext JavaDoc(env2);
467
468             return true;
469         } catch (AuthenticationException JavaDoc e) {
470             log.error(e);
471
472             return false;
473         } catch (NamingException JavaDoc ne) {
474             throw new DBException(ne);
475         }
476     } /* passwordEquals(String) */
477
478
479     /**
480      * Generate a random password
481      *
482      * @return a random password of printable characters
483      */

484     public String JavaDoc randomPassword() {
485         int passwordLength = 6;
486         StringBuffer JavaDoc newPassword = new StringBuffer JavaDoc("");
487         double oneNumber = 0;
488         char oneChar;
489         int iterations = 0;
490
491         //
492
//Read the property value of minimum password length
493
//
494
String JavaDoc propValue = "";
495         try {
496             propValue = StringUtil.notNull(ConfigManager.getContext(DBConnection.DEFAULT_DB_CONTEXT_NAME)
497                     .getMinPasswordSize());
498         } catch (Exception JavaDoc e) {
499             passwordLength = 6;
500         }
501
502
503         if (!propValue.equals("")) {
504             try {
505                 passwordLength = Integer.parseInt(propValue, 10);
506             } catch (NumberFormatException JavaDoc ex) {
507
508                 //Bad number
509
passwordLength = 6;
510             }
511         }
512
513         /////////////////////////////////
514
//
515
//Now Generate the new password. (Code from servlet.RegisterUser) before)
516
//
517
while ((newPassword.length() < passwordLength) && (iterations < 200)) {
518             iterations++;
519             oneNumber = Math.random() * 100;
520
521             if (okNumber(oneNumber)) {
522                 oneChar = (char) new Double JavaDoc(oneNumber).intValue();
523                 newPassword.append(oneChar);
524             }
525         }
526
527         return newPassword.toString();
528     } /* randomPassword() */
529
530     /**
531      * retrieve method comment.
532      */

533     public void retrieve()
534             throws DBException {
535         String JavaDoc myName = thisClass + "retrieve()";
536         SearchControls JavaDoc constraints = new SearchControls JavaDoc();
537         constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
538
539         if (StringUtil.notNull(getField("UserName")).equals("")) {
540             throw new DBException(myName +
541                     ":UserName must be specified before retrieve");
542         }
543         try {
544             setDBName("default");
545
546             NamingEnumeration JavaDoc results = ctx.search("o=" + ldapRoot,
547                     "(uid=" + userName + ")",
548                     constraints);
549
550             if (results == null) {
551                 throw new DBException(myName +
552                         ":No results for search of LDAP server");
553             }
554             if (results.hasMore()) {
555                 SearchResult JavaDoc sr = (SearchResult JavaDoc) results.next();
556                 name = sr.getName();
557
558                 Attributes JavaDoc a = sr.getAttributes();
559                 Attribute JavaDoc attr = a.get("mail");
560
561                 if (attr != null) {
562                     NamingEnumeration JavaDoc vals = attr.getAll();
563
564                     if (vals.hasMoreElements()) {
565                         email = (String JavaDoc) vals.nextElement();
566                     }
567                 }
568
569                 attr = a.get("sn");
570
571                 if (attr != null) {
572                     NamingEnumeration JavaDoc vals = attr.getAll();
573
574                     if (vals.hasMoreElements()) {
575                         lastName = (String JavaDoc) vals.nextElement();
576                     }
577                 }
578
579                 attr = a.get("fn");
580
581                 if (attr != null) {
582                     NamingEnumeration JavaDoc vals = attr.getAll();
583
584                     if (vals.hasMoreElements()) {
585                         firstName = (String JavaDoc) vals.nextElement();
586                     }
587                 }
588
589                 attr = a.get("cn");
590
591                 if (attr != null) {
592                     NamingEnumeration JavaDoc vals = attr.getAll();
593
594                     if (vals.hasMoreElements()) {
595                         descrip = (String JavaDoc) vals.nextElement();
596                     }
597                 }
598             } else {
599                 throw new DBException(myName + ":No such user '" + userName +
600                         "' found in LDAP server directory");
601             }
602         } catch (NamingException JavaDoc ne) {
603             throw new DBException(myName, ne);
604         }
605     } /* retrieve() */
606
607
608     /**
609      * Sends an Authorization Email to a new User.
610      * The user must click on the link encoded in this email before
611      * his account will be activated.
612      */

613     public void sendAuthEmail()
614             throws DBException {
615         try {
616             String JavaDoc dbContext = "default";
617             String JavaDoc authURL = Setup.getValue(dbContext, "EmailValidateURL");
618             String JavaDoc emailAuthCode = getEmailAuthCode();
619             this.setEmailValCode(emailAuthCode);
620             this.update();
621             authURL = authURL + "?UserName=" + getField("UserName") +
622                     "&EmailAuthCode=" + emailAuthCode;
623
624             String JavaDoc subject = "New Account Validation - Please Respond";
625             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
626
627             if (!"".equals(getField("Descrip"))) {
628                 sb.append("Dear " + getField("Descrip") + ",");
629             }
630
631             sb.append("\n");
632             sb.append("\n");
633             sb.append("Thank you for registering");
634
635             String JavaDoc companyName = Setup.getValue(dbContext, "CompanyName");
636             String JavaDoc homePageURL = Setup.getValue(dbContext, "HomePageURL");
637
638             if (companyName != null && !"".equals(companyName)) {
639                 sb.append(" with " + companyName);
640             }
641             if (homePageURL != null && !"".equals(homePageURL)) {
642                 sb.append(" at " + homePageURL);
643             }
644
645             sb.append("!");
646             sb.append("\n");
647             sb.append("\n");
648             sb.append("Your account has been successfully created. The final " +
649                     "step in the");
650             sb.append("\n");
651             sb.append("registration process is to simply follow the link " +
652                     "below to let us");
653             sb.append("\n");
654             sb.append("know that you received this message. You must follow " +
655                     "the link below");
656             sb.append("\n");
657             sb.append("before your account will be activated.");
658             sb.append("\n");
659             sb.append("\n");
660             sb.append("NOTE: If you did not register, you may safely");
661             sb.append("\n");
662             sb.append("ignore this message.");
663             sb.append("\n");
664             sb.append("\n");
665             sb.append("In many email clients, you may simply click on the " +
666                     "link below to");
667             sb.append("\n");
668             sb.append("complete the registration process. If your email " +
669                     "client does not");
670             sb.append("\n");
671             sb.append("support this, cut-and-paste the link below into your " +
672                     "web browser's");
673             sb.append("\n");
674             sb.append("\"Location\" window:");
675             sb.append("\n");
676             sb.append("\n");
677             sb.append(authURL);
678             sb.append("\n");
679             sb.append("\n");
680
681             if (companyName != null && !"".equals(companyName)) {
682                 sb.append("Thank you from all of us at " + companyName + ".");
683             }
684
685             sb.append("\n");
686
687             if (companyName != null && !"".equals(homePageURL)) {
688                 sb.append(homePageURL);
689             }
690
691             sb.append("\n");
692
693             String JavaDoc message = sb.toString();
694             notify(subject, message);
695         } catch (Exception JavaDoc e) {
696             throw new DBException("Error in sending account verification message to " +
697                     getField("UserName") + " at " + getField("EMail") + ": " +
698                     e.toString());
699         }
700     } /* sendAuthEmail() */
701
702
703     /**
704      * Once a user has validated his email address through the email validation
705      * servlet, the user will receive this message giving previously requested
706      * username/password.
707      * <p/>
708      * Creation date: (8/8/00 11:44:26 PM)
709      * author: Adam Rossi, PlatinumSolutions, Inc.
710      *
711      * @throws com.jcorporate.expresso.core.db.DBException
712      * The exception description.
713      */

714     public void sendFollowUpEmail()
715             throws DBException {
716         try {
717             String JavaDoc subject = "New Registration Complete - Welcome!";
718             String JavaDoc dbContext = "default";
719
720             // We can't retrieve the passsword in plain text to transmit to the user
721
// So we just create and set a new passord and send that over.
722
String JavaDoc password = this.randomPassword();
723             this.setPassword(password);
724             this.update();
725
726             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
727
728             if (!"".equals(getField("Descrip"))) {
729                 sb.append("Dear " + getField("Descrip") + ",");
730             }
731
732             sb.append("\n");
733             sb.append("\n");
734             sb.append("Thank you for registering");
735
736             String JavaDoc companyName = Setup.getValue(dbContext, "CompanyName");
737             String JavaDoc homePageURL = Setup.getValue(dbContext, "HomePageURL");
738
739             if (companyName != null && !"".equals(companyName)) {
740                 sb.append(" with " + companyName);
741             }
742             if (homePageURL != null && !"".equals(homePageURL)) {
743                 sb.append(" at " + homePageURL);
744             }
745
746             sb.append("!");
747             sb.append("\n");
748             sb.append("\n");
749             sb.append("Your account is now active. Below is the information " +
750                     "you will need to log in.");
751             sb.append("\n");
752             sb.append("Please keep this information in a safe place.We hope " +
753                     "you enjoy the site and");
754             sb.append("\n");
755             sb.append("look forward to your participation.");
756             sb.append("\n");
757             sb.append("\n");
758             sb.append("User Name: " + getField("UserName"));
759             sb.append("\n");
760             sb.append("Password: " + password);
761             sb.append("\n");
762             sb.append("\n");
763
764             if (companyName != null && !"".equals(companyName)) {
765                 sb.append("Thank you from all of us at " + companyName + ".");
766             }
767
768             sb.append("\n");
769
770             if (companyName != null && !"".equals(homePageURL)) {
771                 sb.append(homePageURL);
772             }
773
774             sb.append("\n");
775
776             String JavaDoc message = sb.toString();
777             notify(subject, message);
778         } catch (Exception JavaDoc e) {
779             throw new DBException("Error in sending account verification follow up message to " +
780                     getField("UserName") + " at " + getField("EMail") + ": " +
781                     e.toString());
782         }
783     } /* sendFollowUpEmail() */
784
785
786     public void setAccountStatus(String JavaDoc name)
787             throws DBException {
788     }
789
790     public void setCreateDate(String JavaDoc name)
791             throws DBException {
792     }
793
794     /**
795      * Sets the data context for this user
796      *
797      * @param newDBName The new data context
798      */

799     public void setDBName(String JavaDoc newDBName)
800             throws DBException {
801         String JavaDoc myName = thisClass + "setDBName(String)";
802         dbName = newDBName;
803         env = new Hashtable JavaDoc();
804         env.put(Context.INITIAL_CONTEXT_FACTORY,
805                 "com.sun.jndi.ldap.LdapCtxFactory");
806
807         try {
808             String JavaDoc ldapServer = StringUtil.notNull(ConfigManager.getContext(newDBName).getLdap().getServer());
809
810             if (ldapServer.equals("")) {
811                 throw new DBException(myName +
812                         ":Must specify ldapServer property in properties file");
813             }
814
815             env.put(Context.PROVIDER_URL, ldapServer);
816             env.put(Context.SECURITY_AUTHENTICATION, "simple");
817
818             String JavaDoc ldapUser = StringUtil.notNull(ConfigManager.getContext(newDBName).getLdap().getUser());
819
820             if (ldapUser.equals("")) {
821                 throw new DBException(myName +
822                         ":Must specify ldapUser property in properties file");
823             }
824
825             env.put(Context.SECURITY_PRINCIPAL, "cn=" + ldapUser);
826
827             String JavaDoc ldapPassword = StringUtil.notNull(ConfigManager.getContext(newDBName).getLdap().getPassword());
828
829             if (ldapPassword.equals("")) {
830                 throw new DBException(myName +
831                         ":Must specify ldapPassword property in properties file");
832             }
833
834             env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
835             ldapRoot = StringUtil.notNull(ConfigManager.getContext(newDBName).getLdap().getRoot());
836
837             if (ldapRoot.equals("")) {
838                 throw new DBException(myName +
839                         ":Must specify ldapRoot property in properties file");
840             }
841             try {
842                 ctx = new InitialDirContext JavaDoc(env);
843             } catch (NamingException JavaDoc ne) {
844                 throw new DBException(ne);
845             }
846         } catch (ConfigurationException ce) {
847             throw new DBException(ce);
848         }
849     } /* setDBName(String) */
850
851
852     public void setEmail(String JavaDoc name)
853             throws DBException {
854     }
855
856     public void setEmailAuthCode(String JavaDoc name)
857             throws DBException {
858     }
859
860     public void setEmailValCode(String JavaDoc code)
861             throws DBException {
862     }
863
864     public void setLoginName(String JavaDoc name)
865             throws DBException {
866         userName = name;
867     }
868
869     public void setPassword(String JavaDoc password)
870             throws DBException {
871     }
872
873     public void setRegComplete(boolean status)
874             throws DBException {
875
876         // Ignore for now...
877
}
878
879     public void setRegistrationDomain(String JavaDoc domain) {
880     }
881
882     public void setUid(int uid)
883             throws DBException {
884     } /* setUid(String) */
885
886
887     public void setUpdateDate(String JavaDoc name)
888             throws DBException {
889     }
890
891     public void setUserName(String JavaDoc name)
892             throws DBException {
893     }
894
895     /**
896      * update method comment.
897      */

898     public void update()
899             throws DBException {
900         String JavaDoc myName = thisClass + "update()";
901         throw new DBException(myName + ":Update not yet implemented");
902     } /* update() */
903
904
905     /**
906      * Method to return a Vector of ValidValue
907      * This method may be implemented by objects that want to provide a
908      * list of valid values for other DB objects. It is strongly recommended
909      * that the valid value list be cached (via the CacheManager) for performance.
910      * The naming convention used in Expresso is to store the ValidValue list with
911      * a cache name the same as the db objects class name with ".validValues" appended
912      *
913      * @return The vector of values
914      * @todo Required to be implemented
915      */

916     public Vector JavaDoc getValues()
917             throws DBException {
918         throw new DBException("This object does not have valid values defined.");
919     } /* getValues() */
920
921     /**
922      * Return the name of the context/database connection that this DB object is using.
923      * If none is set, then we are using the "default" database/context.
924      *
925      * @return a String containing the name of the DBName to use.
926      */

927     public String JavaDoc getDBName() {
928         return this.dbName;
929     } /* getDBName() */
930
931     /**
932      * gets the DB context; can return null
933      */

934     public String JavaDoc getDataContext() {
935         return this.dbName;
936     }
937
938     /**
939      * the primary group of this user is appropriate for unix-like purposes,
940      * such as setting the group for a file permission
941      *
942      * @return name of the primary group of this user; null if no group is found
943      */

944     public String JavaDoc getPrimaryGroup()
945             throws DBException {
946         return DefaultUserInfo.getPrimaryGroup(this);
947     }
948
949     /**
950      * this returns an appropriately hashed password.
951      * @param password to be hashed
952      * @return appropriately hashed password.
953      */

954     public String JavaDoc hashEncodePassword(String JavaDoc password) throws DBException {
955         if (password == null) {
956             throw new DBException("Password Must not be NULL");
957         }
958         if (password.length() == 0) {
959             return password;
960         }
961         try {
962             return Base64.encode(CryptoManager.getInstance().getStringHash().produceHash(password.getBytes()));
963         } catch (Exception JavaDoc ex) {
964             throw new DBException("Error hashing Password:" +
965                     " You may not have installed the" +
966                     " Cryptography Extensions Properly:", ex);
967         }
968     }
969
970 } /* UserLDAP */
Popular Tags