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                      getUser(actionRequest);
78                  }
79                  else {
80                      sendPassword(actionRequest, actionResponse);
81                  }
82              }
83              else {
84                  sendPassword(actionRequest, actionResponse);
85              }
86          }
87          catch (Exception e) {
88              if (e instanceof CaptchaTextException ||
89                  e instanceof NoSuchUserException ||
90                  e instanceof SendPasswordException ||
91                  e instanceof UserEmailAddressException ||
92                  e instanceof UserReminderQueryException) {
93  
94                  SessionErrors.add(actionRequest, e.getClass().getName());
95              }
96              else {
97                  PortalUtil.sendError(e, actionRequest, actionResponse);
98              }
99          }
100     }
101 
102     public ActionForward render(
103             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
104             RenderRequest renderRequest, RenderResponse renderResponse)
105         throws Exception {
106 
107         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
108             WebKeys.THEME_DISPLAY);
109 
110         renderResponse.setTitle(
111             LanguageUtil.get(
112                 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
113                 "forgot-password"));
114 
115         return mapping.findForward("portlet.login.forgot_password");
116     }
117 
118     protected User getUser(ActionRequest actionRequest) throws Exception {
119         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
120             WebKeys.THEME_DISPLAY);
121 
122         long userId = ParamUtil.getLong(actionRequest, "userId");
123         String screenName = ParamUtil.getString(actionRequest, "screenName");
124         String emailAddress = ParamUtil.getString(
125             actionRequest, "emailAddress");
126 
127         User user = null;
128 
129         if (Validator.isNotNull(emailAddress)) {
130             user = UserLocalServiceUtil.getUserByEmailAddress(
131                 themeDisplay.getCompanyId(), emailAddress);
132         }
133         else if (Validator.isNotNull(screenName)) {
134             user = UserLocalServiceUtil.getUserByScreenName(
135                 themeDisplay.getCompanyId(), screenName);
136         }
137         else if (userId > 0) {
138             user = UserLocalServiceUtil.getUserById(userId);
139         }
140 
141         actionRequest.setAttribute(ForgotPasswordAction.class.getName(), user);
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         if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
155             User user = getUser(actionRequest);
156 
157             String answer = ParamUtil.getString(actionRequest, "answer");
158 
159             if (!user.getReminderQueryAnswer().equals(answer)) {
160                 throw new UserReminderQueryException();
161             }
162         }
163         else if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
164             CaptchaUtil.check(actionRequest);
165         }
166 
167         PortletPreferences preferences = actionRequest.getPreferences();
168 
169         String languageId = LanguageUtil.getLanguageId(actionRequest);
170 
171         String emailFromName = preferences.getValue("emailFromName", null);
172         String emailFromAddress = preferences.getValue(
173             "emailFromAddress", null);
174         String subject = preferences.getValue(
175             "emailPasswordSentSubject_" + languageId, null);
176         String body = preferences.getValue(
177             "emailPasswordSentBody_" + languageId, null);
178 
179         LoginUtil.sendPassword(
180             actionRequest, emailFromName, emailFromAddress, subject, body);
181 
182         sendRedirect(actionRequest, actionResponse);
183     }
184 
185     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
186 
187 }