1 17 package org.alfresco.web.bean.wizard; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.List ; 23 import java.util.ResourceBundle ; 24 import java.util.Set ; 25 26 import javax.faces.component.UISelectOne; 27 import javax.faces.context.FacesContext; 28 import javax.faces.event.ActionEvent; 29 import javax.faces.model.DataModel; 30 import javax.faces.model.ListDataModel; 31 import javax.faces.model.SelectItem; 32 import javax.transaction.UserTransaction ; 33 34 import org.alfresco.model.ContentModel; 35 import org.alfresco.service.cmr.repository.NodeRef; 36 import org.alfresco.service.cmr.security.AuthorityService; 37 import org.alfresco.service.cmr.security.AuthorityType; 38 import org.alfresco.service.cmr.security.PermissionService; 39 import org.alfresco.service.cmr.security.PersonService; 40 import org.alfresco.service.namespace.NamespaceService; 41 import org.alfresco.web.app.Application; 42 import org.alfresco.web.app.context.UIContextService; 43 import org.alfresco.web.bean.repository.Node; 44 import org.alfresco.web.bean.repository.Repository; 45 import org.alfresco.web.bean.repository.User; 46 import org.alfresco.web.ui.common.SortableSelectItem; 47 import org.alfresco.web.ui.common.Utils; 48 import org.alfresco.web.ui.common.component.UIGenericPicker; 49 import org.apache.commons.logging.Log; 50 import org.apache.commons.logging.LogFactory; 51 import org.springframework.mail.SimpleMailMessage; 52 import org.springframework.mail.javamail.JavaMailSender; 53 54 57 public abstract class InviteUsersWizard extends AbstractWizardBean 58 { 59 private static Log logger = LogFactory.getLog(InviteUsersWizard.class); 60 61 62 private static final String MSG_USERS = "users"; 63 private static final String MSG_GROUPS = "groups"; 64 private static final String MSG_INVITED_TO = "invited_to"; 65 private static final String MSG_INVITED_ROLE = "invite_role"; 66 private static final String STEP1_TITLE_ID = "invite_step1_title"; 67 private static final String STEP2_TITLE_ID = "invite_step2_title"; 68 private static final String STEP2_DESCRIPTION_ID = "invite_step2_desc"; 69 private static final String FINISH_INSTRUCTION_ID = "invite_finish_instruction"; 70 71 private static final String NOTIFY_YES = "yes"; 72 73 74 protected NamespaceService namespaceService; 75 76 77 protected JavaMailSender mailSender; 78 79 80 protected AuthorityService authorityService; 81 82 83 protected PermissionService permissionService; 84 85 86 protected PersonService personService; 87 88 89 private DataModel userRolesDataModel = null; 90 91 92 private List <UserGroupRole> userGroupRoles = null; 93 94 95 private String notify = NOTIFY_YES; 96 private String subject = null; 97 private String body = null; 98 private String internalSubject = null; 99 private String automaticText = null; 100 101 104 protected abstract Set <String > getPermissionsForType(); 105 106 109 protected abstract Node getNode(); 110 111 114 protected abstract String getStep1DescriptionText(); 115 116 119 public void setNamespaceService(NamespaceService namespaceService) 120 { 121 this.namespaceService = namespaceService; 122 } 123 124 127 public void setMailSender(JavaMailSender mailSender) 128 { 129 this.mailSender = mailSender; 130 } 131 132 135 public void setPermissionService(PermissionService permissionService) 136 { 137 this.permissionService = permissionService; 138 } 139 140 143 public void setPersonService(PersonService personService) 144 { 145 this.personService = personService; 146 } 147 148 151 public void setAuthorityService(AuthorityService authorityService) 152 { 153 this.authorityService = authorityService; 154 } 155 156 159 public void init() 160 { 161 super.init(); 162 163 notify = NOTIFY_YES; 164 userGroupRoles = new ArrayList <UserGroupRole>(8); 165 subject = ""; 166 body = ""; 167 automaticText = ""; 168 internalSubject = null; 169 } 170 171 174 public String finish() 175 { 176 String outcome = FINISH_OUTCOME; 177 178 UserTransaction tx = null; 179 180 try 181 { 182 FacesContext context = FacesContext.getCurrentInstance(); 183 184 tx = Repository.getUserTransaction(context); 185 tx.begin(); 186 187 String subject = this.subject; 188 if (subject == null || subject.length() == 0) 189 { 190 subject = this.internalSubject; 191 } 192 193 User user = Application.getCurrentUser(context); 194 String from = (String )this.nodeService.getProperty(user.getPerson(), ContentModel.PROP_EMAIL); 195 if (from == null || from.length() == 0) 196 { 197 from = Application.getClientConfig(context).getFromEmailAddress(); 199 } 200 201 NodeRef nodeRef = this.getNode().getNodeRef(); 203 204 for (int i=0; i<this.userGroupRoles.size(); i++) 206 { 207 UserGroupRole userGroupRole = this.userGroupRoles.get(i); 208 String authority = userGroupRole.getAuthority(); 209 210 Set <String > perms = getPermissionsForType(); 212 for (String permission : perms) 213 { 214 if (userGroupRole.getRole().equals(permission)) 215 { 216 this.permissionService.setPermission( 217 nodeRef, 218 authority, 219 permission, 220 true); 221 break; 222 } 223 } 224 225 if (NOTIFY_YES.equals(this.notify)) 227 { 228 AuthorityType authType = AuthorityType.getAuthorityType(authority); 230 if (authType.equals(AuthorityType.USER)) 231 { 232 if (this.personService.personExists(authority) == true) 233 { 234 notifyUser(this.personService.getPerson(authority), nodeRef, from, userGroupRole.getRole()); 235 } 236 } 237 else if (authType.equals(AuthorityType.GROUP)) 238 { 239 Set <String > users = this.authorityService.getContainedAuthorities(AuthorityType.USER, authority, false); 241 for (String userAuth : users) 242 { 243 if (this.personService.personExists(userAuth) == true) 244 { 245 notifyUser(this.personService.getPerson(userAuth), nodeRef, from, userGroupRole.getRole()); 246 } 247 } 248 } 249 } 250 } 251 252 tx.commit(); 254 255 UIContextService.getInstance(context).notifyBeans(); 256 } 257 catch (Throwable e) 258 { 259 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 261 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 262 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e); 263 outcome = null; 264 } 265 266 return outcome; 267 } 268 269 277 private void notifyUser(NodeRef person, NodeRef node, String from, String roleText) 278 { 279 String to = (String )this.nodeService.getProperty(person, ContentModel.PROP_EMAIL); 280 281 if (to != null && to.length() != 0) 282 { 283 String msgRole = Application.getMessage(FacesContext.getCurrentInstance(), MSG_INVITED_ROLE); 284 String roleMessage = MessageFormat.format(msgRole, new Object [] {roleText}); 285 286 292 String body = this.internalSubject + "\r\n\r\n" + roleMessage + "\r\n\r\n"; if (this.body != null && this.body.length() != 0) 294 { 295 body += this.body; 296 } 297 298 SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); 299 simpleMailMessage.setTo(to); 300 simpleMailMessage.setSubject(subject); 301 simpleMailMessage.setText(body); 302 simpleMailMessage.setFrom(from); 303 304 if (logger.isDebugEnabled()) 305 logger.debug("Sending notification email to: " + to + "\n...with subject:\n" + subject + "\n...with body:\n" + body); 306 307 try 308 { 309 this.mailSender.send(simpleMailMessage); 311 } 312 catch (Throwable e) 313 { 314 logger.error("Failed to send email to " + to, e); 316 } 317 } 318 } 319 320 325 public DataModel getUserRolesDataModel() 326 { 327 if (this.userRolesDataModel == null) 328 { 329 this.userRolesDataModel = new ListDataModel(); 330 } 331 332 this.userRolesDataModel.setWrappedData(this.userGroupRoles); 333 334 return this.userRolesDataModel; 335 } 336 337 347 public SelectItem[] pickerCallback(int filterIndex, String contains) 348 { 349 FacesContext context = FacesContext.getCurrentInstance(); 350 351 SelectItem[] items; 352 353 UserTransaction tx = null; 354 try 355 { 356 tx = Repository.getUserTransaction(context, true); 357 tx.begin(); 358 359 if (filterIndex == 0) 360 { 361 NodeRef peopleRef = personService.getPeopleContainer(); 363 String xpath = "*[like(@" + NamespaceService.CONTENT_MODEL_PREFIX + ":" + "firstName, '%" + contains + "%', false)" + 365 " or " + "like(@" + NamespaceService.CONTENT_MODEL_PREFIX + ":" + "lastName, '%" + contains + "%', false)]"; 366 367 List <NodeRef> nodes = searchService.selectNodes( 368 peopleRef, 369 xpath, 370 null, 371 this.namespaceService, 372 false); 373 374 items = new SelectItem[nodes.size()]; 375 for (int index=0; index<nodes.size(); index++) 376 { 377 NodeRef personRef = nodes.get(index); 378 String firstName = (String )this.nodeService.getProperty(personRef, ContentModel.PROP_FIRSTNAME); 379 String lastName = (String )this.nodeService.getProperty(personRef, ContentModel.PROP_LASTNAME); 380 String username = (String )this.nodeService.getProperty(personRef, ContentModel.PROP_USERNAME); 381 SelectItem item = new SortableSelectItem(username, firstName + " " + lastName, lastName); 382 items[index] = item; 383 } 384 } 385 else 386 { 387 Set <String > groups = authorityService.getAllAuthorities(AuthorityType.GROUP); 389 groups.addAll(authorityService.getAllAuthorities(AuthorityType.EVERYONE)); 390 391 List <SelectItem> results = new ArrayList <SelectItem>(groups.size()); 392 String containsLower = contains.toLowerCase(); 393 int offset = PermissionService.GROUP_PREFIX.length(); 394 for (String group : groups) 395 { 396 if (group.toLowerCase().indexOf(containsLower, offset) != -1) 397 { 398 results.add(new SortableSelectItem(group, group.substring(offset), group)); 399 } 400 } 401 items = new SelectItem[results.size()]; 402 results.toArray(items); 403 } 404 405 Arrays.sort(items); 406 407 tx.commit(); 409 } 410 catch (Throwable err) 411 { 412 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 413 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err ); 414 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 415 416 items = new SelectItem[0]; 417 } 418 419 return items; 420 } 421 422 425 public void addSelection(ActionEvent event) 426 { 427 UIGenericPicker picker = (UIGenericPicker)event.getComponent().findComponent("picker"); 428 UISelectOne rolePicker = (UISelectOne)event.getComponent().findComponent("roles"); 429 430 String [] results = picker.getSelectedResults(); 431 if (results != null) 432 { 433 String role = (String )rolePicker.getValue(); 434 435 if (role != null) 436 { 437 for (int i=0; i<results.length; i++) 438 { 439 String authority = results[i]; 440 441 boolean foundExisting = false; 443 for (int n=0; n<this.userGroupRoles.size(); n++) 444 { 445 UserGroupRole wrapper = this.userGroupRoles.get(n); 446 if (authority.equals(wrapper.getAuthority()) && 447 role.equals(wrapper.getRole())) 448 { 449 foundExisting = true; 450 break; 451 } 452 } 453 454 if (foundExisting == false) 455 { 456 StringBuilder label = new StringBuilder (64); 457 458 AuthorityType authType = AuthorityType.getAuthorityType(authority); 460 if (authType == AuthorityType.GUEST || authType == AuthorityType.USER) 461 { 462 if (authType == AuthorityType.GUEST || this.personService.personExists(authority) == true) 463 { 464 NodeRef ref = this.personService.getPerson(authority); 466 String firstName = (String )this.nodeService.getProperty(ref, ContentModel.PROP_FIRSTNAME); 467 String lastName = (String )this.nodeService.getProperty(ref, ContentModel.PROP_LASTNAME); 468 469 label.append(firstName) 470 .append(" ") 471 .append(lastName != null ? lastName : "") 472 .append(" (") 473 .append(Application.getMessage(FacesContext.getCurrentInstance(), role)) 474 .append(")"); 475 } 476 } 477 else 478 { 479 label.append(authority.substring(PermissionService.GROUP_PREFIX.length())) 481 .append(" (") 482 .append(Application.getMessage(FacesContext.getCurrentInstance(), role)) 483 .append(")"); 484 } 485 486 this.userGroupRoles.add(new UserGroupRole(authority, role, label.toString())); 487 } 488 } 489 } 490 } 491 } 492 493 496 public void removeSelection(ActionEvent event) 497 { 498 UserGroupRole wrapper = (UserGroupRole)this.userRolesDataModel.getRowData(); 499 if (wrapper != null) 500 { 501 this.userGroupRoles.remove(wrapper); 502 } 503 } 504 505 510 public SelectItem[] getFilters() 511 { 512 ResourceBundle bundle = Application.getBundle(FacesContext.getCurrentInstance()); 513 514 return new SelectItem[] { 515 new SelectItem("0", bundle.getString(MSG_USERS)), 516 new SelectItem("1", bundle.getString(MSG_GROUPS)) }; 517 } 518 519 522 public SelectItem[] getRoles() 523 { 524 ResourceBundle bundle = Application.getBundle(FacesContext.getCurrentInstance()); 525 526 Set <String > perms = getPermissionsForType(); 528 SelectItem[] roles = new SelectItem[perms.size()]; 529 int index = 0; 530 for (String permission : perms) 531 { 532 String displayLabel = bundle.getString(permission); 533 roles[index++] = new SelectItem(permission, displayLabel); 534 } 535 536 return roles; 537 } 538 539 542 public String getNotify() 543 { 544 return this.notify; 545 } 546 547 550 public void setNotify(String notify) 551 { 552 this.notify = notify; 553 } 554 555 558 public String getAutomaticText() 559 { 560 return this.automaticText; 561 } 562 563 566 public void setAutomaticText(String automaticText) 567 { 568 this.automaticText = automaticText; 569 } 570 571 574 public String getBody() 575 { 576 return this.body; 577 } 578 579 582 public void setBody(String body) 583 { 584 this.body = body; 585 } 586 587 590 public String getSubject() 591 { 592 return this.subject; 593 } 594 595 598 public void setSubject(String subject) 599 { 600 this.subject = subject; 601 } 602 603 606 public String getStepDescription() 607 { 608 String stepDesc = null; 609 610 switch (this.currentStep) 611 { 612 case 1: 613 { 614 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), getStep1DescriptionText()); 615 break; 616 } 617 case 2: 618 { 619 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), STEP2_DESCRIPTION_ID); 620 break; 621 } 622 default: 623 { 624 stepDesc = ""; 625 } 626 } 627 628 return stepDesc; 629 } 630 631 634 public String getStepTitle() 635 { 636 String stepTitle = null; 637 638 switch (this.currentStep) 639 { 640 case 1: 641 { 642 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP1_TITLE_ID); 643 break; 644 } 645 case 2: 646 { 647 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP2_TITLE_ID); 648 break; 649 } 650 default: 651 { 652 stepTitle = ""; 653 } 654 } 655 656 return stepTitle; 657 } 658 659 662 public String getStepInstructions() 663 { 664 String stepInstruction = null; 665 666 switch (this.currentStep) 667 { 668 case 2: 669 { 670 stepInstruction = Application.getMessage(FacesContext.getCurrentInstance(), FINISH_INSTRUCTION_ID); 671 break; 672 } 673 default: 674 { 675 stepInstruction = Application.getMessage(FacesContext.getCurrentInstance(), DEFAULT_INSTRUCTION_ID); 676 } 677 } 678 679 return stepInstruction; 680 } 681 682 685 public String next() 686 { 687 String outcome = super.next(); 688 689 if (outcome.equals("notify")) 690 { 691 FacesContext context = FacesContext.getCurrentInstance(); 692 693 StringBuilder buf = new StringBuilder (256); 695 696 String personName = Application.getCurrentUser(context).getFullName(getNodeService()); 697 String msgInvitedTo = Application.getMessage(context, MSG_INVITED_TO); 698 Node node = this.getNode(); 699 String path = this.nodeService.getPath(node.getNodeRef()).toDisplayPath(this.nodeService); 700 buf.append(MessageFormat.format(msgInvitedTo, new Object [] { 701 path + '/' + node.getName(), 702 personName}) ); 703 704 this.internalSubject = buf.toString(); 705 706 buf.append("<br>"); 707 708 String msgRole = Application.getMessage(context, MSG_INVITED_ROLE); 709 String roleText; 710 if (this.userGroupRoles.size() != 0) 711 { 712 String roleMsg = Application.getMessage(context, userGroupRoles.get(0).getRole()); 713 roleText = MessageFormat.format(msgRole, roleMsg); 714 } 715 else 716 { 717 roleText = MessageFormat.format(msgRole, "[role]"); 718 } 719 720 buf.append(roleText); 721 722 this.automaticText = buf.toString(); 723 } 724 725 return outcome; 726 } 727 728 731 protected String determineOutcomeForStep(int step) 732 { 733 String outcome = null; 734 735 switch(step) 736 { 737 case 1: 738 { 739 outcome = "invite"; 740 break; 741 } 742 case 2: 743 { 744 outcome = "notify"; 745 break; 746 } 747 default: 748 { 749 outcome = CANCEL_OUTCOME; 750 } 751 } 752 753 return outcome; 754 } 755 756 759 public static class UserGroupRole 760 { 761 public UserGroupRole(String authority, String role, String label) 762 { 763 this.authority = authority; 764 this.role = role; 765 this.label = label; 766 } 767 768 public String getAuthority() 769 { 770 return this.authority; 771 } 772 773 public String getRole() 774 { 775 return this.role; 776 } 777 778 public String getLabel() 779 { 780 return this.label; 781 } 782 783 private String authority; 784 private String role; 785 private String label; 786 } 787 } 788 | Popular Tags |