View Javadoc
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.util.Date; 60 import java.util.Hashtable; 61 import javax.servlet.http.HttpSessionBindingEvent; 62 import org.apache.turbine.services.security.TurbineSecurity; 63 import org.apache.turbine.util.Log; 64 import org.apache.turbine.util.ObjectUtils; 65 66 /*** 67 * A generic implementation of User interface. 68 * 69 * This basic implementation contains the functionality that is 70 * expected to be common among all User implementations. 71 * You are welcome to extend this class if you wish to have 72 * custom functionality in your user objects (like accessor methods 73 * for custom attributes). Note* that implementing a different scheme 74 * of user data storage involves writing an implementation of 75 * {@link org.apache.turbine.services.security.UserManager} interface. 76 * 77 * @author <a href="mailto:josh@stonecottage.com">Josh Lucas</a> 78 * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a> 79 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 80 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> 81 * @author <a href="mailto:cberry@gluecode.com">Craig D. Berry</a> 82 * @version $Id: TurbineUser.java,v 1.21.4.3 2001/06/01 01:33:38 jvanzyl Exp $ 83 */ 84 public class TurbineUser extends SecurityObject implements User 85 { 86 /* A few attributes common to a User. */ 87 private java.util.Date createDate = null; 88 private java.util.Date lastAccessDate = null; 89 90 /*** This is data that will survive a servlet engine restart. */ 91 private Hashtable permStorage = null; 92 93 /*** This is data that will not survive a servlet engine restart. */ 94 private Hashtable tempStorage = null; 95 96 /*** 97 * Constructor. 98 * Create a new User and set the createDate. 99 */ 100 public TurbineUser() 101 { 102 createDate = new java.util.Date(); 103 tempStorage = new Hashtable(10); 104 permStorage = new Hashtable(10); 105 setHasLoggedIn(new Boolean(false)); 106 } 107 108 /*** 109 * Gets the access counter for a user during a session. 110 * 111 * @return The access counter for the user for the session. 112 */ 113 public int getAccessCounterForSession() 114 { 115 try 116 { 117 return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER)).intValue(); 118 } 119 catch (Exception e) 120 { 121 return 0; 122 } 123 } 124 125 /*** 126 * Gets the access counter for a user from perm storage. 127 * 128 * @return The access counter for the user. 129 */ 130 public int getAccessCounter() 131 { 132 try 133 { 134 return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue(); 135 } 136 catch (Exception e) 137 { 138 return 0; 139 } 140 } 141 142 /*** 143 * Gets the create date for this User. This is the time at which 144 * the user object was created. 145 * 146 * @return A Java Date with the date of creation for the user. 147 */ 148 public java.util.Date getCreateDate() 149 { 150 return createDate; 151 } 152 153 /*** 154 * Gets the last access date for this User. This is the last time 155 * that the user object was referenced. 156 * 157 * @return A Java Date with the last access date for the user. 158 */ 159 public java.util.Date getLastAccessDate() 160 { 161 if ( lastAccessDate == null ) 162 setLastAccessDate(); 163 return lastAccessDate; 164 } 165 166 /*** 167 * Get last login date/time for this user. 168 * 169 * @return A Java Date with the last login date for the user. 170 */ 171 public java.util.Date getLastLogin() 172 { 173 return (java.util.Date) getPerm(User.LAST_LOGIN); 174 } 175 176 /*** 177 * Get password for this user. 178 * 179 * @return A String with the password for the user. 180 */ 181 public String getPassword() 182 { 183 return (String) getPerm(User.PASSWORD); 184 } 185 186 /*** 187 * Get an object from permanent storage. 188 * 189 * @param name The object's name. 190 * @return An Object with the given name. 191 */ 192 public Object getPerm ( String name ) 193 { 194 return permStorage.get (name); 195 } 196 197 /*** 198 * Get an object from permanent storage; return default if value 199 * is null. 200 * 201 * @param name The object's name. 202 * @param def A default value to return. 203 * @return An Object with the given name. 204 */ 205 public Object getPerm ( String name, 206 Object def ) 207 { 208 try 209 { 210 Object val = permStorage.get (name); 211 if ( val == null ) 212 return def; 213 return val; 214 } 215 catch (Exception e) 216 { 217 return def; 218 } 219 } 220 221 /*** 222 * This should only be used in the case where we want to save the 223 * data to the database. 224 * 225 * @return A Hashtable. 226 */ 227 public Hashtable getPermStorage() 228 { 229 if ( this.permStorage == null ) 230 this.permStorage = new Hashtable(); 231 232 return this.permStorage; 233 } 234 235 /*** 236 * Get an object from temporary storage. 237 * 238 * @param name The object's name. 239 * @return An Object with the given name. 240 */ 241 public Object getTemp ( String name ) 242 { 243 return tempStorage.get (name); 244 } 245 246 /*** 247 * Get an object from temporary storage; return default if value 248 * is null. 249 * 250 * @param name The object's name. 251 * @param def A default value to return. 252 * @return An Object with the given name. 253 */ 254 public Object getTemp ( String name, 255 Object def ) 256 { 257 Object val; 258 try 259 { 260 val = tempStorage.get (name); 261 if (val == null) 262 { 263 val = def; 264 } 265 } 266 catch (Exception e) 267 { 268 val = def; 269 } 270 return val; 271 } 272 273 /*** 274 * Returns the username for this user. If this is defined, then 275 * the user is considered logged in. 276 * 277 * @return A String with the username. 278 */ 279 public String getUserName() 280 { 281 String tmp = null; 282 try 283 { 284 tmp = (String) getPerm (User.USERNAME); 285 if ( tmp.length() == 0 ) 286 tmp = null; 287 } 288 catch ( Exception e ) 289 { 290 } 291 return tmp; 292 } 293 294 /*** 295 * Returns the first name for this user. If this is defined, then 296 * the user is considered logged in. 297 * 298 * @return A String with the user's first name. 299 */ 300 public String getFirstName() 301 { 302 String tmp = null; 303 try 304 { 305 tmp = (String) getPerm (User.FIRST_NAME); 306 if ( tmp.length() == 0 ) 307 tmp = null; 308 } 309 catch ( Exception e ) 310 { 311 } 312 return tmp; 313 } 314 315 /*** 316 * Returns the last name for this user. If this is defined, then 317 * the user is considered logged in. 318 * 319 * @return A String with the user's last name. 320 */ 321 public String getLastName() 322 { 323 String tmp = null; 324 try 325 { 326 tmp = (String) getPerm (User.LAST_NAME); 327 if ( tmp.length() == 0 ) 328 tmp = null; 329 } 330 catch ( Exception e ) 331 { 332 } 333 return tmp; 334 } 335 336 /*** 337 * The user is considered logged in if they have not timed out. 338 * 339 * @return True if the user has logged in. 340 */ 341 public boolean hasLoggedIn() 342 { 343 Boolean tmp = getHasLoggedIn(); 344 if ( tmp != null && tmp.booleanValue()) 345 return true; 346 else 347 return false; 348 } 349 350 /*** 351 * Returns the email address for this user. 352 * 353 * @return A String with the user's email address. 354 */ 355 public String getEmail() 356 { 357 return (String)getPerm(User.EMAIL); 358 } 359 360 /*** 361 * Increments the permanent hit counter for the user. 362 */ 363 public void incrementAccessCounter() 364 { 365 setAccessCounter(getAccessCounter() + 1); 366 } 367 368 /*** 369 * Increments the session hit counter for the user. 370 */ 371 public void incrementAccessCounterForSession() 372 { 373 setAccessCounterForSession(getAccessCounterForSession() + 1); 374 } 375 376 /*** 377 * Remove an object from temporary storage and return the object. 378 * 379 * @param name The name of the object to remove. 380 * @return An Object. 381 */ 382 public Object removeTemp ( String name ) 383 { 384 return tempStorage.remove (name); 385 } 386 387 /*** 388 * Sets the access counter for a user, saved in perm storage. 389 * 390 * @param cnt The new count. 391 */ 392 public void setAccessCounter(int cnt) 393 { 394 setPerm(User.ACCESS_COUNTER, new Integer(cnt)); 395 } 396 397 /*** 398 * Sets the session access counter for a user, saved in temp 399 * storage. 400 * 401 * @param cnt The new count. 402 */ 403 public void setAccessCounterForSession(int cnt) 404 { 405 setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt)); 406 } 407 408 /*** 409 * Sets the last access date for this User. This is the last time 410 * that the user object was referenced. 411 */ 412 public void setLastAccessDate() 413 { 414 lastAccessDate = new java.util.Date(); 415 } 416 417 /*** 418 * Sets the create date for this User. This is the time at which 419 * the user object was created. 420 * 421 * @param date The create date. 422 */ 423 public void setCreateDate(java.util.Date date) 424 { 425 createDate = date; 426 } 427 428 /*** 429 * Set last login date/time. 430 * 431 * @param date The last login date. 432 */ 433 public void setLastLogin(java.util.Date date) 434 { 435 setPerm( User.LAST_LOGIN, date ); 436 } 437 438 /*** 439 * Set password. 440 * 441 * @param password The new password. 442 */ 443 public void setPassword(String password) 444 { 445 setPerm( User.PASSWORD, password ); 446 } 447 448 /*** 449 * Put an object into permanent storage. If the value is null, 450 * it will convert that to a "" because the underlying storage 451 * mechanism within TurbineUser is currently a Hashtable and 452 * null is not a valid value. 453 * 454 * @param name The object's name. 455 * @param value The object. 456 */ 457 public void setPerm ( String name, 458 Object value ) 459 { 460 ObjectUtils.safeAddToHashtable ( permStorage, name, value ); 461 } 462 463 /*** 464 * This should only be used in the case where we want to save the 465 * data to the database. 466 * 467 * @param stuff A Hashtable. 468 */ 469 public void setPermStorage(Hashtable stuff) 470 { 471 this.permStorage = stuff; 472 } 473 474 /*** 475 * This should only be used in the case where we want to save the 476 * data to the database. 477 * 478 * @return A Hashtable. 479 */ 480 public Hashtable getTempStorage() 481 { 482 if ( this.tempStorage == null ) 483 this.tempStorage = new Hashtable(); 484 return this.tempStorage; 485 } 486 487 /*** 488 * This should only be used in the case where we want to save the 489 * data to the database. 490 * 491 * @param storage A Hashtable. 492 */ 493 public void setTempStorage(Hashtable storage) 494 { 495 this.tempStorage = storage; 496 } 497 498 /*** 499 * This gets whether or not someone has logged in. hasLoggedIn() 500 * returns this value as a boolean. This is private because you 501 * should use hasLoggedIn() instead. 502 * 503 * @return True if someone has logged in. 504 */ 505 private Boolean getHasLoggedIn() 506 { 507 return (Boolean) getTemp (User.HAS_LOGGED_IN); 508 } 509 510 /*** 511 * This sets whether or not someone has logged in. hasLoggedIn() 512 * returns this value. 513 * 514 * @param value Whether someone has logged in or not. 515 */ 516 public void setHasLoggedIn (Boolean value) 517 { 518 setTemp (User.HAS_LOGGED_IN, value); 519 } 520 521 /*** 522 * Put an object into temporary storage. If the value is null, 523 * it will convert that to a "" because the underlying storage 524 * mechanism within TurbineUser is currently a Hashtable and 525 * null is not a valid value. 526 * 527 * @param name The object's name. 528 * @param value The object. 529 */ 530 public void setTemp ( String name, 531 Object value ) 532 { 533 ObjectUtils.safeAddToHashtable ( tempStorage, name, value ); 534 } 535 536 /*** 537 * Sets the username for this user. 538 * 539 * @param username The user's username. 540 */ 541 public void setUserName(String username) 542 { 543 setPerm (User.USERNAME, username); 544 } 545 546 /*** 547 * Sets the first name for this user. 548 * 549 * @param firstName User's first name. 550 */ 551 public void setFirstName(String firstName) 552 { 553 setPerm(User.FIRST_NAME, firstName); 554 } 555 556 /*** 557 * Sets the last name for this user. 558 * 559 * @param lastName User's last name. 560 */ 561 public void setLastName(String lastName) 562 { 563 setPerm(User.LAST_NAME, lastName); 564 } 565 566 567 /*** 568 * Sets the email address. 569 * 570 * @param address The email address. 571 */ 572 public void setEmail(String address) 573 { 574 setPerm(User.EMAIL, address); 575 } 576 577 /*** 578 * This method reports whether or not the user has been confirmed 579 * in the system by checking the User.CONFIRM_VALUE 580 * column in the users record to see if it is equal to 581 * User.CONFIRM_DATA. 582 * 583 * @return True if the user has been confirmed. 584 */ 585 public boolean isConfirmed() 586 { 587 String value = getConfirmed(); 588 return (value != null && value.equals(User.CONFIRM_DATA)); 589 } 590 591 /*** 592 * Sets the confirmation value. The value should 593 * be either a random string or User.CONFIRM_DATA 594 * 595 * @param value The confirmation key value. 596 */ 597 public void setConfirmed(String value) 598 { 599 String val = ""; 600 if (value != null) 601 val = value; 602 setPerm(User.CONFIRM_VALUE, val); 603 } 604 605 /*** 606 * Gets the confirmation value. 607 * 608 * @return status The confirmation value for this User 609 */ 610 public String getConfirmed() 611 { 612 return (String)getPerm(User.CONFIRM_VALUE); 613 } 614 615 /*** 616 * Updates the last login date in the database. 617 * 618 * @exception Exception, a generic exception. 619 */ 620 public void updateLastLogin() 621 throws Exception 622 { 623 setPerm( User.LAST_LOGIN, new java.util.Date() ); 624 } 625 626 /*** 627 * Implement this method if you wish to be notified when the User 628 * has been Bound to the session. 629 * 630 * @param hsbe The HttpSessionBindingEvent. 631 */ 632 public void valueBound(HttpSessionBindingEvent hsbe) 633 { 634 // Currently we have no need for this method. 635 } 636 637 /*** 638 * Implement this method if you wish to be notified when the User 639 * has been Unbound from the session. 640 * 641 * @param hsbe The HttpSessionBindingEvent. 642 */ 643 public void valueUnbound(HttpSessionBindingEvent hsbe) 644 { 645 try 646 { 647 if (hasLoggedIn()) 648 { 649 TurbineSecurity.saveUser(this); 650 } 651 } 652 catch ( Exception e ) 653 { 654 org.apache.turbine.util.Log.error("TurbineUser.valueUnbobund(): " + 655 e.getMessage()); 656 org.apache.turbine.util.Log.error(e); 657 658 // To prevent messages being lost in case the logging system 659 // goes away before sessions get unbound on servlet container 660 // shutdown, print the stcktrace to the container's console. 661 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); 662 e.printStackTrace(new PrintWriter(ostr,true)); 663 String stackTrace = ostr.toString(); 664 System.out.println(stackTrace); 665 } 666 } 667 668 /*** 669 * Saves this object to the data store. 670 */ 671 public void save() 672 throws Exception 673 { 674 if (TurbineSecurity.accountExists(this)) 675 { 676 TurbineSecurity.saveUser(this); 677 } 678 else 679 { 680 TurbineSecurity.addUser(this, getPassword()); 681 } 682 } 683 }

This page was automatically generated by Maven