View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.controllers;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.promotego.beans.User;
25  import org.promotego.beans.UserHolder;
26  import org.promotego.dao.interfaces.UserDao;
27  import org.promotego.interfaces.PasswordHashTool;
28  import org.promotego.viewbeans.LoginBean;
29  import org.springframework.beans.factory.annotation.Required;
30  import org.springframework.validation.BindException;
31  import org.springframework.web.servlet.ModelAndView;
32  import org.springframework.web.servlet.mvc.SimpleFormController;
33  import org.springframework.web.servlet.view.RedirectView;
34  
35  public class LoginController extends SimpleFormController
36  {
37      private UserDao m_userDao;
38      private PasswordHashTool m_passwordHashTool;
39      private UserHolder m_userHolder;
40  
41      @Override
42      public ModelAndView onSubmit(HttpServletRequest request,
43              HttpServletResponse response, Object command, BindException errors)
44      {
45          LoginBean loginBean = (LoginBean) command;
46          String username = loginBean.getUsername();
47          String password = loginBean.getPassword();
48  
49          if (username != null && password != null)
50          {
51              // Allow only lowercase user names
52              username = username.toLowerCase();
53  
54              User theUser = m_userDao.getUserByUsername(username);
55              if (theUser != null)
56              {
57                  if (m_passwordHashTool.match(theUser.getCryptedPassword(),
58                          password))
59                  {
60                      // Wipe out existing session, to guarantee starting with new info
61                      request.getSession().invalidate();
62  
63                      // Set the user to show that she is logged in
64                      m_userHolder.setUser(theUser);
65  
66                      String redirect = request.getParameter("redirect");
67                      if (redirect != null && redirect.length() > 0)
68                      {
69                          if (!redirect.matches("^[a-zA-Z0-9]*:.*"))
70                          {
71                              // Don't use redirect: prefix, because it will put the entire Model
72                              // in the redirect URL.
73                              return new ModelAndView(new RedirectView(redirect,
74                                      false, true, false));
75                          }
76                      }
77  
78                      return new ModelAndView("welcome");
79                  }
80              }
81          }
82  
83          errors.reject("login.invalid", "Username or password invalid");
84          return new ModelAndView("login", errors.getModel());
85      }
86  
87      @Required
88      public void setUserDao(UserDao userDao)
89      {
90          m_userDao = userDao;
91      }
92  
93      @Required
94      public void setPasswordHashTool(PasswordHashTool passwordHashTool)
95      {
96          m_passwordHashTool = passwordHashTool;
97      }
98  
99      @Required
100     public void setUserHolder(UserHolder theUserHolder)
101     {
102         m_userHolder = theUserHolder;
103     }
104 }