1 64 65 package com.jcorporate.expresso.services.dbobj; 66 67 import com.jcorporate.expresso.core.cache.Cache; 68 import com.jcorporate.expresso.core.cache.CacheException; 69 import com.jcorporate.expresso.core.cache.CacheManager; 70 import com.jcorporate.expresso.core.cache.CacheSystem; 71 import com.jcorporate.expresso.core.cache.CachedObject; 72 import com.jcorporate.expresso.core.controller.ControllerRequest; 73 import com.jcorporate.expresso.core.db.DBConnection; 74 import com.jcorporate.expresso.core.db.DBException; 75 import com.jcorporate.expresso.core.dbobj.DBField; 76 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 77 import com.jcorporate.expresso.core.dbobj.ValidValue; 78 import com.jcorporate.expresso.core.logging.LogException; 79 import com.jcorporate.expresso.core.misc.Base64; 80 import com.jcorporate.expresso.core.misc.ConfigManager; 81 import com.jcorporate.expresso.core.misc.ConfigurationException; 82 import com.jcorporate.expresso.core.misc.DateTime; 83 import com.jcorporate.expresso.core.misc.EMailSender; 84 import com.jcorporate.expresso.core.misc.EventHandler; 85 import com.jcorporate.expresso.core.misc.StringUtil; 86 import com.jcorporate.expresso.core.security.CryptoManager; 87 import com.jcorporate.expresso.core.security.User; 88 import com.jcorporate.expresso.core.security.UserInfo; 89 import com.jcorporate.expresso.kernel.exception.ChainedException; 90 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 91 import org.apache.log4j.Logger; 92 93 import java.util.Date ; 94 import java.util.Enumeration ; 95 import java.util.Hashtable ; 96 import java.util.Iterator ; 97 import java.util.Vector ; 98 99 100 106 public class DefaultUserInfo 107 extends SecurityDBObject 108 implements UserInfo { 109 110 113 public static final String THIS_CLASS = DefaultUserInfo.class.getName(); 114 115 118 public static final String NAME_2_ID_CACHE_NAME = THIS_CLASS + ".nameToId"; 119 120 121 124 public static final String EXPUID = "ExpUid"; 125 126 129 public static final String LOGIN = "LoginName"; 130 131 134 public static final String PASSWORD = "Passwd"; 135 136 139 public static final String EMAIL_ADDRESS = "EMail"; 140 141 144 public static final String REGISTRATION_DOMAIN = "RegDomId"; 145 146 149 public static final String ACCOUNT_STATUS = "AccountStatus"; 150 151 154 public static final String IS_REG_COMPLETE = "RegComplete"; 155 156 159 public static final String CREATED_DATE = "CreateDate"; 160 161 164 public static final String UPDATED_DATE = "UpdateDate"; 165 166 169 public static final String VALIDATION_CODE = "EmailValCode"; 170 171 174 public static final String FULL_NAME = "UserName"; 175 176 177 180 public static final int FULL_NAME_LENGTH = 80; 181 182 185 public static final int EMAIL_ADDRESS_LENGTH = 80; 186 187 190 public static final int LOGIN_LENGTH = 30; 191 192 195 public static final int PASSWORD_LENGTH = 30; 196 197 200 public static final int VALIDATION_CODE_LENGTH = 30; 201 202 205 private static Logger log = Logger.getLogger(DefaultUserInfo.class); 206 207 210 private static final boolean IS_UPDATE = true; 211 private static final boolean IS_ADD = false; 212 213 219 public void setUid(String uid) 220 throws DBException { 221 StringUtil.assertNotBlank(uid, "Can't set a blank UID"); 222 setField(EXPUID, uid); 223 } 224 225 226 232 public void setUid(int uid) 233 throws DBException { 234 setField(EXPUID, uid); 235 } 236 237 240 public DefaultUserInfo() 241 throws DBException { 242 } 243 244 245 251 public DefaultUserInfo(int uid) 252 throws DBException { 253 super(uid); 254 } 255 256 261 public DefaultUserInfo(DBConnection localConnection) throws DBException { 262 if (localConnection != null) { 263 setConnection(localConnection); 264 } 265 } 266 267 273 public DefaultUserInfo(ControllerRequest request) 274 throws DBException { 275 super(request); 276 } 277 278 283 public void add() 284 throws DBException { 285 286 checkEmailAddrAlreadyUsed(IS_ADD); 287 288 if (getField(FULL_NAME).equals("")) { 290 setField(FULL_NAME, getField(LOGIN)); 291 } 292 if (getField(ACCOUNT_STATUS).equals("")) { 295 setField(ACCOUNT_STATUS, "I"); 296 } 297 if (getRegistrationDomain().equals("")) { 299 setRegistrationDomain("default"); 300 } 301 302 RegistrationDomain rd = new RegistrationDomain(); 303 rd.setDataContext(getDataContext()); 304 rd.setField("Name", getRegistrationDomain()); 305 306 if (!rd.find()) { 307 throw new DBException("Registration domain \"" + 308 getRegistrationDomain() + 309 "\" has not been defined"); 310 } 311 312 setField(REGISTRATION_DOMAIN, rd.getField(REGISTRATION_DOMAIN)); 313 314 if (getField(IS_REG_COMPLETE).equals("")) { 316 setField(IS_REG_COMPLETE, "N"); 317 } 318 if (getField(CREATED_DATE).equals("")) { 320 setField(CREATED_DATE, DateTime.getDateTimeForDB(this.getDataContext())); 321 } 322 if (getField(UPDATED_DATE).equals("")) { 324 setField(UPDATED_DATE, getField(CREATED_DATE)); 325 } 326 327 super.add(); 328 329 User user = new User(); 330 user.setDataContext(this.getDataContext()); 331 user.addNotify(this); 332 333 } 334 335 336 341 protected void checkAllRefs() 342 throws DBException { 343 checkRef(REGISTRATION_DOMAIN, new RegistrationDomain(), 344 "Invalid " + getString(getMetaData().getDescription(REGISTRATION_DOMAIN))); 345 } 346 347 348 353 public void delete() 354 throws DBException { 355 356 find(); 359 User user = new User(); 360 user.setDataContext(this.getDataContext()); 361 user.deleteNotify(this); super.delete(); 364 365 366 } 367 368 369 375 public String getAccountStatus() 376 throws DBException { 377 return getField(ACCOUNT_STATUS); 378 } 379 380 381 387 public Vector getAllUsers() 388 throws DBException { 389 return new Vector (searchAndRetrieveList(LOGIN)); 390 } 391 392 398 public String getCreateDate() 399 throws DBException { 400 return getField(CREATED_DATE); 401 } 402 403 404 410 public String getEmail() 411 throws DBException { 412 return getField(EMAIL_ADDRESS); 413 } 414 415 421 public String getEmailAuthCode() 422 throws DBException { 423 Date createDate = getFieldDate(CREATED_DATE); 424 long dateLong = createDate.getTime(); 425 long emailAuthCode = Math.round(dateLong * 1.71); 426 427 return Long.toString(emailAuthCode); 428 } 429 430 431 436 public String getEmailValCode() 437 throws DBException { 438 return getField(VALIDATION_CODE); 439 } 440 441 442 448 public Vector getGroups() 449 throws DBException { 450 451 CacheSystem cs = CacheManager.getCacheSystem(this.getDataContext()); 455 String cacheName = THIS_CLASS + ".groups"; 456 String key = Integer.toString(this.getUid()); 457 Cache cache = cs.getCache(cacheName); 458 if (cache == null) { 459 try { 460 cache = cs.createCache(cacheName, false, 200); 461 cs.addListener(cacheName, 462 com.jcorporate.expresso.services.dbobj.GroupMembers.class.getName()); 463 } catch (CacheException ex) { 464 log.error("Error creating cache: " + cacheName); 465 } 466 } 467 468 if (cache != null) { 469 CachedObject c = (CachedObject) cache.getItem(key); 470 if (c != null) { 471 return (Vector ) c.getValue(); 472 } 473 } 474 475 Vector myGroups = new Vector (3); 476 477 478 GroupMembers memberList = new GroupMembers(); 479 memberList.setDataContext(getDataContext()); 480 memberList.setField(GroupMembers.EXPUID, getUid()); 481 482 483 if (log.isDebugEnabled()) { 484 log.debug("Getting groups for uid " + getUid() + ", login name " + 485 getLoginName()); 486 } 487 for (Iterator e = memberList.searchAndRetrieveList().iterator(); 488 e.hasNext();) { 489 GroupMembers oneMember = null; 490 oneMember = (GroupMembers) e.next(); 491 myGroups.addElement(oneMember.getField("GroupName")); 492 493 if (log.isDebugEnabled()) { 494 log.debug("" + getUid() + " is a Member of group " + 495 oneMember.getField("GroupName")); 496 } 497 } 498 499 507 508 myGroups.addElement(UserGroup.UNKNOWN_USERS_GROUP); 509 510 511 if (log.isDebugEnabled()) { 512 log.debug("" + getUid() + " is a member of " + myGroups.size() + 513 " groups"); 514 } 515 516 if (cache != null) { 517 CachedObject co = new CachedObject(key, myGroups); 518 try { 519 cs.addItem(cacheName, co, 1000 * 60 * 10); } catch (CacheException ex) { 521 log.error("Error adding group list to cache", ex); 522 } 523 } 524 525 return myGroups; 526 } 527 528 529 532 public String getLoginName() 533 throws DBException { 534 return getField(LOGIN); 535 } 536 537 538 public String getPassword() 539 throws DBException { 540 return getField(PASSWORD); 541 } 542 543 544 public boolean getRegComplete() 545 throws DBException { 546 String statusString = getField(IS_REG_COMPLETE); 547 boolean status = false; 548 549 if (statusString.equals("Y")) { 550 status = true; 551 } 552 553 return status; 554 } 555 556 557 public String getRegistrationDomain() 558 throws DBException { 559 String domain = "default"; 560 RegistrationDomain rd = new RegistrationDomain(); 561 rd.setDataContext(getDataContext()); 562 rd.setField(REGISTRATION_DOMAIN, getField(REGISTRATION_DOMAIN)); 563 564 if (rd.find()) { 565 domain = rd.getField("Name"); 566 } 567 568 return domain; 569 } 570 571 572 public int getUid() 573 throws DBException { 574 return getFieldInt(EXPUID); 575 } 576 577 578 public String getUpdateDate() 579 throws DBException { 580 return getField(UPDATED_DATE); 581 } 582 583 584 590 public String getUserName() 591 throws DBException { 592 return getField(FULL_NAME); 593 } 594 595 598 public String getFullName() 599 throws DBException { 600 return getField(FULL_NAME); 601 } 602 603 604 612 public synchronized Vector getValidValues(String fieldName) 613 throws DBException { 614 if (ACCOUNT_STATUS.equals(fieldName)) { 615 Vector values = new Vector (); 616 values.addElement(new ValidValue("A", "Active")); 617 values.addElement(new ValidValue("I", 618 "Inactive Until Email Confirmation")); 619 values.addElement(new ValidValue("D", "Disabled")); 620 values.addElement(new ValidValue("W", "Waiting For Approval")); 621 values.addElement(new ValidValue("X", 622 "Registration Denied By Administrator")); 623 624 return values; 625 } else if (fieldName.equals(IS_REG_COMPLETE)) { 626 Vector rv = new Vector (2); 627 ValidValue rvv = new ValidValue("Y", "Complete"); 628 rv.addElement(rvv); 629 rvv = new ValidValue("N", "Incomplete"); 630 rv.addElement(rvv); 631 632 return rv; 633 } 634 635 return super.getValidValues(fieldName); 637 } 638 639 public Vector getValues() throws DBException { 640 return getValuesDefault(EXPUID, LOGIN); 642 } 643 644 656 public String hashEncodePassword(String plaintext) throws DBException { 657 if (plaintext == null) { 658 throw new DBException("Password Must not be NULL"); 659 } 660 if (plaintext.length() == 0) { 661 return plaintext; 662 } 663 try { 664 return Base64.encode(CryptoManager.getInstance().getStringHash().produceHash(plaintext.getBytes())); 665 } catch (Exception ex) { 666 throw new DBException("Error hashing Password:" + 667 " You may not have installed the" + 668 " Cryptography Extensions Properly:", ex); 669 } 670 } 671 672 673 680 public void notify(String subject, String message) 681 throws DBException, LogException { 682 log.info("Notifying user " + getField(EXPUID) + " of " + subject); 683 684 String sendToUser = getField(EMAIL_ADDRESS); 685 686 try { 687 EMailSender ems = new EMailSender(); 688 ems.setDBName(getDataContext()); 689 ems.send(sendToUser, subject, message); 690 } catch (Exception e) { 691 throw new DBException("Uncaught exception sending e-mail", e); 692 } 693 } 694 695 696 704 private boolean okNumber(byte x) { 705 if ((x >= 65) && (x <= 90)) { 706 return true; 707 } 708 if ((x >= 48) && (x <= 57)) { 709 return true; 710 } 711 if ((x >= 97) && (x <= 122)) { 712 return true; 713 } 714 715 return false; 716 } 717 718 730 public boolean passwordEquals(String tryPassword) 731 throws DBException { 732 if (tryPassword == null) { 733 throw new DBException("tryPassword Must not be NULL"); 734 } 735 736 String fieldData = getPassword(); 737 String trieduser = getLoginName(); 738 739 if (log.isDebugEnabled()) { 740 log.debug("Trying user " + trieduser + ", password '" + 741 tryPassword + "' - field data is '" + fieldData + "'"); 742 } 743 744 String triedhash = hashEncodePassword(tryPassword); 745 746 if (log.isDebugEnabled()) { 747 log.debug("Password hashed is " + triedhash + "'"); 748 } 749 if (triedhash.equals(fieldData)) { 750 return true; 751 } else { 752 if (log.isDebugEnabled()) { 753 log.debug("Hash doesn't equal data - trying plaintext"); 754 } 755 if (tryPassword.equals(fieldData) && fieldData.length() < 20) { 759 if (log.isDebugEnabled()) { 760 log.debug("Password matches in plain text - hashing & writing to DB"); 761 } 762 763 setPassword(hashEncodePassword(fieldData)); 764 update(); 765 766 return true; 767 } else { 768 769 if (log.isDebugEnabled()) { 771 log.debug("Password doesn't equal plain text either ('" + 772 tryPassword + "' <> '" + fieldData + 773 "') or field data is over 20 characters"); 774 } 775 776 return false; 777 } 778 } 779 } 780 781 782 786 public void postLogin() 787 throws DBException, LogException { 788 UserGroup oneGroup = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 789 oneGroup.setDataContext(getDataContext()); 790 791 String theEvent = null; 792 Hashtable allEvents = new Hashtable (1); 793 String oneGroupName = null; 794 795 for (Enumeration gl = getGroups().elements(); gl.hasMoreElements();) { 796 oneGroupName = (String ) gl.nextElement(); 797 oneGroup.clear(); 798 oneGroup.setField("GroupName", oneGroupName); 799 800 if (oneGroup.find()) { 801 theEvent = oneGroup.getField("LoginEvent"); 802 803 if (!theEvent.equals("")) { 804 allEvents.put(theEvent, oneGroup.getField("GroupName")); 805 } 806 } 807 808 } 809 810 811 812 String theMessage = null; 813 814 if (allEvents.size() > 0) { 815 for (Enumeration el = allEvents.keys(); el.hasMoreElements();) { 816 theEvent = (String ) el.nextElement(); 817 theMessage = ("User " + getLoginName() + " (" + 818 getFullName() + ") who is a member " + 819 " of group " + 820 (String ) allEvents.get(theEvent) + 821 " has just logged in."); 822 EventHandler.Event(getDataContext(), theEvent, theMessage, true); 823 } 824 825 } 826 827 } 828 829 830 833 public String randomPassword() { 834 int passwordLength; 835 int iterations = 0; 836 837 String propValue = null; 841 842 try { 843 propValue = StringUtil.notNull(ConfigManager.getContext(getDataContext()).getMinPasswordSize()); 844 } catch (ConfigurationException ce) { 845 throw new IllegalArgumentException (ce.getMessage()); 846 } 847 if (!propValue.equals("")) { 848 try { 849 passwordLength = Integer.parseInt(propValue, 10); 850 } catch (NumberFormatException ex) { 851 852 passwordLength = 6; 854 } 855 } else { 856 passwordLength = 6; 857 } 858 FastStringBuffer newPassword = new FastStringBuffer(passwordLength); 859 860 byte[] possibleNumbers; 869 870 try { 871 possibleNumbers = CryptoManager.getInstance().getRandomGenerator().getRandomBytes(200); 872 } catch (ChainedException e) { 873 possibleNumbers = new byte[200]; 874 875 Logger.getLogger("com.jcorporate.expresso.core.security.").error("Random Password", e); 878 879 for (int i = 0; i < 200; i++) { 880 possibleNumbers[i] = (byte) (Math.random() * 122); 881 } 882 } 883 while ((newPassword.length() < passwordLength) && (iterations < 200)) { 884 iterations++; 885 886 if (okNumber(possibleNumbers[iterations])) { 888 newPassword.append((char) possibleNumbers[iterations]); 889 } 890 } 891 892 return newPassword.toString(); 893 } 894 895 900 public void sendAuthEmail() 901 throws DBException { 902 try { 903 String dbContext = getDataContext(); 904 String authURL = Setup.getValue(dbContext, "EmailValidateURL"); 905 String emailAuthCode = getEmailAuthCode(); 906 this.setField(VALIDATION_CODE, emailAuthCode); 907 this.update(); 908 authURL = authURL + "?UserName=" + getField(LOGIN) + "&db=" + 909 getDataContext() + "&EmailAuthCode=" + emailAuthCode; 910 911 String subject = "New Account Validation - Please Respond"; 912 FastStringBuffer sb = new FastStringBuffer(512); 913 914 if (!"".equals(getFullName())) { 915 sb.append("Dear " + getFullName() + ","); 916 } 917 918 sb.append("\n"); 919 sb.append("\n"); 920 sb.append("Thank you for registering"); 921 922 String companyName = Setup.getValue(dbContext, "CompanyName"); 923 String homePageURL = Setup.getValue(dbContext, "HomePageURL"); 924 925 if (companyName != null && !"".equals(companyName)) { 926 sb.append(" with " + companyName); 927 } 928 if (homePageURL != null && !"".equals(homePageURL)) { 929 sb.append(" at " + homePageURL); 930 } 931 932 sb.append("!"); 933 sb.append("\n"); 934 sb.append("\n"); 935 sb.append("Your account has been successfully created. " + 936 "The final step in the"); 937 sb.append("\n"); 938 sb.append("registration process is to simply follow the link " + 939 "below to let us"); 940 sb.append("\n"); 941 sb.append("know that you received this message. You must follow " + 942 "the link below"); 943 sb.append("\n"); 944 sb.append("before your account will be activated."); 945 sb.append("\n"); 946 sb.append("\n"); 947 sb.append("NOTE: If you did not register, you may safely"); 948 sb.append("\n"); 949 sb.append("ignore this message."); 950 sb.append("\n"); 951 sb.append("\n"); 952 sb.append("In many email clients, you may simply click on the " + 953 "link below to"); 954 sb.append("\n"); 955 sb.append("complete the registration process. If your email " + 956 "client does not"); 957 sb.append("\n"); 958 sb.append("support this, cut-and-paste the link below into your " + 959 "web browser's"); 960 sb.append("\n"); 961 sb.append("\"Location\" window:"); 962 sb.append("\n"); 963 sb.append("\n"); 964 sb.append(authURL); 965 sb.append("\n"); 966 sb.append("\n"); 967 968 if (companyName != null && !"".equals(companyName)) { 969 sb.append("Thank you from all of us at " + companyName + "."); 970 } 971 972 sb.append("\n"); 973 974 if (companyName != null && !"".equals(homePageURL)) { 975 sb.append(homePageURL); 976 } 977 978 sb.append("\n"); 979 980 String message = sb.toString(); 981 notify(subject, message); 982 } catch (Exception e) { 983 throw new DBException("Error in sending account verification message to " + 984 getField(FULL_NAME) + " at " + getField(EMAIL_ADDRESS) + ": " + 985 e.toString()); 986 } 987 } 988 989 990 998 public void sendFileTo(String subject, String message, Vector fileNames) 999 throws DBException, LogException { 1000 log.info("Sending " + fileNames.size() + " files via e-mail to " + 1001 getField(LOGIN)); 1002 1003 String sendToUser = getField(EMAIL_ADDRESS); 1004 1005 try { EMailSender ems = new EMailSender(); 1007 ems.setDBName(getDataContext()); 1008 ems.addFileAttachments(fileNames); 1009 ems.send(sendToUser, subject, message); 1010 } catch (Exception e) { 1011 throw new DBException("Error sending e-mail", e); 1012 } 1013 } 1014 1015 1016 1027 public void sendFollowUpEmail() 1028 throws DBException { 1029 try { 1030 String subject = "New Registration Complete - Welcome!"; 1031 String dbContext = getDataContext(); 1032 1033 String password = this.randomPassword(); 1036 this.setPassword(password); 1037 this.update(); 1038 1039 FastStringBuffer sb = new FastStringBuffer(512); 1040 1041 if (!"".equals(getFullName())) { 1042 sb.append("Dear " + getFullName() + ","); 1043 } 1044 1045 sb.append("\n"); 1046 sb.append("\n"); 1047 sb.append("Thank you for registering"); 1048 1049 String companyName = Setup.getValue(dbContext, "CompanyName"); 1050 String homePageURL = Setup.getValue(dbContext, "HomePageURL"); 1051 1052 if (companyName != null && !"".equals(companyName)) { 1053 sb.append(" with " + companyName); 1054 } 1055 if (homePageURL != null && !"".equals(homePageURL)) { 1056 sb.append(" at " + homePageURL); 1057 } 1058 1059 sb.append("!"); 1060 sb.append("\n"); 1061 sb.append("\n"); 1062 sb.append("Your account is now active. Below is the information " + 1063 "you will need to log in."); 1064 sb.append("\n"); 1065 sb.append("Please keep this information in a safe place.We hope " + 1066 "you enjoy the site and"); 1067 sb.append("\n"); 1068 sb.append("look forward to your participation."); 1069 sb.append("\n"); 1070 sb.append("\n"); 1071 sb.append("Login Name: " + getLoginName()); 1072 sb.append("\n"); 1073 sb.append("Password: " + password); 1074 sb.append("\n"); 1075 sb.append("\n"); 1076 1077 if (companyName != null && !"".equals(companyName)) { 1078 sb.append("Thank you from all of us at " + companyName + "."); 1079 } 1080 1081 sb.append("\n"); 1082 1083 if (companyName != null && !"".equals(homePageURL)) { 1084 sb.append(homePageURL); 1085 } 1086 1087 sb.append("\n"); 1088 1089 String message = sb.toString(); 1090 notify(subject, message); 1091 } catch (Exception e) { 1092 throw new DBException("Error in sending account verification follow up message to " + 1093 getLoginName() + " at " + getEmail() + ": " + 1094 e.toString()); 1095 } 1096 } 1097 1098 1099 public void setAccountStatus(String accountStatus) 1100 throws DBException { 1101 setField(ACCOUNT_STATUS, accountStatus); 1102 } 1103 1104 1105 public void setEmail(String email) 1106 throws DBException { 1107 setField(EMAIL_ADDRESS, email); 1108 } 1109 1110 1111 public void setEmailValCode(String code) 1112 throws DBException { 1113 setField(VALIDATION_CODE, code); 1114 } 1115 1116 1117 public void setLoginName(String loginName) 1118 throws DBException { 1119 setField(LOGIN, loginName); 1120 } 1121 1122 1123 public void setPassword(String password) 1124 throws DBException { 1125 setField(PASSWORD, password); 1126 } 1127 1128 1129 public void setRegComplete(boolean status) 1130 throws DBException { 1131 String statusString = "N"; 1132 1133 if (status) { 1134 statusString = "Y"; 1135 } 1136 1137 setField(IS_REG_COMPLETE, statusString); 1138 } 1139 1140 1141 public void setRegistrationDomain(String domain) 1142 throws DBException { 1143 RegistrationDomain rd = new RegistrationDomain(); 1144 rd.setDataContext(getDataContext()); 1145 rd.setField("Name", domain); 1146 1147 if (rd.find()) { 1148 setField(REGISTRATION_DOMAIN, rd.getField(REGISTRATION_DOMAIN)); 1149 } else { 1150 throw new DBException("Registration domain \"" + domain + 1151 "\" has not been defined."); 1152 } 1153 } 1154 1155 1156 1159 public void setupFields() 1160 throws DBException { 1161 setTargetTable("USERSTABLE"); 1162 setDescription("DBuser"); 1163 setCharset("ISO-8859-1"); 1164 addField(EXPUID, DBField.AUTOINC_TYPE, 0, false, "userId"); 1165 addField(LOGIN, DBField.CHAR_TYPE, LOGIN_LENGTH, false, "userLogin"); 1166 addField(PASSWORD, DBField.VARCHAR_TYPE, PASSWORD_LENGTH, true, "password"); 1167 addField(EMAIL_ADDRESS, DBField.VARCHAR_TYPE, EMAIL_ADDRESS_LENGTH, false, "email"); 1168 addField(REGISTRATION_DOMAIN, DBField.INTEGER_TYPE, 0, false, "RegDomainId"); 1169 addField(ACCOUNT_STATUS, DBField.CHAR_TYPE, 1, false, "isActive"); 1170 addField(IS_REG_COMPLETE, DBField.CHAR_TYPE, 1, false, "RegComplete"); 1171 addField(CREATED_DATE, DBField.DATETIME_TYPE, 0, false, "accountCreation"); 1172 addField(UPDATED_DATE, DBField.DATETIME_TYPE, 0, false, "accountUpdate"); 1173 addField(VALIDATION_CODE, DBField.VARCHAR_TYPE, VALIDATION_CODE_LENGTH, true, "validationCode"); 1174 addField(FULL_NAME, DBField.VARCHAR_TYPE, FULL_NAME_LENGTH, true, "userFullName"); 1175 1176 setStringFilter(LOGIN, "stripFilter"); 1178 1179 setStringFilter(FULL_NAME, "stripFilter"); 1181 1182 setStringFilter(EMAIL_ADDRESS, "stripFilter"); 1184 1185 setStringFilter(PASSWORD, "rawFilter"); 1187 1188 setStringFilter(FULL_NAME, "rawFilter"); 1190 1191 setStringFilter(VALIDATION_CODE, "stripFilter"); 1193 1194 addKey(EXPUID); 1195 setSecret(PASSWORD); 1196 setSecret(VALIDATION_CODE); 1197 setMultiValued(ACCOUNT_STATUS); 1198 setMultiValued(IS_REG_COMPLETE); 1199 setMultiValued(REGISTRATION_DOMAIN); 1200 setLookupObject(REGISTRATION_DOMAIN, RegistrationDomain.class.getName()); 1201 1202 setReadOnly(CREATED_DATE); 1203 setReadOnly(UPDATED_DATE); 1204 addIndex("LoginNames", LOGIN, true); 1205 addIndex("EMails", EMAIL_ADDRESS, false); 1206 addIndex("Acct_status_idx", ACCOUNT_STATUS, false); 1207 addDetail(GroupMembers.class.getName(), EXPUID, GroupMembers.EXPUID); 1208 } 1209 1210 1211 public void setUserName(String name) 1212 throws DBException { 1213 setField(FULL_NAME, name); 1214 } 1215 1216 1217 1222 public void update() 1223 throws DBException { 1224 setCheckZeroUpdate(false); 1225 1226 checkEmailAddrAlreadyUsed(IS_UPDATE); 1227 1228 if (getField(FULL_NAME).equals("")) { 1229 setField(FULL_NAME, getField(LOGIN)); 1230 } 1231 if (getField(ACCOUNT_STATUS).equals("")) { 1234 setField(ACCOUNT_STATUS, "I"); 1235 } 1236 if (getRegistrationDomain().equals("")) { 1238 setRegistrationDomain("default"); 1239 } 1240 1241 RegistrationDomain rd = new RegistrationDomain(); 1242 rd.setDataContext(getDataContext()); 1243 rd.setField("Name", getRegistrationDomain()); 1244 1245 if (!rd.find()) { 1246 throw new DBException("Regsitration domain \"" + 1247 getRegistrationDomain() + 1248 "\" has not been defined"); 1249 } 1250 1251 setField(REGISTRATION_DOMAIN, rd.getField(REGISTRATION_DOMAIN)); 1252 1253 if (getField(IS_REG_COMPLETE).equals("")) { 1255 setField(IS_REG_COMPLETE, "N"); 1256 } 1257 if (getField(CREATED_DATE).equals("")) { 1259 setField(CREATED_DATE, DateTime.getDateTimeForDB(this.getDataContext())); 1260 } 1261 1262 setField(UPDATED_DATE, DateTime.getDateTimeForDB(this.getDataContext())); 1264 1265 super.update(); 1266 1267 1268 User user = new User(); 1269 user.setDataContext(this.getDataContext()); 1270 user.updateNotify(this); 1271 1272 } 1273 1274 1279 private void checkEmailAddrAlreadyUsed(boolean isUpdate) throws DBException { 1280 1281 boolean useEmailAsLogin = false; 1283 1284 try { 1285 useEmailAsLogin = ConfigManager.getContext(getDataContext()).useEmailAsLogin(); 1286 } catch (ConfigurationException ce) { 1287 throw new DBException(ce); 1288 } 1289 1290 if (isUpdate) { 1291 String onlychanged = Setup.getValueUnrequired(getDataContext(), UPDATE_CHANGED_ONLY); 1293 if (StringUtil.toBoolean(onlychanged)) { 1294 if (getDataField(EMAIL_ADDRESS).isChanged() || (!useEmailAsLogin && getDataField(LOGIN).isChanged())) { 1297 } else { 1299 return; } 1301 } 1302 } 1303 1304 if (!getField(EMAIL_ADDRESS).equalsIgnoreCase("none")) { 1305 UserInfo anotherUser = new User().getUserInfo(); 1306 anotherUser.setDBName(getDataContext()); 1307 anotherUser.setEmail(getField(EMAIL_ADDRESS)); 1308 1309 boolean foundEmail = anotherUser.find(); 1310 if (foundEmail && isUpdate && (anotherUser.getUid() != getUid())) { 1311 throw new DBException("Another user with the same email address \"" + 1313 getField(EMAIL_ADDRESS) + "\" already exists"); 1314 } else if (foundEmail && !isUpdate) { 1315 throw new DBException("Another user with the same email address \"" + 1317 getField(EMAIL_ADDRESS) + "\" already exists"); 1318 1319 } 1320 } 1321 1322 if (useEmailAsLogin) { 1323 if (getField(LOGIN).equals("")) { 1324 setField(LOGIN, getField(EMAIL_ADDRESS)); 1325 } 1326 } else { 1327 1328 UserInfo anotherUser = new User().getUserInfo(); 1330 anotherUser.setDBName(getDataContext()); 1331 anotherUser.setLoginName(getField(LOGIN)); 1332 boolean foundLogin = anotherUser.find(); 1333 1334 if (!isUpdate && foundLogin) { 1335 throw new DBException("Another user with the same login \"" + 1336 getField(LOGIN) + 1337 "\" already exists"); 1338 } else if (isUpdate && foundLogin && (anotherUser.getUid() != getUid())) { 1339 throw new DBException("Another user with the same login \"" + 1340 getField(LOGIN) + 1341 "\" already exists"); 1342 } 1343 } 1344 } 1345 1346 1355 public boolean find() throws com.jcorporate.expresso.core.db.DBException { 1356 1357 if (this.getField(EXPUID).length() > 0) { 1359 boolean returnValue = super.find(); 1360 return returnValue; } 1362 1363 if (getLoginName().length() > 0) { 1365 1366 String keyString = getName2IdCacheKey(); 1367 try { 1368 Cache cache = getName2IdCache(); 1369 CachedObject c = (CachedObject) cache.getItem(keyString); 1370 if (c != null) { 1371 Integer cachedUID = (Integer ) c.getValue(); 1372 this.setUid(cachedUID.intValue()); 1373 try { 1374 this.retrieve(); 1375 1376 return true; 1378 } catch (com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException e) { 1379 log.error("Error retrieving user from uid found in cache", e); 1381 } 1383 } 1384 } catch (CacheException e) { 1385 log.error("Error retrieving from cache", e); 1386 } 1388 } 1389 1390 boolean returnValue = super.find(); 1392 if (returnValue) { 1393 cacheUser(); } 1395 return returnValue; 1396 1397 } 1398 1399 private void cacheUser() throws DBException { 1400 String keyString = getName2IdCacheKey(); 1401 try { 1402 Cache cache = getName2IdCache(); 1403 CachedObject c = (CachedObject) cache.getItem(keyString); 1404 if (c == null) { 1405 CachedObject co = new CachedObject(keyString, new Integer (this.getUid())); 1406 CacheSystem cs = CacheManager.getCacheSystem(this.getDataContext()); 1407 cs.addItem(NAME_2_ID_CACHE_NAME, co, 1000 * 60 * 10); } 1409 } catch (CacheException e) { 1410 log.error("Error adding to cache", e); 1411 } 1412 } 1413 1414 1417 private Cache getName2IdCache() throws CacheException { 1418 CacheSystem cs = CacheManager.getCacheSystem(this.getDataContext()); 1419 Cache cache = cs.getCache(NAME_2_ID_CACHE_NAME); 1420 if (cache == null) { 1421 cache = cs.createCache(NAME_2_ID_CACHE_NAME, false, 20); 1422 cs.addListener(NAME_2_ID_CACHE_NAME, this.getClass().getName()); 1423 } 1424 return cache; 1425 } 1426 1427 1431 private String getName2IdCacheKey() throws DBException { 1432 FastStringBuffer key = new FastStringBuffer(16); 1433 key.append(this.getDataContext()); 1434 key.append("."); 1435 key.append(this.getLoginName()); 1436 String keyString = key.toString(); 1437 return keyString; 1438 } 1439 1440 1447 public String getPrimaryGroup() 1448 throws DBException { 1449 return getPrimaryGroup(this); 1450 } 1451 1452 1460 public static String getPrimaryGroup(UserInfo user) throws DBException { 1461 UserGroup primary = null; 1462 1463 UserPreference pref = new UserPreference(); 1464 pref.setDataContext(user.getDataContext()); 1465 pref.setField(UserPreference.PREF_CODE, UserPreferenceDef.PRIMARY_GROUP_PREF); 1466 pref.setField(UserPreference.CLASS_NAME, UserPreferenceDef.ANY_CLASS); 1467 pref.setField(UserPreference.EXPUID, user.getUid()); 1468 if (!pref.find()) { 1469 1470 Vector groupNames = user.getGroups(); 1472 if (groupNames.size() > 0) { 1473 primary = new UserGroup(); 1474 primary.setDataContext(user.getDataContext()); 1475 primary.setField(UserGroup.GROUP_NAME_FIELD, 1476 (String ) groupNames.get(0)); 1477 if (!primary.find()) { 1478 primary = null; 1479 } 1480 } 1481 } else { 1482 String primStr = pref.getField(UserPreference.PREF_VALUE); 1484 primary = new UserGroup(); 1485 primary.setDataContext(user.getDataContext()); 1486 primary.setField(UserGroup.GROUP_NAME_FIELD, primStr); 1487 if (!primary.find()) { 1488 throw new DBException("cannot locate primary group: " 1490 + primStr + " which is specified for user: " 1491 + user.getLoginName()); 1492 } 1493 } 1494 1495 if (primary == null) { 1496 return null; 1497 } else { 1498 return primary.getField(UserGroup.GROUP_NAME_FIELD); 1499 } 1500 } 1501 1502 1509 public void setPrimaryGroup(UserGroup group) throws DBException { 1510 UserPreference pref = new UserPreference(); 1511 pref.setDataContext(this.getDataContext()); 1512 pref.setField(UserPreference.PREF_CODE, UserPreferenceDef.PRIMARY_GROUP_PREF); 1513 pref.setField(UserPreference.CLASS_NAME, UserPreferenceDef.ANY_CLASS); 1514 pref.setField(UserPreference.EXPUID, this.getUid()); 1515 pref.setField(UserPreference.PREF_VALUE, group.getGroupName()); 1516 pref.addOrUpdate(); 1517 } 1518} 1519 | Popular Tags |