2018-07-23 16:55:28 +02:00
|
|
|
package zall.page;
|
|
|
|
|
|
|
|
|
|
import zall.ZalleryServlet;
|
|
|
|
|
import zall.bean.User;
|
|
|
|
|
import zall.util.ZalleryEmail;
|
|
|
|
|
import zall.util.UserMessage;
|
|
|
|
|
import zutil.db.DBConnection;
|
|
|
|
|
import zutil.log.LogUtil;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
|
|
import javax.servlet.http.*;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
|
|
@WebServlet("/register")
|
|
|
|
|
public class RegisterServlet extends ZalleryServlet {
|
|
|
|
|
private static Logger logger = LogUtil.getLogger();
|
|
|
|
|
|
|
|
|
|
private static final String JSP_FILE = "register.jsp";
|
|
|
|
|
|
|
|
|
|
private static final String INPUT_ID_FIRST_NAME = "first-name";
|
|
|
|
|
private static final String INPUT_ID_LAST_NAME = "last-name";
|
|
|
|
|
private static final String INPUT_ID_EMAIL = "email";
|
|
|
|
|
private static final String INPUT_ID_PASSWORD = "password";
|
|
|
|
|
private static final String INPUT_ID_PASSWORD_REPEATE = "password-re";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws ServletException, IOException {
|
|
|
|
|
include(JSP_FILE, request, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws SQLException, IOException, ServletException {
|
2018-07-24 16:23:53 +02:00
|
|
|
UserMessage msgs = UserMessage.getUserMessage(request.getSession());
|
2018-07-23 16:55:28 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check inputs
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
String firstName = (String)request.getAttribute(INPUT_ID_FIRST_NAME);
|
|
|
|
|
String lastName = (String)request.getAttribute(INPUT_ID_LAST_NAME);
|
|
|
|
|
String email = (String)request.getAttribute(INPUT_ID_EMAIL);
|
|
|
|
|
String password = (String)request.getAttribute(INPUT_ID_PASSWORD);
|
|
|
|
|
String passwordRe = (String)request.getAttribute(INPUT_ID_PASSWORD_REPEATE);
|
|
|
|
|
|
|
|
|
|
if (firstName == null)
|
2018-07-24 16:23:53 +02:00
|
|
|
msgs.add(UserMessage.MessageType.ERROR, lang.getString("error.not.found.first.name"));
|
2018-07-23 16:55:28 +02:00
|
|
|
if (lastName == null)
|
2018-07-24 16:23:53 +02:00
|
|
|
msgs.add(UserMessage.MessageType.ERROR, lang.getString("error.not.found.last.name"));
|
2018-07-23 16:55:28 +02:00
|
|
|
if (email == null)
|
2018-07-24 16:23:53 +02:00
|
|
|
msgs.add(UserMessage.MessageType.ERROR, lang.getString("error.not.found.email"));
|
2018-07-23 16:55:28 +02:00
|
|
|
if (password == null)
|
2018-07-24 16:23:53 +02:00
|
|
|
msgs.add(UserMessage.MessageType.ERROR, lang.getString("error.not.found.password"));
|
2018-07-23 16:55:28 +02:00
|
|
|
if (passwordRe == null)
|
2018-07-24 16:23:53 +02:00
|
|
|
msgs.add(UserMessage.MessageType.ERROR, lang.getString("error.not.found.password"));
|
2018-07-23 16:55:28 +02:00
|
|
|
else if (passwordRe.equals(password))
|
2018-07-24 16:23:53 +02:00
|
|
|
msgs.add(UserMessage.MessageType.ERROR, lang.getString("error.incorrect.password.matching"));
|
2018-07-23 16:55:28 +02:00
|
|
|
|
|
|
|
|
if(User.load(db, (String)request.getAttribute(INPUT_ID_EMAIL)) != null){
|
2018-07-24 16:23:53 +02:00
|
|
|
msgs.add(UserMessage.MessageType.ERROR, lang.getString("error.email_exists"));
|
2018-07-23 16:55:28 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create new user
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
User user = new User();
|
|
|
|
|
user.setName(firstName + " " + lastName);
|
|
|
|
|
user.setEmail(email);
|
|
|
|
|
user.setPassword(password);
|
|
|
|
|
user.save(db);
|
|
|
|
|
ZalleryEmail.sendVerificationEmail( user );
|
|
|
|
|
request.getSession().setAttribute("user", user);
|
|
|
|
|
logger.info("Registered new user: "+user.getName()+".");
|
2018-07-24 16:23:53 +02:00
|
|
|
msgs.add(UserMessage.MessageType.INFO,
|
|
|
|
|
lang.getString("info.created.account") + " " + lang.getString("info.sent.email.verification"));
|
2018-07-23 16:55:28 +02:00
|
|
|
|
|
|
|
|
include(JSP_FILE, request, response);
|
|
|
|
|
}
|
|
|
|
|
}
|