1 package org.apache.turbine.om.security;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.ByteArrayOutputStream;
58 import java.io.PrintWriter;
59 import java.sql.Connection;
60 import java.util.Date;
61 import java.util.Hashtable;
62 import javax.servlet.http.HttpSessionBindingEvent;
63 import org.apache.turbine.services.security.TurbineSecurity;
64 import org.apache.turbine.util.Log;
65 import org.apache.turbine.util.ObjectUtils;
66
67 /***
68 * A generic implementation of User interface.
69 *
70 * This basic implementation contains the functionality that is
71 * expected to be common among all User implementations.
72 * You are welcome to extend this class if you wish to have
73 * custom functionality in your user objects (like accessor methods
74 * for custom attributes). Note* that implementing a different scheme
75 * of user data storage involves writing an implementation of
76 * {@link org.apache.turbine.services.security.UserManager} interface.
77 *
78 * @author <a href="mailto:josh@stonecottage.com">Josh Lucas</a>
79 * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
80 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
81 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
82 * @author <a href="mailto:cberry@gluecode.com">Craig D. Berry</a>
83 * @version $Id: TurbineUser.java,v 1.2 2002/07/11 07:34:30 mpoeschl Exp $
84 */
85 public class TurbineUser extends SecurityObject implements User
86 {
87 /*** The date on which the user account was created. */
88 private Date createDate = null;
89 /*** The date on which the user last accessed the application. */
90 private Date lastAccessDate = null;
91
92 /*** This is data that will survive a servlet engine restart. */
93 private Hashtable permStorage = null;
94
95 /*** This is data that will not survive a servlet engine restart. */
96 private Hashtable tempStorage = null;
97
98 /***
99 * Constructor.
100 * Create a new User and set the createDate.
101 */
102 public TurbineUser()
103 {
104 createDate = new Date();
105 tempStorage = new Hashtable(10);
106 permStorage = new Hashtable(10);
107 setHasLoggedIn(Boolean.FALSE);
108 }
109
110 /***
111 * Gets the access counter for a user during a session.
112 *
113 * @return The access counter for the user for the session.
114 */
115 public int getAccessCounterForSession()
116 {
117 try
118 {
119 return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER)).intValue();
120 }
121 catch (Exception e)
122 {
123 return 0;
124 }
125 }
126
127 /***
128 * Gets the access counter for a user from perm storage.
129 *
130 * @return The access counter for the user.
131 */
132 public int getAccessCounter()
133 {
134 try
135 {
136 return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue();
137 }
138 catch (Exception e)
139 {
140 return 0;
141 }
142 }
143
144 /***
145 * Gets the create date for this User. This is the time at which
146 * the user object was created.
147 *
148 * @return A Java Date with the date of creation for the user.
149 */
150 public java.util.Date getCreateDate()
151 {
152 return createDate;
153 }
154
155 /***
156 * Gets the last access date for this User. This is the last time
157 * that the user object was referenced.
158 *
159 * @return A Java Date with the last access date for the user.
160 */
161 public java.util.Date getLastAccessDate()
162 {
163 if (lastAccessDate == null)
164 {
165 setLastAccessDate();
166 }
167 return lastAccessDate;
168 }
169
170 /***
171 * Get last login date/time for this user.
172 *
173 * @return A Java Date with the last login date for the user.
174 */
175 public java.util.Date getLastLogin()
176 {
177 return (java.util.Date) getPerm(User.LAST_LOGIN);
178 }
179
180 /***
181 * Get password for this user.
182 *
183 * @return A String with the password for the user.
184 */
185 public String getPassword()
186 {
187 return (String) getPerm(User.PASSWORD);
188 }
189
190 /***
191 * Get an object from permanent storage.
192 *
193 * @param name The object's name.
194 * @return An Object with the given name.
195 */
196 public Object getPerm(String name)
197 {
198 return permStorage.get(name);
199 }
200
201 /***
202 * Get an object from permanent storage; return default if value
203 * is null.
204 *
205 * @param name The object's name.
206 * @param def A default value to return.
207 * @return An Object with the given name.
208 */
209 public Object getPerm(String name, Object def)
210 {
211 try
212 {
213 Object val = permStorage.get (name);
214 return (val == null ? def : val);
215 }
216 catch (Exception e)
217 {
218 return def;
219 }
220 }
221
222 /***
223 * This should only be used in the case where we want to save the
224 * data to the database.
225 *
226 * @return A Hashtable.
227 */
228 public Hashtable getPermStorage()
229 {
230 if (this.permStorage == null)
231 {
232 this.permStorage = new Hashtable();
233 }
234 return this.permStorage;
235 }
236
237 /***
238 * Get an object from temporary storage.
239 *
240 * @param name The object's name.
241 * @return An Object with the given name.
242 */
243 public Object getTemp(String name)
244 {
245 return tempStorage.get(name);
246 }
247
248 /***
249 * Get an object from temporary storage; return default if value
250 * is null.
251 *
252 * @param name The object's name.
253 * @param def A default value to return.
254 * @return An Object with the given name.
255 */
256 public Object getTemp(String name, Object def)
257 {
258 Object val;
259 try
260 {
261 val = tempStorage.get(name);
262 if (val == null)
263 {
264 val = def;
265 }
266 }
267 catch (Exception e)
268 {
269 val = def;
270 }
271 return val;
272 }
273
274 /***
275 * Returns the username for this user. If this is defined, then
276 * the user is considered logged in.
277 *
278 * @return A String with the username.
279 */
280 public String getUserName()
281 {
282 String tmp = null;
283 try
284 {
285 tmp = (String) getPerm (User.USERNAME);
286 if ( tmp.length() == 0 )
287 {
288 tmp = null;
289 }
290 }
291 catch (Exception e)
292 {
293 }
294 return tmp;
295 }
296
297 /***
298 * Returns the first name for this user. If this is defined, then
299 * the user is considered logged in.
300 *
301 * @return A String with the user's first name.
302 */
303 public String getFirstName()
304 {
305 String tmp = null;
306 try
307 {
308 tmp = (String) getPerm (User.FIRST_NAME);
309 if (tmp.length() == 0)
310 {
311 tmp = null;
312 }
313 }
314 catch (Exception e)
315 {
316 }
317 return tmp;
318 }
319
320 /***
321 * Returns the last name for this user. If this is defined, then
322 * the user is considered logged in.
323 *
324 * @return A String with the user's last name.
325 */
326 public String getLastName()
327 {
328 String tmp = null;
329 try
330 {
331 tmp = (String) getPerm (User.LAST_NAME);
332 if (tmp.length() == 0)
333 tmp = null;
334 }
335 catch (Exception e)
336 {
337 }
338 return tmp;
339 }
340
341 /***
342 * The user is considered logged in if they have not timed out.
343 *
344 * @return Whether the user has logged in.
345 */
346 public boolean hasLoggedIn()
347 {
348 Boolean loggedIn = getHasLoggedIn();
349 return (loggedIn != null && loggedIn.booleanValue());
350 }
351
352 /***
353 * Returns the email address for this user.
354 *
355 * @return A String with the user's email address.
356 */
357 public String getEmail()
358 {
359 return (String)getPerm(User.EMAIL);
360 }
361
362 /***
363 * Increments the permanent hit counter for the user.
364 */
365 public void incrementAccessCounter()
366 {
367 setAccessCounter(getAccessCounter() + 1);
368 }
369
370 /***
371 * Increments the session hit counter for the user.
372 */
373 public void incrementAccessCounterForSession()
374 {
375 setAccessCounterForSession(getAccessCounterForSession() + 1);
376 }
377
378 /***
379 * Remove an object from temporary storage and return the object.
380 *
381 * @param name The name of the object to remove.
382 * @return An Object.
383 */
384 public Object removeTemp(String name)
385 {
386 return tempStorage.remove(name);
387 }
388
389 /***
390 * Sets the access counter for a user, saved in perm storage.
391 *
392 * @param cnt The new count.
393 */
394 public void setAccessCounter(int cnt)
395 {
396 setPerm(User.ACCESS_COUNTER, new Integer(cnt));
397 }
398
399 /***
400 * Sets the session access counter for a user, saved in temp
401 * storage.
402 *
403 * @param cnt The new count.
404 */
405 public void setAccessCounterForSession(int cnt)
406 {
407 setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt));
408 }
409
410 /***
411 * Sets the last access date for this User. This is the last time
412 * that the user object was referenced.
413 */
414 public void setLastAccessDate()
415 {
416 lastAccessDate = new java.util.Date();
417 }
418
419 /***
420 * Sets the create date for this User. This is the time at which
421 * the user object was created.
422 *
423 * @param date The create date.
424 */
425 public void setCreateDate(java.util.Date date)
426 {
427 createDate = date;
428 }
429
430 /***
431 * Set last login date/time.
432 *
433 * @param date The last login date.
434 */
435 public void setLastLogin(java.util.Date date)
436 {
437 setPerm(User.LAST_LOGIN, date);
438 }
439
440 /***
441 * Set password.
442 *
443 * @param password The new password.
444 */
445 public void setPassword(String password)
446 {
447 setPerm(User.PASSWORD, password);
448 }
449
450 /***
451 * Put an object into permanent storage. If the value is null,
452 * it will convert that to a "" because the underlying storage
453 * mechanism within TurbineUser is currently a Hashtable and
454 * null is not a valid value.
455 *
456 * @param name The object's name.
457 * @param value The object.
458 */
459 public void setPerm(String name, Object value)
460 {
461 ObjectUtils.safeAddToHashtable(getPermStorage(), name, value);
462 }
463
464 /***
465 * This should only be used in the case where we want to save the
466 * data to the database.
467 *
468 * @param stuff A Hashtable.
469 */
470 public void setPermStorage(Hashtable stuff)
471 {
472 this.permStorage = stuff;
473 }
474
475 /***
476 * This should only be used in the case where we want to save the
477 * data to the database.
478 *
479 * @return A Hashtable.
480 */
481 public Hashtable getTempStorage()
482 {
483 if (this.tempStorage == null)
484 {
485 this.tempStorage = new Hashtable();
486 }
487 return this.tempStorage;
488 }
489
490 /***
491 * This should only be used in the case where we want to save the
492 * data to the database.
493 *
494 * @param storage A Hashtable.
495 */
496 public void setTempStorage(Hashtable storage)
497 {
498 this.tempStorage = storage;
499 }
500
501 /***
502 * This gets whether or not someone has logged in. hasLoggedIn()
503 * returns this value as a boolean. This is private because you
504 * should use hasLoggedIn() instead.
505 *
506 * @return True if someone has logged in.
507 */
508 private Boolean getHasLoggedIn()
509 {
510 return (Boolean) getTemp (User.HAS_LOGGED_IN);
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 * Put an object into temporary storage. If the value is null,
526 * it will convert that to a "" because the underlying storage
527 * mechanism within TurbineUser is currently a Hashtable and
528 * null is not a valid value.
529 *
530 * @param name The object's name.
531 * @param value The object.
532 */
533 public void setTemp(String name, Object value)
534 {
535 ObjectUtils.safeAddToHashtable(tempStorage, name, value);
536 }
537
538 /***
539 * Sets the username for this user.
540 *
541 * @param username The user's username.
542 */
543 public void setUserName(String username)
544 {
545 setPerm (User.USERNAME, username);
546 }
547
548 /***
549 * Sets the first name for this user.
550 *
551 * @param firstName User's first name.
552 */
553 public void setFirstName(String firstName)
554 {
555 setPerm(User.FIRST_NAME, firstName);
556 }
557
558 /***
559 * Sets the last name for this user.
560 *
561 * @param lastName User's last name.
562 */
563 public void setLastName(String lastName)
564 {
565 setPerm(User.LAST_NAME, lastName);
566 }
567
568
569 /***
570 * Sets the email address.
571 *
572 * @param address The email address.
573 */
574 public void setEmail(String address)
575 {
576 setPerm(User.EMAIL, address);
577 }
578
579 /***
580 * This method reports whether or not the user has been confirmed
581 * in the system by checking the User.CONFIRM_VALUE
582 * column in the users record to see if it is equal to
583 * User.CONFIRM_DATA.
584 *
585 * @return True if the user has been confirmed.
586 */
587 public boolean isConfirmed()
588 {
589 String value = getConfirmed();
590 return (value != null && value.equals(User.CONFIRM_DATA));
591 }
592
593 /***
594 * Sets the confirmation value. The value should
595 * be either a random string or User.CONFIRM_DATA
596 *
597 * @param value The confirmation key value.
598 */
599 public void setConfirmed(String value)
600 {
601 String val = "";
602 if (value != null)
603 {
604 val = value;
605 }
606 setPerm(User.CONFIRM_VALUE, val);
607 }
608
609 /***
610 * Gets the confirmation value.
611 *
612 * @return status The confirmation value for this User
613 */
614 public String getConfirmed()
615 {
616 return (String)getPerm(User.CONFIRM_VALUE);
617 }
618
619 /***
620 * Updates the last login date in the database.
621 *
622 * @exception Exception, a generic exception.
623 */
624 public void updateLastLogin()
625 throws Exception
626 {
627 setPerm( User.LAST_LOGIN, new java.util.Date() );
628 }
629
630 /***
631 * Implement this method if you wish to be notified when the User
632 * has been Bound to the session.
633 *
634 * @param hsbe The HttpSessionBindingEvent.
635 */
636 public void valueBound(HttpSessionBindingEvent hsbe)
637 {
638 // Currently we have no need for this method.
639 }
640
641 /***
642 * Implement this method if you wish to be notified when the User
643 * has been Unbound from the session.
644 *
645 * @param hsbe The HttpSessionBindingEvent.
646 */
647 public void valueUnbound(HttpSessionBindingEvent hsbe)
648 {
649 try
650 {
651 if (hasLoggedIn())
652 {
653 TurbineSecurity.saveUser(this);
654 }
655 }
656 catch ( Exception e )
657 {
658 Log.error("TurbineUser.valueUnbobund(): " + e.getMessage(), e);
659
660 // To prevent messages being lost in case the logging system
661 // goes away before sessions get unbound on servlet container
662 // shutdown, print the stcktrace to the container's console.
663 ByteArrayOutputStream ostr = new ByteArrayOutputStream();
664 e.printStackTrace(new PrintWriter(ostr,true));
665 String stackTrace = ostr.toString();
666 System.out.println(stackTrace);
667 }
668 }
669
670 /***
671 * Saves this object to the data store.
672 */
673 public void save()
674 throws Exception
675 {
676 if (TurbineSecurity.accountExists(this))
677 {
678 TurbineSecurity.saveUser(this);
679 }
680 else
681 {
682 TurbineSecurity.addUser(this, getPassword());
683 }
684 }
685
686 /***
687 * not implemented
688 *
689 * @param conn
690 * @throws Exception
691 */
692 public void save(Connection conn) throws Exception
693 {
694 throw new Exception("not implemented");
695 }
696
697 /***
698 * not implemented
699 *
700 * @param dbname
701 * @throws Exception
702 */
703 public void save(String dbname) throws Exception
704 {
705 throw new Exception("not implemented");
706 }
707
708 }
This page was automatically generated by Maven