View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.web.controller;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.log4j.Logger;
26  import org.kuali.rice.core.framework.parameter.ParameterService;
27  import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
28  import org.kuali.rice.kim.api.KimConstants;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
31  import org.kuali.rice.krad.service.KualiModuleService;
32  import org.kuali.rice.krad.service.ModuleService;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.KRADConstants;
35  import org.springframework.web.servlet.HandlerInterceptor;
36  import org.springframework.web.servlet.ModelAndView;
37  
38  /**
39   * TODO jawbenne don't forget to fill this in. 
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class ModuleLockingHandlerInterceptor implements HandlerInterceptor {
44  
45      private static final Logger LOG = Logger.getLogger(ModuleLockingHandlerInterceptor.class);
46      
47      
48      private KualiModuleService kualiModuleService;
49      private String moduleLockedMapping;
50      
51      /**
52       * @return the moduleLockedMapping
53       */
54      public String getModuleLockedMapping() {
55          return this.moduleLockedMapping;
56      }
57  
58      /**
59       * @param moduleLockedMapping the moduleLockedMapping to set
60       */
61      public void setModuleLockedMapping(String moduleLockedMapping) {
62          this.moduleLockedMapping = moduleLockedMapping;
63      }
64  
65      /**
66       * @param kualiModuleService the kualiModuleService to set
67       */
68      public void setKualiModuleService(KualiModuleService kualiModuleService) {
69          this.kualiModuleService = kualiModuleService;
70      }
71  
72      /**
73       * This overridden method ...
74       * 
75       * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
76       */
77      @Override
78      public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
79          // do nothing
80      }
81  
82      /**
83       * This overridden method ...
84       * 
85       * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
86       */
87      @Override
88      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndview)
89              throws Exception {
90          // do nothing
91      }
92  
93      /**
94       * This overridden method ...
95       * 
96       * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
97       */
98      @Override
99      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
100         if(isModuleLocked(request)) {
101             response.sendRedirect(this.getModuleLockedMapping() + "?" + ModuleLockedController.MODULE_PARAMETER + "=" + getModuleService(request).getModuleConfiguration().getNamespaceCode());
102         }
103         return true;
104     }
105     
106     private ModuleService getModuleService(HttpServletRequest request) {
107         String boClass = request.getParameter(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE);
108         if(StringUtils.isBlank(boClass)) {
109             boClass= request.getParameter(KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE);
110         }
111         ModuleService moduleService = null;
112         if(StringUtils.isNotBlank(boClass)) {
113             try {
114                 moduleService = getKualiModuleService().getResponsibleModuleService(Class.forName(boClass));
115             } catch (ClassNotFoundException classNotFoundException) {
116                 LOG.warn("BO class not found: " + boClass, classNotFoundException);
117             }
118         } else {
119             moduleService = getKualiModuleService().getResponsibleModuleService(this.getClass());
120         }
121         return moduleService;
122     }
123 
124     protected boolean isModuleLocked(HttpServletRequest request) {
125         ModuleService moduleService = getModuleService(request);
126         if(moduleService != null && moduleService.isLocked()) {
127             String principalId = GlobalVariables.getUserSession().getPrincipalId();
128             String namespaceCode = KRADConstants.KUALI_RICE_SYSTEM_NAMESPACE;
129             String permissionName = KimConstants.PermissionNames.ACCESS_LOCKED_MODULE;
130             Map<String, String> permissionDetails = new HashMap<String, String>();
131             Map<String, String> qualification = new HashMap<String, String>();
132             if(!KimApiServiceLocator.getPermissionService().isAuthorized(principalId, namespaceCode, permissionName, permissionDetails, qualification)) {
133                 return true;
134             }
135         }
136         return false;
137     }
138     
139     protected KualiModuleService getKualiModuleService() {
140         if ( kualiModuleService == null ) {
141             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
142         }
143         return kualiModuleService;
144     }
145 }