View Javadoc
1   /**
2    * Copyright 2005-2014 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.web.controller.UifControllerBase;
24  import org.kuali.rice.krad.web.form.UifFormBase;
25  import org.springframework.stereotype.Controller;
26  import org.springframework.validation.BindingResult;
27  import org.springframework.web.bind.annotation.ModelAttribute;
28  import org.springframework.web.bind.annotation.RequestMapping;
29  import org.springframework.web.bind.annotation.RequestMethod;
30  import org.springframework.web.servlet.ModelAndView;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import java.io.UnsupportedEncodingException;
35  import java.net.URLDecoder;
36  import java.util.Map;
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(HttpServletRequest request) {
50          return new DummyLoginForm();
51      }
52  
53      @Override
54      @RequestMapping(params = "methodToCall=start")
55      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, HttpServletRequest request,
56              HttpServletResponse response) {
57          // check view authorization
58          if (form.getView() != null) {
59              String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER);
60              checkViewAuthorization(form, methodToCall);
61          }
62  
63          return getUIFModelAndView(form);
64      }
65  
66      @RequestMapping(method = RequestMethod.POST, params = "methodToCall=submit")
67      public ModelAndView submit(@ModelAttribute("KualiForm") DummyLoginForm uifForm, BindingResult result,
68              HttpServletRequest request, HttpServletResponse response) {
69          String returnUrl = decode(uifForm.getReturnLocation());
70          if (StringUtils.isBlank(returnUrl)) {
71              returnUrl = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY);
72          }
73  
74          Properties props = new Properties();
75          String user = uifForm.getLogin_user();
76          if (StringUtils.isNotBlank(user)) {
77              props.put("__login_user", user);
78          }
79  
80          String password = uifForm.getLogin_pw();
81          if (StringUtils.isNotBlank(password)) {
82              props.put("__login_pw", password);
83          }
84  
85          return performRedirect(uifForm, returnUrl, props);
86      }
87  
88      @RequestMapping(params = "methodToCall=logout")
89      public ModelAndView logout(@ModelAttribute("KualiForm") UifFormBase form, HttpServletRequest request,
90              HttpServletResponse response) {
91          UserSession userSession = GlobalVariables.getUserSession();
92  
93          if (userSession.isBackdoorInUse()) {
94              userSession.clearBackdoorUser();
95          } else {
96              request.getSession().invalidate();
97          }
98  
99          return returnToHub(form);
100     }
101 
102     private String decode(String encodedUrl) {
103         try {
104             return URLDecoder.decode(encodedUrl, "UTF-8");
105         } catch (UnsupportedEncodingException e) {
106             throw new RuntimeException("Unable to decode value: " + encodedUrl, e);
107         }
108     }
109 
110 }