1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.login.action;
24  
25  import com.liferay.portal.NoSuchUserException;
26  import com.liferay.portal.SendPasswordException;
27  import com.liferay.portal.UserEmailAddressException;
28  import com.liferay.portal.UserReminderQueryException;
29  import com.liferay.portal.kernel.captcha.CaptchaTextException;
30  import com.liferay.portal.kernel.captcha.CaptchaUtil;
31  import com.liferay.portal.kernel.language.LanguageUtil;
32  import com.liferay.portal.kernel.servlet.SessionErrors;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.service.UserLocalServiceUtil;
37  import com.liferay.portal.struts.PortletAction;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.PropsValues;
41  import com.liferay.portal.util.WebKeys;
42  import com.liferay.portlet.login.util.LoginUtil;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.PortletPreferences;
48  import javax.portlet.RenderRequest;
49  import javax.portlet.RenderResponse;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="ForgotPasswordAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class ForgotPasswordAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              ActionRequest actionRequest, ActionResponse actionResponse)
66          throws Exception {
67  
68          try {
69              if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
70                  int step = ParamUtil.getInteger(actionRequest, "step");
71  
72                  if (step == 1) {
73                      if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
74                          CaptchaUtil.check(actionRequest);
75                      }
76  
77                      User user = getUser(actionRequest);
78  
79                      actionRequest.setAttribute(
80                          ForgotPasswordAction.class.getName(), user);
81                  }
82                  else {
83                      sendPassword(actionRequest, actionResponse);
84                  }
85              }
86              else {
87                  if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
88                      CaptchaUtil.check(actionRequest);
89                  }
90  
91                  sendPassword(actionRequest, actionResponse);
92              }
93          }
94          catch (Exception e) {
95              if (e instanceof CaptchaTextException ||
96                  e instanceof NoSuchUserException ||
97                  e instanceof SendPasswordException ||
98                  e instanceof UserEmailAddressException ||
99                  e instanceof UserReminderQueryException) {
100 
101                 SessionErrors.add(actionRequest, e.getClass().getName());
102             }
103             else {
104                 PortalUtil.sendError(e, actionRequest, actionResponse);
105             }
106         }
107     }
108 
109     public ActionForward render(
110             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
111             RenderRequest renderRequest, RenderResponse renderResponse)
112         throws Exception {
113 
114         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
115             WebKeys.THEME_DISPLAY);
116 
117         renderResponse.setTitle(
118             LanguageUtil.get(
119                 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
120                 "forgot-password"));
121 
122         return mapping.findForward("portlet.login.forgot_password");
123     }
124 
125     protected User getUser(ActionRequest actionRequest) throws Exception {
126         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
127             WebKeys.THEME_DISPLAY);
128 
129         long userId = ParamUtil.getLong(actionRequest, "userId");
130         String screenName = ParamUtil.getString(actionRequest, "screenName");
131         String emailAddress = ParamUtil.getString(
132             actionRequest, "emailAddress");
133 
134         User user = null;
135 
136         if (Validator.isNotNull(emailAddress)) {
137             user = UserLocalServiceUtil.getUserByEmailAddress(
138                 themeDisplay.getCompanyId(), emailAddress);
139         }
140         else if (Validator.isNotNull(screenName)) {
141             user = UserLocalServiceUtil.getUserByScreenName(
142                 themeDisplay.getCompanyId(), screenName);
143         }
144         else if (userId > 0) {
145             user = UserLocalServiceUtil.getUserById(userId);
146         }
147 
148         return user;
149     }
150 
151     protected boolean isCheckMethodOnProcessAction() {
152         return _CHECK_METHOD_ON_PROCESS_ACTION;
153     }
154 
155     protected void sendPassword(
156             ActionRequest actionRequest, ActionResponse actionResponse)
157         throws Exception {
158 
159         User user = getUser(actionRequest);
160 
161         if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
162             String answer = ParamUtil.getString(actionRequest, "answer");
163 
164             if (!user.getReminderQueryAnswer().equals(answer)) {
165                 throw new UserReminderQueryException();
166             }
167         }
168 
169         PortletPreferences preferences = actionRequest.getPreferences();
170 
171         String languageId = LanguageUtil.getLanguageId(actionRequest);
172 
173         String emailFromName = preferences.getValue("emailFromName", null);
174         String emailFromAddress = preferences.getValue(
175             "emailFromAddress", null);
176         String emailToAddress = user.getEmailAddress();
177         String subject = preferences.getValue(
178             "emailPasswordSentSubject_" + languageId, null);
179         String body = preferences.getValue(
180             "emailPasswordSentBody_" + languageId, null);
181 
182         LoginUtil.sendPassword(
183             actionRequest, emailFromName, emailFromAddress, emailToAddress,
184             subject, body);
185 
186         sendRedirect(actionRequest, actionResponse);
187     }
188 
189     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
190 
191 }