1 17 package org.alfresco.web.bean.wizard; 18 19 import java.io.Serializable ; 20 import java.text.MessageFormat ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.ResourceBundle ; 25 26 import javax.faces.application.FacesMessage; 27 import javax.faces.component.UIComponent; 28 import javax.faces.context.FacesContext; 29 import javax.faces.event.ActionEvent; 30 import javax.faces.validator.ValidatorException; 31 import javax.transaction.UserTransaction ; 32 33 import org.alfresco.error.AlfrescoRuntimeException; 34 import org.alfresco.model.ContentModel; 35 import org.alfresco.service.cmr.repository.ChildAssociationRef; 36 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 37 import org.alfresco.service.cmr.repository.NodeRef; 38 import org.alfresco.service.cmr.security.AuthenticationService; 39 import org.alfresco.service.cmr.security.OwnableService; 40 import org.alfresco.service.cmr.security.PermissionService; 41 import org.alfresco.service.cmr.security.PersonService; 42 import org.alfresco.service.namespace.NamespaceService; 43 import org.alfresco.service.namespace.QName; 44 import org.alfresco.web.app.Application; 45 import org.alfresco.web.app.context.UIContextService; 46 import org.alfresco.web.bean.repository.Node; 47 import org.alfresco.web.bean.repository.Repository; 48 import org.alfresco.web.bean.users.UsersBean; 49 import org.alfresco.web.config.ClientConfigElement; 50 import org.alfresco.web.ui.common.Utils; 51 import org.alfresco.web.ui.common.component.UIActionLink; 52 import org.apache.log4j.Logger; 53 54 57 public class NewUserWizard extends AbstractWizardBean 58 { 59 private static Logger logger = Logger.getLogger(NewUserWizard.class); 60 61 private static final String WIZARD_TITLE_NEW_ID = "new_user_title"; 62 private static final String WIZARD_DESC_NEW_ID = "new_user_desc"; 63 private static final String WIZARD_TITLE_EDIT_ID = "new_user_title_edit"; 64 private static final String WIZARD_DESC_EDIT_ID = "new_user_desc_edit"; 65 private static final String STEP1_TITLE_ID = "new_user_step1_title"; 66 private static final String STEP1_DESCRIPTION_ID = "new_user_step1_desc"; 67 private static final String STEP2_TITLE_ID = "new_user_step2_title"; 68 private static final String STEP2_DESCRIPTION_ID = "new_user_step2_desc"; 69 private static final String FINISH_INSTRUCTION_ID = "new_user_finish_instruction"; 70 private static final String ERROR = "error_person"; 71 72 73 private String firstName = null; 74 private String lastName = null; 75 private String userName = null; 76 private String password = null; 77 private String confirm = null; 78 private String email = null; 79 private String companyId = null; 80 private String homeSpaceName = ""; 81 private NodeRef homeSpaceLocation = null; 82 83 84 private AuthenticationService authenticationService; 85 86 87 private NamespaceService namespaceService; 88 89 90 private PermissionService permissionService; 91 92 93 private PersonService personService; 94 95 96 private OwnableService ownableService; 97 98 99 private Node person = null; 100 101 102 private NodeRef peopleRef = null; 103 104 105 private NodeRef companyHomeSpaceRef = null; 106 107 108 111 public void setAuthenticationService(AuthenticationService authenticationService) 112 { 113 this.authenticationService = authenticationService; 114 } 115 116 119 public void setNamespaceService(NamespaceService namespaceService) 120 { 121 this.namespaceService = namespaceService; 122 } 123 124 127 public void setPermissionService(PermissionService permissionService) 128 { 129 this.permissionService = permissionService; 130 } 131 132 135 public void setPersonService(PersonService personService) 136 { 137 this.personService = personService; 138 } 139 140 143 public void setOwnableService(OwnableService ownableService) 144 { 145 this.ownableService = ownableService; 146 } 147 148 151 public void init() 152 { 153 super.init(); 154 155 this.firstName = ""; 157 this.lastName = ""; 158 this.userName = ""; 159 this.password = ""; 160 this.confirm = ""; 161 this.email = ""; 162 this.companyId = ""; 163 this.homeSpaceName = ""; 164 this.homeSpaceLocation = getCompanyHomeSpace(); 165 } 166 167 170 public void populate() 171 { 172 Map <String , Object > props = getPerson().getProperties(); 174 175 this.firstName = (String ) props.get("firstName"); 176 this.lastName = (String ) props.get("lastName"); 177 this.userName = (String ) props.get("userName"); 178 this.password = ""; 179 this.confirm = ""; 180 this.email = (String ) props.get("email"); 181 this.companyId = (String ) props.get("organizationId"); 182 183 this.homeSpaceLocation = null; this.homeSpaceName = ""; NodeRef homeFolderRef = (NodeRef) props.get("homeFolder"); 187 if (this.nodeService.exists(homeFolderRef) == true) 188 { 189 ChildAssociationRef childAssocRef = this.nodeService.getPrimaryParent(homeFolderRef); 190 NodeRef parentRef = childAssocRef.getParentRef(); 191 if (this.nodeService.getRootNode(Repository.getStoreRef()).equals(parentRef) == false) 192 { 193 this.homeSpaceLocation = parentRef; 194 this.homeSpaceName = Repository.getNameForNode(nodeService, homeFolderRef); 195 } 196 else 197 { 198 this.homeSpaceLocation = homeFolderRef; 199 } 200 } 201 202 if (logger.isDebugEnabled()) 203 logger.debug("Edit user home space location: " + homeSpaceLocation + " home space name: " + homeSpaceName); 204 } 205 206 209 public String getWizardDescription() 210 { 211 if (this.editMode) 212 { 213 return Application.getMessage(FacesContext.getCurrentInstance(), WIZARD_DESC_EDIT_ID); 214 } 215 else 216 { 217 return Application.getMessage(FacesContext.getCurrentInstance(), WIZARD_DESC_NEW_ID); 218 } 219 } 220 221 224 public String getWizardTitle() 225 { 226 if (this.editMode) 227 { 228 return Application.getMessage(FacesContext.getCurrentInstance(), WIZARD_TITLE_EDIT_ID); 229 } 230 else 231 { 232 return Application.getMessage(FacesContext.getCurrentInstance(), WIZARD_TITLE_NEW_ID); 233 } 234 } 235 236 239 public String getStepTitle() 240 { 241 String stepTitle = null; 242 243 switch (this.currentStep) 244 { 245 case 1: 246 { 247 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP1_TITLE_ID); 248 break; 249 } 250 case 2: 251 { 252 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP2_TITLE_ID); 253 break; 254 } 255 case 3: 256 { 257 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), SUMMARY_TITLE_ID); 258 break; 259 } 260 default: 261 { 262 stepTitle = ""; 263 } 264 } 265 266 return stepTitle; 267 } 268 269 272 public String getStepDescription() 273 { 274 String stepDesc = null; 275 276 switch (this.currentStep) 277 { 278 case 1: 279 { 280 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), STEP1_DESCRIPTION_ID); 281 break; 282 } 283 case 2: 284 { 285 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), STEP2_DESCRIPTION_ID); 286 break; 287 } 288 case 3: 289 { 290 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), SUMMARY_DESCRIPTION_ID); 291 break; 292 } 293 default: 294 { 295 stepDesc = ""; 296 } 297 } 298 299 return stepDesc; 300 } 301 302 305 public String getStepInstructions() 306 { 307 String stepInstruction = null; 308 309 switch (this.currentStep) 310 { 311 case 3: 312 { 313 stepInstruction = Application.getMessage(FacesContext.getCurrentInstance(), FINISH_INSTRUCTION_ID); 314 break; 315 } 316 default: 317 { 318 stepInstruction = Application.getMessage(FacesContext.getCurrentInstance(), DEFAULT_INSTRUCTION_ID); 319 } 320 } 321 322 return stepInstruction; 323 } 324 325 328 protected String determineOutcomeForStep(int step) 329 { 330 String outcome = null; 331 332 switch (step) 333 { 334 case 1: 335 { 336 outcome = "person-properties"; 337 break; 338 } 339 case 2: 340 { 341 outcome = "user-properties"; 342 break; 343 } 344 case 3: 345 { 346 outcome = "summary"; 347 break; 348 } 349 default: 350 { 351 outcome = CANCEL_OUTCOME; 352 } 353 } 354 355 return outcome; 356 } 357 358 361 public String finish() 362 { 363 String outcome = FINISH_OUTCOME; 364 365 UserTransaction tx = null; 367 368 try 369 { 370 FacesContext context = FacesContext.getCurrentInstance(); 371 tx = Repository.getUserTransaction(context); 372 tx.begin(); 373 374 if (this.editMode) 375 { 376 NodeRef nodeRef = getPerson().getNodeRef(); 378 379 Map <QName, Serializable > props = this.nodeService.getProperties(nodeRef); 380 props.put(ContentModel.PROP_USERNAME, this.userName); 381 props.put(ContentModel.PROP_FIRSTNAME, this.firstName); 382 props.put(ContentModel.PROP_LASTNAME, this.lastName); 383 384 NodeRef newHomeFolderRef; 386 NodeRef oldHomeFolderRef = (NodeRef)this.nodeService.getProperty(nodeRef, ContentModel.PROP_HOMEFOLDER); 387 boolean moveHomeSpace = false; 388 boolean renameHomeSpace = false; 389 if (oldHomeFolderRef != null && this.nodeService.exists(oldHomeFolderRef) == true) 390 { 391 ChildAssociationRef childAssocRef = this.nodeService.getPrimaryParent(oldHomeFolderRef); 393 NodeRef currentHomeSpaceLocation = childAssocRef.getParentRef(); 394 if (this.homeSpaceName.length() != 0) 395 { 396 if (currentHomeSpaceLocation.equals(this.homeSpaceLocation) == false && 397 oldHomeFolderRef.equals(this.homeSpaceLocation) == false && 398 currentHomeSpaceLocation.equals(getCompanyHomeSpace()) == false) 399 { 400 moveHomeSpace = true; 401 } 402 403 String oldHomeSpaceName = Repository.getNameForNode(nodeService, oldHomeFolderRef); 404 if (oldHomeSpaceName.equals(this.homeSpaceName) == false && 405 oldHomeFolderRef.equals(this.homeSpaceLocation) == false) 406 { 407 renameHomeSpace = true; 408 } 409 } 410 } 411 412 if (logger.isDebugEnabled()) 413 logger.debug("Moving space: " + moveHomeSpace + " and renaming space: " + renameHomeSpace); 414 415 if (moveHomeSpace == false && renameHomeSpace == false) 416 { 417 if (this.homeSpaceLocation != null && this.homeSpaceName.length() != 0) 418 { 419 newHomeFolderRef = createHomeSpace(this.homeSpaceLocation.getId(), this.homeSpaceName, false); 420 } 421 else if (this.homeSpaceLocation != null) 422 { 423 newHomeFolderRef = this.homeSpaceLocation; 426 427 429 } 430 else 431 { 432 newHomeFolderRef = getCompanyHomeSpace(); 434 } 435 } 436 else 437 { 438 if (moveHomeSpace == true) 440 { 441 this.nodeService.moveNode( 442 oldHomeFolderRef, 443 this.homeSpaceLocation, 444 ContentModel.ASSOC_CONTAINS, 445 this.nodeService.getPrimaryParent(oldHomeFolderRef).getQName()); 446 } 447 newHomeFolderRef = oldHomeFolderRef; 449 if (renameHomeSpace == true) 450 { 451 this.nodeService.setProperty(newHomeFolderRef, ContentModel.PROP_NAME, this.homeSpaceName); 453 } 454 } 455 456 props.put(ContentModel.PROP_HOMEFOLDER, newHomeFolderRef); 457 props.put(ContentModel.PROP_EMAIL, this.email); 458 props.put(ContentModel.PROP_ORGID, this.companyId); 459 this.nodeService.setProperties(nodeRef, props); 460 461 } 464 else 465 { 466 if (this.password.equals(this.confirm)) 467 { 468 if (!this.personService.getUserNamesAreCaseSensitive()) 469 { 470 this.userName = this.userName.toLowerCase(); 471 } 472 473 Map <QName, Serializable > props = new HashMap <QName, Serializable >(7, 1.0f); 475 props.put(ContentModel.PROP_USERNAME, this.userName); 476 props.put(ContentModel.PROP_FIRSTNAME, this.firstName); 477 props.put(ContentModel.PROP_LASTNAME, this.lastName); 478 NodeRef homeSpaceNodeRef; 479 if (this.homeSpaceLocation != null && this.homeSpaceName.length() != 0) 480 { 481 homeSpaceNodeRef = createHomeSpace(this.homeSpaceLocation.getId(), this.homeSpaceName, true); 483 } 484 else if (this.homeSpaceLocation != null) 485 { 486 homeSpaceNodeRef = homeSpaceLocation; 488 setupHomeSpacePermissions(homeSpaceNodeRef); 489 } 490 else 491 { 492 homeSpaceNodeRef = getCompanyHomeSpace(); 494 } 495 props.put(ContentModel.PROP_HOMEFOLDER, homeSpaceNodeRef); 496 props.put(ContentModel.PROP_EMAIL, this.email); 497 props.put(ContentModel.PROP_ORGID, this.companyId); 498 499 String assocName = QName.createValidLocalName(this.userName); 501 NodeRef newPerson = this.personService.createPerson(props); 502 503 this.permissionService.setPermission(newPerson, this.userName, permissionService.getAllPermission(), true); 505 506 if (logger.isDebugEnabled()) logger.debug("Created Person node for username: " + this.userName); 507 508 this.authenticationService.createAuthentication(this.userName, this.password.toCharArray()); 510 511 if (logger.isDebugEnabled()) logger.debug("Created User Authentication instance for username: " + this.userName); 512 } 513 else 514 { 515 outcome = null; 516 Utils.addErrorMessage(Application.getMessage(context, UsersBean.ERROR_PASSWORD_MATCH)); 517 } 518 } 519 520 tx.commit(); 522 523 invalidateUserList(); 525 } 526 catch (Throwable e) 527 { 528 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 530 Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), ERROR), e 531 .getMessage()), e); 532 outcome = null; 533 } 534 535 return outcome; 536 } 537 538 541 public String getSummary() 542 { 543 String homeSpaceLabel = this.homeSpaceName; 544 if (this.homeSpaceName.length() == 0 && this.homeSpaceLocation != null) 545 { 546 homeSpaceLabel = Repository.getNameForNode(this.nodeService, this.homeSpaceLocation); 547 } 548 549 ResourceBundle bundle = Application.getBundle(FacesContext.getCurrentInstance()); 550 551 return buildSummary(new String [] { bundle.getString("name"), bundle.getString("username"), 552 bundle.getString("password"), bundle.getString("homespace") }, new String [] { 553 this.firstName + " " + this.lastName, this.userName, "********", homeSpaceLabel }); 554 } 555 556 559 public void setupUsers(ActionEvent event) 560 { 561 invalidateUserList(); 562 } 563 564 568 public void startWizardForEdit(ActionEvent event) 569 { 570 UIActionLink link = (UIActionLink) event.getComponent(); 571 Map <String , String > params = link.getParameterMap(); 572 String id = params.get("id"); 573 if (id != null && id.length() != 0) 574 { 575 try 576 { 577 NodeRef ref = new NodeRef(Repository.getStoreRef(), id); 579 Node node = new Node(ref); 580 581 setPerson(node); 583 584 this.editMode = true; 586 587 init(); 590 populate(); 591 592 invalidateUserList(); 595 596 if (logger.isDebugEnabled()) logger.debug("Started wizard : " + getWizardTitle() + " for editing"); 597 } 598 catch (InvalidNodeRefException refErr) 599 { 600 Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), 601 Repository.ERROR_NODEREF), new Object [] { id })); 602 } 603 } 604 else 605 { 606 setPerson(null); 607 } 608 } 609 610 613 public String getCompanyId() 614 { 615 return this.companyId; 616 } 617 618 622 public void setCompanyId(String companyId) 623 { 624 this.companyId = companyId; 625 } 626 627 630 public String getEmail() 631 { 632 return this.email; 633 } 634 635 639 public void setEmail(String email) 640 { 641 this.email = email; 642 } 643 644 647 public String getFirstName() 648 { 649 return this.firstName; 650 } 651 652 655 public void setFirstName(String firstName) 656 { 657 this.firstName = firstName; 658 } 659 660 663 public NodeRef getHomeSpaceLocation() 664 { 665 return this.homeSpaceLocation; 666 } 667 668 671 public void setHomeSpaceLocation(NodeRef homeSpaceLocation) 672 { 673 this.homeSpaceLocation = homeSpaceLocation; 674 } 675 676 679 public String getHomeSpaceName() 680 { 681 return this.homeSpaceName; 682 } 683 684 687 public void setHomeSpaceName(String homeSpaceName) 688 { 689 this.homeSpaceName = homeSpaceName; 690 } 691 692 695 public String getLastName() 696 { 697 return this.lastName; 698 } 699 700 703 public void setLastName(String lastName) 704 { 705 this.lastName = lastName; 706 } 707 708 711 public String getUserName() 712 { 713 return this.userName; 714 } 715 716 719 public void setUserName(String userName) 720 { 721 this.userName = userName; 722 } 723 724 727 public String getPassword() 728 { 729 return this.password; 730 } 731 732 735 public void setPassword(String password) 736 { 737 this.password = password; 738 } 739 740 743 public String getConfirm() 744 { 745 return this.confirm; 746 } 747 748 751 public void setConfirm(String confirm) 752 { 753 this.confirm = confirm; 754 } 755 756 759 public Node getPerson() 760 { 761 return this.person; 762 } 763 764 767 public void setPerson(Node person) 768 { 769 this.person = person; 770 } 771 772 public boolean getEditMode() 773 { 774 return this.editMode; 775 } 776 777 778 781 784 public void validatePassword(FacesContext context, UIComponent component, Object value) throws ValidatorException 785 { 786 String pass = (String ) value; 787 if (pass.length() < 5 || pass.length() > 12) 788 { 789 String err = "Password must be between 5 and 12 characters in length."; 790 throw new ValidatorException(new FacesMessage(err)); 791 } 792 793 for (int i = 0; i < pass.length(); i++) 794 { 795 if (Character.isLetterOrDigit(pass.charAt(i)) == false) 796 { 797 String err = "Password can only contain characters or digits."; 798 throw new ValidatorException(new FacesMessage(err)); 799 } 800 } 801 } 802 803 806 public void validateUsername(FacesContext context, UIComponent component, Object value) throws ValidatorException 807 { 808 String pass = (String ) value; 809 if (pass.length() < 5 || pass.length() > 12) 810 { 811 String err = "Username must be between 5 and 12 characters in length."; 812 throw new ValidatorException(new FacesMessage(err)); 813 } 814 815 for (int i = 0; i < pass.length(); i++) 816 { 817 if (Character.isLetterOrDigit(pass.charAt(i)) == false) 818 { 819 String err = "Username can only contain characters or digits."; 820 throw new ValidatorException(new FacesMessage(err)); 821 } 822 } 823 } 824 825 826 829 834 private NodeRef getCompanyHomeSpace() 835 { 836 if (this.companyHomeSpaceRef == null) 837 { 838 String companyXPath = Application.getRootPath(FacesContext.getCurrentInstance()); 839 840 NodeRef rootNodeRef = this.nodeService.getRootNode(Repository.getStoreRef()); 841 List <NodeRef> nodes = this.searchService.selectNodes(rootNodeRef, companyXPath, null, this.namespaceService, 842 false); 843 844 if (nodes.size() == 0) 845 { 846 throw new IllegalStateException ("Unable to find company home space path: " + companyXPath); 847 } 848 849 this.companyHomeSpaceRef = nodes.get(0); 850 } 851 852 return this.companyHomeSpaceRef; 853 } 854 855 868 private NodeRef createHomeSpace(String locationId, String spaceName, boolean error) 869 { 870 String homeSpaceId = locationId; 871 NodeRef homeSpaceNodeRef = null; 872 if (spaceName != null && spaceName.length() != 0) 873 { 874 NodeRef parentRef = new NodeRef(Repository.getStoreRef(), locationId); 875 876 List <ChildAssociationRef> children = this.nodeService.getChildAssocs(parentRef); 880 for (ChildAssociationRef ref : children) 881 { 882 String childNodeName = (String ) this.nodeService.getProperty(ref.getChildRef(), ContentModel.PROP_NAME); 883 if (spaceName.equals(childNodeName)) 884 { 885 if (error) 886 { 887 throw new AlfrescoRuntimeException("A Home Space with the same name already exists."); 888 } 889 else 890 { 891 return ref.getChildRef(); 892 } 893 } 894 } 895 896 String qname = QName.createValidLocalName(spaceName); 899 ChildAssociationRef assocRef = this.nodeService.createNode(parentRef, ContentModel.ASSOC_CONTAINS, 900 QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, qname), ContentModel.TYPE_FOLDER); 901 902 NodeRef nodeRef = assocRef.getChildRef(); 903 904 this.nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, spaceName); 906 907 if (logger.isDebugEnabled()) logger.debug("Created Home Space for with name: " + spaceName); 908 909 Map <QName, Serializable > uiFacetsProps = new HashMap <QName, Serializable >(3); 911 uiFacetsProps.put(ContentModel.PROP_ICON, NewSpaceWizard.SPACE_ICON_DEFAULT); 912 uiFacetsProps.put(ContentModel.PROP_TITLE, spaceName); 913 this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_UIFACETS, uiFacetsProps); 914 915 setupHomeSpacePermissions(nodeRef); 916 917 homeSpaceNodeRef = nodeRef; 919 homeSpaceId = nodeRef.getId(); 920 } 921 922 return homeSpaceNodeRef; 923 } 924 925 930 private void setupHomeSpacePermissions(NodeRef homeSpaceRef) 931 { 932 this.permissionService.setPermission(homeSpaceRef, this.userName, permissionService.getAllPermission(), true); 935 936 String permission = getDefaultPermission(); 939 if (permission != null && permission.length() != 0) 940 { 941 this.permissionService.setPermission(homeSpaceRef, permissionService.getAllAuthorities(), permission, true); 942 } 943 944 this.ownableService.setOwner(homeSpaceRef, this.userName); 946 this.permissionService.setPermission(homeSpaceRef, permissionService.getOwnerAuthority(), permissionService.getAllPermission(), true); 947 948 this.permissionService.setInheritParentPermissions(homeSpaceRef, false); 950 } 951 952 955 private String getDefaultPermission() 956 { 957 ClientConfigElement config = Application.getClientConfig(FacesContext.getCurrentInstance()); 958 return config.getHomeSpacePermission(); 959 } 960 961 private void invalidateUserList() 962 { 963 UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans(); 964 } 965 } 966 | Popular Tags |