001/**
002 * Copyright 2005-2018 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.service.impl;
017
018import org.kuali.rice.kim.api.identity.Person;
019import org.kuali.rice.krad.exception.AuthorizationException;
020import org.kuali.rice.krad.uif.view.View;
021import org.kuali.rice.krad.util.GlobalVariables;
022import org.kuali.rice.krad.web.form.UifFormBase;
023import org.kuali.rice.krad.web.service.ControllerService;
024import org.kuali.rice.krad.web.service.ModelAndViewService;
025import org.kuali.rice.krad.web.service.NavigationControllerService;
026import org.springframework.web.servlet.ModelAndView;
027
028/**
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class ControllerServiceImpl implements ControllerService {
032
033    private ModelAndViewService modelAndViewService;
034    private NavigationControllerService navigationControllerService;
035
036    /**
037     * {@inheritDoc}
038     */
039    @Override
040    public ModelAndView start(UifFormBase form) {
041        checkViewAuthorization(form);
042
043        if (form.getView() != null) {
044            form.setApplyDefaultValues(true);
045        }
046
047        return getModelAndViewService().getModelAndView(form);
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    public void checkViewAuthorization(UifFormBase form) throws AuthorizationException {
055        // if user session or view not established we cannnot authorize the view request
056        View view = form.getView();
057        if ((GlobalVariables.getUserSession() == null) || view == null) {
058            return;
059        }
060
061        Person user = GlobalVariables.getUserSession().getPerson();
062        boolean viewAuthorized = view.getAuthorizer().canOpenView(view, form, user);
063
064        if (!viewAuthorized) {
065            throw new AuthorizationException(user.getPrincipalName(), "open", view.getId(),
066                    "User '" + user.getPrincipalName() + "' is not authorized to open view ID: " +
067                            view.getId(), null);
068        }
069    }
070
071    /**
072     * Default impl does nothing but render the view.
073     *
074     * {@inheritDoc}
075     */
076    @Override
077    public ModelAndView sessionTimeout(UifFormBase form) {
078        return getModelAndViewService().getModelAndView(form);
079    }
080
081    /**
082     * Navigates back to a previous point (depending on how the view was requested).
083     *
084     * {@inheritDoc}
085     * org.kuali.rice.krad.web.service.impl.NavigationControllerServiceImpl#back(org.kuali.rice.krad.web.form.UifFormBase,
086     * boolean)
087     */
088    @Override
089    public ModelAndView cancel(UifFormBase form) {
090        GlobalVariables.getUifFormManager().removeSessionForm(form);
091
092        return getNavigationControllerService().returnToHistory(form, false, false, true);
093    }
094
095    protected ModelAndViewService getModelAndViewService() {
096        return modelAndViewService;
097    }
098
099    public void setModelAndViewService(ModelAndViewService modelAndViewService) {
100        this.modelAndViewService = modelAndViewService;
101    }
102
103    protected NavigationControllerService getNavigationControllerService() {
104        return navigationControllerService;
105    }
106
107    public void setNavigationControllerService(NavigationControllerService navigationControllerService) {
108        this.navigationControllerService = navigationControllerService;
109    }
110}