001: package org.apache.turbine.services.security.torque;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.PrintWriter;
024: import java.sql.Connection;
025: import java.util.Date;
026: import java.util.Hashtable;
027:
028: import javax.servlet.http.HttpSessionBindingEvent;
029:
030: import org.apache.torque.om.Persistent;
031: import org.apache.turbine.om.security.User;
032: import org.apache.turbine.services.security.TurbineSecurity;
033: import org.apache.turbine.util.ObjectUtils;
034: import org.apache.turbine.util.security.TurbineSecurityException;
035:
036: /**
037: * This is the User class used by the TorqueSecurity Service. It decouples
038: * all the database peer access from the actual Peer object
039: *
040: * @author <a href="mailto:josh@stonecottage.com">Josh Lucas</a>
041: * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
042: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
043: * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
044: * @author <a href="mailto:cberry@gluecode.com">Craig D. Berry</a>
045: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
046: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
047: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
048: * @version $Id: TorqueUser.java 534527 2007-05-02 16:10:59Z tv $
049: */
050:
051: public class TorqueUser extends TorqueObject implements User {
052: /** Serial Version UID */
053: private static final long serialVersionUID = 6623129207135917717L;
054:
055: /** The date on which the user last accessed the application. */
056: private Date lastAccessDate = null;
057:
058: /** This is data that will survive a servlet engine restart. */
059: private Hashtable permStorage = null;
060:
061: /** This is data that will not survive a servlet engine restart. */
062: private Hashtable tempStorage = null;
063:
064: /**
065: * Constructor.
066: * Create a new User and set the createDate.
067: */
068: public TorqueUser() {
069: super ();
070: setCreateDate(new Date());
071: tempStorage = new Hashtable(10);
072: setHasLoggedIn(Boolean.FALSE);
073: }
074:
075: /**
076: * This Constructor is used when the UserPeerManager
077: * has retrieved a list of Database Objects from the peer and
078: * must 'wrap' them into TorqueRole Objects. You should not use it directly!
079: *
080: * @param obj An Object from the peer
081: */
082: public TorqueUser(Persistent obj) {
083: super (obj);
084:
085: // Do not set creation date. This is only called on retrieval from
086: // storage!
087:
088: tempStorage = new Hashtable(10);
089: setHasLoggedIn(Boolean.FALSE);
090: }
091:
092: /**
093: * Returns the underlying Object for the Peer
094: *
095: * Used in the UserPeerManager when building a new Criteria.
096: *
097: * @return The underlying persistent object
098: *
099: */
100:
101: public Persistent getPersistentObj() {
102: if (obj == null) {
103: obj = UserPeerManager.newPersistentInstance();
104: }
105: return obj;
106: }
107:
108: /**
109: * Stores the object in the database. If the object is new,
110: * it inserts it; otherwise an update is performed.
111: *
112: * @param torqueName The name under which the object should be stored.
113: *
114: * @exception Exception This method might throw an exceptions
115: */
116: public void save(String torqueName) throws Exception {
117: setObjectdata(ObjectUtils.serializeHashtable(getPermStorage()));
118: super .save(torqueName);
119: }
120:
121: /**
122: * Stores the object in the database. If the object is new,
123: * it inserts it; otherwise an update is performed. This method
124: * is meant to be used as part of a transaction, otherwise use
125: * the save() method and the connection details will be handled
126: * internally
127: *
128: * @param con A Connection object to save the object
129: *
130: * @exception Exception This method might throw an exceptions
131: */
132: public void save(Connection con) throws Exception {
133: setObjectdata(ObjectUtils.serializeHashtable(getPermStorage()));
134: super .save(con);
135: }
136:
137: /**
138: * Makes changes made to the User attributes permanent.
139: *
140: * @throws TurbineSecurityException if there is a problem while
141: * saving data.
142: */
143: public void save() throws TurbineSecurityException {
144: //
145: // This is inconsistent with all the other security classes
146: // because it calls save() on the underlying object directly.
147: // But the UserManager calls ((Persistent)user).save()
148: // so if we do TurbineSecurity.saveUser(this) here, we get
149: // a nice endless loop. :-(
150: //
151: // Users seem to be a special kind of security objects...
152: //
153:
154: try {
155: setObjectdata(ObjectUtils
156: .serializeHashtable(getPermStorage()));
157: getPersistentObj().save();
158: } catch (Exception e) {
159: throw new TurbineSecurityException("User object said "
160: + e.getMessage(), e);
161: }
162: }
163:
164: /**
165: * Returns the name of this object.
166: *
167: * @return The name of the object.
168: */
169: public String getName() {
170: return UserPeerManager.getName(getPersistentObj());
171: }
172:
173: /**
174: * Sets the name of this object
175: *
176: * @param name The name of the object
177: */
178: public void setName(String name) {
179: setUserName(name);
180: }
181:
182: /**
183: * Gets the Id of this object
184: *
185: * @return The Id of the object
186: */
187: public int getId() {
188: return UserPeerManager.getIdAsObj(getPersistentObj())
189: .intValue();
190: }
191:
192: /**
193: * Gets the Id of this object
194: *
195: * @return The Id of the object
196: */
197: public Integer getIdAsObj() {
198: return UserPeerManager.getIdAsObj(getPersistentObj());
199: }
200:
201: /**
202: * Sets the Id of this object
203: *
204: * @param id The new Id
205: */
206: public void setId(int id) {
207: UserPeerManager.setId(getPersistentObj(), id);
208: }
209:
210: /**
211: * Returns the name of this user.
212: *
213: * @return The name of the user.
214: * @deprecated Use getName() instead.
215: */
216: public String getUserName() {
217: return getName();
218: }
219:
220: /**
221: * Sets the name of this user.
222: *
223: * @param name The name of the user.
224: */
225: public void setUserName(String name) {
226: UserPeerManager.setUserName(getPersistentObj(), name);
227: }
228:
229: /**
230: * Returns the password of the User
231: *
232: * @return The password of the User
233: */
234: public String getPassword() {
235: return UserPeerManager.getUserPassword(getPersistentObj());
236: }
237:
238: /**
239: * Sets the password of the User
240: *
241: * @param password The new password of the User
242: */
243: public void setPassword(String password) {
244: UserPeerManager.setUserPassword(getPersistentObj(), password);
245: }
246:
247: /**
248: * Returns the first name of the User
249: *
250: * @return The first name of the User
251: */
252: public String getFirstName() {
253: return UserPeerManager.getUserFirstName(getPersistentObj());
254: }
255:
256: /**
257: * Sets the first name of the User
258: *
259: * @param firstName The new first name of the User
260: */
261: public void setFirstName(String firstName) {
262: UserPeerManager.setUserFirstName(getPersistentObj(), firstName);
263: }
264:
265: /**
266: * Returns the last name of the User
267: *
268: * @return The last name of the User
269: */
270: public String getLastName() {
271: return UserPeerManager.getUserLastName(getPersistentObj());
272: }
273:
274: /**
275: * Sets the last name of User
276: *
277: * @param lastName The new last name of the User
278: */
279: public void setLastName(String lastName) {
280: UserPeerManager.setUserLastName(getPersistentObj(), lastName);
281: }
282:
283: /**
284: * Returns the email address of the user
285: *
286: * @return The email address of the user
287: */
288: public String getEmail() {
289: return UserPeerManager.getUserEmail(getPersistentObj());
290: }
291:
292: /**
293: * Sets the new email address of the user
294: *
295: * @param email The new email address of the user
296: */
297: public void setEmail(String email) {
298: UserPeerManager.setUserEmail(getPersistentObj(), email);
299: }
300:
301: /**
302: * Returns the confirm value of the user
303: *
304: * @return The confirm value of the user
305: */
306: public String getConfirmed() {
307: return UserPeerManager.getUserConfirmed(getPersistentObj());
308: }
309:
310: /**
311: * Sets the new confirm value of the user
312: *
313: * @param confirm The new confirm value of the user
314: */
315: public void setConfirmed(String confirm) {
316: UserPeerManager.setUserConfirmed(getPersistentObj(), confirm);
317: }
318:
319: /**
320: * Returns the creation date of the user
321: *
322: * @return The creation date of the user
323: */
324: public java.util.Date getCreateDate() {
325: return UserPeerManager.getUserCreateDate(getPersistentObj());
326: }
327:
328: /**
329: * Sets the new creation date of the user
330: *
331: * @param createDate The new creation date of the user
332: */
333: public void setCreateDate(java.util.Date createDate) {
334: UserPeerManager.setUserCreateDate(getPersistentObj(),
335: createDate);
336: }
337:
338: /**
339: * Returns the date of the last login of the user
340: *
341: * @return The date of the last login of the user
342: */
343: public java.util.Date getLastLogin() {
344: return UserPeerManager.getUserLastLogin(getPersistentObj());
345: }
346:
347: /**
348: * Sets the new date of the last login of the user
349: *
350: * @param lastLogin The new the date of the last login of the user
351: */
352: public void setLastLogin(java.util.Date lastLogin) {
353: UserPeerManager.setUserLastLogin(getPersistentObj(), lastLogin);
354: }
355:
356: /**
357: * Returns the value of the objectdata for this user.
358: * Objectdata is a VARBINARY column in the table used
359: * to store the permanent storage table from the User
360: * object.
361: *
362: * @return The bytes in the objectdata for this user
363: */
364: public byte[] getObjectdata() {
365: return UserPeerManager.getUserObjectdata(getPersistentObj());
366: }
367:
368: /**
369: * Sets the value of the objectdata for the user
370: *
371: * @param objectdata The new the date of the last login of the user
372: */
373: public void setObjectdata(byte[] objectdata) {
374: UserPeerManager.setUserObjectdata(getPersistentObj(),
375: objectdata);
376: }
377:
378: /**
379: * Gets the access counter for a user from perm storage.
380: *
381: * @return The access counter for the user.
382: */
383: public int getAccessCounter() {
384: try {
385: return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue();
386: } catch (Exception e) {
387: return 0;
388: }
389: }
390:
391: /**
392: * Gets the access counter for a user during a session.
393: *
394: * @return The access counter for the user for the session.
395: */
396: public int getAccessCounterForSession() {
397: try {
398: return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER))
399: .intValue();
400: } catch (Exception e) {
401: return 0;
402: }
403: }
404:
405: /**
406: * Increments the permanent hit counter for the user.
407: */
408: public void incrementAccessCounter() {
409: // Ugh. Race city, here I come...
410: setAccessCounter(getAccessCounter() + 1);
411: }
412:
413: /**
414: * Increments the session hit counter for the user.
415: */
416: public void incrementAccessCounterForSession() {
417: setAccessCounterForSession(getAccessCounterForSession() + 1);
418: }
419:
420: /**
421: * Sets the access counter for a user, saved in perm storage.
422: *
423: * @param cnt The new count.
424: */
425: public void setAccessCounter(int cnt) {
426: setPerm(User.ACCESS_COUNTER, new Integer(cnt));
427: }
428:
429: /**
430: * Sets the session access counter for a user, saved in temp
431: * storage.
432: *
433: * @param cnt The new count.
434: */
435: public void setAccessCounterForSession(int cnt) {
436: setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt));
437: }
438:
439: /**
440: * This method reports whether or not the user has been confirmed
441: * in the system by checking the User.CONFIRM_VALUE
442: * column in the users record to see if it is equal to
443: * User.CONFIRM_DATA.
444: *
445: * @return True if the user has been confirmed.
446: */
447: public boolean isConfirmed() {
448: String value = getConfirmed();
449: return (value != null && value.equals(User.CONFIRM_DATA));
450: }
451:
452: /**
453: * The user is considered logged in if they have not timed out.
454: *
455: * @return Whether the user has logged in.
456: */
457: public boolean hasLoggedIn() {
458: Boolean loggedIn = getHasLoggedIn();
459: return (loggedIn != null && loggedIn.booleanValue());
460: }
461:
462: /**
463: * This sets whether or not someone has logged in. hasLoggedIn()
464: * returns this value.
465: *
466: * @param value Whether someone has logged in or not.
467: */
468: public void setHasLoggedIn(Boolean value) {
469: setTemp(User.HAS_LOGGED_IN, value);
470: }
471:
472: /**
473: * Gets the last access date for this User. This is the last time
474: * that the user object was referenced.
475: *
476: * @return A Java Date with the last access date for the user.
477: */
478: public java.util.Date getLastAccessDate() {
479: if (lastAccessDate == null) {
480: setLastAccessDate();
481: }
482: return lastAccessDate;
483: }
484:
485: /**
486: * Sets the last access date for this User. This is the last time
487: * that the user object was referenced.
488: */
489: public void setLastAccessDate() {
490: lastAccessDate = new java.util.Date();
491: }
492:
493: /**
494: * Returns the permanent storage. This is implemented
495: * as a Hashtable and backed by an VARBINARY column in
496: * the database.
497: *
498: * @return A Hashtable.
499: */
500: public Hashtable getPermStorage() {
501: if (permStorage == null) {
502: byte[] objectdata = getObjectdata();
503:
504: if (objectdata != null) {
505: permStorage = (Hashtable) ObjectUtils
506: .deserialize(objectdata);
507: }
508:
509: if (permStorage == null) {
510: permStorage = new Hashtable();
511: }
512: }
513:
514: return permStorage;
515: }
516:
517: /**
518: * This should only be used in the case where we want to save the
519: * data to the database.
520: *
521: * @param storage A Hashtable.
522: */
523: public void setPermStorage(Hashtable permStorage) {
524: if (permStorage != null) {
525: this .permStorage = permStorage;
526: }
527: }
528:
529: /**
530: * Returns the temporary storage. This is implemented
531: * as a Hashtable
532: *
533: * @return A Hashtable.
534: */
535: public Hashtable getTempStorage() {
536: if (tempStorage == null) {
537: tempStorage = new Hashtable();
538: }
539: return tempStorage;
540: }
541:
542: /**
543: * This should only be used in the case where we want to save the
544: * data to the database.
545: *
546: * @param storage A Hashtable.
547: */
548: public void setTempStorage(Hashtable tempStorage) {
549: if (tempStorage != null) {
550: this .tempStorage = tempStorage;
551: }
552: }
553:
554: /**
555: * Get an object from permanent storage.
556: *
557: * @param name The object's name.
558: * @return An Object with the given name.
559: */
560: public Object getPerm(String name) {
561: return getPermStorage().get(name);
562: }
563:
564: /**
565: * Get an object from permanent storage; return default if value
566: * is null.
567: *
568: * @param name The object's name.
569: * @param def A default value to return.
570: * @return An Object with the given name.
571: */
572: public Object getPerm(String name, Object def) {
573: try {
574: Object val = getPermStorage().get(name);
575: return (val == null ? def : val);
576: } catch (Exception e) {
577: return def;
578: }
579: }
580:
581: /**
582: * Put an object into permanent storage. If the value is null,
583: * it will convert that to a "" because the underlying storage
584: * mechanism within TorqueUser is currently a Hashtable and
585: * null is not a valid value.
586: *
587: * @param name The object's name.
588: * @param value The object.
589: */
590: public void setPerm(String name, Object value) {
591: getPermStorage().put(name, (value == null) ? "" : value);
592: }
593:
594: /**
595: * Get an object from temporary storage.
596: *
597: * @param name The object's name.
598: * @return An Object with the given name.
599: */
600: public Object getTemp(String name) {
601: return getTempStorage().get(name);
602: }
603:
604: /**
605: * Get an object from temporary storage; return default if value
606: * is null.
607: *
608: * @param name The object's name.
609: * @param def A default value to return.
610: * @return An Object with the given name.
611: */
612: public Object getTemp(String name, Object def) {
613: Object val;
614: try {
615: val = getTempStorage().get(name);
616: if (val == null) {
617: val = def;
618: }
619: } catch (Exception e) {
620: val = def;
621: }
622: return val;
623: }
624:
625: /**
626: * Put an object into temporary storage. If the value is null,
627: * it will convert that to a "" because the underlying storage
628: * mechanism within TorqueUser is currently a Hashtable and
629: * null is not a valid value.
630: *
631: * @param name The object's name.
632: * @param value The object.
633: */
634: public void setTemp(String name, Object value) {
635: getTempStorage().put(name, (value == null) ? "" : value);
636: }
637:
638: /**
639: * Remove an object from temporary storage and return the object.
640: *
641: * @param name The name of the object to remove.
642: * @return An Object.
643: */
644: public Object removeTemp(String name) {
645: return getTempStorage().remove(name);
646: }
647:
648: /**
649: * Updates the last login date in the database.
650: *
651: * @exception Exception A generic exception.
652: */
653: public void updateLastLogin() throws Exception {
654: setLastLogin(new java.util.Date());
655: }
656:
657: /**
658: * Implement this method if you wish to be notified when the User
659: * has been Bound to the session.
660: *
661: * @param event Indication of value/session binding.
662: */
663: public void valueBound(HttpSessionBindingEvent hsbe) {
664: // Currently we have no need for this method.
665: }
666:
667: /**
668: * Implement this method if you wish to be notified when the User
669: * has been Unbound from the session.
670: *
671: * @param event Indication of value/session unbinding.
672: */
673: public void valueUnbound(HttpSessionBindingEvent hsbe) {
674: try {
675: if (hasLoggedIn()) {
676: TurbineSecurity.saveOnSessionUnbind(this );
677: }
678: } catch (Exception e) {
679: //Log.error("TorqueUser.valueUnbound(): " + e.getMessage(), e);
680:
681: // To prevent messages being lost in case the logging system
682: // goes away before sessions get unbound on servlet container
683: // shutdown, print the stcktrace to the container's console.
684: ByteArrayOutputStream ostr = new ByteArrayOutputStream();
685: e.printStackTrace(new PrintWriter(ostr, true));
686: String stackTrace = ostr.toString();
687: System.out.println(stackTrace);
688: }
689: }
690:
691: /**
692: * This gets whether or not someone has logged in. hasLoggedIn()
693: * returns this value as a boolean. This is private because you
694: * should use hasLoggedIn() instead.
695: *
696: * @return True if someone has logged in.
697: */
698: private Boolean getHasLoggedIn() {
699: return (Boolean) getTemp(User.HAS_LOGGED_IN);
700: }
701:
702: }
|