1 | package edu.iu.uis.sit.portal.filter; |
2 | |
3 | import java.io.IOException; |
4 | import java.sql.Timestamp; |
5 | import java.util.ArrayList; |
6 | import java.util.Collections; |
7 | import java.util.Iterator; |
8 | import java.util.List; |
9 | |
10 | import javax.servlet.Filter; |
11 | import javax.servlet.FilterChain; |
12 | import javax.servlet.FilterConfig; |
13 | import javax.servlet.ServletException; |
14 | import javax.servlet.ServletRequest; |
15 | import javax.servlet.ServletResponse; |
16 | import javax.servlet.http.HttpServletRequest; |
17 | import javax.servlet.http.HttpServletResponse; |
18 | |
19 | import org.apache.pluto.driver.services.impl.resource.OneStartResourceConfigReader; |
20 | import org.apache.pluto.driver.services.portal.PageConfig; |
21 | |
22 | import edu.iu.uis.cas.filter.FilterCAS; |
23 | import edu.iu.uis.sit.portal.SpringServiceLocator; |
24 | import edu.iu.uis.sit.portal.portlet.personalize.domain.GroupLink; |
25 | import edu.iu.uis.sit.portal.portlet.personalize.web.WebGroup; |
26 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Tab; |
27 | import edu.iu.uis.sit.portal.user.User; |
28 | import edu.iu.uis.sit.portal.user.domain.OneStartUser; |
29 | import edu.iu.uis.sit.portal.user.service.AclPerson; |
30 | |
31 | public class PortalFilter implements Filter { |
32 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PortalFilter.class); |
33 | |
34 | public void init(FilterConfig config) throws ServletException { |
35 | } |
36 | |
37 | public void destroy() { |
38 | } |
39 | |
40 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
41 | HttpServletRequest httpRequest = (HttpServletRequest) request; |
42 | |
43 | establishUserSession((HttpServletRequest) request, (HttpServletResponse) response); |
44 | |
45 | User userSession = (User) httpRequest.getSession().getAttribute(User.USER_KEY); |
46 | |
47 | if (httpRequest != null && httpRequest.getPathInfo() != null && httpRequest.getPathInfo().indexOf("_maximized") > -1) { |
48 | if (httpRequest.getParameter("focus") != null) { |
49 | userSession.setFocus(httpRequest.getParameter("focus")); |
50 | } |
51 | } else { |
52 | userSession.setFocus(null); |
53 | } |
54 | |
55 | if (httpRequest != null && httpRequest.getPathInfo() != null && "/portal".equals(httpRequest.getServletPath())) { |
56 | String pathInfo = httpRequest.getPathInfo() + "?" + httpRequest.getQueryString(); |
57 | if (userSession.getFocus() != null) { |
58 | pathInfo += "&focus=" + userSession.getFocus(); |
59 | if (httpRequest.getParameter("portletTitle") != null) { |
60 | userSession.setPortletTitle(httpRequest.getParameter("portletTitle")); |
61 | } |
62 | } |
63 | userSession.setPathInfo(pathInfo); |
64 | } |
65 | |
66 | if (userSession.getPathInfo() == null) { |
67 | userSession.setPathInfo(""); |
68 | } |
69 | |
70 | if (httpRequest.getParameter("subtab") != null && httpRequest.getParameter("subtab").equalsIgnoreCase("true") && httpRequest.getParameter("parentId") != null) { |
71 | String parentId = httpRequest.getParameter("parentId"); |
72 | userSession.setParentId(parentId); |
73 | userSession.setSubtab(true); |
74 | PageConfig parent = OneStartResourceConfigReader.getFactory().getResourceConfig().getRenderConfig().getPageConfig(userSession.getParentId()); |
75 | userSession.setParent(parent); |
76 | } |
77 | |
78 | if (httpRequest.getParameter("__clear_user_session__") != null) { |
79 | userSession.setParentId(null); |
80 | userSession.setSubtab(false); |
81 | userSession.setSubSubtab(false); |
82 | userSession.setParent(null); |
83 | } |
84 | |
85 | chain.doFilter(request, response); |
86 | } |
87 | |
88 | private boolean establishUserSession(HttpServletRequest request, HttpServletResponse response) { |
89 | User userSession = (User) request.getSession(true).getAttribute(User.USER_KEY); |
90 | if (!isUserSessionEstablished(request)) { |
91 | // try { |
92 | userSession = login(request); |
93 | request.getSession().setAttribute(User.USER_KEY, userSession); |
94 | // } catch (LoginException le) { |
95 | // LOG.error("Login Exception.", le); |
96 | // MyClassifiedsErrors myClassifiedsErrors = new |
97 | // MyClassifiedsErrors(); |
98 | // myClassifiedsErrors.setError(getClassifiedsService().getApplicationConstant("Error.Login.GdsUserHasNoAttributes").getConstantValue()); |
99 | // request.setAttribute("MyClassifiedsErrors", myClassifiedsErrors); |
100 | // // try { |
101 | // // super.processForwardConfig(request, response, |
102 | // moduleConfig.findForwardConfig("ClassifiedsErrorPage")); |
103 | // // } catch (Exception e) { |
104 | // // LOG.error("error", e); |
105 | // // } |
106 | // return false; |
107 | // } |
108 | } |
109 | // if (isAccessDenied(request) || isBannedUser(userSession)) { |
110 | // LOG.info("responding with access denied for request: " + |
111 | // request.getRequestURI() + " with remote user: " + |
112 | // userSession.getUsername() + " from remote addr: " + |
113 | // request.getHeader("X-CLUSTER-CLIENT-IP")); |
114 | // MyClassifiedsErrors myClassifiedsErrors = new MyClassifiedsErrors(); |
115 | // myClassifiedsErrors.setError("<li>You are not currently authorized to |
116 | // use this service."); |
117 | // myClassifiedsErrors.setError("<li>Please contact your administrator |
118 | // or, "); |
119 | // myClassifiedsErrors.setError("<li>Contact: <a |
120 | // href='mailto:onestart.indiana.edu'>OneStart Project Team</a>"); |
121 | // request.setAttribute("MyClassifiedsErrors", myClassifiedsErrors); |
122 | // // try { |
123 | // // super.processForwardConfig(request, response, |
124 | // moduleConfig.findForwardConfig("ClassifiedsErrorPage")); |
125 | // // } catch (Exception e) { |
126 | // // LOG.error("error", e); |
127 | // // } |
128 | // return false; |
129 | // } |
130 | // LOG.info("forwarding request to action servlet for processing: " + |
131 | // request.getRequestURI() + " with remote user: " + |
132 | // userSession.getUsername() + " from remote addr: " + |
133 | // request.getHeader("X-CLUSTER-CLIENT-IP")); |
134 | // addP3pHeaders(request, response); |
135 | return true; |
136 | } |
137 | |
138 | /** |
139 | * Checks if the user who made the request has a UserSession established |
140 | * |
141 | * @param request |
142 | * the HTTPServletRequest object passed in |
143 | * @return true if the user session has been established, false otherwise |
144 | */ |
145 | private boolean isUserSessionEstablished(HttpServletRequest request) { |
146 | return (request.getSession(false) != null && request.getSession(false).getAttribute(User.USER_KEY) != null); |
147 | } |
148 | |
149 | /** |
150 | * create a UserSession object for the user |
151 | */ |
152 | private User login(HttpServletRequest request) { |
153 | String kerberosUserName = FilterCAS.getRemoteUser(request); |
154 | LOG.info("performing Kerberos login: " + kerberosUserName); |
155 | String firstName = null; |
156 | String lastName = null; |
157 | String emailAddress = null; |
158 | String campusCode = null; |
159 | String emplId = null; |
160 | List groups = new ArrayList(); |
161 | OneStartUser oneStartUser = null; |
162 | try { |
163 | LOG.info("starting eds lookup: "); |
164 | AclPerson person = SpringServiceLocator.getAclService().findUserByUsername(kerberosUserName); |
165 | firstName = person.getFirstName(); |
166 | lastName = person.getLastName(); |
167 | campusCode = person.getCampus(); |
168 | emplId = person.getPersonId(); |
169 | emailAddress = person.getEmailAddress(); |
170 | groups = person.getGroups(); |
171 | |
172 | oneStartUser = SpringServiceLocator.getUserService().findOneStartUserByEmplId(emplId); |
173 | if (oneStartUser == null) { |
174 | oneStartUser = SpringServiceLocator.getUserService().findOneStartUserByUserName(kerberosUserName); |
175 | } |
176 | if (oneStartUser == null) { |
177 | oneStartUser = new OneStartUser(); |
178 | } |
179 | oneStartUser.setEmplId(emplId); |
180 | oneStartUser.setUserName(kerberosUserName); |
181 | oneStartUser.setLastLoginDate(new Timestamp(System.currentTimeMillis())); |
182 | SpringServiceLocator.getUserService().saveOneStartUser(oneStartUser); |
183 | LOG.info("ending eds lookup: "); |
184 | } catch (Exception e) { |
185 | LOG.error("Error in eds Kerberos login", e); |
186 | } |
187 | |
188 | /** |
189 | * now before we move on, we need to make sure that we have personSequenceNumber and if we dont then we need to throw a login error |
190 | */ |
191 | if (emplId == null || emplId.trim().length() < 1) { |
192 | LOG.error("Error in kerberos login: no person empl id retrieved"); |
193 | throw new RuntimeException("Error Performing Login, unable to locate account definition in directory"); |
194 | } |
195 | User user = new User(oneStartUser, lastName, firstName, emailAddress, campusCode, groups); |
196 | List groupQuickLinks = new ArrayList(); |
197 | for (Iterator iter = SpringServiceLocator.getPersonalizeService().findUsersGroupLinks(oneStartUser.getPersonId()).iterator(); iter.hasNext();) { |
198 | GroupLink groupLink = (GroupLink) iter.next(); |
199 | WebGroup webGroup = new WebGroup(groupLink); |
200 | Tab tab = SpringServiceLocator.getCacheService().findTab(user, groupLink.getTabId()); |
201 | if (tab != null) { |
202 | webGroup.setGroupLinkName(tab.getName()); |
203 | groupQuickLinks.add(webGroup); |
204 | } |
205 | } |
206 | Collections.sort(groupQuickLinks); |
207 | user.setGroupQuickLinks(groupQuickLinks); |
208 | return user; |
209 | } |
210 | } |