001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
044            public void run(HttpServletRequest request, HttpServletResponse response)
045                    throws ActionException {
046    
047                    try {
048                            if (_log.isDebugEnabled()) {
049                                    _log.debug("Running " + request.getRemoteUser());
050                            }
051    
052                            HttpSession session = request.getSession();
053    
054                            long companyId = PortalUtil.getCompanyId(request);
055                            long userId = 0;
056    
057                            // Language
058    
059                            session.removeAttribute(Globals.LOCALE_KEY);
060    
061                            // Live users
062    
063                            if (PropsValues.LIVE_USERS_ENABLED) {
064                                    userId = PortalUtil.getUserId(request);
065    
066                                    String sessionId = session.getId();
067                                    String remoteAddr = request.getRemoteAddr();
068                                    String remoteHost = request.getRemoteHost();
069                                    String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
070    
071                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
072    
073                                    jsonObject.put("command", "signIn");
074                                    jsonObject.put("companyId", companyId);
075                                    jsonObject.put("userId", userId);
076                                    jsonObject.put("sessionId", sessionId);
077                                    jsonObject.put("remoteAddr", remoteAddr);
078                                    jsonObject.put("remoteHost", remoteHost);
079                                    jsonObject.put("userAgent", userAgent);
080    
081                                    MessageBusUtil.sendMessage(
082                                            DestinationNames.LIVE_USERS, jsonObject.toString());
083                            }
084    
085                            if (PrefsPropsUtil.getBoolean(
086                                            companyId, PropsKeys.ADMIN_SYNC_DEFAULT_ASSOCIATIONS)) {
087    
088                                    if (userId == 0) {
089                                            userId = PortalUtil.getUserId(request);
090                                    }
091    
092                                    UserLocalServiceUtil.addDefaultGroups(userId);
093                                    UserLocalServiceUtil.addDefaultRoles(userId);
094                                    UserLocalServiceUtil.addDefaultUserGroups(userId);
095                            }
096                    }
097                    catch (Exception e) {
098                            throw new ActionException(e);
099                    }
100            }
101    
102            private static Log _log = LogFactoryUtil.getLog(LoginPostAction.class);
103    
104    }