Revize ef8cdd70
Přidáno uživatelem Pavel Fidranský před více než 6 roky(ů)
sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/Login.java | ||
---|---|---|
3 | 3 |
import javax.servlet.http.HttpServletRequest; |
4 | 4 |
import javax.servlet.http.HttpServletResponse; |
5 | 5 |
|
6 |
import com.google.common.base.Strings; |
|
6 | 7 |
import cz.zcu.kiv.offscreen.servlets.BaseServlet; |
7 | 8 |
import cz.zcu.kiv.offscreen.user.DB; |
8 | 9 |
import cz.zcu.kiv.offscreen.user.User; |
9 | 10 |
import cz.zcu.kiv.offscreen.vo.UserVO; |
11 |
import org.json.JSONObject; |
|
12 |
|
|
13 |
import java.io.IOException; |
|
14 |
import java.util.HashMap; |
|
15 |
import java.util.Map; |
|
10 | 16 |
|
11 | 17 |
public class Login extends BaseServlet { |
12 | 18 |
|
13 | 19 |
@Override |
14 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) { |
|
15 |
DB db = new DB(getServletContext()); |
|
16 |
User user = new User(db); |
|
17 |
|
|
20 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { |
|
18 | 21 |
String username = request.getParameter("username"); |
19 | 22 |
String password = request.getParameter("password"); |
20 | 23 |
|
21 |
if (user.login(username, password)) { |
|
22 |
UserVO userVO = new UserVO(); |
|
23 |
userVO.setId(user.getId()); |
|
24 |
userVO.setUsername(user.getNick()); |
|
24 |
Map<String, String> errors = new HashMap<>(); |
|
25 |
|
|
26 |
if (Strings.isNullOrEmpty(username)) { |
|
27 |
errors.put("username", "Please enter username."); |
|
28 |
} |
|
29 |
|
|
30 |
if (Strings.isNullOrEmpty(password)) { |
|
31 |
errors.put("password", "Please enter password."); |
|
32 |
} |
|
33 |
|
|
34 |
if (errors.isEmpty()) { |
|
35 |
DB db = new DB(getServletContext()); |
|
36 |
User user = new User(db); |
|
37 |
|
|
38 |
if (user.login(username, password)) { |
|
39 |
UserVO userVO = new UserVO(); |
|
40 |
userVO.setId(user.getId()); |
|
41 |
userVO.setUsername(user.getNick()); |
|
42 |
|
|
43 |
request.getSession().setAttribute("isLoggedIn", true); |
|
44 |
request.getSession().setAttribute("userId", userVO.getId()); |
|
45 |
request.getSession().setAttribute("user", userVO); |
|
46 |
|
|
47 |
response.setStatus(HttpServletResponse.SC_ACCEPTED); |
|
25 | 48 |
|
26 |
request.getSession().setAttribute("isLoggedIn", true); |
|
27 |
request.getSession().setAttribute("userId", userVO.getId()); |
|
28 |
request.getSession().setAttribute("user", userVO); |
|
49 |
} else { |
|
50 |
request.getSession().setAttribute("isLoggedIn", false); |
|
51 |
request.getSession().setAttribute("userId", null); |
|
52 |
request.getSession().setAttribute("user", null); |
|
29 | 53 |
|
30 |
response.setStatus(HttpServletResponse.SC_ACCEPTED); |
|
54 |
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); |
|
55 |
} |
|
31 | 56 |
|
32 | 57 |
} else { |
33 |
request.getSession().setAttribute("isLoggedIn", false); |
|
34 |
request.getSession().setAttribute("userId", null); |
|
35 |
request.getSession().setAttribute("user", null); |
|
58 |
JSONObject json = new JSONObject(); |
|
59 |
json.put("error", new JSONObject(errors)); |
|
36 | 60 |
|
37 |
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); |
|
61 |
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
|
62 |
response.setContentType("application/json"); |
|
63 |
response.getWriter().write(json.toString()); |
|
64 |
response.getWriter().flush(); |
|
38 | 65 |
} |
39 | 66 |
} |
40 | 67 |
} |
sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/Register.java | ||
---|---|---|
8 | 8 |
import javax.servlet.http.HttpServletRequest; |
9 | 9 |
import javax.servlet.http.HttpServletResponse; |
10 | 10 |
|
11 |
import com.google.common.base.Strings; |
|
11 | 12 |
import org.json.JSONObject; |
12 | 13 |
|
13 | 14 |
import cz.zcu.kiv.offscreen.servlets.BaseServlet; |
... | ... | |
33 | 34 |
|
34 | 35 |
Map<String, String> errors = new HashMap<>(); |
35 | 36 |
|
36 |
if (name == null || name.length() == 0) {
|
|
37 |
errors.put("user_name", "Please enter name.");
|
|
37 |
if (Strings.isNullOrEmpty(name)) {
|
|
38 |
errors.put("name", "Please enter name."); |
|
38 | 39 |
} |
39 | 40 |
|
40 |
if (email == null || email.length() == 0) {
|
|
41 |
errors.put("user_email", "Please enter e-mail address.");
|
|
41 |
if (Strings.isNullOrEmpty(email)) {
|
|
42 |
errors.put("email", "Please enter e-mail address."); |
|
42 | 43 |
} else if (!isEmailAddressValid(email)) { |
43 |
errors.put("user_email", "Please enter valid e-mail address.");
|
|
44 |
errors.put("email", "Please enter valid e-mail address."); |
|
44 | 45 |
} else if (user.existsEmail(email)) { |
45 |
errors.put("user_email", "E-mail already exists.");
|
|
46 |
errors.put("email", "E-mail already exists."); |
|
46 | 47 |
} |
47 | 48 |
|
48 |
if (username == null || username.length() == 0) {
|
|
49 |
errors.put("user_nick", "Please enter username.");
|
|
49 |
if (Strings.isNullOrEmpty(username)) {
|
|
50 |
errors.put("username", "Please enter username.");
|
|
50 | 51 |
} else if (user.existsNick(username)) { |
51 |
errors.put("user_nick", "Nickname already exists.");
|
|
52 |
errors.put("username", "Nickname already exists.");
|
|
52 | 53 |
} |
53 | 54 |
|
54 |
if (password == null || password.length() == 0 || passwordCheck == null || passwordCheck.length() == 0) { |
|
55 |
errors.put("user_password", "Please enter password."); |
|
56 |
} else if (!password.equals(passwordCheck)) { |
|
57 |
errors.put("user_password", "Passwords must be equal."); |
|
55 |
if (Strings.isNullOrEmpty(password) || Strings.isNullOrEmpty(passwordCheck)) { |
|
56 |
errors.put("password", "Please enter password."); |
|
58 | 57 |
} else if (password.length() < 5) { |
59 |
errors.put("user_password", "Passwords must be at least 5 characters long."); |
|
58 |
errors.put("password", "Passwords must be at least 5 characters long."); |
|
59 |
} else if (!password.equals(passwordCheck)) { |
|
60 |
errors.put("passwordCheck", "Passwords must be equal."); |
|
60 | 61 |
} |
61 | 62 |
|
62 | 63 |
if (errors.isEmpty()) { |
Také k dispozici: Unified diff
Login and Register servlets return details on why the request failed