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