1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.action;
21  
22  import com.liferay.portal.NoSuchUserException;
23  import com.liferay.portal.UserPasswordException;
24  import com.liferay.portal.kernel.servlet.SessionErrors;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.service.UserServiceUtil;
29  import com.liferay.portal.struts.ActionConstants;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.WebKeys;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import javax.servlet.http.HttpSession;
36  
37  import org.apache.struts.action.Action;
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="ChangePasswordAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class ChangePasswordAction extends Action {
49  
50      public ActionForward execute(
51              ActionMapping mapping, ActionForm form, HttpServletRequest request,
52              HttpServletResponse response)
53          throws Exception {
54  
55          String cmd = ParamUtil.getString(request, Constants.CMD);
56  
57          if (cmd.equals("password")) {
58              try {
59                  updatePassword(request, response);
60  
61                  return mapping.findForward(ActionConstants.COMMON_REFERER);
62              }
63              catch (Exception e) {
64                  if (e instanceof UserPasswordException) {
65                      UserPasswordException upe = (UserPasswordException)e;
66  
67                      SessionErrors.add(request, e.getClass().getName(), upe);
68  
69                      return mapping.findForward("portal.change_password");
70                  }
71                  else if (e instanceof NoSuchUserException ||
72                           e instanceof PrincipalException) {
73  
74                      SessionErrors.add(request, e.getClass().getName());
75  
76                      return mapping.findForward("portal.error");
77                  }
78                  else {
79                      PortalUtil.sendError(e, request, response);
80  
81                      return null;
82                  }
83              }
84          }
85          else {
86              return mapping.findForward("portal.change_password");
87          }
88      }
89  
90      protected void updatePassword(
91              HttpServletRequest request, HttpServletResponse response)
92          throws Exception {
93  
94          HttpSession session = request.getSession();
95  
96          long userId = PortalUtil.getUserId(request);
97          String password1 = ParamUtil.getString(request, "password1");
98          String password2 = ParamUtil.getString(request, "password2");
99          boolean passwordReset = ParamUtil.getBoolean(request, "passwordReset");
100 
101         UserServiceUtil.updatePassword(
102             userId, password1, password2, passwordReset);
103 
104         session.setAttribute(WebKeys.USER_PASSWORD, password1);
105     }
106 
107 }