1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.security.auth.PrincipalException;
23  import com.liferay.portal.service.UserServiceUtil;
24  import com.liferay.portal.struts.ActionConstants;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.WebKeys;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpSession;
31  
32  import org.apache.struts.action.Action;
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="ChangePasswordAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class ChangePasswordAction extends Action {
43  
44      public ActionForward execute(
45              ActionMapping mapping, ActionForm form, HttpServletRequest request,
46              HttpServletResponse response)
47          throws Exception {
48  
49          String cmd = ParamUtil.getString(request, Constants.CMD);
50  
51          if (cmd.equals("password")) {
52              try {
53                  updatePassword(request, response);
54  
55                  return mapping.findForward(ActionConstants.COMMON_REFERER);
56              }
57              catch (Exception e) {
58                  if (e instanceof UserPasswordException) {
59                      UserPasswordException upe = (UserPasswordException)e;
60  
61                      SessionErrors.add(request, e.getClass().getName(), upe);
62  
63                      return mapping.findForward("portal.change_password");
64                  }
65                  else if (e instanceof NoSuchUserException ||
66                           e instanceof PrincipalException) {
67  
68                      SessionErrors.add(request, e.getClass().getName());
69  
70                      return mapping.findForward("portal.error");
71                  }
72                  else {
73                      PortalUtil.sendError(e, request, response);
74  
75                      return null;
76                  }
77              }
78          }
79          else {
80              return mapping.findForward("portal.change_password");
81          }
82      }
83  
84      protected void updatePassword(
85              HttpServletRequest request, HttpServletResponse response)
86          throws Exception {
87  
88          HttpSession session = request.getSession();
89  
90          long userId = PortalUtil.getUserId(request);
91          String password1 = ParamUtil.getString(request, "password1");
92          String password2 = ParamUtil.getString(request, "password2");
93          boolean passwordReset = ParamUtil.getBoolean(request, "passwordReset");
94  
95          UserServiceUtil.updatePassword(
96              userId, password1, password2, passwordReset);
97  
98          session.setAttribute(WebKeys.USER_PASSWORD, password1);
99      }
100 
101 }