1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.login.action;
16  
17  import com.liferay.portal.NoSuchUserException;
18  import com.liferay.portal.RequiredReminderQueryException;
19  import com.liferay.portal.SendPasswordException;
20  import com.liferay.portal.UserEmailAddressException;
21  import com.liferay.portal.UserReminderQueryException;
22  import com.liferay.portal.kernel.captcha.CaptchaTextException;
23  import com.liferay.portal.kernel.captcha.CaptchaUtil;
24  import com.liferay.portal.kernel.language.LanguageUtil;
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.PropsValues;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.login.util.LoginUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.PortletPreferences;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="ForgotPasswordAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   */
53  public class ForgotPasswordAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          try {
61              User user = getUser(actionRequest);
62  
63              if (PropsValues.USERS_REMINDER_QUERIES_ENABLED &&
64                  (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD ||
65                   user.hasReminderQuery())) {
66  
67                  actionRequest.setAttribute(
68                      ForgotPasswordAction.class.getName(), user);
69  
70                  int step = ParamUtil.getInteger(actionRequest, "step");
71  
72                  if (step == 2) {
73                      if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
74                          CaptchaUtil.check(actionRequest);
75                      }
76  
77                      sendPassword(actionRequest, actionResponse);
78                  }
79              }
80              else {
81                  if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
82                      CaptchaUtil.check(actionRequest);
83                  }
84  
85                  sendPassword(actionRequest, actionResponse);
86              }
87          }
88          catch (Exception e) {
89              if (e instanceof CaptchaTextException ||
90                  e instanceof NoSuchUserException ||
91                  e instanceof RequiredReminderQueryException ||
92                  e instanceof SendPasswordException ||
93                  e instanceof UserEmailAddressException ||
94                  e instanceof UserReminderQueryException) {
95  
96                  SessionErrors.add(actionRequest, e.getClass().getName());
97              }
98              else {
99                  PortalUtil.sendError(e, actionRequest, actionResponse);
100             }
101         }
102     }
103 
104     public ActionForward render(
105             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
106             RenderRequest renderRequest, RenderResponse renderResponse)
107         throws Exception {
108 
109         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
110             WebKeys.THEME_DISPLAY);
111 
112         renderResponse.setTitle(themeDisplay.translate("forgot-password"));
113 
114         return mapping.findForward("portlet.login.forgot_password");
115     }
116 
117     protected User getUser(ActionRequest actionRequest) throws Exception {
118         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
119             WebKeys.THEME_DISPLAY);
120 
121         long userId = ParamUtil.getLong(actionRequest, "userId");
122         String screenName = ParamUtil.getString(actionRequest, "screenName");
123         String emailAddress = ParamUtil.getString(
124             actionRequest, "emailAddress");
125 
126         User user = null;
127 
128         if (Validator.isNotNull(emailAddress)) {
129             user = UserLocalServiceUtil.getUserByEmailAddress(
130                 themeDisplay.getCompanyId(), emailAddress);
131         }
132         else if (Validator.isNotNull(screenName)) {
133             user = UserLocalServiceUtil.getUserByScreenName(
134                 themeDisplay.getCompanyId(), screenName);
135         }
136         else if (userId > 0) {
137             user = UserLocalServiceUtil.getUserById(userId);
138         }
139         else {
140             throw new NoSuchUserException();
141         }
142 
143         return user;
144     }
145 
146     protected boolean isCheckMethodOnProcessAction() {
147         return _CHECK_METHOD_ON_PROCESS_ACTION;
148     }
149 
150     protected void sendPassword(
151             ActionRequest actionRequest, ActionResponse actionResponse)
152         throws Exception {
153 
154         User user = getUser(actionRequest);
155 
156         if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
157             if (PropsValues.USERS_REMINDER_QUERIES_REQUIRED &&
158                 !user.hasReminderQuery()) {
159 
160                 throw new RequiredReminderQueryException(
161                     "No reminder query or answer is defined for user " +
162                         user.getUserId());
163             }
164 
165             String answer = ParamUtil.getString(actionRequest, "answer");
166 
167             if (!user.getReminderQueryAnswer().equals(answer)) {
168                 throw new UserReminderQueryException();
169             }
170         }
171 
172         PortletPreferences preferences = actionRequest.getPreferences();
173 
174         String languageId = LanguageUtil.getLanguageId(actionRequest);
175 
176         String emailFromName = preferences.getValue("emailFromName", null);
177         String emailFromAddress = preferences.getValue(
178             "emailFromAddress", null);
179         String emailToAddress = user.getEmailAddress();
180         String subject = preferences.getValue(
181             "emailPasswordSentSubject_" + languageId, null);
182         String body = preferences.getValue(
183             "emailPasswordSentBody_" + languageId, null);
184 
185         LoginUtil.sendPassword(
186             actionRequest, emailFromName, emailFromAddress, emailToAddress,
187             subject, body);
188 
189         sendRedirect(actionRequest, actionResponse);
190     }
191 
192     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
193 
194 }