View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad.web.login;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
20  import org.kuali.rice.krad.UserSession;
21  import org.kuali.rice.krad.util.GlobalVariables;
22  import org.kuali.rice.krad.util.KRADConstants;
23  import org.kuali.rice.krad.util.KRADUtils;
24  import org.kuali.rice.krad.web.controller.UifControllerBase;
25  import org.kuali.rice.krad.web.form.UifFormBase;
26  import org.springframework.stereotype.Controller;
27  import org.springframework.validation.BindingResult;
28  import org.springframework.web.bind.annotation.ModelAttribute;
29  import org.springframework.web.bind.annotation.RequestMapping;
30  import org.springframework.web.bind.annotation.RequestMethod;
31  import org.springframework.web.servlet.ModelAndView;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.io.UnsupportedEncodingException;
36  import java.net.URLDecoder;
37  import java.util.Properties;
38  
39  /**
40   * Basic controller KRAD dummy login.
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  @Controller
45  @RequestMapping(value = "/login")
46  public class DummyLoginController extends UifControllerBase {
47  
48      @Override
49      protected UifFormBase createInitialForm() {
50          return new DummyLoginForm();
51      }
52  
53      @RequestMapping(method = RequestMethod.POST, params = "methodToCall=submit")
54      public ModelAndView submit(@ModelAttribute("KualiForm") DummyLoginForm uifForm, BindingResult result,
55              HttpServletRequest request, HttpServletResponse response) {
56          String returnUrl = decode(uifForm.getReturnLocation());
57          if (StringUtils.isBlank(returnUrl)) {
58              returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
59          }
60  
61          Properties props = new Properties();
62          String user = uifForm.getLogin_user();
63          if (StringUtils.isNotBlank(user)) {
64              props.put("__login_user", user);
65          }
66  
67          String password = uifForm.getLogin_pw();
68          if (StringUtils.isNotBlank(password)) {
69              props.put("__login_pw", password);
70          }
71  
72          return performRedirect(uifForm, returnUrl, props);
73      }
74  
75      /**
76       * Method to logout the backdoor user and return to the view.
77       *
78       * @return the view to return to
79       */
80      @RequestMapping(params = "methodToCall=backdoorLogout")
81      public ModelAndView backdoorLogout(@ModelAttribute("KualiForm") DummyLoginForm uifForm, BindingResult result,
82              HttpServletRequest request, HttpServletResponse response) {
83          String returnUrl = decode(uifForm.getReturnLocation());
84  
85          if (StringUtils.isBlank(returnUrl)) {
86              returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
87          }
88  
89          UserSession userSession = KRADUtils.getUserSessionFromRequest(request);
90          if (userSession.isBackdoorInUse()) {
91              userSession.clearBackdoorUser();
92          }
93  
94          return performRedirect(uifForm, returnUrl, new Properties());
95      }
96  
97      @RequestMapping(params = "methodToCall=logout")
98      public ModelAndView logout(@ModelAttribute("KualiForm") UifFormBase form, HttpServletRequest request,
99              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 }