private void addNewUser( HttpServletRequest request, HttpServletResponse response, Command command )
{
ArrayList<String> errors = new ArrayList<String>();
HashMap<String, String> fieldMessages = new HashMap<String, String>();
UserManager userManager = new UserManager();
User user = new User();
populateVBOFromRequest(user, request, fieldMessages, true);
String pwd = user.getPwd();
String oldPwd = request.getParameter("oldPwd");
boolean isErrors = false;
boolean isUser = false;
Logger.debug("\n\n adding a new user and role... roles -=" + user.getRoles() + "\n\n");
if ((!oldPwd.equals(pwd)) && (fieldMessages.size() == 0))
{
try
{
// encrypt and set new password
user.setPwd(Encrypt.encrypt(Configuration.getString("keyfile"), pwd));
if (pwd.length() > 40)
{
errors.add("Password length is greater than 40. Please re-enter password.");
isErrors = true;
}
}
catch( Exception ex )
{
Logger.error("\n Could not ENCRYPT password \n", ex);
errors.add("Security errors occurred while processing password");
// request.setAttribute("errorMessage", "Security(encryption)
// errors
// occurred while processing password");
isErrors = true;
}
}
// if user exist add to errors log
isUser = userManager.isUserExist(user.getUserName().toLowerCase());
if (isUser)
{
errors.add("User Name: " + user.getUserName() + " already exist. Please enter another user name.");
isErrors = true;
}
// if no errors and user doesn't already exist add new user record
if ((!isErrors) && (fieldMessages.size() == 0) && (errors.isEmpty()))
{
try
{
int projectId = new ProjectManager().getProjectID(command.getProject());
userManager.addUser(((User) request.getSession().getAttribute("user")), user, projectId);
request.setAttribute("successMessage", "New user created.");
}
catch( NoSuchUserException nsuex )
{
Logger.error("\n Could not add user data \n", nsuex);
// request.setAttribute("errorMessage", "Errors occurred while
// processing
// add new user request...");
errors.add("Errors occurred while processing add new user request...");
isErrors = true;
}
catch( RoleUpdateAddException ruaex )
{
Logger.debug("Couldn't add role for user : " + user.getUserName(), ruaex);
errors.add("Couldn't add role for user : " + user.getUserName());
}
catch( NoSuchProjectException ex )
{
Logger.error("System can't retrieve correct Project" + Calendar.getInstance().getTime().toString());
errors.add("Project does not exist. Please contact an Administrator.");
}
}
if (errors.size() > 0)
{
request.setAttribute("errorMessage", generateErrorMessage(errors));
}
if (fieldMessages.size() > 0)
{
request.setAttribute("fieldMessages", fieldMessages);
isErrors = true;
}
// attach full list of roles to user
try
{
user = new RoleManager().setFullListRoleNames(new ProjectManager().getProjectID(command.getProject()), user);
}
catch( NoSuchProjectException ex )
{
Logger.error("System can't retrieve correct Project" + Calendar.getInstance().getTime().toString());
errors.add("Project does not exist. Please contact an Administrator.");
}
user.setRoles(user.getRoles());
request.setAttribute("user", user);
request.setAttribute("oldPwd", user.getPwd());
if ((errors.size() > 0) || (isErrors) || (fieldMessages.size() > 0))
{
// go back to user.jsp and state error
sendRequestToUserJSP(request, response, command);