1
19
20 package com.liferay.portlet.login.action;
21
22 import com.liferay.portal.NoSuchUserException;
23 import com.liferay.portal.SendPasswordException;
24 import com.liferay.portal.UserEmailAddressException;
25 import com.liferay.portal.UserReminderQueryException;
26 import com.liferay.portal.kernel.captcha.CaptchaTextException;
27 import com.liferay.portal.kernel.captcha.CaptchaUtil;
28 import com.liferay.portal.kernel.language.LanguageUtil;
29 import com.liferay.portal.kernel.servlet.SessionErrors;
30 import com.liferay.portal.kernel.util.ParamUtil;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.model.User;
33 import com.liferay.portal.service.UserLocalServiceUtil;
34 import com.liferay.portal.struts.PortletAction;
35 import com.liferay.portal.theme.ThemeDisplay;
36 import com.liferay.portal.util.PortalUtil;
37 import com.liferay.portal.util.PropsValues;
38 import com.liferay.portal.util.WebKeys;
39 import com.liferay.portlet.login.util.LoginUtil;
40
41 import javax.portlet.ActionRequest;
42 import javax.portlet.ActionResponse;
43 import javax.portlet.PortletConfig;
44 import javax.portlet.PortletPreferences;
45 import javax.portlet.RenderRequest;
46 import javax.portlet.RenderResponse;
47
48 import org.apache.struts.action.ActionForm;
49 import org.apache.struts.action.ActionForward;
50 import org.apache.struts.action.ActionMapping;
51
52
58 public class ForgotPasswordAction extends PortletAction {
59
60 public void processAction(
61 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62 ActionRequest actionRequest, ActionResponse actionResponse)
63 throws Exception {
64
65 try {
66 User user = getUser(actionRequest);
67
68 if (PropsValues.USERS_REMINDER_QUERIES_ENABLED &&
69 (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD ||
70 (Validator.isNotNull(user.getReminderQueryQuestion()) &&
71 Validator.isNotNull(user.getReminderQueryAnswer())))) {
72
73 actionRequest.setAttribute(
74 ForgotPasswordAction.class.getName(), user);
75
76 int step = ParamUtil.getInteger(actionRequest, "step");
77
78 if (step == 2) {
79 if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
80 CaptchaUtil.check(actionRequest);
81 }
82
83 sendPassword(actionRequest, actionResponse);
84 }
85 }
86 else {
87 if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
88 CaptchaUtil.check(actionRequest);
89 }
90
91 sendPassword(actionRequest, actionResponse);
92 }
93 }
94 catch (Exception e) {
95 if (e instanceof CaptchaTextException ||
96 e instanceof NoSuchUserException ||
97 e instanceof SendPasswordException ||
98 e instanceof UserEmailAddressException ||
99 e instanceof UserReminderQueryException) {
100
101 SessionErrors.add(actionRequest, e.getClass().getName());
102 }
103 else {
104 PortalUtil.sendError(e, actionRequest, actionResponse);
105 }
106 }
107 }
108
109 public ActionForward render(
110 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
111 RenderRequest renderRequest, RenderResponse renderResponse)
112 throws Exception {
113
114 ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
115 WebKeys.THEME_DISPLAY);
116
117 renderResponse.setTitle(
118 LanguageUtil.get(
119 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
120 "forgot-password"));
121
122 return mapping.findForward("portlet.login.forgot_password");
123 }
124
125 protected User getUser(ActionRequest actionRequest) throws Exception {
126 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
127 WebKeys.THEME_DISPLAY);
128
129 long userId = ParamUtil.getLong(actionRequest, "userId");
130 String screenName = ParamUtil.getString(actionRequest, "screenName");
131 String emailAddress = ParamUtil.getString(
132 actionRequest, "emailAddress");
133
134 User user = null;
135
136 if (Validator.isNotNull(emailAddress)) {
137 user = UserLocalServiceUtil.getUserByEmailAddress(
138 themeDisplay.getCompanyId(), emailAddress);
139 }
140 else if (Validator.isNotNull(screenName)) {
141 user = UserLocalServiceUtil.getUserByScreenName(
142 themeDisplay.getCompanyId(), screenName);
143 }
144 else if (userId > 0) {
145 user = UserLocalServiceUtil.getUserById(userId);
146 }
147 else {
148 throw new NoSuchUserException();
149 }
150
151 return user;
152 }
153
154 protected boolean isCheckMethodOnProcessAction() {
155 return _CHECK_METHOD_ON_PROCESS_ACTION;
156 }
157
158 protected void sendPassword(
159 ActionRequest actionRequest, ActionResponse actionResponse)
160 throws Exception {
161
162 User user = getUser(actionRequest);
163
164 if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
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 }