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