1 package org.tigris.scarab.om; 2 3 48 49 import java.io.Serializable ; 51 import java.util.Comparator ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.ArrayList ; 55 import java.util.Collections ; 56 57 import org.apache.commons.lang.StringUtils; 59 60 import org.apache.torque.TorqueException; 62 import org.apache.torque.om.Persistent; 63 import org.apache.torque.util.Criteria; 64 65 import java.sql.Connection ; 66 import org.apache.fulcrum.security.TurbineSecurity; 67 import org.apache.fulcrum.security.util.RoleSet; 68 import org.apache.fulcrum.security.util.TurbineSecurityException; 69 import org.apache.fulcrum.security.entity.User; 70 import org.apache.fulcrum.security.entity.Group; 71 import org.apache.fulcrum.security.entity.Role; 72 73 import org.tigris.scarab.om.Module; 75 import org.tigris.scarab.om.MITList; 76 import org.tigris.scarab.om.ScarabUserManager; 77 import org.tigris.scarab.tools.localization.L10NKeySet; 78 import org.tigris.scarab.util.ScarabConstants; 79 import org.tigris.scarab.util.ScarabException; 80 import org.tigris.scarab.util.ScarabPaginatedList; 81 import org.tigris.scarab.util.ScarabLocalizedTorqueException; 82 import org.tigris.scarab.util.ScarabLocalizedTurbineSecurityException; 83 import org.tigris.scarab.services.cache.ScarabCache; 84 85 import org.apache.turbine.Turbine; 88 import org.apache.fulcrum.security.impl.db.entity 89 .TurbinePermissionPeer; 90 import org.apache.fulcrum.security.impl.db.entity 91 .TurbineUserGroupRolePeer; 92 import org.apache.fulcrum.security.impl.db.entity 93 .TurbineRolePermissionPeer; 94 import org.apache.fulcrum.security.impl.db.entity.TurbineUserPeer; 95 96 109 public class ScarabModule 110 extends BaseScarabModule 111 implements Persistent, Module, Group 112 { 113 private static final String GET_USERS = "getUsers"; 114 115 protected static final Integer ROOT_ID = new Integer (0); 116 117 private String httpDomain = null; 118 private String instanceId = null; 119 private String port = null; 120 private String scheme = null; 121 private String scriptName = null; 122 123 127 public String getHttpDomain() 128 { 129 if (httpDomain == null || httpDomain.length() == 0) 130 { 131 try 132 { 133 httpDomain = GlobalParameterManager 134 .getString(ScarabConstants.HTTP_DOMAIN); 135 } 136 catch (Exception e) 137 { 138 getLog().error("Error getting HTTP_DOMAIN:", e); 139 } 140 } 141 return httpDomain; 142 } 143 144 148 public void setHttpDomain(String v) 149 { 150 if (v != null) 151 { 152 this.httpDomain = v; 153 } 154 } 155 156 160 public String getScarabInstanceId() 161 { 162 if (instanceId == null || instanceId.length() == 0) 163 { 164 try 165 { 166 instanceId = GlobalParameterManager 167 .getString(ScarabConstants.INSTANCE_ID); 168 } 169 catch (Exception e) 170 { 171 getLog().error("Error getting DOMAIN_NAME:", e); 172 } 173 } 174 return instanceId; 175 } 176 177 183 public void setScarabInstanceId(String v) 184 { 185 if (v != null) 186 { 187 this.instanceId = v; 188 } 189 } 190 191 195 public String getPort() 196 throws Exception 197 { 198 if (port == null) 199 { 200 port = GlobalParameterManager 201 .getString(ScarabConstants.HTTP_PORT); 202 } 203 return port; 204 } 205 206 210 public void setPort(String v) 211 throws Exception 212 { 213 if (v != null) 214 { 215 this.port = v; 216 } 217 } 218 219 223 public String getScheme() 224 throws Exception 225 { 226 if (scheme == null) 227 { 228 scheme = GlobalParameterManager 229 .getString(ScarabConstants.HTTP_SCHEME); 230 } 231 return scheme; 232 } 233 234 238 public void setScheme(String v) 239 throws Exception 240 { 241 if (v != null) 242 { 243 this.scheme = v; 244 } 245 } 246 247 251 public String getScriptName() 252 throws Exception 253 { 254 if (scriptName == null) 255 { 256 scriptName = GlobalParameterManager 257 .getString(ScarabConstants.HTTP_SCRIPT_NAME); 258 } 259 return scriptName; 260 } 261 262 266 public void setScriptName(String v) 267 throws Exception 268 { 269 if (v != null) 270 { 271 this.scriptName = v; 272 } 273 } 274 275 278 public ScarabUser[] getUsers(String permission) 279 { 280 List perms = new ArrayList (1); 281 perms.add(permission); 282 return getUsers(perms); 283 } 284 285 288 public ScarabUser[] getUsers(List permissions) 289 { 290 ScarabUser[] result = null; 291 Object obj = ScarabCache.get(this, GET_USERS, 292 (Serializable )permissions); 293 if (obj == null) 294 { 295 Criteria crit = new Criteria(); 296 crit.setDistinct(); 297 if (permissions.size() == 1) 298 { 299 crit.add(TurbinePermissionPeer.NAME, permissions.get(0)); 300 } 301 else if (permissions.size() > 1) 302 { 303 crit.addIn(TurbinePermissionPeer.NAME, permissions); 304 } 305 306 if (permissions.size() >= 1) 307 { 308 ArrayList groups = new ArrayList (2); 309 groups.add(getModuleId()); 310 groups.add(ROOT_ID); 311 crit.addJoin(TurbinePermissionPeer.PERMISSION_ID, 312 TurbineRolePermissionPeer.PERMISSION_ID); 313 crit.addJoin(TurbineRolePermissionPeer.ROLE_ID, 314 TurbineUserGroupRolePeer.ROLE_ID); 315 crit.addIn(TurbineUserGroupRolePeer.GROUP_ID, groups); 316 crit.addJoin(ScarabUserImplPeer.USER_ID, 317 TurbineUserGroupRolePeer.USER_ID); 318 319 crit.add(ScarabUserImplPeer.getColumnName(User.CONFIRM_VALUE),(Object )ScarabUser.DELETED,Criteria.NOT_EQUAL); 320 321 322 try 323 { 324 User[] users = TurbineSecurity.getUsers(crit); 325 result = new ScarabUser[users.length]; 326 for (int i=result.length-1; i>=0; i--) 327 { 328 result[i] = (ScarabUser)users[i]; 329 } 330 } 331 catch (Exception e) 332 { 333 getLog().error( 334 "An exception prevented retrieving any users", e); 335 throw new RuntimeException ( 338 "Please check turbine.log for more info: " + 339 e.getMessage()); } 341 } 342 else 343 { 344 result = new ScarabUser[0]; 345 } 346 ScarabCache.put(result, this, GET_USERS, 347 (Serializable )permissions); 348 } 349 else 350 { 351 result = (ScarabUser[])obj; 352 } 353 return result; 354 } 355 356 357 361 public ScarabPaginatedList getUsers(String name, String username, 362 MITList mitList, 363 int pageNum, int resultsPerPage, 364 final String sortColumn, String sortPolarity, 365 boolean includeCommitters) 366 throws Exception 367 { 368 final int polarity = sortPolarity.equals("asc") ? 1 : -1; 369 List result = null; 370 ScarabPaginatedList paginated = null; 371 372 Comparator c = new Comparator () 373 { 374 public int compare(Object o1, Object o2) 375 { 376 int i = 0; 377 if ("username".equals(sortColumn)) 378 { 379 i = polarity * ((ScarabUser)o1).getUserName() 380 .compareTo(((ScarabUser)o2).getUserName()); 381 } 382 else 383 { 384 i = polarity * ((ScarabUser)o1).getName() 385 .compareTo(((ScarabUser)o2).getName()); 386 } 387 return i; 388 } 389 }; 390 391 Criteria crit = new Criteria(); Criteria critCount = new Criteria(); 393 critCount.addSelectColumn("COUNT(DISTINCT " + TurbineUserPeer.USERNAME + ")"); 394 if (mitList != null) 395 { 396 List modules = mitList.getModules(); 397 for (Iterator it = modules.iterator(); it.hasNext(); ) 398 { 399 Module mod = (Module)it.next(); 400 List perms = mitList.getUserAttributePermissions(); 401 if (includeCommitters && !perms.contains(org.tigris.scarab.services.security.ScarabSecurity.ISSUE__ENTER)) 402 { 403 perms.add(org.tigris.scarab.services.security.ScarabSecurity.ISSUE__ENTER); 404 } 405 406 crit.addIn(TurbinePermissionPeer.PERMISSION_NAME, perms); 407 crit.setDistinct(); 408 critCount.addIn(TurbinePermissionPeer.PERMISSION_NAME, perms); 409 } 410 crit.addIn(TurbineUserGroupRolePeer.GROUP_ID, mitList.getModuleIds()); 411 critCount.addIn(TurbineUserGroupRolePeer.GROUP_ID, mitList.getModuleIds()); 412 } 413 crit.addJoin(TurbineUserPeer.USER_ID, TurbineUserGroupRolePeer.USER_ID); 414 crit.addJoin(TurbineUserGroupRolePeer.ROLE_ID, TurbineRolePermissionPeer.ROLE_ID); 415 crit.addJoin(TurbineRolePermissionPeer.PERMISSION_ID, TurbinePermissionPeer.PERMISSION_ID); 416 critCount.addJoin(TurbineUserPeer.USER_ID, TurbineUserGroupRolePeer.USER_ID); 417 critCount.addJoin(TurbineUserGroupRolePeer.ROLE_ID, TurbineRolePermissionPeer.ROLE_ID); 418 critCount.addJoin(TurbineRolePermissionPeer.PERMISSION_ID, TurbinePermissionPeer.PERMISSION_ID); 419 420 if (name != null) 421 { 422 int nameSeparator = name.indexOf(" "); 423 if (nameSeparator != -1) 424 { 425 String firstName = name.substring(0, nameSeparator); 426 String lastName = name.substring(nameSeparator+1, name.length()); 427 crit.add(ScarabUserImplPeer.FIRST_NAME, 428 addWildcards(firstName), Criteria.LIKE); 429 crit.add(ScarabUserImplPeer.LAST_NAME, 430 addWildcards(lastName), Criteria.LIKE); 431 critCount.add(ScarabUserImplPeer.FIRST_NAME, 432 addWildcards(firstName), Criteria.LIKE); 433 critCount.add(ScarabUserImplPeer.LAST_NAME, 434 addWildcards(lastName), Criteria.LIKE); 435 436 } 437 else 438 { 439 String [] tableAndColumn = StringUtils.split(ScarabUserImplPeer.FIRST_NAME, "."); 440 Criteria.Criterion fn = crit.getNewCriterion(tableAndColumn[0], 441 tableAndColumn[1], 442 addWildcards(name), 443 Criteria.LIKE); 444 tableAndColumn = StringUtils.split(ScarabUserImplPeer.LAST_NAME, "."); 445 Criteria.Criterion ln = crit.getNewCriterion(tableAndColumn[0], 446 tableAndColumn[1], 447 addWildcards(name), 448 Criteria.LIKE); 449 fn.or(ln); 450 crit.add(fn); 451 critCount.add(fn); 452 } 453 } 454 455 if (username != null) 456 { 457 crit.add(ScarabUserImplPeer.LOGIN_NAME, 458 addWildcards(username), Criteria.LIKE); 459 critCount.add(ScarabUserImplPeer.LOGIN_NAME, 460 addWildcards(username), Criteria.LIKE); 461 } 462 463 String col = ScarabUserImplPeer.FIRST_NAME; 464 if (sortColumn.equals("username")) 465 col = ScarabUserImplPeer.USERNAME; 466 if (sortPolarity.equals("asc")) 467 { 468 crit.addAscendingOrderByColumn(col); 469 } 470 else 471 { 472 crit.addDescendingOrderByColumn(col); 473 } 474 475 int totalResultSize = ScarabUserImplPeer.getUsersCount(critCount); 476 477 crit.setOffset((pageNum - 1)* resultsPerPage); 478 crit.setLimit(resultsPerPage); 479 result = ScarabUserImplPeer.doSelect(crit); 480 481 if (totalResultSize > 0 && resultsPerPage > 0) 483 { 484 485 paginated = new ScarabPaginatedList(result, totalResultSize, 486 pageNum, 487 resultsPerPage); 488 } 489 else 490 { 491 paginated = new ScarabPaginatedList(); 492 } 493 494 return paginated; 495 } 496 497 498 504 public List getUsers(String firstName, String lastName, 505 String username, String email, IssueType issueType) 506 throws Exception 507 { 508 List result = null; 509 Serializable [] keys = {this, GET_USERS, firstName, null, lastName, 511 username, email, issueType}; 512 Object obj = ScarabCache.get(keys); 513 if (obj == null) 514 { 515 ScarabUser[] eligibleUsers = getUsers(getUserPermissions(issueType)); 516 if (eligibleUsers == null || eligibleUsers.length == 0) 517 { 518 result = Collections.EMPTY_LIST; 519 } 520 else 521 { 522 List userIds = new ArrayList (); 523 for (int i = 0; i < eligibleUsers.length; i++) 524 { 525 userIds.add(eligibleUsers[i].getUserId()); 526 } 527 Criteria crit = new Criteria(); 528 crit.addIn(ScarabUserImplPeer.USER_ID, userIds); 529 530 if (firstName != null) 531 { 532 crit.add(ScarabUserImplPeer.FIRST_NAME, 533 addWildcards(firstName), Criteria.LIKE); 534 } 535 if (lastName != null) 536 { 537 crit.add(ScarabUserImplPeer.LAST_NAME, 538 addWildcards(lastName), Criteria.LIKE); 539 } 540 if (username != null) 541 { 542 crit.add(ScarabUserImplPeer.LOGIN_NAME, 543 addWildcards(username), Criteria.LIKE); 544 } 545 if (email != null) 546 { 547 crit.add(ScarabUserImplPeer.EMAIL, addWildcards(email), 548 Criteria.LIKE); 549 } 550 result = ScarabUserImplPeer.doSelect(crit); 551 } 552 ScarabCache.put(result, keys); 553 } 554 else 555 { 556 result = (List )obj; 557 } 558 return result; 559 } 560 561 private Object addWildcards(String s) 562 { 563 return new StringBuffer (s.length() + 2) 564 .append('%').append(s).append('%').toString(); 565 } 566 567 571 public void setParent(Module v) 572 throws Exception 573 { 574 super.setModuleRelatedByParentId(v); 575 setName(null); 578 resetAncestors(); 579 } 580 581 584 public Module getParent() 585 throws Exception 586 { 587 return super.getModuleRelatedByParentId(); 588 } 589 590 595 public void setParentId(Integer id) 596 throws TorqueException 597 { 598 super.setParentId(id); 599 setName(null); 602 resetAncestors(); 603 } 604 605 610 public List getRModuleIssueTypes() 611 throws TorqueException 612 { 613 return super.getRModuleIssueTypes("preferredOrder","asc"); 614 } 615 616 622 public List getRModuleAttributes(Criteria crit) 623 throws TorqueException 624 { 625 return super.getRModuleAttributes(crit); 626 } 627 628 635 public List getRModuleOptions(Criteria crit) 636 throws TorqueException 637 { 638 crit.addJoin(RModuleOptionPeer.OPTION_ID, 639 AttributeOptionPeer.OPTION_ID) 640 .add(AttributeOptionPeer.DELETED, false); 641 return super.getRModuleOptions(crit); 642 } 643 644 645 public boolean allowsIssues() 646 { 647 return (true); 648 } 649 650 653 public void save() 654 throws TurbineSecurityException 655 { 656 try 657 { 658 super.save(); 659 } 660 catch (Exception e) 661 { 662 throw new ScarabLocalizedTurbineSecurityException(e); } 664 } 665 666 672 public void save(Connection dbCon) 673 throws TorqueException 674 { 675 if (isNew()) 677 { 678 Criteria crit = new Criteria(); 679 crit.add(ScarabModulePeer.MODULE_NAME, getRealName()); 680 crit.add(ScarabModulePeer.PARENT_ID, getParentId()); 681 686 List result; 687 try { 688 result = ScarabModulePeer.doSelect(crit); 689 } 690 catch (TorqueException te) 691 { 692 throw new ScarabLocalizedTorqueException( 693 new ScarabException( 694 L10NKeySet.ExceptionTorqueGeneric, te)); 695 } 696 697 if (result.size() > 0) 698 { 699 throw new ScarabLocalizedTorqueException( 700 new ScarabException( 701 L10NKeySet.ExceptionModuleAllreadyExists, 702 getRealName(), 703 getParentId())); 704 } 705 706 String code = getCode(); 707 if (code == null || code.length() == 0) 708 { 709 if (getParentId().equals(ROOT_ID)) 710 { 711 throw new ScarabLocalizedTorqueException(new ScarabException(L10NKeySet.ExceptionTopLevelModuleWithoutCode)); 712 } 713 714 try 715 { 716 setCode(getParent().getCode()); 717 } 718 catch (Exception e) 719 { 720 throw new ScarabLocalizedTorqueException(new ScarabException(L10NKeySet.ExceptionCantPropagateModuleCode, e)); 721 } 722 } 723 724 super.save(dbCon); 727 try 728 { 729 dbCon.commit(); 730 } 731 catch (Exception e) 732 { 733 throw new ScarabLocalizedTorqueException(new ScarabException(L10NKeySet.ExceptionGeneric, e)); 734 } 735 736 if (getOwnerId() == null) 737 { 738 throw new ScarabLocalizedTorqueException(new ScarabException(L10NKeySet.ExceptionSaveNeedsOwner)); 739 } 740 try 742 { 743 User user = ScarabUserManager.getInstance(getOwnerId()); 744 Role role = TurbineSecurity.getRole("Project Owner"); 747 grant (user, role); 748 setInitialAttributesAndIssueTypes(); 749 } 750 catch (Exception e) 751 { 752 throw new ScarabLocalizedTorqueException(new ScarabException(L10NKeySet.ExceptionGeneric, e)); 753 } 754 } 755 else 756 { 757 super.save(dbCon); 758 } 759 760 ScarabCache.clear(); 763 } 764 765 770 775 public void remove() 776 throws TurbineSecurityException 777 { 778 throw new TurbineSecurityException("Not implemented"); } 780 781 787 public void rename(String name) 788 throws TurbineSecurityException 789 { 790 throw new TurbineSecurityException("Not implemented"); } 792 793 801 public void grant(User user, Role role) 802 throws TurbineSecurityException 803 { 804 TurbineSecurity.grant(user,this,role); 805 } 806 807 815 public void grant(User user, RoleSet roleSet) 816 throws TurbineSecurityException 817 { 818 throw new TurbineSecurityException("Not implemented"); } 820 821 829 public void revoke(User user, Role role) 830 throws TurbineSecurityException 831 { 832 throw new TurbineSecurityException("Not implemented"); } 834 835 843 public void revoke(User user, RoleSet roleSet) 844 throws TurbineSecurityException 845 { 846 throw new TurbineSecurityException("Not implemented"); } 848 849 856 public int compareTo(Object obj) 857 { 858 String name1 = ((Group)obj).getName(); 867 String name2 = this.getName(); 868 869 return name2.compareTo(name1); 870 } 871 872 873 878 public String getArchiveEmail() 879 { 880 String email = super.getArchiveEmail(); 881 if (email == null || email.length() == 0) 882 { 883 email = Turbine.getConfiguration() 884 .getString(ScarabConstants.ARCHIVE_EMAIL_ADDRESS, null); 885 } 886 887 return email; 888 } 889 890 894 public String [] getAutoApprovedRoles() 895 { 896 return Turbine.getConfiguration() 897 .getStringArray(ScarabConstants.AUTO_APPROVED_ROLES); 898 } 899 900 903 public List getRoles() 904 throws Exception 905 { 906 return new ArrayList (0); 907 } 908 909 public String toString() 910 { 911 return '{' + super.toString() + " - ID=" + getModuleId() + " - " 912 + getName() + '}'; 913 } 914 } 915 916 | Popular Tags |