1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.action;
16  
17  import com.liferay.portal.DuplicateUserEmailAddressException;
18  import com.liferay.portal.NoSuchUserException;
19  import com.liferay.portal.ReservedUserEmailAddressException;
20  import com.liferay.portal.UserEmailAddressException;
21  import com.liferay.portal.kernel.servlet.SessionErrors;
22  import com.liferay.portal.kernel.util.Constants;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.security.auth.PrincipalException;
26  import com.liferay.portal.service.UserServiceUtil;
27  import com.liferay.portal.struts.ActionConstants;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portlet.admin.util.AdminUtil;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.apache.struts.action.Action;
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  
39  /**
40   * <a href="UpdateEmailAddressAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Julio Camarero
43   * @author Jorge Ferrer
44   * @author Brian Wing Shun Chan
45   */
46  public class UpdateEmailAddressAction extends Action {
47  
48      public ActionForward execute(
49              ActionMapping mapping, ActionForm form, HttpServletRequest request,
50              HttpServletResponse response)
51          throws Exception {
52  
53          String cmd = ParamUtil.getString(request, Constants.CMD);
54  
55          if (Validator.isNull(cmd)) {
56              return mapping.findForward("portal.update_email_address");
57          }
58  
59          try {
60              updateEmailAddress(request);
61  
62              return mapping.findForward(ActionConstants.COMMON_REFERER);
63          }
64          catch (Exception e) {
65              if (e instanceof DuplicateUserEmailAddressException ||
66                  e instanceof ReservedUserEmailAddressException ||
67                  e instanceof UserEmailAddressException) {
68  
69                  SessionErrors.add(request, e.getClass().getName());
70  
71                  return mapping.findForward("portal.update_email_address");
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 updateEmailAddress(HttpServletRequest request)
89          throws Exception {
90  
91          long userId = PortalUtil.getUserId(request);
92          String password = AdminUtil.getUpdateUserPassword(request, userId);
93          String emailAddress1 = ParamUtil.getString(request, "emailAddress1");
94          String emailAddress2 = ParamUtil.getString(request, "emailAddress2");
95  
96          UserServiceUtil.updateEmailAddress(
97              userId, password, emailAddress1, emailAddress2);
98      }
99  
100 }