View Javadoc

1   package org.apache.turbine.om.security;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.sql.Connection;
20  
21  import java.util.Date;
22  import java.util.Hashtable;
23  
24  import javax.servlet.http.HttpSessionBindingEvent;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.apache.turbine.services.security.TurbineSecurity;
30  
31  /***
32   * A generic implementation of User interface.
33   *
34   * This basic implementation contains the functionality that is
35   * expected to be common among all User implementations.
36   * You are welcome to extend this class if you wish to have
37   * custom functionality in your user objects (like accessor methods
38   * for custom attributes). <b>Note</b> that implementing a different scheme
39   * of user data storage normally involves writing an implementation of
40   * {@link org.apache.turbine.services.security.UserManager} interface.
41   *
42   * @author <a href="mailto:josh@stonecottage.com">Josh Lucas</a>
43   * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
44   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
45   * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
46   * @author <a href="mailto:cberry@gluecode.com">Craig D. Berry</a>
47   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
48   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
49   * @version $Id: TurbineUser.java,v 1.16.2.2 2004/05/20 03:05:17 seade Exp $
50   */
51  public class TurbineUser extends SecurityObject implements User
52  {
53      /*** Logging */
54      private static Log log = LogFactory.getLog(TurbineUser.class);
55  
56      /*** The date on which the user account was created. */
57      private Date createDate = null;
58  
59      /*** The date on which the user last accessed the application. */
60      private Date lastAccessDate = null;
61  
62      /*** This is data that will survive a servlet engine restart. */
63      private Hashtable permStorage = null;
64  
65      /*** This is data that will not survive a servlet engine restart. */
66      private Hashtable tempStorage = null;
67  
68      /***
69       * Constructor.
70       *
71       * Create a new User and set the createDate.
72       */
73      public TurbineUser()
74      {
75          super();
76          createDate = new Date();
77          setHasLoggedIn(Boolean.FALSE);
78      }
79  
80      /***
81       * Gets the access counter for a user during a session.
82       *
83       * @return The access counter for the user for the session.
84       */
85      public int getAccessCounterForSession()
86      {
87          try
88          {
89              return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER)).intValue();
90          }
91          catch (Exception e)
92          {
93              return 0;
94          }
95      }
96  
97      /***
98       * Gets the access counter for a user from perm storage.
99       *
100      * @return The access counter for the user.
101      */
102     public int getAccessCounter()
103     {
104         try
105         {
106             return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue();
107         }
108         catch (Exception e)
109         {
110             return 0;
111         }
112     }
113 
114     /***
115      * Gets the create date for this User.  This is the time at which
116      * the user object was created.
117      *
118      * @return A Java Date with the date of creation for the user.
119      */
120     public java.util.Date getCreateDate()
121     {
122         return createDate;
123     }
124 
125     /***
126      * Gets the last access date for this User.  This is the last time
127      * that the user object was referenced.
128      *
129      * @return A Java Date with the last access date for the user.
130      */
131     public java.util.Date getLastAccessDate()
132     {
133         if (lastAccessDate == null)
134         {
135             setLastAccessDate();
136         }
137         return lastAccessDate;
138     }
139 
140     /***
141      * Get last login date/time for this user.
142      *
143      * @return A Java Date with the last login date for the user.
144      */
145     public java.util.Date getLastLogin()
146     {
147         return (java.util.Date) getPerm(User.LAST_LOGIN);
148     }
149 
150     /***
151      * Get password for this user.
152      *
153      * @return A String with the password for the user.
154      */
155     public String getPassword()
156     {
157         return (String) getPerm(User.PASSWORD);
158     }
159 
160     /***
161      * Get an object from permanent storage.
162      *
163      * @param name The object's name.
164      * @return An Object with the given name, or null if not found.
165      */
166     public Object getPerm(String name)
167     {
168         return getPerm(name,null);
169     }
170 
171     /***
172      * Get an object from permanent storage; return default if value
173      * is null.
174      *
175      * @param name The object's name.
176      * @param def A default value to return.
177      * @return An Object with the given name.
178      */
179     public Object getPerm(String name, Object def)
180     {
181         Object val;
182         try
183         {
184             val = getPermStorage().get(name);
185             if (val == null)
186             {
187                 val = def;
188             }
189         }
190         catch (Exception e)
191         {
192             val = def;
193         }
194         return val;
195     }
196 
197     /***
198      * This should only be used in the case where we want to save the
199      * data to the database.
200      *
201      * @return A Hashtable.
202      */
203     public Hashtable getPermStorage()
204     {
205         if (permStorage == null)
206         {
207             permStorage = new Hashtable(10);
208         }
209         return permStorage;
210     }
211 
212     /***
213      * Get an object from temporary storage; return null if the
214      * object can't be found.
215      *
216      * @param name The object's name.
217      * @return An Object with the given name.
218      */
219     public Object getTemp(String name)
220     {
221         return getTemp(name, null);
222     }
223 
224     /***
225      * Get an object from temporary storage; return default if value
226      * is null.
227      *
228      * @param name The object's name.
229      * @param def A default value to return.
230      * @return An Object with the given name.
231      */
232     public Object getTemp(String name, Object def)
233     {
234         Object val;
235         try
236         {
237             val = getTempStorage().get(name);
238             if (val == null)
239             {
240                 val = def;
241             }
242 
243         }
244         catch (Exception e)
245         {
246             val = def;
247         }
248         return val;
249     }
250 
251     /***
252      * Returns the username for this user.
253      *
254      * @return A String with the username.
255      * @deprecated use {@link #getName} instead.
256      */
257     public String getUserName()
258     {
259         return getName();
260     }
261 
262     /***
263      * Returns the first name for this user.
264      *
265      * @return A String with the user's first name.
266      */
267     public String getFirstName()
268     {
269         String tmp = null;
270         try
271         {
272             tmp = (String) getPerm(User.FIRST_NAME);
273             if(tmp.length() == 0)
274             {
275                 tmp = null;
276             }
277         }
278         catch (Exception e)
279         {
280         }
281         return tmp;
282     }
283 
284     /***
285      * Returns the last name for this user.
286      *
287      * @return A String with the user's last name.
288      */
289     public String getLastName()
290     {
291         String tmp = null;
292         try
293         {
294             tmp = (String) getPerm(User.LAST_NAME);
295             if (tmp.length() == 0)
296                 tmp = null;
297         }
298         catch (Exception e)
299         {
300         }
301         return tmp;
302     }
303 
304     /***
305      * The user is considered logged in if they have not timed out.
306      *
307      * @return Whether the user has logged in.
308      */
309     public boolean hasLoggedIn()
310     {
311         Boolean loggedIn = getHasLoggedIn();
312         return (loggedIn != null && loggedIn.booleanValue());
313     }
314 
315     /***
316      * Returns the email address for this user.
317      *
318      * @return A String with the user's email address.
319      */
320     public String getEmail()
321     {
322         return (String) getPerm(User.EMAIL);
323     }
324 
325     /***
326      * Increments the permanent hit counter for the user.
327      */
328     public void incrementAccessCounter()
329     {
330         setAccessCounter(getAccessCounter() + 1);
331     }
332 
333     /***
334      * Increments the session hit counter for the user.
335      */
336     public void incrementAccessCounterForSession()
337     {
338         setAccessCounterForSession(getAccessCounterForSession() + 1);
339     }
340 
341     /***
342      * Remove an object from temporary storage and return the object.
343      *
344      * @param name The name of the object to remove.
345      * @return An Object.
346      */
347     public Object removeTemp(String name)
348     {
349         return getTempStorage().remove(name);
350     }
351 
352     /***
353      * Sets the access counter for a user, saved in perm storage.
354      *
355      * @param cnt The new count.
356      */
357     public void setAccessCounter(int cnt)
358     {
359         setPerm(User.ACCESS_COUNTER, new Integer(cnt));
360     }
361 
362     /***
363      * Sets the session access counter for a user, saved in temp
364      * storage.
365      *
366      * @param cnt The new count.
367      */
368     public void setAccessCounterForSession(int cnt)
369     {
370         setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt));
371     }
372 
373     /***
374      * Sets the last access date for this User. This is the last time
375      * that the user object was referenced.
376      */
377     public void setLastAccessDate()
378     {
379         lastAccessDate = new java.util.Date();
380     }
381 
382     /***
383      * Sets the create date for this User. This is the time at which
384      * the user object was created.
385      *
386      * @param date The create date.
387      */
388     public void setCreateDate(java.util.Date date)
389     {
390         createDate = date;
391     }
392 
393     /***
394      * Set last login date/time.
395      *
396      * @param date The last login date.
397      */
398     public void setLastLogin(java.util.Date date)
399     {
400         setPerm(User.LAST_LOGIN, date);
401     }
402 
403     /***
404      * Set password.
405      *
406      * @param password The new password.
407      */
408     public void setPassword(String password)
409     {
410         setPerm(User.PASSWORD, password);
411     }
412 
413     /***
414      * Put an object into permanent storage. If the value is null,
415      * it will convert that to a "" because the underlying storage
416      * mechanism within TurbineUser is currently a Hashtable and
417      * null is not a valid value.
418      *
419      * @param name The object's name.
420      * @param value The object.
421      */
422     public void setPerm(String name, Object value)
423     {
424         getPermStorage().put(name, (value == null) ? "" : value);
425     }
426 
427     /***
428      * This should only be used in the case where we want to save the
429      * data to the database.
430      *
431      * @param permStorage A Hashtable.
432      */
433     public void setPermStorage(Hashtable permStorage)
434     {
435         this.permStorage = permStorage;
436     }
437 
438     /***
439      * This should only be used in the case where we want to save the
440      * data to the database.
441      *
442      * @return A Hashtable.
443      */
444     public Hashtable getTempStorage()
445     {
446         if (tempStorage == null)
447         {
448             tempStorage = new Hashtable(10);
449         }
450         return tempStorage;
451     }
452 
453     /***
454      * This should only be used in the case where we want to save the
455      * data to the database.
456      *
457      * @param storage A Hashtable.
458      */
459     public void setTempStorage(Hashtable tempStorage)
460     {
461         this.tempStorage = tempStorage;
462     }
463 
464     /***
465      * This gets whether or not someone has logged in.  hasLoggedIn()
466      * returns this value as a boolean.  This is private because you
467      * should use hasLoggedIn() instead.
468      *
469      * @return True if someone has logged in.
470      */
471     private Boolean getHasLoggedIn()
472     {
473         return (Boolean) getTemp(User.HAS_LOGGED_IN);
474     }
475 
476     /***
477      * This sets whether or not someone has logged in.  hasLoggedIn()
478      * returns this value.
479      *
480      * @param value Whether someone has logged in or not.
481      */
482     public void setHasLoggedIn(Boolean value)
483     {
484         setTemp(User.HAS_LOGGED_IN, value);
485     }
486 
487     /***
488      * Put an object into temporary storage. If the value is null,
489      * it will convert that to a "" because the underlying storage
490      * mechanism within TurbineUser is currently a Hashtable and
491      * null is not a valid value.
492      *
493      * @param name The object's name.
494      * @param value The object.
495      */
496     public void setTemp(String name, Object value)
497     {
498         getTempStorage().put(name, (value == null) ? "" : value);
499     }
500 
501     /***
502      * Sets the username for this user.
503      *
504      * @param username The user's username.
505      * @deprecated use {@link #setName} instead
506      */
507     public void setUserName(String username)
508     {
509         setName(username);
510     }
511 
512     /***
513      * Sets the first name for this user.
514      *
515      * @param firstName User's first name.
516      */
517     public void setFirstName(String firstName)
518     {
519         setPerm(User.FIRST_NAME, firstName);
520     }
521 
522     /***
523      * Sets the last name for this user.
524      *
525      * @param lastName User's last name.
526      */
527     public void setLastName(String lastName)
528     {
529         setPerm(User.LAST_NAME, lastName);
530     }
531 
532     /***
533      * Sets the email address.
534      *
535      * @param address The email address.
536      */
537     public void setEmail(String address)
538     {
539         setPerm(User.EMAIL, address);
540     }
541 
542     /***
543      * This method reports whether or not the user has been confirmed
544      * in the system by checking the User.CONFIRM_VALUE
545      * column in the users record to see if it is equal to
546      * User.CONFIRM_DATA.
547      *
548      * @return True if the user has been confirmed.
549      */
550     public boolean isConfirmed()
551     {
552         String value = getConfirmed();
553         return (value != null && value.equals(User.CONFIRM_DATA));
554     }
555 
556     /***
557      * Sets the confirmation value. The value should
558      * be either a random string or User.CONFIRM_DATA
559      *
560      * @param value The confirmation key value.
561      */
562     public void setConfirmed(String value)
563     {
564         String val = "";
565         if (value != null)
566         {
567             val = value;
568         }
569         setPerm(User.CONFIRM_VALUE, val);
570     }
571 
572     /***
573      * Gets the confirmation value.
574      *
575      * @return status The confirmation value for this User
576      */
577     public String getConfirmed()
578     {
579         return (String) getPerm(User.CONFIRM_VALUE);
580     }
581 
582     /***
583      * Updates the last login date in the database.
584      *
585      * @exception Exception a generic exception.
586      */
587     public void updateLastLogin()
588             throws Exception
589     {
590         setPerm(User.LAST_LOGIN, new java.util.Date());
591     }
592 
593     /***
594      * Implement this method if you wish to be notified when the User
595      * has been Bound to the session.
596      *
597      * @param hsbe The HttpSessionBindingEvent.
598      */
599     public void valueBound(HttpSessionBindingEvent hsbe)
600     {
601         // Currently we have no need for this method.
602     }
603 
604     /***
605      * Implement this method if you wish to be notified when the User
606      * has been Unbound from the session.
607      *
608      * @param hsbe The HttpSessionBindingEvent.
609      */
610     public void valueUnbound(HttpSessionBindingEvent hsbe)
611     {
612         try
613         {
614             if (hasLoggedIn())
615             {
616                 TurbineSecurity.saveOnSessionUnbind(this);
617             }
618         }
619         catch (Exception e)
620         {
621             log.error("TurbineUser.valueUnbound(): " + e.getMessage(), e);
622         }
623     }
624 
625     /***
626      * Saves this object to the data store.
627      */
628     public void save()
629             throws Exception
630     {
631         if (TurbineSecurity.accountExists(this))
632         {
633             TurbineSecurity.saveUser(this);
634         }
635         else
636         {
637             TurbineSecurity.addUser(this, getPassword());
638         }
639     }
640 
641     /***
642      * not implemented
643      *
644      * @param conn
645      * @throws Exception
646      */
647     public void save(Connection conn) throws Exception
648     {
649         throw new Exception("not implemented");
650     }
651 
652     /***
653      * not implemented
654      *
655      * @param dbname
656      * @throws Exception
657      */
658     public void save(String dbname) throws Exception
659     {
660         throw new Exception("not implemented");
661     }
662 
663     /***
664      * Returns the name of this user.  This will be the user name/
665      * login name.
666      *
667      * @return The name of the user.
668      */
669     public String getName()
670     {
671         return (String) getPerm(User.USERNAME);
672     }
673 
674     /***
675      * Sets the name of this user.  This will be the user name/
676      * login name.
677      *
678      * @param name The name of the object.
679      */
680     public void setName(String name)
681     {
682         setPerm(User.USERNAME, name);
683     }
684 }