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