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