Rev 20 | Rev 26 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 20 | Rev 23 | ||
---|---|---|---|
1 | package ch.ffhs.webE.action; |
1 | package ch.ffhs.webE.action; |
2 | 2 | ||
3 | 3 | ||
4 | import java.util.Map; |
4 | import java.util.Map; |
5 | 5 | ||
6 | import ch.ffhs.webE.dao.UserDAO; |
6 | import ch.ffhs.webE.dao.UserDAO; |
7 | import ch.ffhs.webE.dao.UserDAOImpl; |
7 | import ch.ffhs.webE.dao.UserDAOImpl; |
8 | import ch.ffhs.webE.domain.User; |
8 | import ch.ffhs.webE.domain.User; |
9 | 9 | ||
10 | import com.opensymphony.xwork2.ActionContext; |
10 | import com.opensymphony.xwork2.ActionContext; |
11 | import com.opensymphony.xwork2.ActionSupport; |
11 | import com.opensymphony.xwork2.ActionSupport; |
12 | import com.opensymphony.xwork2.ModelDriven; |
12 | import com.opensymphony.xwork2.ModelDriven; |
13 | 13 | ||
14 | public class LoginAction extends ActionSupport implements ModelDriven<User>{ |
14 | public class LoginAction extends ActionSupport implements ModelDriven<User>{ |
15 | 15 | ||
16 | private static final long serialVersionUID = 1799753056277211344L; |
16 | private static final long serialVersionUID = 1799753056277211344L; |
17 | private User user = new User(); |
17 | private User user = new User(); |
18 | private UserDAO userDAO = new UserDAOImpl(); |
18 | private UserDAO userDAO = new UserDAOImpl(); |
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 | //Session Object
|
24 | //Session Object
|
25 | Map<String, Object> session = ActionContext.getContext().getSession(); |
25 | Map<String, Object> session = ActionContext.getContext().getSession(); |
26 | 26 | ||
27 | 27 | ||
28 | public LoginAction() { |
28 | public LoginAction() { |
29 | }
|
29 | }
|
30 | 30 | ||
31 | public String doLogin() { |
31 | public String doLogin() { |
32 | 32 | ||
33 | //If password or user name are empty, the login fails.
|
33 | //If password or user name are empty, the login fails.
|
34 | if("".equals(getUserName()) || "".equals(getPw()) || getUserName() == null || getPw() == null) { |
34 | if("".equals(getUserName()) || "".equals(getPw()) || getUserName() == null || getPw() == null) { |
35 | return "failed"; |
35 | return ERROR; |
36 | }
|
36 | }
|
37 | 37 | ||
38 | String verifiedUser = verifyUser(getUserName(), getPw()); |
38 | String verifiedUser = verifyUser(getUserName(), getPw()); |
39 | if(verifiedUser.equals("failed")) { |
39 | if(verifiedUser.equals("failed")) { |
40 | return "failed"; |
40 | return ERROR; |
41 | } else { |
41 | } else { |
42 | 42 | ||
43 | //Put user name, password into session
|
43 | //Put user name, password into session
|
44 | session.put("username", getUserName()); |
44 | session.put("username", getUserName()); |
45 | session.put("pw", getPw()); |
45 | session.put("pw", getPw()); |
46 | return verifiedUser; |
46 | return verifiedUser; |
47 | }
|
47 | }
|
48 | }
|
48 | }
|
49 | 49 | ||
50 | public String doLogout() { |
50 | public String doLogout() { |
51 | //TODO: Kill session content for logout
|
51 | //TODO: Kill session content for logout
|
52 | return SUCCESS; |
52 | return SUCCESS; |
53 | }
|
53 | }
|
54 | 54 | ||
55 | /**
|
55 | /**
|
56 | * Verify user credentials
|
56 | * Verify user credentials
|
57 | * @param String username: User name
|
57 | * @param String username: User name
|
58 | * @param String password: Password (hashed)
|
58 | * @param String password: Password (hashed)
|
59 | * @return
|
59 | * @return
|
60 | */
|
60 | */
|
61 | public String verifyUser(String username, String password) { |
61 | public String verifyUser(String username, String password) { |
62 | //DB Query
|
62 | //DB Query
|
63 | User u = userDAO.searchUsername(username); |
63 | User u = userDAO.searchUsername(username); |
64 | 64 | ||
65 | //User does not exist
|
65 | //User does not exist
|
66 | if(u == null) |
66 | if(u == null) |
67 | return "failed"; |
67 | return ERROR; |
68 | 68 | ||
69 | //User password does not match
|
69 | //User password does not match
|
70 | if(!u.getPassword().equals(password)) |
70 | if(!u.getPassword().equals(password)) |
71 | return "failed"; |
71 | return ERROR; |
72 | 72 | ||
73 | //User credentials are fine, check for admin rights
|
73 | //User credentials are fine, check for admin rights
|
74 | if(u.isAdmin()) { |
74 | if(u.isAdmin()) { |
75 | return "admin"; |
75 | return "admin"; |
76 | } else { |
76 | } else { |
77 | return "user"; |
77 | return "user"; |
78 | }
|
78 | }
|
79 | }
|
79 | }
|
80 | 80 | ||
81 | public String getUserName() { |
81 | public String getUserName() { |
82 | return userName; |
82 | return userName; |
83 | }
|
83 | }
|
84 | 84 | ||
85 | public void setUserName(String userName) { |
85 | public void setUserName(String userName) { |
86 | this.userName = userName; |
86 | this.userName = userName; |
87 | }
|
87 | }
|
88 | 88 | ||
89 | public String getPw() { |
89 | public String getPw() { |
90 | return pw; |
90 | return pw; |
91 | }
|
91 | }
|
92 | 92 | ||
93 | public void setPw(String pw) { |
93 | public void setPw(String pw) { |
94 | this.pw = pw; |
94 | this.pw = pw; |
95 | }
|
95 | }
|
96 | 96 | ||
97 | @Override |
97 | @Override |
98 | public User getModel() { |
98 | public User getModel() { |
99 | return user; |
99 | return user; |
100 | }
|
100 | }
|
101 | - | ||
102 | - | ||
103 | - | ||
104 | - | ||
105 | }
|
101 | }
|
106 | 102 |