View Javadoc

1   /**
2    * Copyright 2005-2012 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.kim.api.KimConstants;
27  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  import org.kuali.rice.krad.service.KualiModuleService;
30  import org.kuali.rice.krad.service.ModuleService;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  import org.springframework.web.servlet.HandlerInterceptor;
34  import org.springframework.web.servlet.ModelAndView;
35  
36  /**
37   * TODO jawbenne don't forget to fill this in.
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class ModuleLockingHandlerInterceptor implements HandlerInterceptor {
42  
43      private static final Logger LOG = Logger.getLogger(ModuleLockingHandlerInterceptor.class);
44  
45  
46      private KualiModuleService kualiModuleService;
47      private String moduleLockedMapping;
48  
49      /**
50       * @return the moduleLockedMapping
51       */
52      public String getModuleLockedMapping() {
53          return this.moduleLockedMapping;
54      }
55  
56      /**
57       * @param moduleLockedMapping the moduleLockedMapping to set
58       */
59      public void setModuleLockedMapping(String moduleLockedMapping) {
60          this.moduleLockedMapping = moduleLockedMapping;
61      }
62  
63      /**
64       * @param kualiModuleService the kualiModuleService to set
65       */
66      public void setKualiModuleService(KualiModuleService kualiModuleService) {
67          this.kualiModuleService = kualiModuleService;
68      }
69  
70      /**
71       * This overridden method ...
72       *
73       * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
74       */
75      @Override
76      public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
77          // do nothing
78      }
79  
80      /**
81       * This overridden method ...
82       *
83       * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
84       */
85      @Override
86      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndview)
87              throws Exception {
88          // do nothing
89      }
90  
91      /**
92       * This overridden method ...
93       *
94       * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
95       */
96      @Override
97      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
98          if(isModuleLocked(request)) {
99              response.sendRedirect(this.getModuleLockedMapping() + "?" + ModuleLockedController.MODULE_PARAMETER + "=" + getModuleService(request).getModuleConfiguration().getNamespaceCode());
100         }
101         return true;
102     }
103 
104     private ModuleService getModuleService(HttpServletRequest request) {
105         String boClass = request.getParameter(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE);
106         if(StringUtils.isBlank(boClass)) {
107             boClass= request.getParameter(KRADConstants.DATA_OBJECT_CLASS_ATTRIBUTE);
108         }
109         ModuleService moduleService = null;
110         if(StringUtils.isNotBlank(boClass)) {
111             try {
112                 moduleService = getKualiModuleService().getResponsibleModuleService(Class.forName(boClass));
113             } catch (ClassNotFoundException classNotFoundException) {
114                 LOG.warn("BO class not found: " + boClass, classNotFoundException);
115             }
116         } else {
117             moduleService = getKualiModuleService().getResponsibleModuleService(this.getClass());
118         }
119         return moduleService;
120     }
121 
122     protected boolean isModuleLocked(HttpServletRequest request) {
123         ModuleService moduleService = getModuleService(request);
124         if(moduleService != null && moduleService.isLocked()) {
125             String principalId = GlobalVariables.getUserSession().getPrincipalId();
126             String namespaceCode = KRADConstants.KUALI_RICE_SYSTEM_NAMESPACE;
127             String permissionName = KimConstants.PermissionNames.ACCESS_LOCKED_MODULE;
128             Map<String, String> permissionDetails = new HashMap<String, String>();
129             Map<String, String> qualification = new HashMap<String, String>();
130             if(!KimApiServiceLocator.getPermissionService().isAuthorized(principalId, namespaceCode, permissionName, qualification)) {
131                 return true;
132             }
133         }
134         return false;
135     }
136 
137     protected KualiModuleService getKualiModuleService() {
138         if ( kualiModuleService == null ) {
139             kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
140         }
141         return kualiModuleService;
142     }
143 }