1/*2 * Copyright 2001-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16packageorg.apache.jetspeed.om.security;
1718import java.io.ByteArrayOutputStream;
19import java.io.PrintWriter;
20import java.util.Date;
21import java.util.Hashtable;
22import javax.servlet.http.HttpSessionBindingEvent;
2324import org.apache.jetspeed.services.JetspeedUserManagement;
25import org.apache.jetspeed.services.JetspeedAuthentication;
26import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
27import org.apache.jetspeed.services.logging.JetspeedLogger;
28import org.apache.jetspeed.services.resources.JetspeedResources;
2930import org.apache.turbine.om.security.User;
31import org.apache.turbine.util.ObjectUtils;
3233/***34 * The default Jetspeed implementation of User interface.35 *36 * This basic implementation contains the functionality that is37 * expected to be common among all User implementations.38 * You are welcome to extend this class if you wish to have39 * custom functionality in your user objects (like accessor methods40 * for custom attributes). 41 *42 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>43 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a> 44 * @version $Id: BaseJetspeedUser.java,v 1.11 2004/02/23 03:14:12 jford Exp $45 */46publicclassBaseJetspeedUser/*extends SecurityObject*/ implements JetspeedUser47 {
48/*** The date on which the user account was created. */49private Date createDate = null;
50/*** The date on which the user last accessed the application. */51private Date lastAccessDate = null;
5253/*** This is data that will survive a servlet engine restart. */54private Hashtable permStorage = null;
5556/*** This is data that will not survive a servlet engine restart. */57private Hashtable tempStorage = null;
5859protected String name = "";
6061protectedboolean isNew = true;
626364/***65 * Static initialization of the logger for this class66 */67privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedUser.class.getName());
6869/***70 * Constructor.71 * Create a new User and set the createDate.72 */73publicBaseJetspeedUser()
74 {
75 createDate = new Date();
76 tempStorage = new Hashtable(10);
77 permStorage = new Hashtable(10);
78 setHasLoggedIn(Boolean.FALSE);
79 setDisabled(false);
80 isNew = true;
81 }
828384/***85 * Returns the primary principle for this User, the user id.86 *87 * @return the user id.88 */89public String getUserId()
90 {
91 String tmp = null;
92try93 {
94 tmp = (String) getPerm (JetspeedUser.USER_ID);
95if (tmp != null && tmp.length() == 0)
96 {
97 tmp = null;
98 }
99 }
100catch (Exception e)
101 {
102 logger.error("getUserId(): " + e.getMessage(), e);
103 }
104return tmp;
105 }
106107publicvoid setUserId(String id)
108 {
109if (getUserId() == null)
110 {
111 setPerm(JetspeedUser.USER_ID, id);
112 }
113 }
114115/***116 * Gets the access counter for a user during a session.117 *118 * @return The access counter for the user for the session.119 */120publicint getAccessCounterForSession()
121 {
122int accessCounter = 0;
123try124 {
125 Integer temp = (Integer) getTemp(User.SESSION_ACCESS_COUNTER);
126if(temp != null)
127 {
128 accessCounter = temp.intValue();
129 }
130 }
131catch (Exception e)
132 {
133 logger.debug("getAccessCounterForSession(): " + e.getMessage(), e);
134 }
135136return accessCounter;
137 }
138139/***140 * Gets the access counter for a user from perm storage.141 *142 * @return The access counter for the user.143 */144publicint getAccessCounter()
145 {
146int accessCounter = 0;
147try148 {
149 Integer temp = (Integer) getPerm(User.ACCESS_COUNTER);
150if(temp != null)
151 {
152 accessCounter = temp.intValue();
153 }
154 }
155catch (Exception e)
156 {
157 logger.debug("getAccessCounter(): " + e.getMessage(), e);
158 }
159return accessCounter;
160 }
161162/***163 * Gets the create date for this User. This is the time at which164 * the user object was created.165 *166 * @return A Java Date with the date of creation for the user.167 */168public java.util.Date getCreateDate()
169 {
170return createDate;
171 }
172173/***174 * Gets the last access date for this User. This is the last time175 * that the user object was referenced.176 *177 * @return A Java Date with the last access date for the user.178 */179public java.util.Date getLastAccessDate()
180 {
181if (lastAccessDate == null)
182 {
183 setLastAccessDate();
184 }
185return lastAccessDate;
186 }
187188/***189 * Get last login date/time for this user.190 *191 * @return A Java Date with the last login date for the user.192 */193public java.util.Date getLastLogin()
194 {
195return (java.util.Date) getPerm(User.LAST_LOGIN);
196 }
197198/***199 * Get password for this user.200 *201 * @return A String with the password for the user.202 */203public String getPassword()
204 {
205return (String) getPerm(User.PASSWORD);
206 }
207208/***209 * Get an object from permanent storage.210 *211 * @param name The object's name.212 * @return An Object with the given name.213 */214public Object getPerm(String name)
215 {
216return permStorage.get(name);
217 }
218219/***220 * Get an object from permanent storage; return default if value221 * is null.222 *223 * @param name The object's name.224 * @param def A default value to return.225 * @return An Object with the given name.226 */227public Object getPerm(String name, Object def)
228 {
229try230 {
231 Object val = permStorage.get (name);
232return (val == null ? def : val);
233 }
234catch (Exception e)
235 {
236 logger.error("getPerm(" + name + "): " + e.getMessage(), e);
237return def;
238 }
239 }
240241/***242 * This should only be used in the case where we want to save the243 * data to the database.244 *245 * @return A Hashtable.246 */247public Hashtable getPermStorage()
248 {
249if (this.permStorage == null)
250 {
251this.permStorage = new Hashtable();
252 }
253returnthis.permStorage;
254 }
255256/***257 * Get an object from temporary storage.258 *259 * @param name The object's name.260 * @return An Object with the given name.261 */262public Object getTemp(String name)
263 {
264return tempStorage.get(name);
265 }
266267/***268 * Get an object from temporary storage; return default if value269 * is null.270 *271 * @param name The object's name.272 * @param def A default value to return.273 * @return An Object with the given name.274 */275public Object getTemp(String name, Object def)
276 {
277 Object val;
278try279 {
280 val = tempStorage.get(name);
281if (val == null)
282 {
283 val = def;
284 }
285 }
286catch (Exception e)
287 {
288 logger.error("getTemp(" + name + "): " + e.getMessage(), e);
289 val = def;
290 }
291return val;
292 }
293294/***295 * Returns the username for this user. If this is defined, then296 * the user is considered logged in.297 *298 * @return A String with the username.299 */300public String getUserName()
301 {
302 String tmp = null;
303try304 {
305 tmp = (String) getPerm (User.USERNAME);
306if ( tmp.length() == 0 )
307 {
308 tmp = null;
309 }
310 }
311catch (Exception e)
312 {
313 logger.error("getUserName(): " + e.getMessage(), e);
314 }
315return tmp;
316 }
317318/***319 * Returns the first name for this user. If this is defined, then320 * the user is considered logged in.321 *322 * @return A String with the user's first name.323 */324public String getFirstName()
325 {
326 String tmp = null;
327try328 {
329 tmp = (String) getPerm (User.FIRST_NAME);
330if (tmp.length() == 0)
331 {
332 tmp = null;
333 }
334 }
335catch (Exception e)
336 {
337 logger.error("getFirstName(): " + e.getMessage(), e);
338 }
339return tmp;
340 }
341342/***343 * Returns the last name for this user. If this is defined, then344 * the user is considered logged in.345 *346 * @return A String with the user's last name.347 */348public String getLastName()
349 {
350 String tmp = null;
351try352 {
353 tmp = (String) getPerm (User.LAST_NAME);
354if (tmp.length() == 0)
355 tmp = null;
356 }
357catch (Exception e)
358 {
359 logger.error("getLastName(): " + e.getMessage(), e);
360 }
361return tmp;
362 }
363364/***365 * The user is considered logged in if they have not timed out.366 *367 * @return Whether the user has logged in.368 */369publicboolean hasLoggedIn()
370 {
371 Boolean loggedIn = getHasLoggedIn();
372return (loggedIn != null && loggedIn.booleanValue());
373 }
374375/***376 * Returns the email address for this user.377 *378 * @return A String with the user's email address.379 */380public String getEmail()
381 {
382return (String)getPerm(User.EMAIL);
383 }
384385/***386 * Increments the permanent hit counter for the user.387 */388publicvoid incrementAccessCounter()
389 {
390 setAccessCounter(getAccessCounter() + 1);
391 }
392393/***394 * Increments the session hit counter for the user.395 */396publicvoid incrementAccessCounterForSession()
397 {
398 setAccessCounterForSession(getAccessCounterForSession() + 1);
399 }
400401/***402 * Remove an object from temporary storage and return the object.403 *404 * @param name The name of the object to remove.405 * @return An Object.406 */407public Object removeTemp(String name)
408 {
409return tempStorage.remove(name);
410 }
411412/***413 * Sets the access counter for a user, saved in perm storage.414 *415 * @param cnt The new count.416 */417publicvoid setAccessCounter(int cnt)
418 {
419 setPerm(User.ACCESS_COUNTER, new Integer(cnt));
420 }
421422/***423 * Sets the session access counter for a user, saved in temp424 * storage.425 *426 * @param cnt The new count.427 */428publicvoid setAccessCounterForSession(int cnt)
429 {
430 setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt));
431 }
432433/***434 * Sets the last access date for this User. This is the last time435 * that the user object was referenced.436 */437publicvoid setLastAccessDate()
438 {
439 lastAccessDate = new java.util.Date();
440 }
441442/***443 * Sets the create date for this User. This is the time at which444 * the user object was created.445 *446 * @param date The create date.447 */448publicvoid setCreateDate(java.util.Date date)
449 {
450 createDate = date;
451 }
452453/***454 * Set last login date/time.455 *456 * @param date The last login date.457 */458publicvoid setLastLogin(java.util.Date date)
459 {
460 setPerm(User.LAST_LOGIN, date);
461 }
462463/***464 * Set password.465 *466 * @param password The new password.467 */468publicvoid setPassword(String password)
469 {
470 setPerm(User.PASSWORD, password);
471 }
472473/***474 * Put an object into permanent storage. If the value is null,475 * it will convert that to a "" because the underlying storage476 * mechanism within TurbineUser is currently a Hashtable and477 * null is not a valid value.478 *479 * @param name The object's name.480 * @param value The object.481 */482publicvoid setPerm(String name, Object value)
483 {
484 ObjectUtils.safeAddToHashtable(getPermStorage(), name, value);
485 }
486487/***488 * This should only be used in the case where we want to save the489 * data to the database.490 *491 * @param stuff A Hashtable.492 */493publicvoid setPermStorage(Hashtable stuff)
494 {
495this.permStorage = stuff;
496 }
497498/***499 * This should only be used in the case where we want to save the500 * data to the database.501 *502 * @return A Hashtable.503 */504public Hashtable getTempStorage()
505 {
506if (this.tempStorage == null)
507 {
508this.tempStorage = new Hashtable();
509 }
510returnthis.tempStorage;
511 }
512513/***514 * This should only be used in the case where we want to save the515 * data to the database.516 *517 * @param storage A Hashtable.518 */519publicvoid setTempStorage(Hashtable storage)
520 {
521this.tempStorage = storage;
522 }
523524/***525 * This gets whether or not someone has logged in. hasLoggedIn()526 * returns this value as a boolean. This is private because you527 * should use hasLoggedIn() instead.528 *529 * @return True if someone has logged in.530 */531private Boolean getHasLoggedIn()
532 {
533return (Boolean) getTemp (User.HAS_LOGGED_IN);
534 }
535536/***537 * This sets whether or not someone has logged in. hasLoggedIn()538 * returns this value.539 *540 * @param value Whether someone has logged in or not.541 */542publicvoid setHasLoggedIn (Boolean value)
543 {
544 setTemp (User.HAS_LOGGED_IN, value);
545 }
546547/***548 * Put an object into temporary storage. If the value is null,549 * it will convert that to a "" because the underlying storage550 * mechanism within TurbineUser is currently a Hashtable and551 * null is not a valid value.552 *553 * @param name The object's name.554 * @param value The object.555 */556publicvoid setTemp(String name, Object value)
557 {
558 ObjectUtils.safeAddToHashtable(tempStorage, name, value);
559 }
560561/***562 * Sets the username for this user.563 *564 * @param username The user's username.565 */566publicvoid setUserName(String username)
567 {
568 setPerm (User.USERNAME, username);
569 }
570571/***572 * Sets the first name for this user.573 *574 * @param firstName User's first name.575 */576publicvoid setFirstName(String firstName)
577 {
578 setPerm(User.FIRST_NAME, firstName);
579 }
580581/***582 * Sets the last name for this user.583 *584 * @param lastName User's last name.585 */586publicvoid setLastName(String lastName)
587 {
588 setPerm(User.LAST_NAME, lastName);
589 }
590591592/***593 * Sets the email address.594 *595 * @param address The email address.596 */597publicvoid setEmail(String address)
598 {
599 setPerm(User.EMAIL, address);
600 }
601602/***603 * This method reports whether or not the user has been confirmed604 * in the system by checking the User.CONFIRM_VALUE605 * column in the users record to see if it is equal to606 * User.CONFIRM_DATA.607 *608 * @return True if the user has been confirmed.609 */610publicboolean isConfirmed()
611 {
612 String value = getConfirmed();
613return (value != null && value.equals(User.CONFIRM_DATA));
614 }
615616/***617 * Sets the confirmation value. The value should618 * be either a random string or User.CONFIRM_DATA619 *620 * @param value The confirmation key value.621 */622publicvoid setConfirmed(String value)
623 {
624 String val = "";
625if (value != null)
626 {
627 val = value;
628 }
629 setPerm(User.CONFIRM_VALUE, val);
630 }
631632/***633 * Gets the confirmation value.634 *635 * @return status The confirmation value for this User636 */637public String getConfirmed()
638 {
639return (String)getPerm(User.CONFIRM_VALUE);
640 }
641642/***643 * Updates the last login date in the database.644 *645 * @exception Exception, a generic exception.646 */647publicvoid updateLastLogin()
648 throws Exception
649 {
650 setPerm( User.LAST_LOGIN, new java.util.Date() );
651 }
652653/***654 * Implement this method if you wish to be notified when the User655 * has been Bound to the session.656 *657 * @param hsbe The HttpSessionBindingEvent.658 */659publicvoid valueBound(HttpSessionBindingEvent hsbe)
660 {
661// Currently we have no need for this method.662 }
663664/***665 * Implement this method if you wish to be notified when the User666 * has been Unbound from the session.667 *668 * @param hsbe The HttpSessionBindingEvent.669 */670publicvoid valueUnbound(HttpSessionBindingEvent hsbe)
671 {
672673try674 {
675 java.util.Date now = new java.util.Date();
676//System.out.println("*********** value unbound ********************: " + now.toString());677if (this.hasLoggedIn())
678 {
679if ( JetspeedResources.getBoolean("automatic.logout.save", false) )
680 {
681 JetspeedUserManagement.saveUser(this);
682 }
683 JetspeedAuthentication.logout();
684 }
685686 }
687catch ( Exception e )
688 {
689 logger.error("TurbineUser.valueUnbound(): " + e.getMessage(), e);
690691// To prevent messages being lost in case the logging system692// goes away before sessions get unbound on servlet container693// shutdown, print the stcktrace to the container's console.694 ByteArrayOutputStream ostr = new ByteArrayOutputStream();
695 e.printStackTrace(new PrintWriter(ostr,true));
696 String stackTrace = ostr.toString();
697 System.out.println(stackTrace);
698 }
699 }
700701702/***703 * Saves this object to the data store.704 */705publicvoid save()
706 throws Exception
707 {
708if (this.isNew())
709 {
710 JetspeedUserManagement.addUser(this);
711 }
712else713 {
714 JetspeedUserManagement.saveUser(this);
715 }
716 }
717718/***719 * Returns the disabled status for the user720 *721 * @return True when the account is disabled722 */723publicboolean getDisabled()
724 {
725boolean disabled = false;
726try727 {
728 String tmp = (String) getPerm (JetspeedUser.DISABLED);
729if ( tmp != null && tmp.length() > 0 )
730 {
731if (tmp.equalsIgnoreCase("T"))
732 disabled = true;
733 }
734 }
735catch (Exception e)
736 {
737 logger.error("getDisabled(): " + e.getMessage(), e);
738 }
739return disabled;
740 }
741742publicvoid setDisabled(boolean disabled)
743 {
744 setPerm(JetspeedUser.DISABLED, (disabled) ? "T" : "F");
745 }
746747public String getName()
748 {
749return name;
750 }
751752publicvoid setName(String name)
753 {
754this.name = name;
755 }
756757publicboolean isNew()
758 {
759return isNew;
760 }
761762protectedvoid setNew(boolean isNew)
763 {
764this.isNew = isNew;
765 }
766767/***768 * @see org.apache.jetspeed.om.security.JetspeedUser#getPasswordChanged769 */770public Date getPasswordChanged()
771 {
772return (Date) getPerm(JetspeedUser.PASSWORD_CHANGED);
773 }
774775/***776 * @see org.apache.jetspeed.om.security.JetspeedUser#setPasswordChanged777 */778publicvoid setPasswordChanged(Date value)
779 {
780 setPerm(JetspeedUser.PASSWORD_CHANGED, value);
781 }
782783 }