1 16 17 package org.apache.jetspeed.om.security.ldap; 18 19 import java.io.ByteArrayOutputStream; 20 import java.io.PrintWriter; 21 import java.util.Date; 22 import java.util.Enumeration; 23 import java.util.Hashtable; 24 import java.util.Vector; 25 import javax.naming.directory.Attribute; 26 import javax.naming.directory.Attributes; 27 import javax.naming.directory.BasicAttribute; 28 import javax.naming.directory.BasicAttributes; 29 import javax.servlet.http.HttpSessionBindingEvent; 30 import javax.servlet.http.HttpSessionBindingListener; 31 32 import org.apache.jetspeed.om.security.JetspeedUser; 33 import org.apache.jetspeed.services.JetspeedAuthentication; 34 import org.apache.jetspeed.services.JetspeedLDAP; 35 import org.apache.jetspeed.services.JetspeedUserManagement; 36 import org.apache.jetspeed.services.ldap.LDAPURL; 37 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 38 import org.apache.jetspeed.services.logging.JetspeedLogger; 39 import org.apache.jetspeed.services.resources.JetspeedResources; 40 import org.apache.jetspeed.services.security.JetspeedSecurityException; 41 import org.apache.jetspeed.services.security.UserException; 42 43 import org.apache.turbine.om.security.User; 44 import org.apache.turbine.util.ObjectUtils; 45 46 54 public class LDAPUser extends BaseLDAPObject implements JetspeedUser, HttpSessionBindingListener 55 { 56 57 60 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(LDAPUser.class.getName()); 61 62 64 protected static final String OBJECT_CLASS = "jetspeeduser"; 65 protected static final String ORGANIZATIONAL_UNIT = "ou=users"; 66 67 protected static final String ATTR_UID = "uid"; 68 protected static final String ATTR_UID_NUMBER = "uidNumber"; 69 protected static final String ATTR_USER_PASSWORD = "userPassword"; 70 protected static final String ATTR_NAME = "name"; 71 protected static final String ATTR_GIVEN_NAME = "givenName"; 72 protected static final String ATTR_SN = "sn"; 73 protected static final String ATTR_MAIL = "mail"; 74 protected static final String ATTR_OBJECT_DATA = "objectdata"; 75 protected static final String ATTR_OBJECT_CLASS = "objectClass"; 76 protected static final String ATTR_USER_GROUP_ROLE = "usergrouprole"; 77 protected static final String ATTR_LAST_LOGIN_DATE = "lastlogindate"; 78 protected static final String ATTR_LAST_MODIFIED_DATE = "lastmodifieddate"; 79 protected static final String ATTR_CREATION_DATE = "creationdate"; 80 protected static final String ATTR_CONFIRMED = "confirm"; 81 protected static final String ATTR_DISABLED = "disabled"; 82 83 85 86 private Date createDate = null; 87 88 89 private Date lastAccessDate = null; 90 91 92 private Date passwordChanged = null; 93 94 95 private Hashtable permStorage = null; 96 97 98 private Hashtable tempStorage = null; 99 100 101 protected String name = ""; 102 103 104 protected boolean isNew = false; 105 106 107 protected Vector groupRoles = null; 108 109 111 public LDAPUser() 112 { 113 this("TempUser", true); 114 } 115 116 120 public LDAPUser(String username, boolean isNew) 121 { 122 super.ldapurl = JetspeedLDAP.buildURL(ATTR_UID + "=" + username + "," + ORGANIZATIONAL_UNIT); 123 this.isNew = isNew; 124 125 createDate = new Date(); 126 lastAccessDate = createDate; 127 tempStorage = new Hashtable(20); 128 permStorage = new Hashtable(50); 129 groupRoles = new Vector(); 130 setHasLoggedIn(Boolean.FALSE); 131 132 if (isNew) 133 { 134 setDisabled(false); 135 setUserName(username); 136 String uidNumber = new Long(System.currentTimeMillis()).toString(); 137 setUserId(uidNumber); 138 myAttrs = new BasicAttributes(); 139 myAttrs.put(ATTR_UID, username); 140 myAttrs.put(ATTR_UID_NUMBER, uidNumber); 141 Attribute oc = new BasicAttribute(ATTR_OBJECT_CLASS); 142 oc.add("jetspeeduser"); 143 myAttrs.put(oc); 144 } 145 else 146 { 147 myAttrs = JetspeedLDAP.read(ldapurl); 148 fillObject(myAttrs); 149 } 150 } 151 152 public LDAPUser(LDAPURL ldapurl) 153 { 154 fillObject(JetspeedLDAP.read(ldapurl)); 155 } 156 157 public LDAPUser(Attributes attributes) 158 { 159 fillObject(attributes); 160 } 161 162 private void fillObject(Attributes attributes) 163 { 164 tempStorage = new Hashtable(20); 165 permStorage = new Hashtable(50); 166 setHasLoggedIn(Boolean.FALSE); 167 168 myAttrs = attributes; 169 170 try 171 { 172 setPermStorage(deserializePerm(getutil(ATTR_OBJECT_DATA, attributes))); 173 } 174 catch (Exception e) 175 { 176 logger.error("fillobject()" , e); 177 } 178 179 setUserName(getutil(ATTR_UID, attributes)); 180 setUserId(getutil(ATTR_UID_NUMBER, attributes)); 181 setEmail(getutil(ATTR_MAIL, attributes)); 182 setFirstName(getutil(ATTR_GIVEN_NAME, attributes)); 183 setLastName(getutil(ATTR_SN, attributes)); 184 setConfirmed(getutil(ATTR_CONFIRMED, attributes)); 186 187 setLastLogin(parseDate(getutil(ATTR_LAST_LOGIN_DATE, attributes))); 188 lastAccessDate = parseDate(getutil(ATTR_LAST_MODIFIED_DATE, attributes)); 189 setCreateDate(parseDate(getutil(ATTR_CREATION_DATE, attributes))); 190 String temp = getutil(ATTR_DISABLED, attributes); 191 if (temp != null && temp.equals("TRUE")) setDisabled(true); 192 else setDisabled(false); 193 194 try 195 { 196 setPassword(new String ((byte[]) attributes.get(ATTR_USER_PASSWORD).getAll().nextElement())); 197 } 198 catch (Exception e) 199 { 200 logger.error("fillobject() could not set password" , e); 201 } 202 203 this.groupRoles = getutil( ATTR_USER_GROUP_ROLE, attributes, true ); 204 ldapurl = JetspeedLDAP.buildURL(ATTR_UID + "=" + getUserName() + "," + ORGANIZATIONAL_UNIT); 205 } 206 207 209 public void update(boolean create) 210 throws JetspeedSecurityException 211 { 212 removeutil("createTimeStamp", false); 213 removeutil("modifyTimeStamp", false); 214 215 setutil(ATTR_USER_PASSWORD,(String)getPerm(User.PASSWORD) ); 216 setutil(ATTR_MAIL,(String)getPerm(User.EMAIL)); 217 setutil(ATTR_CONFIRMED,(String)getPerm(User.CONFIRM_VALUE)); 218 setutil(ATTR_SN,(String)getPerm(User.LAST_NAME)); 219 setutil(ATTR_GIVEN_NAME,(String)getPerm(User.FIRST_NAME)); 220 setutil(ATTR_USER_GROUP_ROLE, this.getGroupRoles(), create); 221 setutil(ATTR_LAST_LOGIN_DATE, formatDate(getLastLogin())); 223 setutil(ATTR_LAST_MODIFIED_DATE, formatDate(getLastAccessDate())); 224 setutil(ATTR_CREATION_DATE, formatDate(getCreateDate())); 225 if (getDisabled() == true) setutil(ATTR_DISABLED, "TRUE"); 226 else setutil(ATTR_DISABLED, "FALSE"); 227 228 try 229 { 230 setutil(ATTR_OBJECT_DATA, serializePerm(permStorage)); 231 } 232 catch (Exception e) 233 { 234 logger.warn("Could not serialize object data!" , e); 235 } 236 237 if (create) 238 { 239 ldapurl = JetspeedLDAP.buildURL("uid=" + (String)getPerm(User.USERNAME) + ",ou=users"); 240 setutil(ATTR_UID, (String)getPerm(User.USERNAME)); 241 if (JetspeedLDAP.addEntry(ldapurl, myAttrs) == false) throw new UserException("Could not insert user data to LDAP!"); 242 } 243 else if (JetspeedLDAP.exists(ldapurl)) 244 { 245 JetspeedLDAP.deleteAttrs(ldapurl, rmAttrs); 246 if (JetspeedLDAP.updateEntry(ldapurl, myAttrs) == false) throw new UserException("Could not update user data to LDAP!"); 247 } 248 } 249 250 252 public Vector getGroupRoles() 253 { 254 return this.groupRoles; 255 } 256 257 public void addGroupRole(String groupName, String roleName) 258 { 259 groupRoles.add(groupName + "," + roleName); 260 } 261 262 public void removeGroup(String groupName) 263 { 264 for (Enumeration enum = groupRoles.elements();enum.hasMoreElements();) 265 { 266 String groupRoleStr = (String)enum.nextElement(); 267 if (groupRoleStr.startsWith(groupName + ",")) 268 { 269 groupRoles.remove(groupRoleStr); 270 } 271 } 272 } 273 274 public void removeGroupRole(String groupName, String roleName) 275 { 276 for (Enumeration enum = groupRoles.elements(); enum.hasMoreElements();) 277 { 278 String groupRoleStr = (String)enum.nextElement(); 279 if (groupRoleStr.equalsIgnoreCase(groupName + "," + roleName)) 280 { 281 groupRoles.remove(groupRoleStr); 282 } 283 } 284 } 285 286 292 public String getUserId() 293 { 294 String tmp = null; 295 296 try 297 { 298 tmp = (String) getPerm (JetspeedUser.USER_ID); 299 if ( tmp.length() == 0 ) 300 { 301 tmp = null; 302 } 303 } 304 catch (Exception e) 305 { 306 logger.error("getUserId():" , e); 307 } 308 return tmp; 309 } 310 311 public void setUserId(String id) 312 { 313 if (getUserId() == null) 314 { 315 setPerm(JetspeedUser.USER_ID, id); 316 } 317 } 318 319 324 public int getAccessCounterForSession() 325 { 326 try 327 { 328 return ((Integer) getTemp(User.SESSION_ACCESS_COUNTER)).intValue(); 329 } 330 catch (Exception e) 331 { 332 logger.error("getAccessCounterForSession():" , e); 333 return 0; 334 } 335 } 336 337 342 public int getAccessCounter() 343 { 344 try 345 { 346 return ((Integer) getPerm(User.ACCESS_COUNTER)).intValue(); 347 } 348 catch (Exception e) 349 { 350 logger.error("getAccessCounter():" , e); 351 return 0; 352 353 } 354 } 355 356 362 public java.util.Date getCreateDate() 363 { 364 return createDate; 365 } 366 367 373 public java.util.Date getLastAccessDate() 374 { 375 if (lastAccessDate == null) 376 { 377 setLastAccessDate(); 378 } 379 return lastAccessDate; 380 } 381 382 388 public java.util.Date getLastLogin() 389 { 390 return (java.util.Date) getPerm(User.LAST_LOGIN); 391 } 392 393 398 public String getPassword() 399 { 400 return (String) getPerm(User.PASSWORD); 401 } 402 403 409 public Object getPerm(String name) 410 { 411 return permStorage.get(name); 412 } 413 414 422 public Object getPerm(String name, Object def) 423 { 424 try 425 { 426 Object val = permStorage.get (name); 427 428 return (val == null ? def : val); 429 } 430 catch (Exception e) 431 { 432 logger.error("getPerm():" , e); 433 return def; 434 } 435 } 436 437 443 public Hashtable getPermStorage() 444 { 445 if (this.permStorage == null) 446 { 447 this.permStorage = new Hashtable(50); 448 } 449 return this.permStorage; 450 } 451 452 458 public Object getTemp(String name) 459 { 460 return tempStorage.get(name); 461 } 462 463 471 public Object getTemp(String name, Object def) 472 { 473 Object val; 474 try 475 { 476 val = tempStorage.get(name); 477 if (val == null) 478 { 479 val = def; 480 } 481 } 482 catch (Exception e) 483 { 484 logger.error("getTemp():" , e); 485 val = def; 486 } 487 return val; 488 489 } 490 491 497 public String getUserName() 498 { 499 String tmp = null; 500 try 501 { 502 tmp = (String) getPerm (User.USERNAME); 503 if ( tmp.length() == 0 ) 504 { 505 tmp = null; 506 } 507 } 508 catch (Exception e) 509 { 510 logger.error("getUserName():" , e); 511 } 512 513 return tmp; 514 } 515 516 522 public String getFirstName() 523 { 524 String tmp = null; 525 526 try 527 { 528 tmp = (String) getPerm (User.FIRST_NAME); 529 if (tmp.length() == 0) 530 { 531 tmp = null; 532 } 533 } 534 catch (Exception e) 535 { 536 logger.error("getFirstName():" , e); 537 } 538 return tmp; 539 } 540 541 547 public String getLastName() 548 { 549 String tmp = null; 550 551 try 552 { 553 tmp = (String) getPerm (User.LAST_NAME); 554 if (tmp.length() == 0) tmp = null; 555 } 556 catch (Exception e) 557 { 558 logger.error("getLastName():" , e); 559 } 560 561 return tmp; 562 } 563 564 570 public boolean hasLoggedIn() 571 { 572 573 Boolean loggedIn = getHasLoggedIn(); 574 return (loggedIn != null && loggedIn.booleanValue()); 575 } 576 577 583 public String getEmail() 584 { 585 return (String)getPerm(User.EMAIL); 586 } 587 588 591 public void incrementAccessCounter() 592 { 593 setAccessCounter(getAccessCounter() + 1); 594 } 595 596 599 public void incrementAccessCounterForSession() 600 { 601 setAccessCounterForSession(getAccessCounterForSession() + 1); 602 } 603 604 610 public Object removeTemp(String name) 611 { 612 return tempStorage.remove(name); 613 } 614 615 620 public void setAccessCounter(int cnt) 621 { 622 setPerm(User.ACCESS_COUNTER, new Integer(cnt)); 623 } 624 625 631 public void setAccessCounterForSession(int cnt) 632 { 633 setTemp(User.SESSION_ACCESS_COUNTER, new Integer(cnt)); 634 } 635 636 640 public void setLastAccessDate() 641 { 642 lastAccessDate = new java.util.Date(); 643 } 644 645 651 public void setCreateDate(java.util.Date date) 652 { 653 createDate = date; 654 } 655 656 661 public void setLastLogin(java.util.Date date) 662 { 663 setPerm(User.LAST_LOGIN, date); 664 } 665 666 671 public void setPassword(String password) 672 { 673 setPerm(User.PASSWORD, password); 674 } 675 676 685 public void setPerm(String name, Object value) 686 { 687 ObjectUtils.safeAddToHashtable(getPermStorage(), name, value); 688 } 689 690 696 public void setPermStorage(Hashtable stuff) 697 { 698 this.permStorage = stuff; 699 } 700 701 707 public Hashtable getTempStorage() 708 { 709 if (this.tempStorage == null) 710 { 711 this.tempStorage = new Hashtable(20); 712 } 713 return this.tempStorage; 714 } 715 716 722 public void setTempStorage(Hashtable storage) 723 { 724 this.tempStorage = storage; 725 } 726 727 735 private Boolean getHasLoggedIn() 736 { 737 return (Boolean) getTemp (User.HAS_LOGGED_IN); 738 } 739 740 746 public void setHasLoggedIn (Boolean value) 747 { 748 setTemp (User.HAS_LOGGED_IN, value); 749 } 750 751 760 public void setTemp(String name, Object value) 761 { 762 ObjectUtils.safeAddToHashtable(tempStorage, name, value); 763 } 764 765 770 public void setUserName(String username) 771 { 772 setPerm (User.USERNAME, username); 773 } 774 775 780 public void setFirstName(String firstName) 781 { 782 setPerm(User.FIRST_NAME, firstName); 783 } 784 785 790 public void setLastName(String lastName) 791 { 792 setPerm(User.LAST_NAME, lastName); 793 } 794 795 800 public void setEmail(String address) 801 { 802 setPerm(User.EMAIL, address); 803 } 804 805 813 public boolean isConfirmed() 814 { 815 String value = getConfirmed(); 816 return (value != null && value.equals(User.CONFIRM_DATA)); 817 } 818 819 825 public void setConfirmed(String value) 826 { 827 String val = ""; 828 if (value != null) 829 { 830 val = value; 831 } 832 setPerm(User.CONFIRM_VALUE, val); 833 } 834 835 840 public String getConfirmed() 841 { 842 return (String)getPerm(User.CONFIRM_VALUE); 843 } 844 845 850 public void updateLastLogin() 851 throws Exception 852 { 853 setPerm( User.LAST_LOGIN, new java.util.Date() ); 854 } 855 856 862 public void valueBound(HttpSessionBindingEvent hsbe) 863 { 864 } 865 866 872 public void valueUnbound(HttpSessionBindingEvent hsbe) 873 { 874 try 875 { 876 java.util.Date now = new java.util.Date(); 877 878 if (this.hasLoggedIn()) 879 { 880 if ( JetspeedResources.getBoolean("automatic.logout.save", false) ) 881 { 882 JetspeedUserManagement.saveUser(this); 883 } 884 885 JetspeedAuthentication.logout(); 886 } 887 } 888 catch ( Exception e ) 889 { 890 logger.error("LDAPUser.valueUnbound(): " + e.getMessage(), e); 891 892 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); 896 e.printStackTrace(new PrintWriter(ostr,true)); 897 String stackTrace = ostr.toString(); 898 System.out.println(stackTrace); 899 } 900 } 901 902 905 public void save() 906 throws Exception 907 { 908 if (this.isNew()) 909 { 910 JetspeedUserManagement.saveUser(this); 911 } 912 else 913 { 914 JetspeedUserManagement.addUser(this); 915 } 916 } 917 918 923 public boolean getDisabled() 924 { 925 boolean disabled = false; 926 927 try 928 { 929 String tmp = (String) getPerm (JetspeedUser.DISABLED); 930 if ( tmp != null && tmp.length() > 0 ) 931 { 932 if (tmp.equalsIgnoreCase("T")) 933 disabled = true; 934 } 935 } 936 catch (Exception e) 937 { 938 logger.error("getDisabled():" , e); 939 } 940 941 return disabled; 942 } 943 944 public void setDisabled(boolean disabled) 945 { 946 setPerm(JetspeedUser.DISABLED, (disabled) ? "T" : "F"); 947 } 948 949 public String getName() 950 { 951 return name; 952 } 953 954 public void setName(String name) 955 { 956 this.name = name; 957 } 958 959 960 public boolean isNew() 961 { 962 return isNew; 963 } 964 965 void setNew(boolean isNew) 966 { 967 this.isNew = isNew; 968 } 969 970 976 public Date getPasswordChanged() 977 { 978 return this.passwordChanged; 979 } 980 981 986 public void setPasswordChanged(Date value) 987 { 988 this.passwordChanged = value; 989 } 990 991 } 992 | Popular Tags |