001    /**
002     * Copyright (c) 2000-2012 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.portal.events;
016    
017    import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
018    import com.liferay.portal.kernel.cluster.ClusterNode;
019    import com.liferay.portal.kernel.events.Action;
020    import com.liferay.portal.kernel.events.ActionException;
021    import com.liferay.portal.kernel.json.JSONFactoryUtil;
022    import com.liferay.portal.kernel.json.JSONObject;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.messaging.DestinationNames;
026    import com.liferay.portal.kernel.messaging.MessageBusUtil;
027    import com.liferay.portal.kernel.servlet.HttpHeaders;
028    import com.liferay.portal.kernel.util.PropsKeys;
029    import com.liferay.portal.service.UserLocalServiceUtil;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PrefsPropsUtil;
032    import com.liferay.portal.util.PropsValues;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    import javax.servlet.http.HttpSession;
037    
038    import org.apache.struts.Globals;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class LoginPostAction extends Action {
044    
045            @Override
046            public void run(HttpServletRequest request, HttpServletResponse response)
047                    throws ActionException {
048    
049                    try {
050                            if (_log.isDebugEnabled()) {
051                                    _log.debug("Running " + request.getRemoteUser());
052                            }
053    
054                            HttpSession session = request.getSession();
055    
056                            long companyId = PortalUtil.getCompanyId(request);
057                            long userId = 0;
058    
059                            // Language
060    
061                            session.removeAttribute(Globals.LOCALE_KEY);
062    
063                            // Live users
064    
065                            if (PropsValues.LIVE_USERS_ENABLED) {
066                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
067    
068                                    ClusterNode clusterNode =
069                                            ClusterExecutorUtil.getLocalClusterNode();
070    
071                                    jsonObject.put("clusterNodeId", clusterNode.getClusterNodeId());
072                                    jsonObject.put("command", "signIn");
073                                    jsonObject.put("companyId", companyId);
074                                    jsonObject.put("remoteAddr", request.getRemoteAddr());
075                                    jsonObject.put("remoteHost", request.getRemoteHost());
076                                    jsonObject.put("sessionId", session.getId());
077    
078                                    String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
079    
080                                    jsonObject.put("userAgent", userAgent);
081    
082                                    userId = PortalUtil.getUserId(request);
083    
084                                    jsonObject.put("userId", userId);
085    
086                                    MessageBusUtil.sendMessage(
087                                            DestinationNames.LIVE_USERS, jsonObject.toString());
088                            }
089    
090                            if (PrefsPropsUtil.getBoolean(
091                                            companyId, PropsKeys.ADMIN_SYNC_DEFAULT_ASSOCIATIONS)) {
092    
093                                    if (userId == 0) {
094                                            userId = PortalUtil.getUserId(request);
095                                    }
096    
097                                    UserLocalServiceUtil.addDefaultGroups(userId);
098                                    UserLocalServiceUtil.addDefaultRoles(userId);
099                                    UserLocalServiceUtil.addDefaultUserGroups(userId);
100                            }
101                    }
102                    catch (Exception e) {
103                            throw new ActionException(e);
104                    }
105            }
106    
107            private static Log _log = LogFactoryUtil.getLog(LoginPostAction.class);
108    
109    }