001: package org.apache.turbine.om.security;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.sql.Connection;
020:
021: import java.util.Date;
022: import java.util.Hashtable;
023:
024: import javax.servlet.http.HttpSessionBindingEvent;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.apache.turbine.services.security.TurbineSecurity;
030:
031: /**
032: * A generic implementation of User interface.
033: *
034: * This basic implementation contains the functionality that is
035: * expected to be common among all User implementations.
036: * You are welcome to extend this class if you wish to have
037: * custom functionality in your user objects (like accessor methods
038: * for custom attributes). <b>Note</b> that implementing a different scheme
039: * of user data storage normally involves writing an implementation of
040: * {@link org.apache.turbine.services.security.UserManager} interface.
041: *
042: * @author <a href="mailto:josh@stonecottage.com">Josh Lucas</a>
043: * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
044: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
045: * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
046: * @author <a href="mailto:cberry@gluecode.com">Craig D. Berry</a>
047: * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
048: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
049: * @version $Id: TurbineUser.java 278822 2005-09-05 19:53:05Z henning $
050: */
051: public class TurbineUser extends SecurityObject implements User {
052: /** Serial Version UID */
053: private static final long serialVersionUID = -6090627713197456117L;
054:
055: /** Logging */
056: private static Log log = LogFactory.getLog(TurbineUser.class);
057:
058: /** The date on which the user account was created. */
059: private Date createDate = null;
060:
061: /** The date on which the user last accessed the application. */
062: private Date lastAccessDate = null;
063:
064: /** This is data that will survive a servlet engine restart. */
065: private Hashtable permStorage = null;
066:
067: /** This is data that will not survive a servlet engine restart. */
068: private Hashtable tempStorage = null;
069:
070: /**
071: * Constructor.
072: *
073: * Create a new User and set the createDate.
074: */
075: public TurbineUser() {
076: super ();
077: createDate = new Date();
078: setHasLoggedIn(Boolean.FALSE);
079: }
080:
081: /**
082: * Gets the access counter for a user during a session.
083: *
084: * @return The access counter for the user for the session.
085: */
086: public int getAccessCounterForSession() {
087: try {
088: return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER))
089: .intValue();
090: } catch (Exception e) {
091: return 0;
092: }
093: }
094:
095: /**
096: * Gets the access counter for a user from perm storage.
097: *
098: * @return The access counter for the user.
099: */
100: public int getAccessCounter() {
101: try {
102: return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue();
103: } catch (Exception e) {
104: return 0;
105: }
106: }
107:
108: /**
109: * Gets the create date for this User. This is the time at which
110: * the user object was created.
111: *
112: * @return A Java Date with the date of creation for the user.
113: */
114: public java.util.Date getCreateDate() {
115: return createDate;
116: }
117:
118: /**
119: * Gets the last access date for this User. This is the last time
120: * that the user object was referenced.
121: *
122: * @return A Java Date with the last access date for the user.
123: */
124: public java.util.Date getLastAccessDate() {
125: if (lastAccessDate == null) {
126: setLastAccessDate();
127: }
128: return lastAccessDate;
129: }
130:
131: /**
132: * Get last login date/time for this user.
133: *
134: * @return A Java Date with the last login date for the user.
135: */
136: public java.util.Date getLastLogin() {
137: return (java.util.Date) getPerm(User.LAST_LOGIN);
138: }
139:
140: /**
141: * Get password for this user.
142: *
143: * @return A String with the password for the user.
144: */
145: public String getPassword() {
146: return (String) getPerm(User.PASSWORD);
147: }
148:
149: /**
150: * Get an object from permanent storage.
151: *
152: * @param name The object's name.
153: * @return An Object with the given name, or null if not found.
154: */
155: public Object getPerm(String name) {
156: return getPerm(name, null);
157: }
158:
159: /**
160: * Get an object from permanent storage; return default if value
161: * is null.
162: *
163: * @param name The object's name.
164: * @param def A default value to return.
165: * @return An Object with the given name.
166: */
167: public Object getPerm(String name, Object def) {
168: Object val;
169: try {
170: val = getPermStorage().get(name);
171: if (val == null) {
172: val = def;
173: }
174: } catch (Exception e) {
175: val = def;
176: }
177: return val;
178: }
179:
180: /**
181: * This should only be used in the case where we want to save the
182: * data to the database.
183: *
184: * @return A Hashtable.
185: */
186: public Hashtable getPermStorage() {
187: if (permStorage == null) {
188: permStorage = new Hashtable(10);
189: }
190: return permStorage;
191: }
192:
193: /**
194: * Get an object from temporary storage; return null if the
195: * object can't be found.
196: *
197: * @param name The object's name.
198: * @return An Object with the given name.
199: */
200: public Object getTemp(String name) {
201: return getTemp(name, null);
202: }
203:
204: /**
205: * Get an object from temporary storage; return default if value
206: * is null.
207: *
208: * @param name The object's name.
209: * @param def A default value to return.
210: * @return An Object with the given name.
211: */
212: public Object getTemp(String name, Object def) {
213: Object val;
214: try {
215: val = getTempStorage().get(name);
216: if (val == null) {
217: val = def;
218: }
219:
220: } catch (Exception e) {
221: val = def;
222: }
223: return val;
224: }
225:
226: /**
227: * Returns the username for this user.
228: *
229: * @return A String with the username.
230: * @deprecated use {@link #getName} instead.
231: */
232: public String getUserName() {
233: return getName();
234: }
235:
236: /**
237: * Returns the first name for this user.
238: *
239: * @return A String with the user's first name.
240: */
241: public String getFirstName() {
242: String tmp = null;
243: try {
244: tmp = (String) getPerm(User.FIRST_NAME);
245: if (tmp.length() == 0) {
246: tmp = null;
247: }
248: } catch (Exception e) {
249: }
250: return tmp;
251: }
252:
253: /**
254: * Returns the last name for this user.
255: *
256: * @return A String with the user's last name.
257: */
258: public String getLastName() {
259: String tmp = null;
260: try {
261: tmp = (String) getPerm(User.LAST_NAME);
262: if (tmp.length() == 0)
263: tmp = null;
264: } catch (Exception e) {
265: }
266: return tmp;
267: }
268:
269: /**
270: * The user is considered logged in if they have not timed out.
271: *
272: * @return Whether the user has logged in.
273: */
274: public boolean hasLoggedIn() {
275: Boolean loggedIn = getHasLoggedIn();
276: return (loggedIn != null && loggedIn.booleanValue());
277: }
278:
279: /**
280: * Returns the email address for this user.
281: *
282: * @return A String with the user's email address.
283: */
284: public String getEmail() {
285: return (String) getPerm(User.EMAIL);
286: }
287:
288: /**
289: * Increments the permanent hit counter for the user.
290: */
291: public void incrementAccessCounter() {
292: setAccessCounter(getAccessCounter() + 1);
293: }
294:
295: /**
296: * Increments the session hit counter for the user.
297: */
298: public void incrementAccessCounterForSession() {
299: setAccessCounterForSession(getAccessCounterForSession() + 1);
300: }
301:
302: /**
303: * Remove an object from temporary storage and return the object.
304: *
305: * @param name The name of the object to remove.
306: * @return An Object.
307: */
308: public Object removeTemp(String name) {
309: return getTempStorage().remove(name);
310: }
311:
312: /**
313: * Sets the access counter for a user, saved in perm storage.
314: *
315: * @param cnt The new count.
316: */
317: public void setAccessCounter(int cnt) {
318: setPerm(User.ACCESS_COUNTER, new Integer(cnt));
319: }
320:
321: /**
322: * Sets the session access counter for a user, saved in temp
323: * storage.
324: *
325: * @param cnt The new count.
326: */
327: public void setAccessCounterForSession(int cnt) {
328: setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt));
329: }
330:
331: /**
332: * Sets the last access date for this User. This is the last time
333: * that the user object was referenced.
334: */
335: public void setLastAccessDate() {
336: lastAccessDate = new java.util.Date();
337: }
338:
339: /**
340: * Sets the create date for this User. This is the time at which
341: * the user object was created.
342: *
343: * @param date The create date.
344: */
345: public void setCreateDate(java.util.Date date) {
346: createDate = date;
347: }
348:
349: /**
350: * Set last login date/time.
351: *
352: * @param date The last login date.
353: */
354: public void setLastLogin(java.util.Date date) {
355: setPerm(User.LAST_LOGIN, date);
356: }
357:
358: /**
359: * Set password.
360: *
361: * @param password The new password.
362: */
363: public void setPassword(String password) {
364: setPerm(User.PASSWORD, password);
365: }
366:
367: /**
368: * Put an object into permanent storage. If the value is null,
369: * it will convert that to a "" because the underlying storage
370: * mechanism within TurbineUser is currently a Hashtable and
371: * null is not a valid value.
372: *
373: * @param name The object's name.
374: * @param value The object.
375: */
376: public void setPerm(String name, Object value) {
377: getPermStorage().put(name, (value == null) ? "" : value);
378: }
379:
380: /**
381: * This should only be used in the case where we want to save the
382: * data to the database.
383: *
384: * @param permStorage A Hashtable.
385: */
386: public void setPermStorage(Hashtable permStorage) {
387: this .permStorage = permStorage;
388: }
389:
390: /**
391: * This should only be used in the case where we want to save the
392: * data to the database.
393: *
394: * @return A Hashtable.
395: */
396: public Hashtable getTempStorage() {
397: if (tempStorage == null) {
398: tempStorage = new Hashtable(10);
399: }
400: return tempStorage;
401: }
402:
403: /**
404: * This should only be used in the case where we want to save the
405: * data to the database.
406: *
407: * @param storage A Hashtable.
408: */
409: public void setTempStorage(Hashtable tempStorage) {
410: this .tempStorage = tempStorage;
411: }
412:
413: /**
414: * This gets whether or not someone has logged in. hasLoggedIn()
415: * returns this value as a boolean. This is private because you
416: * should use hasLoggedIn() instead.
417: *
418: * @return True if someone has logged in.
419: */
420: private Boolean getHasLoggedIn() {
421: return (Boolean) getTemp(User.HAS_LOGGED_IN);
422: }
423:
424: /**
425: * This sets whether or not someone has logged in. hasLoggedIn()
426: * returns this value.
427: *
428: * @param value Whether someone has logged in or not.
429: */
430: public void setHasLoggedIn(Boolean value) {
431: setTemp(User.HAS_LOGGED_IN, value);
432: }
433:
434: /**
435: * Put an object into temporary storage. If the value is null,
436: * it will convert that to a "" because the underlying storage
437: * mechanism within TurbineUser is currently a Hashtable and
438: * null is not a valid value.
439: *
440: * @param name The object's name.
441: * @param value The object.
442: */
443: public void setTemp(String name, Object value) {
444: getTempStorage().put(name, (value == null) ? "" : value);
445: }
446:
447: /**
448: * Sets the username for this user.
449: *
450: * @param username The user's username.
451: * @deprecated use {@link #setName} instead
452: */
453: public void setUserName(String username) {
454: setName(username);
455: }
456:
457: /**
458: * Sets the first name for this user.
459: *
460: * @param firstName User's first name.
461: */
462: public void setFirstName(String firstName) {
463: setPerm(User.FIRST_NAME, firstName);
464: }
465:
466: /**
467: * Sets the last name for this user.
468: *
469: * @param lastName User's last name.
470: */
471: public void setLastName(String lastName) {
472: setPerm(User.LAST_NAME, lastName);
473: }
474:
475: /**
476: * Sets the email address.
477: *
478: * @param address The email address.
479: */
480: public void setEmail(String address) {
481: setPerm(User.EMAIL, address);
482: }
483:
484: /**
485: * This method reports whether or not the user has been confirmed
486: * in the system by checking the User.CONFIRM_VALUE
487: * column in the users record to see if it is equal to
488: * User.CONFIRM_DATA.
489: *
490: * @return True if the user has been confirmed.
491: */
492: public boolean isConfirmed() {
493: String value = getConfirmed();
494: return (value != null && value.equals(User.CONFIRM_DATA));
495: }
496:
497: /**
498: * Sets the confirmation value. The value should
499: * be either a random string or User.CONFIRM_DATA
500: *
501: * @param value The confirmation key value.
502: */
503: public void setConfirmed(String value) {
504: String val = "";
505: if (value != null) {
506: val = value;
507: }
508: setPerm(User.CONFIRM_VALUE, val);
509: }
510:
511: /**
512: * Gets the confirmation value.
513: *
514: * @return status The confirmation value for this User
515: */
516: public String getConfirmed() {
517: return (String) getPerm(User.CONFIRM_VALUE);
518: }
519:
520: /**
521: * Updates the last login date in the database.
522: *
523: * @exception Exception a generic exception.
524: */
525: public void updateLastLogin() throws Exception {
526: setPerm(User.LAST_LOGIN, new java.util.Date());
527: }
528:
529: /**
530: * Implement this method if you wish to be notified when the User
531: * has been Bound to the session.
532: *
533: * @param hsbe The HttpSessionBindingEvent.
534: */
535: public void valueBound(HttpSessionBindingEvent hsbe) {
536: // Currently we have no need for this method.
537: }
538:
539: /**
540: * Implement this method if you wish to be notified when the User
541: * has been Unbound from the session.
542: *
543: * @param hsbe The HttpSessionBindingEvent.
544: */
545: public void valueUnbound(HttpSessionBindingEvent hsbe) {
546: try {
547: if (hasLoggedIn()) {
548: TurbineSecurity.saveOnSessionUnbind(this );
549: }
550: } catch (Exception e) {
551: log.error("TurbineUser.valueUnbound(): " + e.getMessage(),
552: e);
553: }
554: }
555:
556: /**
557: * Saves this object to the data store.
558: */
559: public void save() throws Exception {
560: if (TurbineSecurity.accountExists(this )) {
561: TurbineSecurity.saveUser(this );
562: } else {
563: TurbineSecurity.addUser(this , getPassword());
564: }
565: }
566:
567: /**
568: * not implemented
569: *
570: * @param conn
571: * @throws Exception
572: */
573: public void save(Connection conn) throws Exception {
574: throw new Exception("not implemented");
575: }
576:
577: /**
578: * not implemented
579: *
580: * @param dbname
581: * @throws Exception
582: */
583: public void save(String dbname) throws Exception {
584: throw new Exception("not implemented");
585: }
586:
587: /**
588: * Returns the name of this user. This will be the user name/
589: * login name.
590: *
591: * @return The name of the user.
592: */
593: public String getName() {
594: return (String) getPerm(User.USERNAME);
595: }
596:
597: /**
598: * Sets the name of this user. This will be the user name/
599: * login name.
600: *
601: * @param name The name of the object.
602: */
603: public void setName(String name) {
604: setPerm(User.USERNAME, name);
605: }
606: }
|