001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.controller;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import javax.servlet.http.HttpServletRequest;
022import javax.servlet.http.HttpServletResponse;
023
024import org.apache.commons.lang.StringUtils;
025import org.apache.log4j.Logger;
026import org.kuali.rice.kim.api.KimConstants;
027import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
029import org.kuali.rice.krad.service.KualiModuleService;
030import org.kuali.rice.krad.service.ModuleService;
031import org.kuali.rice.krad.util.GlobalVariables;
032import org.kuali.rice.krad.util.KRADConstants;
033import org.springframework.web.servlet.HandlerInterceptor;
034import org.springframework.web.servlet.ModelAndView;
035
036/**
037 * TODO jawbenne don't forget to fill this in. 
038 * 
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041public class ModuleLockingHandlerInterceptor implements HandlerInterceptor {
042
043    private static final Logger LOG = Logger.getLogger(ModuleLockingHandlerInterceptor.class);
044    
045    
046    private KualiModuleService kualiModuleService;
047    private String moduleLockedMapping;
048    
049    /**
050     * @return the moduleLockedMapping
051     */
052    public String getModuleLockedMapping() {
053        return this.moduleLockedMapping;
054    }
055
056    /**
057     * @param moduleLockedMapping the moduleLockedMapping to set
058     */
059    public void setModuleLockedMapping(String moduleLockedMapping) {
060        this.moduleLockedMapping = moduleLockedMapping;
061    }
062
063    /**
064     * @param kualiModuleService the kualiModuleService to set
065     */
066    public void setKualiModuleService(KualiModuleService kualiModuleService) {
067        this.kualiModuleService = kualiModuleService;
068    }
069
070    /**
071     * This overridden method ...
072     * 
073     * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
074     */
075    @Override
076    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
077        // do nothing
078    }
079
080    /**
081     * This overridden method ...
082     * 
083     * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
084     */
085    @Override
086    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndview)
087            throws Exception {
088        // do nothing
089    }
090
091    /**
092     * This overridden method ...
093     * 
094     * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
095     */
096    @Override
097    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
098        if(isModuleLocked(request)) {
099            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}