001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.action;
022:
023: import com.liferay.portal.NoSuchUserException;
024: import com.liferay.portal.UserPasswordException;
025: import com.liferay.portal.kernel.util.Constants;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.security.auth.PrincipalException;
028: import com.liferay.portal.service.UserServiceUtil;
029: import com.liferay.portal.struts.ActionConstants;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portal.util.WebKeys;
032: import com.liferay.util.servlet.SessionErrors;
033:
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036: import javax.servlet.http.HttpSession;
037: import javax.servlet.jsp.PageContext;
038:
039: import org.apache.struts.action.Action;
040: import org.apache.struts.action.ActionForm;
041: import org.apache.struts.action.ActionForward;
042: import org.apache.struts.action.ActionMapping;
043:
044: /**
045: * <a href="ChangePasswordAction.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Brian Wing Shun Chan
048: *
049: */
050: public class ChangePasswordAction extends Action {
051:
052: public ActionForward execute(ActionMapping mapping,
053: ActionForm form, HttpServletRequest req,
054: HttpServletResponse res) throws Exception {
055:
056: String cmd = ParamUtil.getString(req, Constants.CMD);
057:
058: if (cmd.equals("password")) {
059: try {
060: updatePassword(req, res);
061:
062: return mapping
063: .findForward(ActionConstants.COMMON_REFERER);
064: } catch (Exception e) {
065: if (e instanceof UserPasswordException) {
066: UserPasswordException upe = (UserPasswordException) e;
067:
068: SessionErrors.add(req, e.getClass().getName(), upe);
069:
070: return mapping
071: .findForward("portal.change_password");
072: } else if (e instanceof NoSuchUserException
073: || e instanceof PrincipalException) {
074:
075: SessionErrors.add(req, e.getClass().getName());
076:
077: return mapping.findForward("portal.error");
078: } else {
079: req.setAttribute(PageContext.EXCEPTION, e);
080:
081: return mapping
082: .findForward(ActionConstants.COMMON_ERROR);
083: }
084: }
085: } else {
086: return mapping.findForward("portal.change_password");
087: }
088: }
089:
090: protected void updatePassword(HttpServletRequest req,
091: HttpServletResponse res) throws Exception {
092:
093: HttpSession ses = req.getSession();
094:
095: long userId = PortalUtil.getUserId(req);
096: String password1 = ParamUtil.getString(req, "password1");
097: String password2 = ParamUtil.getString(req, "password2");
098: boolean passwordReset = ParamUtil.getBoolean(req,
099: "passwordReset");
100:
101: UserServiceUtil.updatePassword(userId, password1, password2,
102: passwordReset);
103:
104: ses.setAttribute(WebKeys.USER_PASSWORD, password1);
105: }
106:
107: }
|