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.login;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.config.property.ConfigContext;
020import org.kuali.rice.krad.UserSession;
021import org.kuali.rice.krad.util.GlobalVariables;
022import org.kuali.rice.krad.util.KRADConstants;
023import org.kuali.rice.krad.util.KRADUtils;
024import org.kuali.rice.krad.web.controller.UifControllerBase;
025import org.kuali.rice.krad.web.form.UifFormBase;
026import org.springframework.stereotype.Controller;
027import org.springframework.validation.BindingResult;
028import org.springframework.web.bind.annotation.ModelAttribute;
029import org.springframework.web.bind.annotation.RequestMapping;
030import org.springframework.web.bind.annotation.RequestMethod;
031import org.springframework.web.servlet.ModelAndView;
032
033import javax.servlet.http.HttpServletRequest;
034import javax.servlet.http.HttpServletResponse;
035import java.io.UnsupportedEncodingException;
036import java.net.URLDecoder;
037import java.util.Properties;
038
039/**
040 * Basic controller KRAD dummy login.
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044@Controller
045@RequestMapping(value = "/login")
046public class DummyLoginController extends UifControllerBase {
047
048    @Override
049    protected UifFormBase createInitialForm() {
050        return new DummyLoginForm();
051    }
052
053    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=submit")
054    public ModelAndView submit(@ModelAttribute("KualiForm") DummyLoginForm uifForm, BindingResult result,
055            HttpServletRequest request, HttpServletResponse response) {
056        String returnUrl = decode(uifForm.getReturnLocation());
057        if (StringUtils.isBlank(returnUrl)) {
058            returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
059        }
060
061        Properties props = new Properties();
062        String user = uifForm.getLogin_user();
063        if (StringUtils.isNotBlank(user)) {
064            props.put("__login_user", user);
065        }
066
067        String password = uifForm.getLogin_pw();
068        if (StringUtils.isNotBlank(password)) {
069            props.put("__login_pw", password);
070        }
071
072        return performRedirect(uifForm, returnUrl, props);
073    }
074
075    /**
076     * Method to logout the backdoor user and return to the view.
077     *
078     * @return the view to return to
079     */
080    @RequestMapping(params = "methodToCall=backdoorLogout")
081    public ModelAndView backdoorLogout(@ModelAttribute("KualiForm") DummyLoginForm uifForm, BindingResult result,
082            HttpServletRequest request, HttpServletResponse response) {
083        String returnUrl = decode(uifForm.getReturnLocation());
084
085        if (StringUtils.isBlank(returnUrl)) {
086            returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
087        }
088
089        UserSession userSession = KRADUtils.getUserSessionFromRequest(request);
090        if (userSession.isBackdoorInUse()) {
091            userSession.clearBackdoorUser();
092        }
093
094        return performRedirect(uifForm, returnUrl, new Properties());
095    }
096
097    @RequestMapping(params = "methodToCall=logout")
098    public ModelAndView logout(@ModelAttribute("KualiForm") UifFormBase form, HttpServletRequest request,
099            HttpServletResponse response) {
100        UserSession userSession = GlobalVariables.getUserSession();
101
102        if (userSession.isBackdoorInUse()) {
103            userSession.clearBackdoorUser();
104        }
105
106        request.getSession().invalidate();
107        return returnToHub(form);
108    }
109
110    private String decode(String encodedUrl) {
111        try {
112            if (StringUtils.isNotBlank(encodedUrl)) {
113                return URLDecoder.decode(encodedUrl, "UTF-8");
114            }
115        } catch (UnsupportedEncodingException e) {
116            throw new RuntimeException("Unable to decode value: " + encodedUrl, e);
117        }
118
119        return null;
120    }
121
122}