1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.filters.servletauthorizing;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.ProtectedServletRequest;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.security.auth.CompanyThreadLocal;
31  import com.liferay.portal.security.auth.PrincipalThreadLocal;
32  import com.liferay.portal.security.permission.PermissionChecker;
33  import com.liferay.portal.security.permission.PermissionCheckerFactory;
34  import com.liferay.portal.security.permission.PermissionThreadLocal;
35  import com.liferay.portal.service.UserLocalServiceUtil;
36  import com.liferay.portal.servlet.filters.BasePortalFilter;
37  import com.liferay.portal.util.PortalInstances;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portal.util.PropsValues;
40  import com.liferay.portal.util.WebKeys;
41  
42  import javax.servlet.FilterChain;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  import javax.servlet.http.HttpSession;
46  
47  import org.apache.struts.Globals;
48  
49  /**
50   * <a href="ServletAuthorizingFilter.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Raymond Augé
53   *
54   */
55  public class ServletAuthorizingFilter extends BasePortalFilter {
56  
57      protected void processFilter(
58              HttpServletRequest request, HttpServletResponse response,
59              FilterChain filterChain)
60          throws Exception {
61  
62          HttpSession session = request.getSession();
63  
64          // Company id
65  
66          long companyId = PortalInstances.getCompanyId(request);
67  
68          // We need to set the COMPANY_ID request attribute explicitly because
69          // the above does not.
70  
71          request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
72  
73          // Authorize
74  
75          long userId = PortalUtil.getUserId(request);
76          String remoteUser = request.getRemoteUser();
77  
78          if (!PropsValues.PORTAL_JAAS_ENABLE) {
79              String jRemoteUser = (String)session.getAttribute("j_remoteuser");
80  
81              if (jRemoteUser != null) {
82                  remoteUser = jRemoteUser;
83  
84                  session.removeAttribute("j_remoteuser");
85              }
86          }
87  
88          if ((userId > 0) && (remoteUser == null)) {
89              remoteUser = String.valueOf(userId);
90          }
91  
92          // WebSphere will not return the remote user unless you are
93          // authenticated AND accessing a protected path. Other servers will
94          // return the remote user for all threads associated with an
95          // authenticated user. We use ProtectedServletRequest to ensure we get
96          // similar behavior across all servers.
97  
98          request = new ProtectedServletRequest(request, remoteUser);
99  
100         PermissionChecker permissionChecker = null;
101 
102         if ((userId > 0) || (remoteUser != null)) {
103 
104             // Set the principal associated with this thread
105 
106             String name = String.valueOf(userId);
107 
108             if (remoteUser != null) {
109                 name = remoteUser;
110             }
111 
112             PrincipalThreadLocal.setName(name);
113 
114             // User id
115 
116             userId = GetterUtil.getLong(name);
117 
118             try {
119 
120                 // User
121 
122                 User user = UserLocalServiceUtil.getUserById(userId);
123 
124                 // Permission checker
125 
126                 permissionChecker = PermissionCheckerFactory.create(user, true);
127 
128                 PermissionThreadLocal.setPermissionChecker(permissionChecker);
129 
130                 // User id
131 
132                 session.setAttribute(WebKeys.USER_ID, new Long(userId));
133 
134                 // User locale
135 
136                 session.setAttribute(Globals.LOCALE_KEY, user.getLocale());
137             }
138             catch (Exception e) {
139                 _log.error(e, e);
140             }
141         }
142 
143         try {
144             processFilter(
145                 ServletAuthorizingFilter.class, request, response, filterChain);
146         }
147         finally {
148             try {
149 
150                 // Clean up the permission checker
151 
152                 PermissionCheckerFactory.recycle(permissionChecker);
153             }
154             catch (Exception e) {
155                 _log.error(e, e);
156             }
157 
158             // Clear the company id associated with this thread
159 
160             CompanyThreadLocal.setCompanyId(0);
161 
162             // Clear the principal associated with this thread
163 
164             PrincipalThreadLocal.setName(null);
165         }
166     }
167 
168     private static Log _log =
169         LogFactoryUtil.getLog(ServletAuthorizingFilter.class);
170 
171 }