View Javadoc

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