1 package org.tigris.scarab.om; 2 3 48 49 import java.util.ArrayList ; 50 import java.util.Arrays ; 51 import java.util.Comparator ; 52 import java.util.List ; 53 import java.util.Locale ; 54 import java.util.Map ; 55 import java.util.Calendar ; 56 import java.util.Collections ; 57 import java.util.Set ; 58 import java.util.TreeSet ; 59 import java.util.Vector ; 60 61 import org.apache.commons.lang.RandomStringUtils; 62 import org.apache.fulcrum.security.entity.Role; 63 import org.apache.fulcrum.security.entity.User; 64 import org.apache.fulcrum.security.entity.Group; 65 import org.apache.fulcrum.security.TurbineSecurity; 66 import org.apache.fulcrum.security.impl.db.entity.TurbinePermissionPeer; 67 import org.apache.fulcrum.security.impl.db.entity.TurbineRolePermissionPeer; 68 import org.apache.fulcrum.security.impl.db.entity.TurbineRolePeer; 69 import org.apache.fulcrum.security.impl.db.entity.TurbineUserGroupRolePeer; 70 import org.apache.fulcrum.security.util.AccessControlList; 71 import org.apache.torque.TorqueException; 72 import org.apache.torque.util.Criteria; 73 74 import org.tigris.scarab.reports.ReportBridge; 75 import org.tigris.scarab.services.security.ScarabSecurity; 76 import org.tigris.scarab.services.cache.ScarabCache; 77 import org.tigris.scarab.util.ScarabException; 78 79 import org.apache.turbine.Turbine; 80 import org.apache.log4j.Logger; 81 82 83 92 public class ScarabUserImpl 93 extends BaseScarabUserImpl 94 implements ScarabUser 95 { 96 private static final Logger TORQUE_LOG = 97 Logger.getLogger("org.apache.torque"); 98 99 public static final String PASSWORD_EXPIRE = "PASSWORD_EXPIRE"; 100 101 private AbstractScarabUser internalUser; 102 103 107 private static final int UNIQUE_ID_MAX_LEN = 10; 108 109 112 public ScarabUserImpl() 113 { 114 super(); 115 116 125 internalUser = new AbstractScarabUser() 126 { 127 public Integer getUserId() 128 { 129 return getPrivateUserId(); 130 } 131 132 public String getEmail() 133 { 134 return getPrivateEmail(); 135 } 136 137 public String getFirstName() 138 { 139 return getPrivateFirstName(); 140 } 141 142 public String getLastName() 143 { 144 return getPrivateLastName(); 145 } 146 147 protected List getRModuleUserAttributes(Criteria crit) 148 throws TorqueException 149 { 150 return getPrivateRModuleUserAttributes(crit); 151 } 152 153 public boolean hasPermission(String perm, Module module) 154 { 155 return hasPrivatePermission(perm, module); 156 } 157 158 public List getModules() 159 throws Exception 160 { 161 return getModules(false); 162 } 163 164 public List getModules(boolean showDeletedModules) 165 throws Exception 166 { 167 List permList = ScarabSecurity.getAllPermissions(); 168 String [] perms = new String [permList.size()]; 169 perms = (String [])permList.toArray(perms); 170 171 Module[] modules = getPrivateModules(perms, showDeletedModules); 172 return (modules == null || modules.length == 0 173 ? Collections.EMPTY_LIST : Arrays.asList(modules)); 174 } 175 176 179 public Module[] getModules(String permission) 180 { 181 return getPrivateModules(permission); 182 } 183 184 protected void 185 deleteRModuleUserAttribute(RModuleUserAttribute rmua) 186 throws Exception 187 { 188 privateDeleteRModuleUserAttribute(rmua); 189 } 190 }; 191 } 192 193 private Integer getPrivateUserId() 197 { 198 return getUserId(); 199 } 200 private String getPrivateEmail() 201 { 202 return getEmail(); 203 } 204 private String getPrivateFirstName() 205 { 206 return getFirstName(); 207 } 208 private String getPrivateLastName() 209 { 210 return getLastName(); 211 } 212 public String getName() 213 { 214 return internalUser.getName(); 215 } 216 private List getPrivateRModuleUserAttributes(Criteria crit) 217 throws TorqueException 218 { 219 return getRModuleUserAttributes(crit); 220 } 221 private boolean hasPrivatePermission(String perm, Module module) 222 { 223 return hasPermission(perm, module); 224 } 225 private Module[] getPrivateModules(String permission) 226 { 227 String [] perms = {permission}; 228 return getModules(perms); 229 } 230 private Module[] getPrivateModules(String [] permissions, boolean showDeletedModules) 231 { 232 return getModules(permissions, showDeletedModules); 233 } 234 235 private void privateDeleteRModuleUserAttribute(RModuleUserAttribute rmua) 236 throws Exception 237 { 238 rmua.delete(this); 239 } 240 241 247 public static boolean checkConfirmationCode (String username, 248 String confirm) 249 { 250 if (confirm.equalsIgnoreCase(User.CONFIRM_DATA)) 252 { 253 return false; 254 } 255 256 try 257 { 258 Criteria criteria = new Criteria(); 259 criteria.add (ScarabUserImplPeer.getColumnName(User.USERNAME), 260 username); 261 criteria.add (ScarabUserImplPeer.getColumnName(User.CONFIRM_VALUE), 262 confirm); 263 criteria.setSingleRecord(true); 264 List result = ScarabUserImplPeer.doSelect(criteria); 265 if (result.size() > 0) 266 { 267 return true; 268 } 269 270 criteria = new Criteria(); 275 criteria.add (ScarabUserImplPeer.getColumnName(User.USERNAME), 276 username); 277 criteria.add (ScarabUserImplPeer.getColumnName(User.CONFIRM_VALUE), 278 User.CONFIRM_DATA); 279 criteria.setSingleRecord(true); 280 result = ScarabUserImplPeer.doSelect(criteria); 281 return (result.size() > 0); 282 } 283 catch (Exception e) 284 { 285 return false; 286 } 287 } 288 289 293 public static boolean confirmUser (String username) 294 { 295 try 296 { 297 User user = TurbineSecurity.getUser(username); 298 user.setConfirmed(User.CONFIRM_DATA); 299 TurbineSecurity.saveUser(user); 300 return true; 301 } 302 catch (Exception e) 303 { 304 return false; 305 } 306 } 307 308 313 public boolean hasPermission(String perm, Module module) 314 { 315 boolean hasPermission = false; 316 317 if (TORQUE_LOG.isDebugEnabled()) 318 { 319 String name = (module == null) ? null : module.getName(); 320 TORQUE_LOG.debug("ScarabUserImpl.hasPermission(" + perm + ", " + 321 name + ") started"); 322 } 323 324 if (perm.equals(ScarabSecurity.USER__CHANGE_PASSWORD) && isUserAnonymous()) 325 { 326 return false; 327 } 328 329 String moduleKey = (module == null) ? null : module.getQueryKey(); 336 Object obj = getTemp("hasPermission" + perm + moduleKey); 337 if (obj == null) 338 { 339 try 340 { 341 AccessControlList acl = TurbineSecurity.getACL(this); 342 if (acl != null) 343 { 344 if (module != null) 345 { 346 hasPermission = acl.hasPermission(perm, (Group)module); 348 } 349 350 if (!hasPermission) 351 { 352 Module globalModule = ModuleManager 354 .getInstance(Module.ROOT_ID); 355 hasPermission = acl.hasPermission(perm, 356 (Group)globalModule); 357 } 358 } 359 } 360 catch (Exception e) 361 { 362 hasPermission = false; 363 getLog().error("Permission check failed on:" + perm, e); 364 } 365 366 Boolean b = hasPermission ? Boolean.TRUE : Boolean.FALSE; 367 setTemp("hasPermission" + perm + moduleKey, b); 368 } 369 else 370 { 371 hasPermission = ((Boolean )obj).booleanValue(); 372 } 373 374 if (TORQUE_LOG.isDebugEnabled()) 375 { 376 String name = (module == null) ? null : module.getName(); 377 TORQUE_LOG.debug("ScarabUserImpl.hasPermission(" + perm + ", " + 378 name + ") end\n"); 379 } 380 return hasPermission; 381 } 382 383 386 public boolean hasPermission(String perm, List modules) 387 { 388 return internalUser.hasPermission(perm, modules); 389 } 390 391 394 public List getModules() throws Exception 395 { 396 return internalUser.getModules(); 397 } 398 399 402 public List getModules(boolean showDeletedModules) 403 throws Exception 404 { 405 return internalUser.getModules(showDeletedModules); 406 } 407 408 409 public Module[] getModules(String permission) throws Exception 410 { 411 return internalUser.getModules(permission); 412 } 413 414 private static final String GET_MODULES = 415 "getModules"; 416 417 420 public Module[] getModules(String [] permissions) 421 { 422 return getModules(permissions, false); 423 } 424 425 428 public List getCopyToModules(Module currentModule) throws Exception 429 { 430 return internalUser.getCopyToModules(currentModule); 431 } 432 public List getCopyToModules(Module currentModule, String action) throws Exception 433 { 434 return internalUser.getCopyToModules(currentModule, action, null); 435 } 436 public List getCopyToModules(Module currentModule, String action, 437 String searchString) 438 throws Exception 439 { 440 return internalUser.getCopyToModules(currentModule, action, searchString); 441 } 442 443 444 447 public Module[] getModules(String [] permissions, boolean showDeletedModules) 448 { 449 Module[] result = null; 450 Object obj = ScarabCache.get(this, GET_MODULES, permissions); 451 if (obj == null) 452 { 453 Criteria crit = new Criteria(); 454 crit.setDistinct(); 455 if (!showDeletedModules) 456 { 457 crit.add(ScarabModulePeer.DELETED, 0); 458 } 459 crit.addIn(TurbinePermissionPeer.PERMISSION_NAME, permissions); 460 crit.addJoin(TurbinePermissionPeer.PERMISSION_ID, 461 TurbineRolePermissionPeer.PERMISSION_ID); 462 crit.addJoin(TurbineRolePermissionPeer.ROLE_ID, 463 TurbineUserGroupRolePeer.ROLE_ID); 464 crit.add(TurbineUserGroupRolePeer.USER_ID, getUserId()); 465 crit.addJoin(ScarabModulePeer.MODULE_ID, 466 TurbineUserGroupRolePeer.GROUP_ID); 467 468 try 469 { 470 List scarabModules = ScarabModulePeer.doSelect(crit); 471 for (int i=scarabModules.size()-1; i>=0; i--) 473 { 474 if (Module.ROOT_ID.equals( 475 ((Module)scarabModules.get(i)).getModuleId())) 476 { 477 crit = new Criteria(); 478 if (!showDeletedModules) 479 { 480 crit.add(ScarabModulePeer.DELETED, 0); 481 } 482 scarabModules = ScarabModulePeer.doSelect(crit); 483 break; 484 } 485 } 486 487 Set sortedResult = new TreeSet (new Comparator () { 490 public int compare(Object object1, Object object2) { 491 Module m1 = (Module)object1; 492 Module m2 = (Module)object2; 493 return m1.getName().compareTo(m2.getName()); 494 } 495 }); 496 sortedResult.addAll(scarabModules); 497 498 result = (Module[])sortedResult.toArray(new Module[sortedResult.size()]); 499 } 500 catch (Exception e) 501 { 502 getLog().error("An exception prevented retrieving any modules", e); 503 } 504 ScarabCache.put(result, this, GET_MODULES, permissions); 505 } 506 else 507 { 508 result = (Module[])obj; 509 } 510 return result; 511 } 512 513 516 public boolean hasAnyRoleIn(Module module) 517 throws Exception 518 { 519 return getRoles(module).size() != 0; 520 } 521 522 527 public List getCurrentRoleNames() 528 { 529 Module module = getCurrentModule(); 530 531 List roles=null; 532 try 533 { 534 roles = getRoles(module); 535 } 536 catch (Exception e){} 537 538 List result = new ArrayList (); 539 if(roles!=null) 540 { 541 int size = roles.size(); 542 if(size>0) 543 { 544 for(int index=0; index< size; index++) 545 { 546 Role role = (Role)roles.get(index); 547 result.add (role.getName()); 548 } 549 } 550 } 551 return result; 552 553 } 554 555 private static final String GET_ROLES = 556 "getRoles"; 557 558 566 private List getRoles(Module module) 567 throws Exception 568 { 569 List result = null; 570 Object obj = ScarabCache.get(this, GET_ROLES, module); 571 if (obj == null) 572 { 573 Criteria crit = new Criteria(); 574 crit.setDistinct(); 575 crit.add(TurbineUserGroupRolePeer.USER_ID, getUserId()); 576 crit.add(TurbineUserGroupRolePeer.GROUP_ID, module.getModuleId()); 577 crit.addJoin(TurbineRolePeer.ROLE_ID, 578 TurbineUserGroupRolePeer.ROLE_ID); 579 result = TurbineRolePeer.doSelect(crit); 580 581 ScarabCache.put(result, this, GET_ROLES, module); 582 } 583 else 584 { 585 result = (List )obj; 586 } 587 return result; 588 } 589 590 593 public void createNewUser() 594 throws Exception 595 { 596 String uniqueId = RandomStringUtils 598 .randomAlphanumeric(UNIQUE_ID_MAX_LEN); 599 setConfirmed(uniqueId); 601 TurbineSecurity.addUser (this, getPassword()); 602 setPasswordExpire(); 603 } 604 605 608 public List getEditableModules() throws Exception 609 { 610 return internalUser.getEditableModules(); 611 } 612 613 616 public List getEditableModules(Module currEditModule) 617 throws Exception 618 { 619 return internalUser.getEditableModules(currEditModule); 620 } 621 622 625 public List getRModuleUserAttributes(Module module, 626 IssueType issueType) 627 throws Exception 628 { 629 return internalUser.getRModuleUserAttributes(module, issueType); 630 } 631 632 633 636 public RModuleUserAttribute getRModuleUserAttribute(Module module, 637 Attribute attribute, 638 IssueType issueType) 639 throws Exception 640 { 641 return internalUser 642 .getRModuleUserAttribute(module, attribute, issueType); 643 } 644 645 646 649 public Issue getReportingIssue(String key) 650 throws Exception 651 { 652 return internalUser.getReportingIssue(key); 653 } 654 655 658 public String setReportingIssue(Issue issue) 659 throws ScarabException 660 { 661 return internalUser.setReportingIssue(issue); 662 } 663 664 667 public void setReportingIssue(String key, Issue issue) 668 { 669 internalUser.setReportingIssue(key, issue); 670 } 671 672 675 public ReportBridge getCurrentReport(String key) 676 throws Exception 677 { 678 return internalUser.getCurrentReport(key); 679 } 680 681 684 public String setCurrentReport(ReportBridge report) 685 throws ScarabException 686 { 687 return internalUser.setCurrentReport(report); 688 } 689 690 693 public void setCurrentReport(String key, ReportBridge report) 694 { 695 internalUser.setCurrentReport(key, report); 696 } 697 698 704 public void setPasswordExpire() 705 throws Exception 706 { 707 String expireDays = Turbine.getConfiguration() 708 .getString("scarab.login.password.expire", null); 709 710 if (expireDays == null || expireDays.trim().length() == 0) 711 { 712 setPasswordExpire(null); 713 } 714 else 715 { 716 Calendar expireDate = Calendar.getInstance(); 717 expireDate.add(Calendar.DATE, Integer.parseInt(expireDays)); 718 setPasswordExpire(expireDate); 719 } 720 } 721 722 732 public void setPasswordExpire(Calendar expire) 733 throws Exception 734 { 735 Integer userid = getUserId(); 736 if (userid == null) 737 { 738 throw new Exception ("Userid cannot be null"); } 740 UserPreference up = UserPreferenceManager.getInstance(getUserId()); 741 if (expire == null) 742 { 743 Calendar cal = Calendar.getInstance(); 744 cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 10); 745 up.setPasswordExpire(cal.getTime()); 746 } 747 else 748 { 749 up.setPasswordExpire(expire.getTime()); 750 } 751 up.save(); 752 } 753 754 759 public boolean isPasswordExpired() 760 throws Exception 761 { 762 if (isUserAnonymous()) 764 { 765 return false; 766 } 767 768 Integer userid = getUserId(); 769 if (userid == null) 770 { 771 throw new Exception ("Userid cannot be null"); } 773 Criteria crit = new Criteria(); 774 crit.add(UserPreferencePeer.USER_ID, userid); 775 Calendar cal = Calendar.getInstance(); 776 crit.add(UserPreferencePeer.PASSWORD_EXPIRE, 777 cal.getTime() , Criteria.LESS_THAN); 778 List result = UserPreferencePeer.doSelect(crit); 779 return result.size() == 1 ? true : false; 780 } 781 782 787 public boolean isUserAnonymous() 788 { 789 boolean brdo = false; 790 String anonymous = Turbine.getConfiguration().getString("scarab.anonymous.username", null); 791 if (anonymous != null && getUserName().equals(anonymous)) 792 { 793 brdo = true; 794 } 795 return brdo; 796 } 797 798 804 public int getEnterIssueRedirect() 805 throws Exception 806 { 807 return internalUser.getEnterIssueRedirect(); 808 } 809 810 811 817 public void setEnterIssueRedirect(int templateCode) 818 throws Exception 819 { 820 internalUser.setEnterIssueRedirect(templateCode); 821 } 822 823 826 public String getHomePage() 827 throws Exception 828 { 829 return internalUser.getHomePage(); 830 } 831 832 835 public String getHomePage(Module module) 836 throws Exception 837 { 838 return internalUser.getHomePage(module); 839 } 840 841 844 public void setHomePage(String homePage) 845 throws Exception 846 { 847 internalUser.setHomePage(homePage); 848 } 849 850 851 854 public String getQueryTarget() 855 { 856 return internalUser.getQueryTarget(); 857 } 858 859 862 public void setSingleIssueTypeQueryTarget(IssueType type, String target) 863 { 864 internalUser.setSingleIssueTypeQueryTarget(type, target); 865 } 866 867 870 public List getMITLists() 871 throws TorqueException 872 { 873 return internalUser.getMITLists(); 874 } 875 876 879 public boolean hasAnySearchableRMITs() 880 throws Exception 881 { 882 return internalUser.hasAnySearchableRMITs(); 883 } 884 885 888 public List getSearchableRMITs(String searchField, String searchString, 889 String sortColumn, String sortPolarity, 890 Module skipModule) 891 throws Exception 892 { 893 return internalUser.getSearchableRMITs(searchField, searchString, 894 sortColumn, sortPolarity, skipModule); 895 } 896 897 900 public List getUnusedRModuleIssueTypes(Module module) 901 throws Exception 902 { 903 return internalUser.getUnusedRModuleIssueTypes(module); 904 } 905 906 909 public void addRMITsToCurrentMITList(List rmits) 910 throws TorqueException 911 { 912 internalUser.addRMITsToCurrentMITList(rmits); 913 } 914 915 918 public MITList getCurrentMITList() 919 { 920 MITList mitList = internalUser.getCurrentMITList(); 921 return mitList; 922 } 923 924 927 public void setCurrentMITList(MITList list) 928 { 929 internalUser.setCurrentMITList(list); 930 } 931 932 935 public void removeItemsFromCurrentMITList(String [] ids) 936 { 937 internalUser.removeItemsFromCurrentMITList(ids); 938 } 939 940 943 public Object lastEnteredIssueTypeOrTemplate() 944 { 945 return internalUser.lastEnteredIssueTypeOrTemplate(); 946 } 947 948 951 public void setLastEnteredIssueType(IssueType type) 952 { 953 internalUser.setLastEnteredIssueType(type); 954 } 955 956 959 public void setLastEnteredTemplate(Issue template) 960 { 961 internalUser.setLastEnteredTemplate(template); 962 } 963 964 965 968 public String getMostRecentQuery() 969 { 970 return internalUser.getMostRecentQuery(); 971 } 972 973 976 public void setMostRecentQuery(String queryString) 977 { 978 internalUser.setMostRecentQuery(queryString.toLowerCase()); 979 } 980 981 984 public boolean hasMostRecentQuery() 985 { 986 return internalUser.hasMostRecentQuery(); 987 } 988 989 public Map getAssociatedUsersMap() 990 throws Exception 991 { 992 return internalUser.getAssociatedUsersMap(); 993 } 994 995 public void setAssociatedUsersMap(Map associatedUsers) 996 throws Exception 997 { 998 internalUser.setAssociatedUsersMap(associatedUsers); 999 } 1000 1001 public Map getSelectedUsersMap() 1002 throws Exception 1003 { 1004 return internalUser.getSelectedUsersMap(); 1005 } 1006 1007 public void setSelectedUsersMap(Map selectedUsers) 1008 throws Exception 1009 { 1010 internalUser.setSelectedUsersMap(selectedUsers); 1011 } 1012 1013 1014 1017 public Object getThreadKey() 1018 { 1019 return internalUser.getThreadKey(); 1020 } 1021 1022 1025 public void setThreadKey(Integer key) 1026 { 1027 internalUser.setThreadKey(key); 1028 } 1029 1030 1031 1034 public Module getCurrentModule() 1035 { 1036 return internalUser.getCurrentModule(); 1037 } 1038 1039 1042 public void setCurrentModule(Module v) 1043 { 1044 internalUser.setCurrentModule(v); 1045 } 1046 1047 1050 public IssueType getCurrentIssueType() 1051 throws Exception 1052 { 1053 return internalUser.getCurrentIssueType(); 1054 } 1055 1056 1059 public void setCurrentIssueType(IssueType v) 1060 { 1061 internalUser.setCurrentIssueType(v); 1062 } 1063 1064 1067 public RModuleIssueType getCurrentRModuleIssueType() 1068 throws Exception 1069 { 1070 return internalUser.getCurrentRModuleIssueType(); 1071 } 1072 1073 1076 public void updateIssueListAttributes(List attributes) 1077 throws Exception 1078 { 1079 internalUser.updateIssueListAttributes(attributes); 1080 } 1081 1082 public List getRoleNames(Module module) 1083 throws Exception 1084 { 1085 return null; 1086 } 1087 1088 1091 public String getStats() 1092 { 1093 return internalUser.getStats() 1094 + "; TempStorage=" + getTempStorage().size() 1095 + "; PermStorage=" + getPermStorage().size(); 1096 } 1097 1098 1101 public void setLocale(Locale newLocale) 1102 { 1103 internalUser.setLocale(newLocale); 1104 } 1105 1106 1110 public Locale getLocale() 1111 { 1112 return internalUser.getLocale(); 1113 } 1114 1115 1118 public Locale getPreferredLocale() 1119 { 1120 return internalUser.getPreferredLocale(); 1121 } 1122 1123 1126 public boolean isShowOtherModulesInIssueTypeList() 1127 { 1128 return internalUser.isShowOtherModulesInIssueTypeList(); 1129 } 1130 1131 1134 public void setShowOtherModulesInIssueTypeList( 1135 boolean newShowOtherModulesInIssueTypeList) 1136 { 1137 internalUser.setShowOtherModulesInIssueTypeList( 1138 newShowOtherModulesInIssueTypeList); 1139 } 1140} 1141 | Popular Tags |