1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
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
53
54 public String getModuleLockedMapping() {
55 return this.moduleLockedMapping;
56 }
57
58
59
60
61 public void setModuleLockedMapping(String moduleLockedMapping) {
62 this.moduleLockedMapping = moduleLockedMapping;
63 }
64
65
66
67
68 public void setKualiModuleService(KualiModuleService kualiModuleService) {
69 this.kualiModuleService = kualiModuleService;
70 }
71
72
73
74
75
76
77 @Override
78 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
79
80 }
81
82
83
84
85
86
87 @Override
88 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndview)
89 throws Exception {
90
91 }
92
93
94
95
96
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 }