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