1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ForgotPasswordAction extends PortletAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          try {
68              User user = getUser(actionRequest);
69  
70              if (PropsValues.USERS_REMINDER_QUERIES_ENABLED &&
71                  (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD ||
72                   (Validator.isNotNull(user.getReminderQueryQuestion()) &&
73                    Validator.isNotNull(user.getReminderQueryAnswer())))) {
74  
75                  actionRequest.setAttribute(
76                      ForgotPasswordAction.class.getName(), user);
77  
78                  int step = ParamUtil.getInteger(actionRequest, "step");
79  
80                  if (step == 2) {
81                      if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
82                          CaptchaUtil.check(actionRequest);
83                      }
84  
85                      sendPassword(actionRequest, actionResponse);
86                  }
87              }
88              else {
89                  if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
90                      CaptchaUtil.check(actionRequest);
91                  }
92  
93                  sendPassword(actionRequest, actionResponse);
94              }
95          }
96          catch (Exception e) {
97              if (e instanceof CaptchaTextException ||
98                  e instanceof NoSuchUserException ||
99                  e instanceof SendPasswordException ||
100                 e instanceof UserEmailAddressException ||
101                 e instanceof UserReminderQueryException) {
102 
103                 SessionErrors.add(actionRequest, e.getClass().getName());
104             }
105             else {
106                 PortalUtil.sendError(e, actionRequest, actionResponse);
107             }
108         }
109     }
110 
111     public ActionForward render(
112             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
113             RenderRequest renderRequest, RenderResponse renderResponse)
114         throws Exception {
115 
116         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
117             WebKeys.THEME_DISPLAY);
118 
119         renderResponse.setTitle(themeDisplay.translate("forgot-password"));
120 
121         return mapping.findForward("portlet.login.forgot_password");
122     }
123 
124     protected User getUser(ActionRequest actionRequest) throws Exception {
125         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
126             WebKeys.THEME_DISPLAY);
127 
128         long userId = ParamUtil.getLong(actionRequest, "userId");
129         String screenName = ParamUtil.getString(actionRequest, "screenName");
130         String emailAddress = ParamUtil.getString(
131             actionRequest, "emailAddress");
132 
133         User user = null;
134 
135         if (Validator.isNotNull(emailAddress)) {
136             user = UserLocalServiceUtil.getUserByEmailAddress(
137                 themeDisplay.getCompanyId(), emailAddress);
138         }
139         else if (Validator.isNotNull(screenName)) {
140             user = UserLocalServiceUtil.getUserByScreenName(
141                 themeDisplay.getCompanyId(), screenName);
142         }
143         else if (userId > 0) {
144             user = UserLocalServiceUtil.getUserById(userId);
145         }
146         else {
147             throw new NoSuchUserException();
148         }
149 
150         return user;
151     }
152 
153     protected boolean isCheckMethodOnProcessAction() {
154         return _CHECK_METHOD_ON_PROCESS_ACTION;
155     }
156 
157     protected void sendPassword(
158             ActionRequest actionRequest, ActionResponse actionResponse)
159         throws Exception {
160 
161         User user = getUser(actionRequest);
162 
163         if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
164             String answer = ParamUtil.getString(actionRequest, "answer");
165 
166             if (!user.getReminderQueryAnswer().equals(answer)) {
167                 throw new UserReminderQueryException();
168             }
169         }
170 
171         PortletPreferences preferences = actionRequest.getPreferences();
172 
173         String languageId = LanguageUtil.getLanguageId(actionRequest);
174 
175         String emailFromName = preferences.getValue("emailFromName", null);
176         String emailFromAddress = preferences.getValue(
177             "emailFromAddress", null);
178         String emailToAddress = user.getEmailAddress();
179         String subject = preferences.getValue(
180             "emailPasswordSentSubject_" + languageId, null);
181         String body = preferences.getValue(
182             "emailPasswordSentBody_" + languageId, null);
183 
184         LoginUtil.sendPassword(
185             actionRequest, emailFromName, emailFromAddress, emailToAddress,
186             subject, body);
187 
188         sendRedirect(actionRequest, actionResponse);
189     }
190 
191     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
192 
193 }