KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > wizard > NewUserWizard


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean.wizard;
18
19 import java.io.Serializable JavaDoc;
20 import java.text.MessageFormat JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
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 JavaDoc;
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 /**
55  * @author Kevin Roast
56  */

57 public class NewUserWizard extends AbstractWizardBean
58 {
59    private static Logger logger = Logger.getLogger(NewUserWizard.class);
60
61    private static final String JavaDoc WIZARD_TITLE_NEW_ID = "new_user_title";
62    private static final String JavaDoc WIZARD_DESC_NEW_ID = "new_user_desc";
63    private static final String JavaDoc WIZARD_TITLE_EDIT_ID = "new_user_title_edit";
64    private static final String JavaDoc WIZARD_DESC_EDIT_ID = "new_user_desc_edit";
65    private static final String JavaDoc STEP1_TITLE_ID = "new_user_step1_title";
66    private static final String JavaDoc STEP1_DESCRIPTION_ID = "new_user_step1_desc";
67    private static final String JavaDoc STEP2_TITLE_ID = "new_user_step2_title";
68    private static final String JavaDoc STEP2_DESCRIPTION_ID = "new_user_step2_desc";
69    private static final String JavaDoc FINISH_INSTRUCTION_ID = "new_user_finish_instruction";
70    private static final String JavaDoc ERROR = "error_person";
71
72    /** form variables */
73    private String JavaDoc firstName = null;
74    private String JavaDoc lastName = null;
75    private String JavaDoc userName = null;
76    private String JavaDoc password = null;
77    private String JavaDoc confirm = null;
78    private String JavaDoc email = null;
79    private String JavaDoc companyId = null;
80    private String JavaDoc homeSpaceName = "";
81    private NodeRef homeSpaceLocation = null;
82
83    /** AuthenticationService bean reference */
84    private AuthenticationService authenticationService;
85
86    /** NamespaceService bean reference */
87    private NamespaceService namespaceService;
88
89    /** PermissionService bean reference */
90    private PermissionService permissionService;
91
92    /** PersonService bean reference */
93    private PersonService personService;
94
95    /** OwnableService bean reference */
96    private OwnableService ownableService;
97
98    /** action context */
99    private Node person = null;
100
101    /** ref to system people folder */
102    private NodeRef peopleRef = null;
103
104    /** ref to the company home space folder */
105    private NodeRef companyHomeSpaceRef = null;
106    
107    
108    /**
109     * @param authenticationService The AuthenticationService to set.
110     */

111    public void setAuthenticationService(AuthenticationService authenticationService)
112    {
113       this.authenticationService = authenticationService;
114    }
115
116    /**
117     * @param namespaceService The namespaceService to set.
118     */

119    public void setNamespaceService(NamespaceService namespaceService)
120    {
121       this.namespaceService = namespaceService;
122    }
123
124    /**
125     * @param permissionService The PermissionService to set.
126     */

127    public void setPermissionService(PermissionService permissionService)
128    {
129       this.permissionService = permissionService;
130    }
131
132    /**
133     * @param personService The person service.
134     */

135    public void setPersonService(PersonService personService)
136    {
137       this.personService = personService;
138    }
139
140    /**
141     * @param ownableService The ownableService to set.
142     */

143    public void setOwnableService(OwnableService ownableService)
144    {
145       this.ownableService = ownableService;
146    }
147
148    /**
149     * Initialises the wizard
150     */

151    public void init()
152    {
153       super.init();
154
155       // reset all variables
156
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    /**
168     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#populate()
169     */

170    public void populate()
171    {
172       // set values for edit mode
173
Map JavaDoc<String JavaDoc, Object JavaDoc> props = getPerson().getProperties();
174
175       this.firstName = (String JavaDoc) props.get("firstName");
176       this.lastName = (String JavaDoc) props.get("lastName");
177       this.userName = (String JavaDoc) props.get("userName");
178       this.password = "";
179       this.confirm = "";
180       this.email = (String JavaDoc) props.get("email");
181       this.companyId = (String JavaDoc) props.get("organizationId");
182
183       // calculate home space name and parent space Id from homeFolderId
184
this.homeSpaceLocation = null; // default to Company root space
185
this.homeSpaceName = ""; // default to none set below root
186
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    /**
207     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getWizardDescription()
208     */

209    public String JavaDoc 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    /**
222     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getWizardTitle()
223     */

224    public String JavaDoc 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    /**
237     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getStepTitle()
238     */

239    public String JavaDoc getStepTitle()
240    {
241       String JavaDoc 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    /**
270     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getStepDescription()
271     */

272    public String JavaDoc getStepDescription()
273    {
274       String JavaDoc 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    /**
303     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getStepInstructions()
304     */

305    public String JavaDoc getStepInstructions()
306    {
307       String JavaDoc 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    /**
326     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#determineOutcomeForStep(int)
327     */

328    protected String JavaDoc determineOutcomeForStep(int step)
329    {
330       String JavaDoc 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    /**
359     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#finish()
360     */

361    public String JavaDoc finish()
362    {
363       String JavaDoc outcome = FINISH_OUTCOME;
364
365       // TODO: implement create new Person object from specified details
366
UserTransaction JavaDoc 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             // update the existing node in the repository
377
NodeRef nodeRef = getPerson().getNodeRef();
378             
379             Map JavaDoc<QName, Serializable JavaDoc> 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             // calculate whether we need to move the old home space or create new
385
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                // the original home folder ref exists so may need moving if it has been changed
392
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 JavaDoc 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                   // location selected but no home space name entered,
424
// so the home ref should be set to the newly selected space
425
newHomeFolderRef = this.homeSpaceLocation;
426                   
427                   // set the permissions for this space so the user can access it
428

429                }
430                else
431                {
432                   // nothing selected - use Company Home by default
433
newHomeFolderRef = getCompanyHomeSpace();
434                }
435             }
436             else
437             {
438                // either move, rename or both required
439
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; // ref ID doesn't change
448

449                if (renameHomeSpace == true)
450                {
451                   // change HomeSpace node name
452
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             // TODO: RESET HomeSpace Ref found in top-level navigation bar!
462
// NOTE: not need cos only admin can do this?
463
}
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                // create properties for Person type from submitted Form data
474
Map JavaDoc<QName, Serializable JavaDoc> props = new HashMap JavaDoc<QName, Serializable JavaDoc>(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                   // create new
482
homeSpaceNodeRef = createHomeSpace(this.homeSpaceLocation.getId(), this.homeSpaceName, true);
483                }
484                else if (this.homeSpaceLocation != null)
485                {
486                   // set to existing
487
homeSpaceNodeRef = homeSpaceLocation;
488                   setupHomeSpacePermissions(homeSpaceNodeRef);
489                }
490                else
491                {
492                   // default to Company Home
493
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                // create the node to represent the Person
500
String JavaDoc assocName = QName.createValidLocalName(this.userName);
501                NodeRef newPerson = this.personService.createPerson(props);
502                
503                // ensure the user can access their own Person object
504
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                // create the ACEGI Authentication instance for the new user
509
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          // commit the transaction
521
tx.commit();
522          
523          // reset the richlist component so it rebinds to the users list
524
invalidateUserList();
525       }
526       catch (Throwable JavaDoc e)
527       {
528          // rollback the transaction
529
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc 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    /**
539     * @return Returns the summary data for the wizard.
540     */

541    public String JavaDoc getSummary()
542    {
543       String JavaDoc 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 JavaDoc bundle = Application.getBundle(FacesContext.getCurrentInstance());
550
551       return buildSummary(new String JavaDoc[] { bundle.getString("name"), bundle.getString("username"),
552             bundle.getString("password"), bundle.getString("homespace") }, new String JavaDoc[] {
553             this.firstName + " " + this.lastName, this.userName, "********", homeSpaceLabel });
554    }
555
556    /**
557     * Init the users screen
558     */

559    public void setupUsers(ActionEvent event)
560    {
561       invalidateUserList();
562    }
563
564    /**
565     * Action listener called when the wizard is being launched for editing an
566     * existing node.
567     */

568    public void startWizardForEdit(ActionEvent event)
569    {
570       UIActionLink link = (UIActionLink) event.getComponent();
571       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
572       String JavaDoc id = params.get("id");
573       if (id != null && id.length() != 0)
574       {
575          try
576          {
577             // create the node ref, then our node representation
578
NodeRef ref = new NodeRef(Repository.getStoreRef(), id);
579             Node node = new Node(ref);
580
581             // remember the Person node
582
setPerson(node);
583
584             // set the wizard in edit mode
585
this.editMode = true;
586
587             // populate the wizard's default values with the current value
588
// from the node being edited
589
init();
590             populate();
591
592             // clear the UI state in preparation for finishing the action
593
// and returning to the main page
594
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 JavaDoc[] { id }));
602          }
603       }
604       else
605       {
606          setPerson(null);
607       }
608    }
609
610    /**
611     * @return Returns the companyId.
612     */

613    public String JavaDoc getCompanyId()
614    {
615       return this.companyId;
616    }
617
618    /**
619     * @param companyId
620     * The companyId to set.
621     */

622    public void setCompanyId(String JavaDoc companyId)
623    {
624       this.companyId = companyId;
625    }
626
627    /**
628     * @return Returns the email.
629     */

630    public String JavaDoc getEmail()
631    {
632       return this.email;
633    }
634
635    /**
636     * @param email
637     * The email to set.
638     */

639    public void setEmail(String JavaDoc email)
640    {
641       this.email = email;
642    }
643
644    /**
645     * @return Returns the firstName.
646     */

647    public String JavaDoc getFirstName()
648    {
649       return this.firstName;
650    }
651
652    /**
653     * @param firstName The firstName to set.
654     */

655    public void setFirstName(String JavaDoc firstName)
656    {
657       this.firstName = firstName;
658    }
659
660    /**
661     * @return Returns the homeSpaceLocation.
662     */

663    public NodeRef getHomeSpaceLocation()
664    {
665       return this.homeSpaceLocation;
666    }
667
668    /**
669     * @param homeSpaceLocation The homeSpaceLocation to set.
670     */

671    public void setHomeSpaceLocation(NodeRef homeSpaceLocation)
672    {
673       this.homeSpaceLocation = homeSpaceLocation;
674    }
675
676    /**
677     * @return Returns the homeSpaceName.
678     */

679    public String JavaDoc getHomeSpaceName()
680    {
681       return this.homeSpaceName;
682    }
683
684    /**
685     * @param homeSpaceName The homeSpaceName to set.
686     */

687    public void setHomeSpaceName(String JavaDoc homeSpaceName)
688    {
689       this.homeSpaceName = homeSpaceName;
690    }
691
692    /**
693     * @return Returns the lastName.
694     */

695    public String JavaDoc getLastName()
696    {
697       return this.lastName;
698    }
699
700    /**
701     * @param lastName The lastName to set.
702     */

703    public void setLastName(String JavaDoc lastName)
704    {
705       this.lastName = lastName;
706    }
707
708    /**
709     * @return Returns the userName.
710     */

711    public String JavaDoc getUserName()
712    {
713       return this.userName;
714    }
715
716    /**
717     * @param userName The userName to set.
718     */

719    public void setUserName(String JavaDoc userName)
720    {
721       this.userName = userName;
722    }
723
724    /**
725     * @return Returns the password.
726     */

727    public String JavaDoc getPassword()
728    {
729       return this.password;
730    }
731
732    /**
733     * @param password The password to set.
734     */

735    public void setPassword(String JavaDoc password)
736    {
737       this.password = password;
738    }
739    
740    /**
741     * @return Returns the confirm password.
742     */

743    public String JavaDoc getConfirm()
744    {
745       return this.confirm;
746    }
747
748    /**
749     * @param confirm The confirm password to set.
750     */

751    public void setConfirm(String JavaDoc confirm)
752    {
753       this.confirm = confirm;
754    }
755
756    /**
757     * @return Returns the person context.
758     */

759    public Node getPerson()
760    {
761       return this.person;
762    }
763
764    /**
765     * @param person The person context to set.
766     */

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    // ------------------------------------------------------------------------------
779
// Validator methods
780

781    /**
782     * Validate password field data is acceptable
783     */

784    public void validatePassword(FacesContext context, UIComponent component, Object JavaDoc value) throws ValidatorException
785    {
786       String JavaDoc pass = (String JavaDoc) value;
787       if (pass.length() < 5 || pass.length() > 12)
788       {
789          String JavaDoc 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 JavaDoc err = "Password can only contain characters or digits.";
798             throw new ValidatorException(new FacesMessage(err));
799          }
800       }
801    }
802
803    /**
804     * Validate Username field data is acceptable
805     */

806    public void validateUsername(FacesContext context, UIComponent component, Object JavaDoc value) throws ValidatorException
807    {
808       String JavaDoc pass = (String JavaDoc) value;
809       if (pass.length() < 5 || pass.length() > 12)
810       {
811          String JavaDoc 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 JavaDoc err = "Username can only contain characters or digits.";
820             throw new ValidatorException(new FacesMessage(err));
821          }
822       }
823    }
824    
825    
826    // ------------------------------------------------------------------------------
827
// Helper methods
828

829    /**
830     * Helper to return the company home space
831     *
832     * @return company home space NodeRef
833     */

834    private NodeRef getCompanyHomeSpace()
835    {
836       if (this.companyHomeSpaceRef == null)
837       {
838          String JavaDoc companyXPath = Application.getRootPath(FacesContext.getCurrentInstance());
839          
840          NodeRef rootNodeRef = this.nodeService.getRootNode(Repository.getStoreRef());
841          List JavaDoc<NodeRef> nodes = this.searchService.selectNodes(rootNodeRef, companyXPath, null, this.namespaceService,
842                false);
843          
844          if (nodes.size() == 0)
845          {
846             throw new IllegalStateException JavaDoc("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    /**
856     * Create the specified home space if it does not exist, and return the ID
857     *
858     * @param locationId
859     * Parent location
860     * @param spaceName
861     * Home space to create, can be null to simply return the parent
862     * @param error
863     * True to throw an error if the space already exists, else
864     * ignore and return
865     *
866     * @return ID of the home space
867     */

868    private NodeRef createHomeSpace(String JavaDoc locationId, String JavaDoc spaceName, boolean error)
869    {
870       String JavaDoc homeSpaceId = locationId;
871       NodeRef homeSpaceNodeRef = null;
872       if (spaceName != null && spaceName.length() != 0)
873       {
874          NodeRef parentRef = new NodeRef(Repository.getStoreRef(), locationId);
875          
876          // check for existance of home space with same name - return immediately
877
// if it exists or throw an exception an give user chance to enter another name
878
// TODO: this might be better replaced with an XPath query!
879
List JavaDoc<ChildAssociationRef> children = this.nodeService.getChildAssocs(parentRef);
880          for (ChildAssociationRef ref : children)
881          {
882             String JavaDoc childNodeName = (String JavaDoc) 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          // space does not exist already, create a new Space under it with
897
// the specified name
898
String JavaDoc 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          // set the name property on the node
905
this.nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, spaceName);
906          
907          if (logger.isDebugEnabled()) logger.debug("Created Home Space for with name: " + spaceName);
908          
909          // apply the uifacets aspect - icon, title and description props
910
Map JavaDoc<QName, Serializable JavaDoc> uiFacetsProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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          // return the ID of the created space
918
homeSpaceNodeRef = nodeRef;
919          homeSpaceId = nodeRef.getId();
920       }
921       
922       return homeSpaceNodeRef;
923    }
924
925    /**
926     * Setup the default permissions for this and other users on the Home Space
927     *
928     * @param homeSpaceRef Home Space reference
929     */

930    private void setupHomeSpacePermissions(NodeRef homeSpaceRef)
931    {
932       // Admin Authority has full permissions by default (automatic - set in the permission config)
933
// give full permissions to the new user
934
this.permissionService.setPermission(homeSpaceRef, this.userName, permissionService.getAllPermission(), true);
935       
936       // by default other users will only have GUEST access to the space contents
937
// or whatever is configured as the default in the web-client-xml config
938
String JavaDoc permission = getDefaultPermission();
939       if (permission != null && permission.length() != 0)
940       {
941          this.permissionService.setPermission(homeSpaceRef, permissionService.getAllAuthorities(), permission, true);
942       }
943       
944       // the new user is the OWNER of their own space and always has full permissions
945
this.ownableService.setOwner(homeSpaceRef, this.userName);
946       this.permissionService.setPermission(homeSpaceRef, permissionService.getOwnerAuthority(), permissionService.getAllPermission(), true);
947       
948       // now detach (if we did this first we could not set any permissions!)
949
this.permissionService.setInheritParentPermissions(homeSpaceRef, false);
950    }
951    
952    /**
953     * @return default permission string to set for other users for a new Home Space
954     */

955    private String JavaDoc 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