1
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
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 User user = getUser(actionRequest);
78
79 actionRequest.setAttribute(
80 ForgotPasswordAction.class.getName(), user);
81 }
82 else {
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
148 return user;
149 }
150
151 protected boolean isCheckMethodOnProcessAction() {
152 return _CHECK_METHOD_ON_PROCESS_ACTION;
153 }
154
155 protected void sendPassword(
156 ActionRequest actionRequest, ActionResponse actionResponse)
157 throws Exception {
158
159 User user = getUser(actionRequest);
160
161 if (PropsValues.USERS_REMINDER_QUERIES_ENABLED) {
162 String answer = ParamUtil.getString(actionRequest, "answer");
163
164 if (!user.getReminderQueryAnswer().equals(answer)) {
165 throw new UserReminderQueryException();
166 }
167 }
168
169 PortletPreferences preferences = actionRequest.getPreferences();
170
171 String languageId = LanguageUtil.getLanguageId(actionRequest);
172
173 String emailFromName = preferences.getValue("emailFromName", null);
174 String emailFromAddress = preferences.getValue(
175 "emailFromAddress", null);
176 String emailToAddress = user.getEmailAddress();
177 String subject = preferences.getValue(
178 "emailPasswordSentSubject_" + languageId, null);
179 String body = preferences.getValue(
180 "emailPasswordSentBody_" + languageId, null);
181
182 LoginUtil.sendPassword(
183 actionRequest, emailFromName, emailFromAddress, emailToAddress,
184 subject, body);
185
186 sendRedirect(actionRequest, actionResponse);
187 }
188
189 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
190
191 }