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.kernel.util.Validator;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.service.UserServiceUtil;
30  import com.liferay.portal.struts.ActionConstants;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.http.HttpSession;
37  
38  import org.apache.struts.action.Action;
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="UpdatePasswordAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class UpdatePasswordAction extends Action {
50  
51      public ActionForward execute(
52              ActionMapping mapping, ActionForm form, HttpServletRequest request,
53              HttpServletResponse response)
54          throws Exception {
55  
56          String cmd = ParamUtil.getString(request, Constants.CMD);
57  
58          if (Validator.isNull(cmd)) {
59              return mapping.findForward("portal.update_password");
60          }
61  
62          try {
63              updatePassword(request, response);
64  
65              return mapping.findForward(ActionConstants.COMMON_REFERER);
66          }
67          catch (Exception e) {
68              if (e instanceof UserPasswordException) {
69                  SessionErrors.add(request, e.getClass().getName(), e);
70  
71                  return mapping.findForward(ActionConstants.COMMON_REFERER);
72              }
73              else if (e instanceof NoSuchUserException ||
74                       e instanceof PrincipalException) {
75  
76                  SessionErrors.add(request, e.getClass().getName());
77  
78                  return mapping.findForward("portal.error");
79              }
80              else {
81                  PortalUtil.sendError(e, request, response);
82  
83                  return null;
84              }
85          }
86      }
87  
88      protected void updatePassword(
89              HttpServletRequest request, HttpServletResponse response)
90          throws Exception {
91  
92          HttpSession session = request.getSession();
93  
94          long userId = PortalUtil.getUserId(request);
95          String password1 = ParamUtil.getString(request, "password1");
96          String password2 = ParamUtil.getString(request, "password2");
97          boolean passwordReset = false;
98  
99          UserServiceUtil.updatePassword(
100             userId, password1, password2, passwordReset);
101 
102         session.setAttribute(WebKeys.USER_PASSWORD, password1);
103     }
104 
105 }