001
014
015 package com.liferay.portlet.login.action;
016
017 import com.liferay.portal.NoSuchUserException;
018 import com.liferay.portal.RequiredReminderQueryException;
019 import com.liferay.portal.SendPasswordException;
020 import com.liferay.portal.UserEmailAddressException;
021 import com.liferay.portal.UserReminderQueryException;
022 import com.liferay.portal.kernel.captcha.CaptchaTextException;
023 import com.liferay.portal.kernel.captcha.CaptchaUtil;
024 import com.liferay.portal.kernel.language.LanguageUtil;
025 import com.liferay.portal.kernel.servlet.SessionErrors;
026 import com.liferay.portal.kernel.util.ParamUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.Company;
029 import com.liferay.portal.model.User;
030 import com.liferay.portal.service.UserLocalServiceUtil;
031 import com.liferay.portal.struts.PortletAction;
032 import com.liferay.portal.theme.ThemeDisplay;
033 import com.liferay.portal.util.PortalUtil;
034 import com.liferay.portal.util.PropsValues;
035 import com.liferay.portal.util.WebKeys;
036 import com.liferay.portlet.login.util.LoginUtil;
037
038 import javax.portlet.ActionRequest;
039 import javax.portlet.ActionResponse;
040 import javax.portlet.PortletConfig;
041 import javax.portlet.PortletPreferences;
042 import javax.portlet.RenderRequest;
043 import javax.portlet.RenderResponse;
044
045 import org.apache.struts.action.ActionForm;
046 import org.apache.struts.action.ActionForward;
047 import org.apache.struts.action.ActionMapping;
048
049
052 public class ForgotPasswordAction extends PortletAction {
053
054 public void processAction(
055 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
056 ActionRequest actionRequest, ActionResponse actionResponse)
057 throws Exception {
058
059 try {
060 User user = getUser(actionRequest);
061
062 if (PropsValues.USERS_REMINDER_QUERIES_ENABLED &&
063 (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD ||
064 user.hasReminderQuery())) {
065
066 actionRequest.setAttribute(
067 ForgotPasswordAction.class.getName(), user);
068
069 int step = ParamUtil.getInteger(actionRequest, "step");
070
071 if (step == 2) {
072 if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
073 CaptchaUtil.check(actionRequest);
074 }
075
076 sendPassword(actionRequest, actionResponse);
077 }
078 }
079 else {
080 if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
081 CaptchaUtil.check(actionRequest);
082 }
083
084 sendPassword(actionRequest, actionResponse);
085 }
086 }
087 catch (Exception e) {
088 if (e instanceof CaptchaTextException ||
089 e instanceof NoSuchUserException ||
090 e instanceof RequiredReminderQueryException ||
091 e instanceof SendPasswordException ||
092 e instanceof UserEmailAddressException ||
093 e instanceof UserReminderQueryException) {
094
095 SessionErrors.add(actionRequest, e.getClass().getName());
096 }
097 else {
098 PortalUtil.sendError(e, actionRequest, actionResponse);
099 }
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)
117 throws Exception {
118
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 else {
141 throw new NoSuchUserException();
142 }
143
144 return user;
145 }
146
147 protected boolean isCheckMethodOnProcessAction() {
148 return _CHECK_METHOD_ON_PROCESS_ACTION;
149 }
150
151 protected void sendPassword(
152 ActionRequest actionRequest, ActionResponse actionResponse)
153 throws Exception {
154
155 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
156 WebKeys.THEME_DISPLAY);
157
158 Company company = themeDisplay.getCompany();
159
160 User user = getUser(actionRequest);
161
162 if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
163 if (PropsValues.USERS_REMINDER_QUERIES_REQUIRED &&
164 !user.hasReminderQuery()) {
165
166 throw new RequiredReminderQueryException(
167 "No reminder query or answer is defined for user " +
168 user.getUserId());
169 }
170
171 String answer = ParamUtil.getString(actionRequest, "answer");
172
173 if (!user.getReminderQueryAnswer().equals(answer)) {
174 throw new UserReminderQueryException();
175 }
176 }
177
178 PortletPreferences preferences = actionRequest.getPreferences();
179
180 String languageId = LanguageUtil.getLanguageId(actionRequest);
181
182 String emailFromName = preferences.getValue("emailFromName", null);
183 String emailFromAddress = preferences.getValue(
184 "emailFromAddress", null);
185 String emailToAddress = user.getEmailAddress();
186
187 String emailParam = "emailPasswordSent";
188
189 if (company.isSendPasswordResetLink()) {
190 emailParam = "emailPasswordReset";
191 }
192
193 String subject = preferences.getValue(
194 emailParam + "Subject_" + languageId, null);
195 String body = preferences.getValue(
196 emailParam + "Body_" + languageId, null);
197
198 LoginUtil.sendPassword(
199 actionRequest, emailFromName, emailFromAddress, emailToAddress,
200 subject, body);
201
202 sendRedirect(actionRequest, actionResponse);
203 }
204
205 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
206
207 }