1 16 package org.apache.jetspeed.om.security; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.PrintWriter ; 20 import java.util.Date ; 21 import java.util.Hashtable ; 22 import javax.servlet.http.HttpSessionBindingEvent ; 23 24 import org.apache.jetspeed.services.JetspeedUserManagement; 25 import org.apache.jetspeed.services.JetspeedAuthentication; 26 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 27 import org.apache.jetspeed.services.logging.JetspeedLogger; 28 import org.apache.jetspeed.services.resources.JetspeedResources; 29 30 import org.apache.turbine.om.security.User; 31 import org.apache.turbine.util.ObjectUtils; 32 33 46 public class BaseJetspeedUser implements JetspeedUser 47 { 48 49 private Date createDate = null; 50 51 private Date lastAccessDate = null; 52 53 54 private Hashtable permStorage = null; 55 56 57 private Hashtable tempStorage = null; 58 59 protected String name = ""; 60 61 protected boolean isNew = true; 62 63 64 67 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedUser.class.getName()); 68 69 73 public BaseJetspeedUser() 74 { 75 createDate = new Date (); 76 tempStorage = new Hashtable (10); 77 permStorage = new Hashtable (10); 78 setHasLoggedIn(Boolean.FALSE); 79 setDisabled(false); 80 isNew = true; 81 } 82 83 84 89 public String getUserId() 90 { 91 String tmp = null; 92 try 93 { 94 tmp = (String ) getPerm (JetspeedUser.USER_ID); 95 if (tmp != null && tmp.length() == 0) 96 { 97 tmp = null; 98 } 99 } 100 catch (Exception e) 101 { 102 logger.error("getUserId(): " + e.getMessage(), e); 103 } 104 return tmp; 105 } 106 107 public void setUserId(String id) 108 { 109 if (getUserId() == null) 110 { 111 setPerm(JetspeedUser.USER_ID, id); 112 } 113 } 114 115 120 public int getAccessCounterForSession() 121 { 122 int accessCounter = 0; 123 try 124 { 125 Integer temp = (Integer ) getTemp(User.SESSION_ACCESS_COUNTER); 126 if(temp != null) 127 { 128 accessCounter = temp.intValue(); 129 } 130 } 131 catch (Exception e) 132 { 133 logger.debug("getAccessCounterForSession(): " + e.getMessage(), e); 134 } 135 136 return accessCounter; 137 } 138 139 144 public int getAccessCounter() 145 { 146 int accessCounter = 0; 147 try 148 { 149 Integer temp = (Integer ) getPerm(User.ACCESS_COUNTER); 150 if(temp != null) 151 { 152 accessCounter = temp.intValue(); 153 } 154 } 155 catch (Exception e) 156 { 157 logger.debug("getAccessCounter(): " + e.getMessage(), e); 158 } 159 return accessCounter; 160 } 161 162 168 public java.util.Date getCreateDate() 169 { 170 return createDate; 171 } 172 173 179 public java.util.Date getLastAccessDate() 180 { 181 if (lastAccessDate == null) 182 { 183 setLastAccessDate(); 184 } 185 return lastAccessDate; 186 } 187 188 193 public java.util.Date getLastLogin() 194 { 195 return (java.util.Date ) getPerm(User.LAST_LOGIN); 196 } 197 198 203 public String getPassword() 204 { 205 return (String ) getPerm(User.PASSWORD); 206 } 207 208 214 public Object getPerm(String name) 215 { 216 return permStorage.get(name); 217 } 218 219 227 public Object getPerm(String name, Object def) 228 { 229 try 230 { 231 Object val = permStorage.get (name); 232 return (val == null ? def : val); 233 } 234 catch (Exception e) 235 { 236 logger.error("getPerm(" + name + "): " + e.getMessage(), e); 237 return def; 238 } 239 } 240 241 247 public Hashtable getPermStorage() 248 { 249 if (this.permStorage == null) 250 { 251 this.permStorage = new Hashtable (); 252 } 253 return this.permStorage; 254 } 255 256 262 public Object getTemp(String name) 263 { 264 return tempStorage.get(name); 265 } 266 267 275 public Object getTemp(String name, Object def) 276 { 277 Object val; 278 try 279 { 280 val = tempStorage.get(name); 281 if (val == null) 282 { 283 val = def; 284 } 285 } 286 catch (Exception e) 287 { 288 logger.error("getTemp(" + name + "): " + e.getMessage(), e); 289 val = def; 290 } 291 return val; 292 } 293 294 300 public String getUserName() 301 { 302 String tmp = null; 303 try 304 { 305 tmp = (String ) getPerm (User.USERNAME); 306 if ( tmp.length() == 0 ) 307 { 308 tmp = null; 309 } 310 } 311 catch (Exception e) 312 { 313 logger.error("getUserName(): " + e.getMessage(), e); 314 } 315 return tmp; 316 } 317 318 324 public String getFirstName() 325 { 326 String tmp = null; 327 try 328 { 329 tmp = (String ) getPerm (User.FIRST_NAME); 330 if (tmp.length() == 0) 331 { 332 tmp = null; 333 } 334 } 335 catch (Exception e) 336 { 337 logger.error("getFirstName(): " + e.getMessage(), e); 338 } 339 return tmp; 340 } 341 342 348 public String getLastName() 349 { 350 String tmp = null; 351 try 352 { 353 tmp = (String ) getPerm (User.LAST_NAME); 354 if (tmp.length() == 0) 355 tmp = null; 356 } 357 catch (Exception e) 358 { 359 logger.error("getLastName(): " + e.getMessage(), e); 360 } 361 return tmp; 362 } 363 364 369 public boolean hasLoggedIn() 370 { 371 Boolean loggedIn = getHasLoggedIn(); 372 return (loggedIn != null && loggedIn.booleanValue()); 373 } 374 375 380 public String getEmail() 381 { 382 return (String )getPerm(User.EMAIL); 383 } 384 385 388 public void incrementAccessCounter() 389 { 390 setAccessCounter(getAccessCounter() + 1); 391 } 392 393 396 public void incrementAccessCounterForSession() 397 { 398 setAccessCounterForSession(getAccessCounterForSession() + 1); 399 } 400 401 407 public Object removeTemp(String name) 408 { 409 return tempStorage.remove(name); 410 } 411 412 417 public void setAccessCounter(int cnt) 418 { 419 setPerm(User.ACCESS_COUNTER, new Integer (cnt)); 420 } 421 422 428 public void setAccessCounterForSession(int cnt) 429 { 430 setTemp(User.SESSION_ACCESS_COUNTER, new Integer (cnt)); 431 } 432 433 437 public void setLastAccessDate() 438 { 439 lastAccessDate = new java.util.Date (); 440 } 441 442 448 public void setCreateDate(java.util.Date date) 449 { 450 createDate = date; 451 } 452 453 458 public void setLastLogin(java.util.Date date) 459 { 460 setPerm(User.LAST_LOGIN, date); 461 } 462 463 468 public void setPassword(String password) 469 { 470 setPerm(User.PASSWORD, password); 471 } 472 473 482 public void setPerm(String name, Object value) 483 { 484 ObjectUtils.safeAddToHashtable(getPermStorage(), name, value); 485 } 486 487 493 public void setPermStorage(Hashtable stuff) 494 { 495 this.permStorage = stuff; 496 } 497 498 504 public Hashtable getTempStorage() 505 { 506 if (this.tempStorage == null) 507 { 508 this.tempStorage = new Hashtable (); 509 } 510 return this.tempStorage; 511 } 512 513 519 public void setTempStorage(Hashtable storage) 520 { 521 this.tempStorage = storage; 522 } 523 524 531 private Boolean getHasLoggedIn() 532 { 533 return (Boolean ) getTemp (User.HAS_LOGGED_IN); 534 } 535 536 542 public void setHasLoggedIn (Boolean value) 543 { 544 setTemp (User.HAS_LOGGED_IN, value); 545 } 546 547 556 public void setTemp(String name, Object value) 557 { 558 ObjectUtils.safeAddToHashtable(tempStorage, name, value); 559 } 560 561 566 public void setUserName(String username) 567 { 568 setPerm (User.USERNAME, username); 569 } 570 571 576 public void setFirstName(String firstName) 577 { 578 setPerm(User.FIRST_NAME, firstName); 579 } 580 581 586 public void setLastName(String lastName) 587 { 588 setPerm(User.LAST_NAME, lastName); 589 } 590 591 592 597 public void setEmail(String address) 598 { 599 setPerm(User.EMAIL, address); 600 } 601 602 610 public boolean isConfirmed() 611 { 612 String value = getConfirmed(); 613 return (value != null && value.equals(User.CONFIRM_DATA)); 614 } 615 616 622 public void setConfirmed(String value) 623 { 624 String val = ""; 625 if (value != null) 626 { 627 val = value; 628 } 629 setPerm(User.CONFIRM_VALUE, val); 630 } 631 632 637 public String getConfirmed() 638 { 639 return (String )getPerm(User.CONFIRM_VALUE); 640 } 641 642 647 public void updateLastLogin() 648 throws Exception 649 { 650 setPerm( User.LAST_LOGIN, new java.util.Date () ); 651 } 652 653 659 public void valueBound(HttpSessionBindingEvent hsbe) 660 { 661 } 663 664 670 public void valueUnbound(HttpSessionBindingEvent hsbe) 671 { 672 673 try 674 { 675 java.util.Date now = new java.util.Date (); 676 if (this.hasLoggedIn()) 678 { 679 if ( JetspeedResources.getBoolean("automatic.logout.save", false) ) 680 { 681 JetspeedUserManagement.saveUser(this); 682 } 683 JetspeedAuthentication.logout(); 684 } 685 686 } 687 catch ( Exception e ) 688 { 689 logger.error("TurbineUser.valueUnbound(): " + e.getMessage(), e); 690 691 ByteArrayOutputStream ostr = new ByteArrayOutputStream (); 695 e.printStackTrace(new PrintWriter (ostr,true)); 696 String stackTrace = ostr.toString(); 697 System.out.println(stackTrace); 698 } 699 } 700 701 702 705 public void save() 706 throws Exception 707 { 708 if (this.isNew()) 709 { 710 JetspeedUserManagement.addUser(this); 711 } 712 else 713 { 714 JetspeedUserManagement.saveUser(this); 715 } 716 } 717 718 723 public boolean getDisabled() 724 { 725 boolean disabled = false; 726 try 727 { 728 String tmp = (String ) getPerm (JetspeedUser.DISABLED); 729 if ( tmp != null && tmp.length() > 0 ) 730 { 731 if (tmp.equalsIgnoreCase("T")) 732 disabled = true; 733 } 734 } 735 catch (Exception e) 736 { 737 logger.error("getDisabled(): " + e.getMessage(), e); 738 } 739 return disabled; 740 } 741 742 public void setDisabled(boolean disabled) 743 { 744 setPerm(JetspeedUser.DISABLED, (disabled) ? "T" : "F"); 745 } 746 747 public String getName() 748 { 749 return name; 750 } 751 752 public void setName(String name) 753 { 754 this.name = name; 755 } 756 757 public boolean isNew() 758 { 759 return isNew; 760 } 761 762 protected void setNew(boolean isNew) 763 { 764 this.isNew = isNew; 765 } 766 767 770 public Date getPasswordChanged() 771 { 772 return (Date ) getPerm(JetspeedUser.PASSWORD_CHANGED); 773 } 774 775 778 public void setPasswordChanged(Date value) 779 { 780 setPerm(JetspeedUser.PASSWORD_CHANGED, value); 781 } 782 783 } | Popular Tags |