/**
* Copyright (c) 2000-2005 Liferay, LLC. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.liferay.portal.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import com.dotcms.repackage.org.apache.struts.action.Action;
import com.dotcms.repackage.org.apache.struts.action.ActionForm;
import com.dotcms.repackage.org.apache.struts.action.ActionForward;
import com.dotcms.repackage.org.apache.struts.action.ActionMapping;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.UserPasswordException;
import com.liferay.portal.auth.PrincipalException;
import com.liferay.portal.model.User;
import com.liferay.portal.util.Constants;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.WebKeys;
import com.liferay.util.ParamUtil;
import com.liferay.util.servlet.SessionErrors;
import com.liferay.util.servlet.SessionMessages;
/**
* <a href="ChangePasswordAction.java.html"><b><i>View Source</i></b></a>
*
* @author Brian Wing Shun Chan
* @version $Revision: 1.6 $
*
*/
public class ChangePasswordAction extends Action {
public ActionForward execute(
ActionMapping mapping, ActionForm form, HttpServletRequest req,
HttpServletResponse res)
throws Exception {
String cmd = req.getParameter(Constants.CMD);
if ((cmd != null) && (cmd.equals("password"))) {
try {
_updatePassword(req, res);
return mapping.findForward(Constants.COMMON_REFERER);
}
catch (Exception e) {
if (e != null &&
e instanceof UserPasswordException) {
UserPasswordException upe = (UserPasswordException)e;
SessionErrors.add(req, e.getClass().getName(), upe);
return mapping.findForward("portal.change_password");
}
else if (e != null &&
e instanceof NoSuchUserException ||
e instanceof PrincipalException) {
SessionErrors.add(req, e.getClass().getName());
return mapping.findForward("portal.error");
}
else {
req.setAttribute(PageContext.EXCEPTION, e);
return mapping.findForward(Constants.COMMON_ERROR);
}
}
}
else {
return mapping.findForward("portal.change_password");
}
}
private void _updatePassword(
HttpServletRequest req, HttpServletResponse res)
throws Exception {
HttpSession ses = req.getSession();
String password1 = ParamUtil.getString(req, "password_1");
String password2 = ParamUtil.getString(req, "password_2");
boolean passwordReset = ParamUtil.get(req, "password_reset", false);
User user = PortalUtil.getSelectedUser(req);
PortalUtil.updateUser(
req, res, user.getUserId(), password1, password2, passwordReset);
if (user.getUserId().equals(PortalUtil.getUser(req).getUserId())) {
ses.setAttribute(WebKeys.USER_PASSWORD, password1);
}
// Session messages
SessionMessages.add(req, "password_updated");
}
}