001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.myaccount.action;
016    
017    import com.liferay.portal.UserPasswordException;
018    import com.liferay.portal.kernel.servlet.DynamicServletRequest;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.Company;
021    import com.liferay.portal.model.CompanyConstants;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.security.pwd.PwdAuthenticator;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.RenderRequestImpl;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class EditUserAction
041            extends com.liferay.portlet.usersadmin.action.EditUserAction {
042    
043            @Override
044            public void processAction(
045                            ActionMapping actionMapping, ActionForm actionForm,
046                            PortletConfig portletConfig, ActionRequest actionRequest,
047                            ActionResponse actionResponse)
048                    throws Exception {
049    
050                    if (redirectToLogin(actionRequest, actionResponse)) {
051                            return;
052                    }
053    
054                    super.processAction(
055                            actionMapping, actionForm, portletConfig, actionRequest,
056                            actionResponse);
057            }
058    
059            @Override
060            public ActionForward render(
061                            ActionMapping actionMapping, ActionForm actionForm,
062                            PortletConfig portletConfig, RenderRequest renderRequest,
063                            RenderResponse renderResponse)
064                    throws Exception {
065    
066                    User user = PortalUtil.getUser(renderRequest);
067    
068                    RenderRequestImpl renderRequestImpl = (RenderRequestImpl)renderRequest;
069    
070                    DynamicServletRequest dynamicRequest =
071                            (DynamicServletRequest)renderRequestImpl.getHttpServletRequest();
072    
073                    dynamicRequest.setParameter(
074                            "p_u_i_d", String.valueOf(user.getUserId()));
075    
076                    return super.render(
077                            actionMapping, actionForm, portletConfig, renderRequest,
078                            renderResponse);
079            }
080    
081            @Override
082            protected Object[] updateUser(
083                            ActionRequest actionRequest, ActionResponse actionResponse)
084                    throws Exception {
085    
086                    String currentPassword = actionRequest.getParameter("password0");
087                    String newPassword = actionRequest.getParameter("password1");
088    
089                    if (Validator.isNotNull(currentPassword)) {
090                            if (Validator.isNull(newPassword)) {
091                                    throw new UserPasswordException(
092                                            UserPasswordException.PASSWORD_LENGTH);
093                            }
094    
095                            Company company = PortalUtil.getCompany(actionRequest);
096    
097                            String authType = company.getAuthType();
098    
099                            User user = PortalUtil.getSelectedUser(actionRequest);
100    
101                            String login = null;
102    
103                            if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
104                                    login = user.getEmailAddress();
105                            }
106    
107                            if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
108                                    login = String.valueOf(user.getUserId());
109                            }
110    
111                            if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
112                                    login = user.getScreenName();
113                            }
114    
115                            boolean validPassword = PwdAuthenticator.authenticate(
116                                    login, currentPassword, user.getPassword());
117    
118                            if (!validPassword) {
119                                    throw new UserPasswordException(
120                                            UserPasswordException.PASSWORD_INVALID);
121                            }
122                    }
123                    else if (Validator.isNotNull(newPassword)) {
124                            throw new UserPasswordException(
125                                    UserPasswordException.PASSWORD_INVALID);
126                    }
127    
128                    return super.updateUser(actionRequest, actionResponse);
129            }
130    
131    }