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