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