1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.userProperties.action;
37 import javax.portlet.ActionRequest;
38 import javax.portlet.ActionResponse;
39 import javax.portlet.PortletConfig;
40 import javax.servlet.http.Cookie;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
44 import org.apache.struts.action.ActionForm;
45 import org.apache.struts.action.ActionForward;
46 import org.apache.struts.action.ActionMapping;
48 import com.liferay.portal.events.EventsProcessorUtil;
49 import com.liferay.portal.kernel.exception.PortalException;
50 import com.liferay.portal.kernel.exception.SystemException;
51 import com.liferay.portal.kernel.struts.LastPath;
52 import com.liferay.portal.kernel.util.HttpUtil;
53 import com.liferay.portal.kernel.util.ParamUtil;
54 import com.liferay.portal.kernel.util.PropsKeys;
55 import com.liferay.portal.kernel.util.StringPool;
56 import com.liferay.portal.model.Company;
57 import com.liferay.portal.model.User;
58 import com.liferay.portal.service.UserLocalServiceUtil;
59 import com.liferay.portal.struts.ActionConstants;
60 import com.liferay.portal.struts.PortletAction;
61 import com.liferay.portal.theme.ThemeDisplay;
62 import com.liferay.portal.util.CookieKeys;
63 import com.liferay.portal.util.PortalUtil;
64 import com.liferay.portal.util.PropsValues;
65 import com.liferay.portal.util.WebKeys;
66 import com.liferay.util.Encryptor;
67 import com.liferay.util.EncryptorException;
68 import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
71 * Demande de recuperation de mot de passe
73 public class DoAsUserAction extends PortletAction {
75 public ActionForward strutsExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
77 long doAsUserId = ParamUtil.getLong(request, "userId", 0);
78 boolean rememberMe = ParamUtil.getBoolean(request, "rememberMe", false);
79 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
80 User currentUser = PortalUtil.getUser(request);
82 String doAsUserUrl = getDoAsUserUrl(request, response, themeDisplay, currentUser, doAsUserId, rememberMe);
83 response.sendRedirect(doAsUserUrl);
88 PortalUtil.sendError(e, request, response);
93 public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
95 long doAsUserId = ParamUtil.getLong(actionRequest, "userId", 0);
96 boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe", false);
98 HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
99 HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
101 User currentUser = PortalUtil.getUser(request);
102 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
104 String doAsUserUrl = getDoAsUserUrl(request, response, themeDisplay, currentUser, doAsUserId, rememberMe);
105 actionResponse.sendRedirect(doAsUserUrl);
107 setForward(actionRequest, ActionConstants.COMMON_NULL);
109 catch (Exception e) {
110 PortalUtil.sendError(e, actionRequest, actionResponse);
114 private String getDoAsUserUrl(HttpServletRequest request, HttpServletResponse response, ThemeDisplay themeDisplay, User currentUser, long doAsUserId, boolean rememberMe) throws EncryptorException, PortalException, SystemException{
116 Company company = themeDisplay.getCompany();
118 if (doAsUserId <= 0) {
119 doAsUserId = company.getDefaultUser().getUserId();
122 User doAsUser = UserLocalServiceUtil.getUser(doAsUserId);
123 String defaultPageLanding = ENTMainUtilsLocalServiceUtil.getLoginDefaultPageLanding(doAsUser.getCompanyId());
124 String doAsURL = themeDisplay.getPortalURL() + "/user/" + doAsUser.getScreenName() + defaultPageLanding;
125 String encDoAsUserId = "";
126 encDoAsUserId = Encryptor.encrypt(company.getKeyObj(), String.valueOf(doAsUserId));
127 doAsURL = HttpUtil.addParameter(doAsURL, "doAsUserId", encDoAsUserId);
129 if(doAsUserId != themeDisplay.getRealUserId()){
131 request.setAttribute(WebKeys.USER, UserLocalServiceUtil.getUser(doAsUserId));
134 EventsProcessorUtil.process(
135 PropsKeys.LOGIN_EVENTS_POST, PropsValues.LOGIN_EVENTS_POST,
138 catch (Exception e) {
143 Cookie cookie = new Cookie("DO_AS_USER_ID_" + themeDisplay.getRealUserId(), encDoAsUserId);
144 cookie.setMaxAge(rememberMe?CookieKeys.MAX_AGE:-1);
145 cookie.setPath(StringPool.SLASH);
146 response.addCookie(cookie);