001/**
002 * Copyright 2005-2015 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.kim.web.struts.action;
017
018import org.apache.struts.action.ActionForm;
019import org.apache.struts.action.ActionForward;
020import org.apache.struts.action.ActionMapping;
021import org.kuali.rice.core.api.util.RiceConstants;
022import org.kuali.rice.kim.api.role.RoleService;
023import org.kuali.rice.kim.api.services.KimApiServiceLocator;
024import org.kuali.rice.kim.impl.data.DataIntegrityService;
025import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
026import org.kuali.rice.kns.web.struts.action.KualiAction;
027import org.kuali.rice.krad.exception.AuthorizationException;
028import org.kuali.rice.krad.util.GlobalVariables;
029
030import javax.servlet.http.HttpServletRequest;
031import javax.servlet.http.HttpServletResponse;
032import java.util.Collections;
033import java.util.HashMap;
034import java.util.List;
035
036public class DataIntegrityAction extends KualiAction {
037
038        /**
039         * To avoid having to go through the pain of setting up a KIM permission for "Use Screen" for this utility screen,
040         * we'll hardcode this screen to the "KR-SYS Technical Administrator" role. Without doing this, the screen is open
041         * to all users until that permission is setup which could be considered a security issue.
042         */
043        protected void checkAuthorization( ActionForm form, String methodToCall) throws AuthorizationException
044        {
045                boolean authorized = false;
046                String principalId = GlobalVariables.getUserSession().getPrincipalId();
047                RoleService roleService = KimApiServiceLocator.getRoleService();
048                String roleId = roleService.getRoleIdByNamespaceCodeAndName("KR-SYS", "Technical Administrator");
049                if (roleId != null) {
050                        authorized = roleService.principalHasRole(principalId, Collections.singletonList(roleId),
051                                        new HashMap<String, String>(), true);
052                }
053
054                if (!authorized) {
055                        throw new AuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(),
056                                        methodToCall,
057                                        this.getClass().getSimpleName());
058                }
059        }
060
061        public ActionForward check(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
062                List<String> messages = getDataIntegrityService().checkIntegrity();
063                if (messages.isEmpty()) {
064                        messages = Collections.singletonList("No data integrity issues found.");
065                }
066                request.setAttribute("checkMessages", messages);
067                return mapping.findForward(RiceConstants.MAPPING_BASIC);
068        }
069
070        public ActionForward repair(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
071                List<String> messages = getDataIntegrityService().repair();
072                if (messages.isEmpty()) {
073                        messages = Collections.singletonList("No data repair was necessary.");
074                }
075                request.setAttribute("repairMessages", messages);
076                return mapping.findForward(RiceConstants.MAPPING_BASIC);
077        }
078
079        public DataIntegrityService getDataIntegrityService() {
080                return KimImplServiceLocator.getDataIntegrityService();
081        }
082
083}