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.PermissionCheckerFactoryUtil;
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  public class ServletAuthorizingFilter extends BasePortalFilter {
55  
56      protected void processFilter(
57              HttpServletRequest request, HttpServletResponse response,
58              FilterChain filterChain)
59          throws Exception {
60  
61          HttpSession session = request.getSession();
62  
63          // Company id
64  
65          long companyId = PortalInstances.getCompanyId(request);
66  
67          // We need to set the COMPANY_ID request attribute explicitly because
68          // the above does not.
69  
70          request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
71  
72          // Authorize
73  
74          long userId = PortalUtil.getUserId(request);
75          String remoteUser = request.getRemoteUser();
76  
77          if (!PropsValues.PORTAL_JAAS_ENABLE) {
78              String jRemoteUser = (String)session.getAttribute("j_remoteuser");
79  
80              if (jRemoteUser != null) {
81                  remoteUser = jRemoteUser;
82  
83                  session.removeAttribute("j_remoteuser");
84              }
85          }
86  
87          if ((userId > 0) && (remoteUser == null)) {
88              remoteUser = String.valueOf(userId);
89          }
90  
91          // WebSphere will not return the remote user unless you are
92          // authenticated AND accessing a protected path. Other servers will
93          // return the remote user for all threads associated with an
94          // authenticated user. We use ProtectedServletRequest to ensure we get
95          // similar behavior across all servers.
96  
97          request = new ProtectedServletRequest(request, remoteUser);
98  
99          if ((userId > 0) || (remoteUser != null)) {
100 
101             // Set the principal associated with this thread
102 
103             String name = String.valueOf(userId);
104 
105             if (remoteUser != null) {
106                 name = remoteUser;
107             }
108 
109             PrincipalThreadLocal.setName(name);
110 
111             // User id
112 
113             userId = GetterUtil.getLong(name);
114 
115             try {
116 
117                 // User
118 
119                 User user = UserLocalServiceUtil.getUserById(userId);
120 
121                 // Permission checker
122 
123                 PermissionChecker permissionChecker =
124                     PermissionCheckerFactoryUtil.create(user, true);
125 
126                 PermissionThreadLocal.setPermissionChecker(permissionChecker);
127 
128                 // User id
129 
130                 session.setAttribute(WebKeys.USER_ID, new Long(userId));
131 
132                 // User locale
133 
134                 session.setAttribute(Globals.LOCALE_KEY, user.getLocale());
135             }
136             catch (Exception e) {
137                 _log.error(e, e);
138             }
139         }
140 
141         try {
142             processFilter(
143                 ServletAuthorizingFilter.class, request, response, filterChain);
144         }
145         finally {
146 
147             // Clear the company id associated with this thread
148 
149             CompanyThreadLocal.setCompanyId(0);
150 
151             // Clear the principal associated with this thread
152 
153             PrincipalThreadLocal.setName(null);
154         }
155     }
156 
157     private static Log _log =
158         LogFactoryUtil.getLog(ServletAuthorizingFilter.class);
159 
160 }