1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.action;
24  
25  import com.liferay.portal.DuplicateUserEmailAddressException;
26  import com.liferay.portal.NoSuchUserException;
27  import com.liferay.portal.ReservedUserEmailAddressException;
28  import com.liferay.portal.UserEmailAddressException;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.util.Constants;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.UserServiceUtil;
35  import com.liferay.portal.struts.ActionConstants;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portlet.admin.util.AdminUtil;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  import org.apache.struts.action.Action;
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="UpdateEmailAddressAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Julio Camarero
51   * @author Jorge Ferrer
52   * @author Brian Wing Shun Chan
53   */
54  public class UpdateEmailAddressAction extends Action {
55  
56      public ActionForward execute(
57              ActionMapping mapping, ActionForm form, HttpServletRequest request,
58              HttpServletResponse response)
59          throws Exception {
60  
61          String cmd = ParamUtil.getString(request, Constants.CMD);
62  
63          if (Validator.isNull(cmd)) {
64              return mapping.findForward("portal.update_email_address");
65          }
66  
67          try {
68              updateEmailAddress(request);
69  
70              return mapping.findForward(ActionConstants.COMMON_REFERER);
71          }
72          catch (Exception e) {
73              if (e instanceof DuplicateUserEmailAddressException ||
74                  e instanceof ReservedUserEmailAddressException ||
75                  e instanceof UserEmailAddressException) {
76  
77                  SessionErrors.add(request, e.getClass().getName());
78  
79                  return mapping.findForward("portal.update_email_address");
80              }
81              else if (e instanceof NoSuchUserException ||
82                       e instanceof PrincipalException) {
83  
84                  SessionErrors.add(request, e.getClass().getName());
85  
86                  return mapping.findForward("portal.error");
87              }
88              else {
89                  PortalUtil.sendError(e, request, response);
90  
91                  return null;
92              }
93          }
94      }
95  
96      protected void updateEmailAddress(HttpServletRequest request)
97          throws Exception {
98  
99          long userId = PortalUtil.getUserId(request);
100         String password = AdminUtil.getUpdateUserPassword(request, userId);
101         String emailAddress1 = ParamUtil.getString(request, "emailAddress1");
102         String emailAddress2 = ParamUtil.getString(request, "emailAddress2");
103 
104         UserServiceUtil.updateEmailAddress(
105             userId, password, emailAddress1, emailAddress2);
106     }
107 
108 }