KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > ForgotPasswordAction


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 cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.commons.beanutils.*;
31 import org.apache.struts.action.*;
32 import org.apache.struts.util.*;
33
34 import cowsultants.itracker.ejb.client.exceptions.*;
35 import cowsultants.itracker.ejb.client.interfaces.*;
36 import cowsultants.itracker.ejb.client.models.*;
37 import cowsultants.itracker.ejb.client.resources.*;
38 import cowsultants.itracker.ejb.client.util.*;
39 import cowsultants.itracker.web.forms.*;
40 import cowsultants.itracker.web.util.*;
41
42
43 public class ForgotPasswordAction extends ITrackerAction {
44
45     public ForgotPasswordAction() {
46     }
47
48     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         ActionErrors errors = new ActionErrors();
50
51         try {
52             InitialContext ic = new InitialContext();
53
54             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
55             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
56             SystemConfiguration sc = scHome.create();
57
58             if(! sc.getBooleanProperty("allow_forgot_password", true)) {
59                 throw new PasswordException(PasswordException.FEATURE_DISABLED);
60             }
61
62             Object JavaDoc uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME);
63             UserHandlerHome uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class);
64             UserHandler uh = uhHome.create();
65
66             String JavaDoc login = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "login");
67             String JavaDoc lastName = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "lastName");
68
69             if(login != null && lastName != null && ! login.equals("") && ! lastName.equals("")) {
70                 UserModel user = null;
71                 try {
72                     user = uh.getUserByLogin(login);
73                     if(user == null) {
74                         throw new PasswordException(PasswordException.UNKNOWN_USER);
75                     }
76                     if(user.getLastName() == null || ! user.getLastName().equalsIgnoreCase(lastName)) {
77                         throw new PasswordException(PasswordException.INVALID_NAME);
78                     }
79                     if(user.getEmail() == null || user.getEmail().equals("")) {
80                         throw new PasswordException(PasswordException.INVALID_EMAIL);
81                     }
82                     if(user.getStatus() != UserUtilities.STATUS_ACTIVE) {
83                         throw new PasswordException(PasswordException.INACTIVE_ACCOUNT);
84                     }
85
86                     if(Logger.isLoggingDebug()) {
87                         Logger.logDebug("ForgotPasswordHandler found matching user: " + user.getFirstName() + " " + user.getLastName() + "(" + user.getLogin() + ")");
88                     }
89
90                     String JavaDoc subject = ITrackerResources.getString("itracker.email.forgotpass.subject");
91                     StringBuffer JavaDoc msgText = new StringBuffer JavaDoc();
92                     msgText.append(ITrackerResources.getString("itracker.email.forgotpass.body"));
93                     msgText.append(ITrackerResources.getString("itracker.web.attr.password") + ": " + uh.generateUserPassword(user));
94                     EmailHandler.sendEmail(user.getEmail(), subject, msgText.toString());
95                 } catch(PasswordException pe) {
96                     if(Logger.isLoggingDebug()) {
97                         Logger.logDebug("Password Exception for user " + (login != null ? login : "UNKNOWN") + ". Type = " + pe.getType());
98                     }
99                     if(pe.getType() == PasswordException.INVALID_NAME) {
100                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.forgotpass.lastname"));
101                     } else if(pe.getType() == PasswordException.INVALID_EMAIL) {
102                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.forgotpass.invalidemail"));
103                     } else if(pe.getType() == PasswordException.INACTIVE_ACCOUNT) {
104                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.forgotpass.inactive"));
105                     } else if(pe.getType() == PasswordException.UNKNOWN_USER) {
106                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.forgotpass.unknown"));
107                     }
108                 }
109             }
110         } catch(NamingException ne) {
111             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.forgotpass.system"));
112             Logger.logError("Could not locate session EJB for password retrieval.", ne);
113         } catch(CreateException ce) {
114             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.forgotpass.system"));
115             Logger.logError("Could not create session EJB for password retrieval.", ce);
116         } catch(PasswordException pe) {
117             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.notenabled"));
118             Logger.logError("Forgot Password function has been disabled.", pe);
119         } catch(Exception JavaDoc e) {
120             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.forgotpass.system"));
121             Logger.logError("Error during password retrieval.", e);
122         }
123
124         if(! errors.isEmpty()) {
125             saveErrors(request, errors);
126             return (mapping.getInputForward());
127         }
128
129         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.message.forgotpass"));
130         saveErrors(request, errors);
131         return mapping.findForward("login");
132     }
133
134 }
135   
Popular Tags