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