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.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                                    if (clusterNode != null) {
072                                            jsonObject.put(
073                                                    "clusterNodeId", clusterNode.getClusterNodeId());
074                                    }
075    
076                                    jsonObject.put("command", "signIn");
077                                    jsonObject.put("companyId", companyId);
078                                    jsonObject.put("remoteAddr", request.getRemoteAddr());
079                                    jsonObject.put("remoteHost", request.getRemoteHost());
080                                    jsonObject.put("sessionId", session.getId());
081    
082                                    String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
083    
084                                    jsonObject.put("userAgent", userAgent);
085    
086                                    userId = PortalUtil.getUserId(request);
087    
088                                    jsonObject.put("userId", userId);
089    
090                                    MessageBusUtil.sendMessage(
091                                            DestinationNames.LIVE_USERS, jsonObject.toString());
092                            }
093    
094                            if (PrefsPropsUtil.getBoolean(
095                                            companyId, PropsKeys.ADMIN_SYNC_DEFAULT_ASSOCIATIONS)) {
096    
097                                    if (userId == 0) {
098                                            userId = PortalUtil.getUserId(request);
099                                    }
100    
101                                    UserLocalServiceUtil.addDefaultGroups(userId);
102                                    UserLocalServiceUtil.addDefaultRoles(userId);
103                                    UserLocalServiceUtil.addDefaultUserGroups(userId);
104                            }
105                    }
106                    catch (Exception e) {
107                            throw new ActionException(e);
108                    }
109            }
110    
111            private static Log _log = LogFactoryUtil.getLog(LoginPostAction.class);
112    
113    }