1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.action;
16  
17  import com.liferay.portal.NoSuchUserException;
18  import com.liferay.portal.UserPasswordException;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.UserServiceUtil;
25  import com.liferay.portal.struts.ActionConstants;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.WebKeys;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpSession;
32  
33  import org.apache.struts.action.Action;
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  
38  /**
39   * <a href="UpdatePasswordAction.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class UpdatePasswordAction extends Action {
44  
45      public ActionForward execute(
46              ActionMapping mapping, ActionForm form, HttpServletRequest request,
47              HttpServletResponse response)
48          throws Exception {
49  
50          String cmd = ParamUtil.getString(request, Constants.CMD);
51  
52          if (Validator.isNull(cmd)) {
53              return mapping.findForward("portal.update_password");
54          }
55  
56          try {
57              updatePassword(request, response);
58  
59              return mapping.findForward(ActionConstants.COMMON_REFERER);
60          }
61          catch (Exception e) {
62              if (e instanceof UserPasswordException) {
63                  SessionErrors.add(request, e.getClass().getName(), e);
64  
65                  return mapping.findForward(ActionConstants.COMMON_REFERER);
66              }
67              else if (e instanceof NoSuchUserException ||
68                       e instanceof PrincipalException) {
69  
70                  SessionErrors.add(request, e.getClass().getName());
71  
72                  return mapping.findForward("portal.error");
73              }
74              else {
75                  PortalUtil.sendError(e, request, response);
76  
77                  return null;
78              }
79          }
80      }
81  
82      protected void updatePassword(
83              HttpServletRequest request, HttpServletResponse response)
84          throws Exception {
85  
86          HttpSession session = request.getSession();
87  
88          long userId = PortalUtil.getUserId(request);
89          String password1 = ParamUtil.getString(request, "password1");
90          String password2 = ParamUtil.getString(request, "password2");
91          boolean passwordReset = false;
92  
93          UserServiceUtil.updatePassword(
94              userId, password1, password2, passwordReset);
95  
96          session.setAttribute(WebKeys.USER_PASSWORD, password1);
97      }
98  
99  }