View Javadoc

1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.actions.user;
20  
21  import java.io.IOException;
22  import java.util.Locale;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.commons.beanutils.PropertyUtils;
29  import org.apache.log4j.Logger;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  import org.apache.struts.action.ActionMessage;
34  import org.apache.struts.action.ActionMessages;
35  import org.itracker.core.resources.ITrackerResources;
36  import org.itracker.model.User;
37  import org.itracker.services.ConfigurationService;
38  import org.itracker.services.UserService;
39  import org.itracker.services.exceptions.PasswordException;
40  import org.itracker.services.util.UserUtilities;
41  import org.itracker.web.actions.base.ItrackerBaseAction;
42  
43  public class ForgotPasswordAction extends ItrackerBaseAction {
44  	private static final Logger log = Logger.getLogger(ForgotPasswordAction.class);
45  
46  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47  
48  		ActionMessages errors = new ActionMessages();
49  
50  		try {
51  			ConfigurationService configurationService = getITrackerServices().getConfigurationService();
52  			UserService userService = getITrackerServices().getUserService();
53  
54  			if(! configurationService.getBooleanProperty("allow_forgot_password", true)) {
55  				throw new PasswordException(PasswordException.FEATURE_DISABLED);
56  			}
57  
58  			String login = (String) PropertyUtils.getSimpleProperty(form, "login");
59  			String lastName = (String) PropertyUtils.getSimpleProperty(form, "lastName");
60  
61  			if(login != null && lastName != null && ! login.equals("") && ! lastName.equals("")) {
62  				User user = null;
63                  Locale locale = null;
64  				try {
65  					user = userService.getUserByLogin(login);
66  					if(user == null) {
67  						throw new PasswordException(PasswordException.UNKNOWN_USER);
68  					}
69  					if(user.getLastName() == null || ! user.getLastName().equalsIgnoreCase(lastName)) {
70  						throw new PasswordException(PasswordException.INVALID_NAME);
71  					}
72  					if(user.getEmail() == null || user.getEmail().equals("")) {
73  						throw new PasswordException(PasswordException.INVALID_EMAIL);
74  					}
75  					if(user.getStatus() != UserUtilities.STATUS_ACTIVE) {
76  						throw new PasswordException(PasswordException.INACTIVE_ACCOUNT);
77  					}
78  
79  					if(log.isDebugEnabled()) {
80  						log.debug("ForgotPasswordHandler found matching user: " + user.getFirstName() + " " + user.getLastName() + "(" + user.getLogin() + ")");
81  					}
82  
83                      locale = ITrackerResources.getLocale(user.getPreferences().getUserLocale());
84  					String subject = ITrackerResources.getString("itracker.email.forgotpass.subject", locale);
85  					StringBuffer msgText = new StringBuffer();
86  					msgText.append(ITrackerResources.getString("itracker.email.forgotpass.body", locale));
87  					String newPass = userService.generateUserPassword(user);
88  					//user.setPassword(newPass);
89  					userService.updateUser(user);
90  					msgText.append(ITrackerResources.getString("itracker.web.attr.password", locale)).append(": ").append(newPass);
91  
92  					getITrackerServices().getEmailService()
93  					.sendEmail(user.getEmail(), subject, msgText.toString());
94  				} catch(PasswordException pe) {
95  					if(log.isDebugEnabled()) {
96  						log.debug("Password Exception for user " + login + ". Type = " + pe.getType());
97  					}
98  					if(pe.getType() == PasswordException.INVALID_NAME) {
99  						errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.forgotpass.lastname"));
100 					} else if(pe.getType() == PasswordException.INVALID_EMAIL) {
101 						errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.forgotpass.invalidemail"));
102 					} else if(pe.getType() == PasswordException.INACTIVE_ACCOUNT) {
103 						errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.forgotpass.inactive"));
104 					} else if(pe.getType() == PasswordException.UNKNOWN_USER) {
105 						errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.forgotpass.unknown"));
106 					}
107 				}
108 			}
109 		} catch(PasswordException pe) {
110 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.notenabled"));
111 			log.error("Forgot Password function has been disabled.", pe);
112 		} catch(Exception e) {
113 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.forgotpass.system"));
114 			log.error("Error during password retrieval.", e);
115 		}
116 
117 		if(! errors.isEmpty()) {
118 			saveErrors(request, errors);
119 			return (mapping.findForward("forgotpassword"));
120 		}
121 
122 		errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.message.forgotpass"));
123 		saveErrors(request, errors);
124 		return mapping.findForward("success");
125 	}
126 
127 }