Subversion Repositories WebE

Rev

Rev 33 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33 Rev 37
1
package ch.ffhs.webE.action;
1
package ch.ffhs.webE.action;
2
2
3
import java.util.Map;
3
import java.util.Map;
4
4
5
import ch.ffhs.webE.dao.UserDAOImpl;
5
import ch.ffhs.webE.dao.UserDAO;
6
import ch.ffhs.webE.domain.User;
6
import ch.ffhs.webE.domain.User;
7
7
8
import com.opensymphony.xwork2.Action;
8
import com.opensymphony.xwork2.Action;
9
import com.opensymphony.xwork2.ActionContext;
9
import com.opensymphony.xwork2.ActionContext;
10
import com.opensymphony.xwork2.ActionSupport;
10
import com.opensymphony.xwork2.ActionSupport;
11
import com.opensymphony.xwork2.ModelDriven;
11
import com.opensymphony.xwork2.ModelDriven;
12
12
13
public class LoginAction extends ActionSupport implements ModelDriven<User>
13
public class LoginAction extends ActionSupport implements ModelDriven<User>
14
{
14
{
15
15
16
  private static final long serialVersionUID = 1799753056277211344L;
16
  private static final long serialVersionUID = 1799753056277211344L;
17
  private final User user = new User();
17
  private final User user = new User();
18
  private final UserDAOImpl userDAO = new UserDAOImpl();
18
  private final UserDAO userDAO = new UserDAO();
19
19
20
  /* Form fields */
20
  /* Form fields */
21
  private String userName;
21
  private String userName;
22
  private String pw;
22
  private String pw;
23
23
24
  /**
24
  /**
25
   * JSP session object
25
   * JSP session object
26
   */
26
   */
27
  Map<String, Object> session = ActionContext.getContext().getSession();
27
  Map<String, Object> session = ActionContext.getContext().getSession();
28
28
29
  /**
29
  /**
30
   *
30
   *
31
   */
31
   */
32
  public LoginAction()
32
  public LoginAction()
33
  {
33
  {
34
  }
34
  }
35
35
36
  public String doLogin()
36
  public String doLogin()
37
  {
37
  {
38
38
39
    // If password or user name are empty, the login fails.
39
    // If password or user name are empty, the login fails.
40
    if ("".equals(this.getUserName()) || "".equals(this.getPw())
40
    if ("".equals(this.getUserName()) || "".equals(this.getPw())
41
        || this.getUserName() == null || this.getPw() == null)
41
        || this.getUserName() == null || this.getPw() == null)
42
    {
42
    {
43
      this.addFieldError("userName", "Falscher Username oder Passwort");
43
      this.addFieldError("userName", "Falscher Username oder Passwort");
44
      return Action.ERROR;
44
      return Action.ERROR;
45
    }
45
    }
46
46
47
    String verifiedUser = this.verifyUser(this.getUserName(), this.getPw());
47
    String verifiedUser = this.verifyUser(this.getUserName(), this.getPw());
48
    if (verifiedUser.equals("failed"))
48
    if (verifiedUser.equals("failed"))
49
    {
49
    {
50
      this.addFieldError("userName", "Falscher Username oder Passwort");
50
      this.addFieldError("userName", "Falscher Username oder Passwort");
51
      return Action.ERROR;
51
      return Action.ERROR;
52
    }
52
    }
53
    else
53
    else
54
    {
54
    {
55
55
56
      // Put user name, password into session
56
      // Put user name, password into session
57
      this.session.put("username", this.getUserName());
57
      this.session.put("username", this.getUserName());
58
      this.session.put("pw", this.getPw());
58
      this.session.put("pw", this.getPw());
59
      return verifiedUser;
59
      return verifiedUser;
60
    }
60
    }
61
  }
61
  }
62
62
63
  /**
63
  /**
64
   * Logout ausf�hren. Zerst�rt die Daten in der Session
64
   * Logout ausf�hren. Zerst�rt die Daten in der Session
65
   *
65
   *
66
   * @return String
66
   * @return String
67
   */
67
   */
68
  public String doLogout()
68
  public String doLogout()
69
  {
69
  {
70
    // Kill Session content
70
    // Kill Session content
71
    ActionContext.getContext().getSession().clear();
71
    ActionContext.getContext().getSession().clear();
72
    return Action.SUCCESS;
72
    return Action.SUCCESS;
73
  }
73
  }
74
74
75
  /**
75
  /**
76
   * Verify user credentials
76
   * Verify user credentials
77
   *
77
   *
78
   * @param String
78
   * @param String
79
   *          username: User name
79
   *          username: User name
80
   * @param String
80
   * @param String
81
   *          password: Password (hashed)
81
   *          password: Password (hashed)
82
   * @return
82
   * @return
83
   */
83
   */
84
  public String verifyUser(String username, String password)
84
  public String verifyUser(String username, String password)
85
  {
85
  {
86
    // DB Query
86
    // DB Query
87
    User u = this.userDAO.searchUsername(username);
87
    User u = this.userDAO.getByUsername(username);
88
88
89
    // User does not exist
89
    // User does not exist
90
    if (u == null)
90
    if (u == null)
91
    {
91
    {
92
      return Action.ERROR;
92
      return Action.ERROR;
93
    }
93
    }
94
94
95
    // User password does not match
95
    // User password does not match
96
    if (!u.getPassword().equals(password))
96
    if (!u.getPassword().equals(password))
97
    {
97
    {
98
      return Action.ERROR;
98
      return Action.ERROR;
99
    }
99
    }
100
100
101
    // User credentials are fine, check for admin rights
101
    // User credentials are fine, check for admin rights
102
    if (u.isAdmin())
102
    if (u.isAdmin())
103
    {
103
    {
104
      return "admin";
104
      return "admin";
105
    }
105
    }
106
    else
106
    else
107
    {
107
    {
108
      return "user";
108
      return "user";
109
    }
109
    }
110
  }
110
  }
111
111
112
  public String getUserName()
112
  public String getUserName()
113
  {
113
  {
114
    return this.userName;
114
    return this.userName;
115
  }
115
  }
116
116
117
  public void setUserName(String userName)
117
  public void setUserName(String userName)
118
  {
118
  {
119
    this.userName = userName;
119
    this.userName = userName;
120
  }
120
  }
121
121
122
  public String getPw()
122
  public String getPw()
123
  {
123
  {
124
    return this.pw;
124
    return this.pw;
125
  }
125
  }
126
126
127
  public void setPw(String pw)
127
  public void setPw(String pw)
128
  {
128
  {
129
    this.pw = pw;
129
    this.pw = pw;
130
  }
130
  }
131
131
132
  @Override
132
  @Override
133
  public User getModel()
133
  public User getModel()
134
  {
134
  {
135
    return this.user;
135
    return this.user;
136
  }
136
  }
137
}
137
}
138
 
138