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

This page was automatically generated by Maven