1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.events;
16  
17  import com.liferay.portal.kernel.events.Action;
18  import com.liferay.portal.kernel.events.ActionException;
19  import com.liferay.portal.kernel.json.JSONFactoryUtil;
20  import com.liferay.portal.kernel.json.JSONObject;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.messaging.DestinationNames;
24  import com.liferay.portal.kernel.messaging.MessageBusUtil;
25  import com.liferay.portal.kernel.servlet.HttpHeaders;
26  import com.liferay.portal.kernel.util.PropsKeys;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.PrefsPropsUtil;
30  import com.liferay.portal.util.PropsValues;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.http.HttpSession;
35  
36  import org.apache.struts.Globals;
37  
38  /**
39   * <a href="LoginPostAction.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class LoginPostAction extends Action {
44  
45      public void run(HttpServletRequest request, HttpServletResponse response)
46          throws ActionException {
47  
48          try {
49              if (_log.isDebugEnabled()) {
50                  _log.debug("Running " + request.getRemoteUser());
51              }
52  
53              HttpSession session = request.getSession();
54  
55              long companyId = PortalUtil.getCompanyId(request);
56              long userId = PortalUtil.getUserId(request);
57  
58              // Language
59  
60              session.removeAttribute(Globals.LOCALE_KEY);
61  
62              // Live users
63  
64              if (PropsValues.LIVE_USERS_ENABLED) {
65                  String sessionId = session.getId();
66                  String remoteAddr = request.getRemoteAddr();
67                  String remoteHost = request.getRemoteHost();
68                  String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
69  
70                  JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
71  
72                  jsonObject.put("command", "signIn");
73                  jsonObject.put("companyId", companyId);
74                  jsonObject.put("userId", userId);
75                  jsonObject.put("sessionId", sessionId);
76                  jsonObject.put("remoteAddr", remoteAddr);
77                  jsonObject.put("remoteHost", remoteHost);
78                  jsonObject.put("userAgent", userAgent);
79  
80                  MessageBusUtil.sendMessage(
81                      DestinationNames.LIVE_USERS, jsonObject.toString());
82              }
83  
84              if (PrefsPropsUtil.getBoolean(
85                      companyId, PropsKeys.ADMIN_SYNC_DEFAULT_ASSOCIATIONS)) {
86  
87                  UserLocalServiceUtil.addDefaultGroups(userId);
88                  UserLocalServiceUtil.addDefaultRoles(userId);
89                  UserLocalServiceUtil.addDefaultUserGroups(userId);
90              }
91          }
92          catch (Exception e) {
93              throw new ActionException(e);
94          }
95      }
96  
97      private static Log _log = LogFactoryUtil.getLog(LoginPostAction.class);
98  
99  }