View Javadoc
1   /**
2    * Copyright 2005-2016 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.kim.web.struts.action;
17  
18  import org.apache.struts.action.ActionForm;
19  import org.apache.struts.action.ActionForward;
20  import org.apache.struts.action.ActionMapping;
21  import org.kuali.rice.core.api.util.RiceConstants;
22  import org.kuali.rice.kim.api.role.RoleService;
23  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24  import org.kuali.rice.kim.impl.data.DataIntegrityService;
25  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
26  import org.kuali.rice.kns.web.struts.action.KualiAction;
27  import org.kuali.rice.krad.exception.AuthorizationException;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.List;
35  
36  public class DataIntegrityAction extends KualiAction {
37  
38  	/**
39  	 * To avoid having to go through the pain of setting up a KIM permission for "Use Screen" for this utility screen,
40  	 * we'll hardcode this screen to the "KR-SYS Technical Administrator" role. Without doing this, the screen is open
41  	 * to all users until that permission is setup which could be considered a security issue.
42  	 */
43  	protected void checkAuthorization( ActionForm form, String methodToCall) throws AuthorizationException
44  	{
45  		boolean authorized = false;
46  		String principalId = GlobalVariables.getUserSession().getPrincipalId();
47  		RoleService roleService = KimApiServiceLocator.getRoleService();
48  		String roleId = roleService.getRoleIdByNamespaceCodeAndName("KR-SYS", "Technical Administrator");
49  		if (roleId != null) {
50  			authorized = roleService.principalHasRole(principalId, Collections.singletonList(roleId),
51  					new HashMap<String, String>(), true);
52  		}
53  
54  		if (!authorized) {
55  			throw new AuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(),
56  					methodToCall,
57  					this.getClass().getSimpleName());
58  		}
59  	}
60  
61  	public ActionForward check(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
62  		List<String> messages = getDataIntegrityService().checkIntegrity();
63  		if (messages.isEmpty()) {
64  			messages = Collections.singletonList("No data integrity issues found.");
65  		}
66  		request.setAttribute("checkMessages", messages);
67  		return mapping.findForward(RiceConstants.MAPPING_BASIC);
68  	}
69  
70  	public ActionForward repair(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
71  		List<String> messages = getDataIntegrityService().repair();
72  		if (messages.isEmpty()) {
73  			messages = Collections.singletonList("No data repair was necessary.");
74  		}
75  		request.setAttribute("repairMessages", messages);
76  		return mapping.findForward(RiceConstants.MAPPING_BASIC);
77  	}
78  
79  	public DataIntegrityService getDataIntegrityService() {
80  		return KimImplServiceLocator.getDataIntegrityService();
81  	}
82  
83  }