1 package org.apache.turbine.services.security.torque;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.PrintWriter;
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 import org.apache.torque.om.Persistent;
32
33 import org.apache.turbine.om.security.User;
34 import org.apache.turbine.services.security.TurbineSecurity;
35 import org.apache.turbine.util.ObjectUtils;
36 import org.apache.turbine.util.security.TurbineSecurityException;
37
38 /***
39 * This is the User class used by the TorqueSecurity Service. It decouples
40 * all the database peer access from the actual Peer object
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:jmcnally@collab.net">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:mpoeschl@marmot.at">Martin Poeschl</a>
48 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
49 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
50 * @version $Id: TorqueUser.java,v 1.7.2.3 2004/05/20 03:06:50 seade Exp $
51 */
52
53 public class TorqueUser
54 extends TorqueObject
55 implements User,
56 Persistent
57 {
58 private static Log log = LogFactory.getLog(TorqueUser.class);
59
60 /*** The date on which the user last accessed the application. */
61 private Date lastAccessDate = null;
62
63 /*** This is data that will survive a servlet engine restart. */
64 private Hashtable permStorage = null;
65
66 /*** This is data that will not survive a servlet engine restart. */
67 private Hashtable tempStorage = null;
68
69 /***
70 * Constructor.
71 * Create a new User and set the createDate.
72 */
73 public TorqueUser()
74 {
75 super();
76 setCreateDate(new Date());
77 tempStorage = new Hashtable(10);
78 setHasLoggedIn(Boolean.FALSE);
79 }
80
81 /***
82 * This Constructor is used when the UserPeerManager
83 * has retrieved a list of Database Objects from the peer and
84 * must 'wrap' them into TorqueRole Objects. You should not use it directly!
85 *
86 * @param obj An Object from the peer
87 */
88 public TorqueUser(Persistent obj)
89 {
90 super(obj);
91
92
93
94
95 tempStorage = new Hashtable(10);
96 setHasLoggedIn(Boolean.FALSE);
97 }
98
99 /***
100 * Returns the underlying Object for the Peer
101 *
102 * Used in the UserPeerManager when building a new Criteria.
103 *
104 * @return The underlying persistent object
105 *
106 */
107
108 public Persistent getPersistentObj()
109 {
110 if (obj == null)
111 {
112 obj = UserPeerManager.newPersistentInstance();
113 }
114 return obj;
115 }
116
117 /***
118 * Stores the object in the database. If the object is new,
119 * it inserts it; otherwise an update is performed.
120 *
121 * @param torqueName The name under which the object should be stored.
122 *
123 * @exception Exception This method might throw an exceptions
124 */
125 public void save(String torqueName)
126 throws Exception
127 {
128 setObjectdata(ObjectUtils.serializeHashtable(getPermStorage()));
129 super.save(torqueName);
130 }
131
132 /***
133 * Stores the object in the database. If the object is new,
134 * it inserts it; otherwise an update is performed. This method
135 * is meant to be used as part of a transaction, otherwise use
136 * the save() method and the connection details will be handled
137 * internally
138 *
139 * @param con A Connection object to save the object
140 *
141 * @exception Exception This method might throw an exceptions
142 */
143 public void save(Connection con)
144 throws Exception
145 {
146 setObjectdata(ObjectUtils.serializeHashtable(getPermStorage()));
147 super.save(con);
148 }
149
150 /***
151 * Makes changes made to the User attributes permanent.
152 *
153 * @throws TurbineSecurityException if there is a problem while
154 * saving data.
155 */
156 public void save()
157 throws TurbineSecurityException
158 {
159
160
161
162
163
164
165
166
167
168
169 try
170 {
171 setObjectdata(ObjectUtils.serializeHashtable(getPermStorage()));
172 getPersistentObj().save();
173 }
174 catch (Exception e)
175 {
176 throw new TurbineSecurityException("User object said "
177 + e.getMessage(), e);
178 }
179 }
180
181 /***
182 * Returns the name of this object.
183 *
184 * @return The name of the object.
185 */
186 public String getName()
187 {
188 return UserPeerManager.getName(getPersistentObj());
189 }
190
191 /***
192 * Sets the name of this object
193 *
194 * @param name The name of the object
195 */
196 public void setName(String name)
197 {
198 setUserName(name);
199 }
200
201 /***
202 * Gets the Id of this object
203 *
204 * @return The Id of the object
205 */
206 public int getId()
207 {
208 return UserPeerManager.getIdAsObj(getPersistentObj()).intValue();
209 }
210
211 /***
212 * Gets the Id of this object
213 *
214 * @return The Id of the object
215 */
216 public Integer getIdAsObj()
217 {
218 return UserPeerManager.getIdAsObj(getPersistentObj());
219 }
220
221 /***
222 * Sets the Id of this object
223 *
224 * @param id The new Id
225 */
226 public void setId(int id)
227 {
228 UserPeerManager.setId(getPersistentObj(), id);
229 }
230
231 /***
232 * Returns the name of this user.
233 *
234 * @return The name of the user.
235 * @deprecated Use getName() instead.
236 */
237 public String getUserName()
238 {
239 return getName();
240 }
241
242 /***
243 * Sets the name of this user.
244 *
245 * @param name The name of the user.
246 */
247 public void setUserName(String name)
248 {
249 UserPeerManager.setUserName(getPersistentObj(), name);
250 }
251
252 /***
253 * Returns the password of the User
254 *
255 * @return The password of the User
256 */
257 public String getPassword()
258 {
259 return UserPeerManager.getUserPassword(getPersistentObj());
260 }
261
262 /***
263 * Sets the password of the User
264 *
265 * @param password The new password of the User
266 */
267 public void setPassword(String password)
268 {
269 UserPeerManager.setUserPassword(getPersistentObj(), password);
270 }
271
272 /***
273 * Returns the first name of the User
274 *
275 * @return The first name of the User
276 */
277 public String getFirstName()
278 {
279 return UserPeerManager.getUserFirstName(getPersistentObj());
280 }
281
282 /***
283 * Sets the first name of the User
284 *
285 * @param firstName The new first name of the User
286 */
287 public void setFirstName(String firstName)
288 {
289 UserPeerManager.setUserFirstName(getPersistentObj(), firstName);
290 }
291
292 /***
293 * Returns the last name of the User
294 *
295 * @return The last name of the User
296 */
297 public String getLastName()
298 {
299 return UserPeerManager.getUserLastName(getPersistentObj());
300 }
301
302 /***
303 * Sets the last name of User
304 *
305 * @param lastName The new last name of the User
306 */
307 public void setLastName(String lastName)
308 {
309 UserPeerManager.setUserLastName(getPersistentObj(), lastName);
310 }
311
312 /***
313 * Returns the email address of the user
314 *
315 * @return The email address of the user
316 */
317 public String getEmail()
318 {
319 return UserPeerManager.getUserEmail(getPersistentObj());
320 }
321
322 /***
323 * Sets the new email address of the user
324 *
325 * @param email The new email address of the user
326 */
327 public void setEmail(String email)
328 {
329 UserPeerManager.setUserEmail(getPersistentObj(), email);
330 }
331
332 /***
333 * Returns the confirm value of the user
334 *
335 * @return The confirm value of the user
336 */
337 public String getConfirmed()
338 {
339 return UserPeerManager.getUserConfirmed(getPersistentObj());
340 }
341
342 /***
343 * Sets the new confirm value of the user
344 *
345 * @param confirm The new confirm value of the user
346 */
347 public void setConfirmed(String confirm)
348 {
349 UserPeerManager.setUserConfirmed(getPersistentObj(), confirm);
350 }
351
352 /***
353 * Returns the creation date of the user
354 *
355 * @return The creation date of the user
356 */
357 public java.util.Date getCreateDate()
358 {
359 return UserPeerManager.getUserCreateDate(getPersistentObj());
360 }
361
362 /***
363 * Sets the new creation date of the user
364 *
365 * @param createDate The new creation date of the user
366 */
367 public void setCreateDate(java.util.Date createDate)
368 {
369 UserPeerManager.setUserCreateDate(getPersistentObj(), createDate);
370 }
371
372 /***
373 * Returns the date of the last login of the user
374 *
375 * @return The date of the last login of the user
376 */
377 public java.util.Date getLastLogin()
378 {
379 return UserPeerManager.getUserLastLogin(getPersistentObj());
380 }
381
382 /***
383 * Sets the new date of the last login of the user
384 *
385 * @param lastLogin The new the date of the last login of the user
386 */
387 public void setLastLogin(java.util.Date lastLogin)
388 {
389 UserPeerManager.setUserLastLogin(getPersistentObj(), lastLogin);
390 }
391
392 /***
393 * Returns the value of the objectdata for this user.
394 * Objectdata is a VARBINARY column in the table used
395 * to store the permanent storage table from the User
396 * object.
397 *
398 * @return The bytes in the objectdata for this user
399 */
400 public byte [] getObjectdata()
401 {
402 return UserPeerManager.getUserObjectdata(getPersistentObj());
403 }
404
405 /***
406 * Sets the value of the objectdata for the user
407 *
408 * @param objectdata The new the date of the last login of the user
409 */
410 public void setObjectdata(byte [] objectdata)
411 {
412 UserPeerManager.setUserObjectdata(getPersistentObj(), objectdata);
413 }
414
415
416 /***
417 * Gets the access counter for a user from perm storage.
418 *
419 * @return The access counter for the user.
420 */
421 public int getAccessCounter()
422 {
423 try
424 {
425 return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue();
426 }
427 catch (Exception e)
428 {
429 return 0;
430 }
431 }
432
433 /***
434 * Gets the access counter for a user during a session.
435 *
436 * @return The access counter for the user for the session.
437 */
438 public int getAccessCounterForSession()
439 {
440 try
441 {
442 return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER)).intValue();
443 }
444 catch (Exception e)
445 {
446 return 0;
447 }
448 }
449
450 /***
451 * Increments the permanent hit counter for the user.
452 */
453 public void incrementAccessCounter()
454 {
455
456 setAccessCounter(getAccessCounter() + 1);
457 }
458
459 /***
460 * Increments the session hit counter for the user.
461 */
462 public void incrementAccessCounterForSession()
463 {
464 setAccessCounterForSession(getAccessCounterForSession() + 1);
465 }
466
467 /***
468 * Sets the access counter for a user, saved in perm storage.
469 *
470 * @param cnt The new count.
471 */
472 public void setAccessCounter(int cnt)
473 {
474 setPerm(User.ACCESS_COUNTER, new Integer(cnt));
475 }
476
477 /***
478 * Sets the session access counter for a user, saved in temp
479 * storage.
480 *
481 * @param cnt The new count.
482 */
483 public void setAccessCounterForSession(int cnt)
484 {
485 setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt));
486 }
487
488 /***
489 * This method reports whether or not the user has been confirmed
490 * in the system by checking the User.CONFIRM_VALUE
491 * column in the users record to see if it is equal to
492 * User.CONFIRM_DATA.
493 *
494 * @return True if the user has been confirmed.
495 */
496 public boolean isConfirmed()
497 {
498 String value = getConfirmed();
499 return (value != null && value.equals(User.CONFIRM_DATA));
500 }
501
502 /***
503 * The user is considered logged in if they have not timed out.
504 *
505 * @return Whether the user has logged in.
506 */
507 public boolean hasLoggedIn()
508 {
509 Boolean loggedIn = getHasLoggedIn();
510 return (loggedIn != null && loggedIn.booleanValue());
511 }
512
513 /***
514 * This sets whether or not someone has logged in. hasLoggedIn()
515 * returns this value.
516 *
517 * @param value Whether someone has logged in or not.
518 */
519 public void setHasLoggedIn(Boolean value)
520 {
521 setTemp(User.HAS_LOGGED_IN, value);
522 }
523
524 /***
525 * Gets the last access date for this User. This is the last time
526 * that the user object was referenced.
527 *
528 * @return A Java Date with the last access date for the user.
529 */
530 public java.util.Date getLastAccessDate()
531 {
532 if (lastAccessDate == null)
533 {
534 setLastAccessDate();
535 }
536 return lastAccessDate;
537 }
538
539 /***
540 * Sets the last access date for this User. This is the last time
541 * that the user object was referenced.
542 */
543 public void setLastAccessDate()
544 {
545 lastAccessDate = new java.util.Date();
546 }
547
548 /***
549 * Returns the permanent storage. This is implemented
550 * as a Hashtable and backed by an VARBINARY column in
551 * the database.
552 *
553 * @return A Hashtable.
554 */
555 public Hashtable getPermStorage()
556 {
557 if (permStorage == null)
558 {
559 byte [] objectdata = getObjectdata();
560
561 if (objectdata != null)
562 {
563 permStorage = (Hashtable) ObjectUtils.deserialize(objectdata);
564 }
565
566 if (permStorage == null)
567 {
568 permStorage = new Hashtable();
569 }
570 }
571
572 return permStorage;
573 }
574
575 /***
576 * This should only be used in the case where we want to save the
577 * data to the database.
578 *
579 * @param storage A Hashtable.
580 */
581 public void setPermStorage(Hashtable permStorage)
582 {
583 if (permStorage != null)
584 {
585 this.permStorage = permStorage;
586 }
587 }
588
589 /***
590 * Returns the temporary storage. This is implemented
591 * as a Hashtable
592 *
593 * @return A Hashtable.
594 */
595 public Hashtable getTempStorage()
596 {
597 if (tempStorage == null)
598 {
599 tempStorage = new Hashtable();
600 }
601 return tempStorage;
602 }
603
604 /***
605 * This should only be used in the case where we want to save the
606 * data to the database.
607 *
608 * @param storage A Hashtable.
609 */
610 public void setTempStorage(Hashtable tempStorage)
611 {
612 if (tempStorage != null)
613 {
614 this.tempStorage = tempStorage;
615 }
616 }
617
618 /***
619 * Get an object from permanent storage.
620 *
621 * @param name The object's name.
622 * @return An Object with the given name.
623 */
624 public Object getPerm(String name)
625 {
626 return getPermStorage().get(name);
627 }
628
629 /***
630 * Get an object from permanent storage; return default if value
631 * is null.
632 *
633 * @param name The object's name.
634 * @param def A default value to return.
635 * @return An Object with the given name.
636 */
637 public Object getPerm(String name, Object def)
638 {
639 try
640 {
641 Object val = getPermStorage().get(name);
642 return (val == null ? def : val);
643 }
644 catch (Exception e)
645 {
646 return def;
647 }
648 }
649
650 /***
651 * Put an object into permanent storage. If the value is null,
652 * it will convert that to a "" because the underlying storage
653 * mechanism within TorqueUser is currently a Hashtable and
654 * null is not a valid value.
655 *
656 * @param name The object's name.
657 * @param value The object.
658 */
659 public void setPerm(String name, Object value)
660 {
661 getPermStorage().put(name, (value == null) ? "" : value);
662 }
663
664 /***
665 * Get an object from temporary storage.
666 *
667 * @param name The object's name.
668 * @return An Object with the given name.
669 */
670 public Object getTemp(String name)
671 {
672 return getTempStorage().get(name);
673 }
674
675 /***
676 * Get an object from temporary storage; return default if value
677 * is null.
678 *
679 * @param name The object's name.
680 * @param def A default value to return.
681 * @return An Object with the given name.
682 */
683 public Object getTemp(String name, Object def)
684 {
685 Object val;
686 try
687 {
688 val = getTempStorage().get(name);
689 if (val == null)
690 {
691 val = def;
692 }
693 }
694 catch (Exception e)
695 {
696 val = def;
697 }
698 return val;
699 }
700
701 /***
702 * Put an object into temporary storage. If the value is null,
703 * it will convert that to a "" because the underlying storage
704 * mechanism within TorqueUser is currently a Hashtable and
705 * null is not a valid value.
706 *
707 * @param name The object's name.
708 * @param value The object.
709 */
710 public void setTemp(String name, Object value)
711 {
712 getTempStorage().put(name, (value == null) ? "" : value);
713 }
714
715 /***
716 * Remove an object from temporary storage and return the object.
717 *
718 * @param name The name of the object to remove.
719 * @return An Object.
720 */
721 public Object removeTemp(String name)
722 {
723 return getTempStorage().remove(name);
724 }
725
726 /***
727 * Updates the last login date in the database.
728 *
729 * @exception Exception A generic exception.
730 */
731 public void updateLastLogin()
732 throws Exception
733 {
734 setLastLogin(new java.util.Date());
735 }
736
737 /***
738 * Implement this method if you wish to be notified when the User
739 * has been Bound to the session.
740 *
741 * @param event Indication of value/session binding.
742 */
743 public void valueBound(HttpSessionBindingEvent hsbe)
744 {
745
746 }
747
748 /***
749 * Implement this method if you wish to be notified when the User
750 * has been Unbound from the session.
751 *
752 * @param event Indication of value/session unbinding.
753 */
754 public void valueUnbound(HttpSessionBindingEvent hsbe)
755 {
756 try
757 {
758 if (hasLoggedIn())
759 {
760 TurbineSecurity.saveOnSessionUnbind(this);
761 }
762 }
763 catch (Exception e)
764 {
765
766
767
768
769
770 ByteArrayOutputStream ostr = new ByteArrayOutputStream();
771 e.printStackTrace(new PrintWriter(ostr, true));
772 String stackTrace = ostr.toString();
773 System.out.println(stackTrace);
774 }
775 }
776
777 /***
778 * This gets whether or not someone has logged in. hasLoggedIn()
779 * returns this value as a boolean. This is private because you
780 * should use hasLoggedIn() instead.
781 *
782 * @return True if someone has logged in.
783 */
784 private Boolean getHasLoggedIn()
785 {
786 return (Boolean) getTemp(User.HAS_LOGGED_IN);
787 }
788
789 }