1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.login.action;
16  
17  import com.liferay.portal.NoSuchUserException;
18  import com.liferay.portal.SendPasswordException;
19  import com.liferay.portal.UserEmailAddressException;
20  import com.liferay.portal.UserReminderQueryException;
21  import com.liferay.portal.kernel.captcha.CaptchaTextException;
22  import com.liferay.portal.kernel.captcha.CaptchaUtil;
23  import com.liferay.portal.kernel.language.LanguageUtil;
24  import com.liferay.portal.kernel.servlet.SessionErrors;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.login.util.LoginUtil;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.PortletPreferences;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="ForgotPasswordAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class ForgotPasswordAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          try {
60              User user = getUser(actionRequest);
61  
62              if (PropsValues.USERS_REMINDER_QUERIES_ENABLED &&
63                  (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD ||
64                   (Validator.isNotNull(user.getReminderQueryQuestion()) &&
65                    Validator.isNotNull(user.getReminderQueryAnswer())))) {
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 SendPasswordException ||
92                  e instanceof UserEmailAddressException ||
93                  e instanceof UserReminderQueryException) {
94  
95                  SessionErrors.add(actionRequest, e.getClass().getName());
96              }
97              else {
98                  PortalUtil.sendError(e, actionRequest, actionResponse);
99              }
100         }
101     }
102 
103     public ActionForward render(
104             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105             RenderRequest renderRequest, RenderResponse renderResponse)
106         throws Exception {
107 
108         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
109             WebKeys.THEME_DISPLAY);
110 
111         renderResponse.setTitle(themeDisplay.translate("forgot-password"));
112 
113         return mapping.findForward("portlet.login.forgot_password");
114     }
115 
116     protected User getUser(ActionRequest actionRequest) throws Exception {
117         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
118             WebKeys.THEME_DISPLAY);
119 
120         long userId = ParamUtil.getLong(actionRequest, "userId");
121         String screenName = ParamUtil.getString(actionRequest, "screenName");
122         String emailAddress = ParamUtil.getString(
123             actionRequest, "emailAddress");
124 
125         User user = null;
126 
127         if (Validator.isNotNull(emailAddress)) {
128             user = UserLocalServiceUtil.getUserByEmailAddress(
129                 themeDisplay.getCompanyId(), emailAddress);
130         }
131         else if (Validator.isNotNull(screenName)) {
132             user = UserLocalServiceUtil.getUserByScreenName(
133                 themeDisplay.getCompanyId(), screenName);
134         }
135         else if (userId > 0) {
136             user = UserLocalServiceUtil.getUserById(userId);
137         }
138         else {
139             throw new NoSuchUserException();
140         }
141 
142         return user;
143     }
144 
145     protected boolean isCheckMethodOnProcessAction() {
146         return _CHECK_METHOD_ON_PROCESS_ACTION;
147     }
148 
149     protected void sendPassword(
150             ActionRequest actionRequest, ActionResponse actionResponse)
151         throws Exception {
152 
153         User user = getUser(actionRequest);
154 
155         if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
156             String answer = ParamUtil.getString(actionRequest, "answer");
157 
158             if (!user.getReminderQueryAnswer().equals(answer)) {
159                 throw new UserReminderQueryException();
160             }
161         }
162 
163         PortletPreferences preferences = actionRequest.getPreferences();
164 
165         String languageId = LanguageUtil.getLanguageId(actionRequest);
166 
167         String emailFromName = preferences.getValue("emailFromName", null);
168         String emailFromAddress = preferences.getValue(
169             "emailFromAddress", null);
170         String emailToAddress = user.getEmailAddress();
171         String subject = preferences.getValue(
172             "emailPasswordSentSubject_" + languageId, null);
173         String body = preferences.getValue(
174             "emailPasswordSentBody_" + languageId, null);
175 
176         LoginUtil.sendPassword(
177             actionRequest, emailFromName, emailFromAddress, emailToAddress,
178             subject, body);
179 
180         sendRedirect(actionRequest, actionResponse);
181     }
182 
183     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
184 
185 }