001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.NoSuchUserException;
018    import com.liferay.portal.UserLockoutException;
019    import com.liferay.portal.UserPasswordException;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.CompanyConstants;
026    import com.liferay.portal.model.Ticket;
027    import com.liferay.portal.model.TicketConstants;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.security.auth.AuthTokenUtil;
030    import com.liferay.portal.security.auth.PrincipalException;
031    import com.liferay.portal.security.pwd.PwdToolkitUtilThreadLocal;
032    import com.liferay.portal.service.CompanyLocalServiceUtil;
033    import com.liferay.portal.service.TicketLocalServiceUtil;
034    import com.liferay.portal.service.UserLocalServiceUtil;
035    import com.liferay.portal.struts.ActionConstants;
036    import com.liferay.portal.theme.ThemeDisplay;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.PortletKeys;
039    import com.liferay.portal.util.PropsValues;
040    import com.liferay.portal.util.WebKeys;
041    import com.liferay.portlet.PortletURLImpl;
042    import com.liferay.portlet.login.util.LoginUtil;
043    
044    import javax.portlet.PortletRequest;
045    import javax.portlet.PortletURL;
046    
047    import javax.servlet.http.HttpServletRequest;
048    import javax.servlet.http.HttpServletResponse;
049    import javax.servlet.http.HttpSession;
050    
051    import org.apache.struts.action.Action;
052    import org.apache.struts.action.ActionForm;
053    import org.apache.struts.action.ActionForward;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     * @author Mika Koivisto
059     */
060    public class UpdatePasswordAction extends Action {
061    
062            @Override
063            public ActionForward execute(
064                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
065                            HttpServletResponse response)
066                    throws Exception {
067    
068                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
069                            WebKeys.THEME_DISPLAY);
070    
071                    Ticket ticket = getTicket(request);
072    
073                    if (!themeDisplay.isSignedIn() && (ticket == null)) {
074                            return mapping.findForward(ActionConstants.COMMON_REFERER_JSP);
075                    }
076    
077                    String cmd = ParamUtil.getString(request, Constants.CMD);
078    
079                    if (Validator.isNull(cmd)) {
080                            if (ticket != null) {
081                                    User user = UserLocalServiceUtil.getUser(ticket.getClassPK());
082    
083                                    try {
084                                            UserLocalServiceUtil.checkLockout(user);
085    
086                                            UserLocalServiceUtil.updatePasswordReset(
087                                                    user.getUserId(), true);
088                                    }
089                                    catch (UserLockoutException ule) {
090                                            SessionErrors.add(request, ule.getClass());
091                                    }
092                            }
093    
094                            return mapping.findForward("portal.update_password");
095                    }
096    
097                    try {
098                            updatePassword(request, response, themeDisplay, ticket);
099    
100                            PortletURL portletURL = new PortletURLImpl(
101                                    request, PortletKeys.LOGIN, themeDisplay.getPlid(),
102                                    PortletRequest.RENDER_PHASE);
103    
104                            response.sendRedirect(portletURL.toString());
105    
106                            return null;
107                    }
108                    catch (Exception e) {
109                            if (e instanceof UserPasswordException) {
110                                    SessionErrors.add(request, e.getClass(), e);
111    
112                                    return mapping.findForward("portal.update_password");
113                            }
114                            else if (e instanceof NoSuchUserException ||
115                                             e instanceof PrincipalException) {
116    
117                                    SessionErrors.add(request, e.getClass());
118    
119                                    return mapping.findForward("portal.error");
120                            }
121                            else {
122                                    PortalUtil.sendError(e, request, response);
123    
124                                    return null;
125                            }
126                    }
127            }
128    
129            protected Ticket getTicket(HttpServletRequest request) {
130                    String ticketKey = ParamUtil.getString(request, "ticketKey");
131    
132                    if (Validator.isNull(ticketKey)) {
133                            return null;
134                    }
135    
136                    try {
137                            Ticket ticket = TicketLocalServiceUtil.getTicket(ticketKey);
138    
139                            if (ticket.getType() != TicketConstants.TYPE_PASSWORD) {
140                                    return null;
141                            }
142    
143                            if (!ticket.isExpired()) {
144                                    return ticket;
145                            }
146                            else {
147                                    TicketLocalServiceUtil.deleteTicket(ticket);
148                            }
149                    }
150                    catch (Exception e) {
151                    }
152    
153                    return null;
154            }
155    
156            protected boolean isValidatePassword(HttpServletRequest request) {
157                    HttpSession session = request.getSession();
158    
159                    Boolean setupWizardPasswordUpdated = (Boolean)session.getAttribute(
160                            WebKeys.SETUP_WIZARD_PASSWORD_UPDATED);
161    
162                    if ((setupWizardPasswordUpdated != null) &&
163                             setupWizardPasswordUpdated) {
164    
165                            return false;
166                    }
167    
168                    return true;
169            }
170    
171            protected void updatePassword(
172                            HttpServletRequest request, HttpServletResponse response,
173                            ThemeDisplay themeDisplay, Ticket ticket)
174                    throws Exception {
175    
176                    AuthTokenUtil.check(request);
177    
178                    long userId = 0;
179    
180                    if (ticket != null) {
181                            userId = ticket.getClassPK();
182                    }
183                    else {
184                            userId = themeDisplay.getUserId();
185                    }
186    
187                    String password1 = request.getParameter("password1");
188                    String password2 = request.getParameter("password2");
189                    boolean passwordReset = false;
190    
191                    boolean previousValidate = PwdToolkitUtilThreadLocal.isValidate();
192    
193                    try {
194                            boolean currentValidate = isValidatePassword(request);
195    
196                            PwdToolkitUtilThreadLocal.setValidate(currentValidate);
197    
198                            UserLocalServiceUtil.updatePassword(
199                                    userId, password1, password2, passwordReset);
200                    }
201                    finally {
202                            PwdToolkitUtilThreadLocal.setValidate(previousValidate);
203                    }
204    
205                    if (ticket != null) {
206                            TicketLocalServiceUtil.deleteTicket(ticket);
207    
208                            User user = UserLocalServiceUtil.getUser(userId);
209    
210                            Company company = CompanyLocalServiceUtil.getCompanyById(
211                                    user.getCompanyId());
212    
213                            String login = null;
214    
215                            String authType = company.getAuthType();
216    
217                            if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
218                                    login = user.getEmailAddress();
219                            }
220                            else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
221                                    login = user.getScreenName();
222                            }
223                            else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
224                                    login = String.valueOf(userId);
225                            }
226    
227                            LoginUtil.login(request, response, login, password1, false, null);
228                    }
229                    else if (PropsValues.SESSION_STORE_PASSWORD) {
230                            HttpSession session = request.getSession();
231    
232                            session.setAttribute(WebKeys.USER_PASSWORD, password1);
233                    }
234            }
235    
236    }