From 04236a3706610e88e556b948209e12c985e7e30e Mon Sep 17 00:00:00 2001 From: Mark Rogers Date: Fri, 17 Apr 2009 09:56:13 +0000 Subject: [PATCH] Build fix and overhaul of (unused I think) createPerson method git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13990 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/repo/jscript/People.java | 87 ++++++++++++++----- 1 file changed, 65 insertions(+), 22 deletions(-) diff --git a/source/java/org/alfresco/repo/jscript/People.java b/source/java/org/alfresco/repo/jscript/People.java index 2fa6228b40..7a25c9019f 100644 --- a/source/java/org/alfresco/repo/jscript/People.java +++ b/source/java/org/alfresco/repo/jscript/People.java @@ -24,6 +24,7 @@ */ package org.alfresco.repo.jscript; +import java.util.Map; import java.util.Set; import java.util.StringTokenizer; @@ -67,6 +68,7 @@ public final class People extends BaseScopableProcessorExtension private UserNameGenerator usernameGenerator; private PasswordGenerator passwordGenerator; private StoreRef storeRef; + private int numRetries = 10; /** @@ -167,6 +169,10 @@ public final class People extends BaseScopableProcessorExtension /** * Create a Person with a generated user name * + * @param firstName firstName + * @param lastName lastName + * @param emailAddress emailAddress + * * @param createUserAccount * set to 'true' to create a user account for the person with the * generated user name and a generated password @@ -176,41 +182,48 @@ public final class People extends BaseScopableProcessorExtension * @return the person node (type cm:person) created or null if the person * could not be created */ - public ScriptNode createPerson(boolean createUserAccount, boolean setAccountEnabled) + public ScriptNode createPerson(String firstName, String lastName, String emailAddress, boolean createUserAccount, boolean setAccountEnabled) { + ParameterCheck.mandatory("firstName", firstName); + ParameterCheck.mandatory("lastName", lastName); ParameterCheck.mandatory("createUserAccount", createUserAccount); ParameterCheck.mandatory("setAccountEnabled", setAccountEnabled); ScriptNode person = null; // generate user name - String userName = usernameGenerator.generateUserName(); - - // create person if user name does not already exist - if (!personService.personExists(userName)) + for(int i=0; i < numRetries; i++) { - person = createPerson(userName); + String userName = usernameGenerator.generateUserName(firstName, lastName, emailAddress, i); + + // create person if user name does not already exist + if (!personService.personExists(userName)) + { + person = createPerson(userName, firstName, lastName, emailAddress); - if (createUserAccount) - { - // generate password - char[] password = passwordGenerator.generatePassword().toCharArray(); + if (createUserAccount) + { + // generate password + char[] password = passwordGenerator.generatePassword().toCharArray(); - // create account for person with generated userName and - // password - mutableAuthenticationDao.createUser(userName, password); - mutableAuthenticationDao.setEnabled(userName, setAccountEnabled); + // create account for person with generated userName and + // password + mutableAuthenticationDao.createUser(userName, password); + mutableAuthenticationDao.setEnabled(userName, setAccountEnabled); - // TODO glen johnson at alfresco dot com - - // find a more secure way of making generated password - // available. I need to make it available for the invite - // workflow/service - person.getProperties().put("generatedPassword", new String(password)); - person.save(); - } + // TODO glen johnson at alfresco dot com - + // find a more secure way of making generated password + // available. I need to make it available for the invite + // workflow/service + person.getProperties().put("generatedPassword", new String(password)); + person.save(); + } + + return person; + } } - return person; + return null; } /** @@ -275,6 +288,36 @@ public final class People extends BaseScopableProcessorExtension return person; } + /** + * Create a Person with the given user name, firstName, lastName and emailAddress + * + * @param userName the user name of the person to create + * @return the person node (type cm:person) created or null if the user name already exists + */ + public ScriptNode createPerson(String userName, String firstName, String lastName, String emailAddress) + { + ParameterCheck.mandatoryString("userName", userName); + ParameterCheck.mandatoryString("firstName", firstName); + ParameterCheck.mandatoryString("lastName", lastName); + ParameterCheck.mandatoryString("emailAddress", emailAddress); + + ScriptNode person = null; + + PropertyMap properties = new PropertyMap(); + properties.put(ContentModel.PROP_USERNAME, userName); + properties.put(ContentModel.PROP_FIRSTNAME, firstName); + properties.put(ContentModel.PROP_LASTNAME, lastName); + properties.put(ContentModel.PROP_EMAIL, emailAddress); + + if (!personService.personExists(userName)) + { + NodeRef personRef = personService.createPerson(properties); + person = new ScriptNode(personRef, services, getScope()); + } + + return person; + } + /** * Get the collection of people stored in the repository. * An optional filter query may be provided by which to filter the people collection.