dnet-openaire-users/src/main/java/eu/dnetlib/openaire/usermanagement/RegisterServlet.java

142 lines
6.0 KiB
Java
Raw Normal View History

2017-10-20 14:31:39 +02:00
package eu.dnetlib.openaire.usermanagement;
2017-10-25 16:15:07 +02:00
import eu.dnetlib.openaire.user.utils.EmailSender;
2017-10-20 14:31:39 +02:00
import eu.dnetlib.openaire.user.utils.LDAPActions;
import eu.dnetlib.openaire.user.utils.VerificationActions;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
2017-10-25 16:15:07 +02:00
import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;
2017-10-20 14:31:39 +02:00
/**
* Created by sofia on 20/10/2017.
*/
public class RegisterServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
@Autowired
private VerificationActions verificationActions;
2017-10-25 16:15:07 +02:00
@Autowired
private EmailSender emailSender;
2017-10-20 14:31:39 +02:00
@Autowired
private LDAPActions ldapActions;
private Logger logger = Logger.getLogger(RegisterServlet.class);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
2017-11-07 11:56:43 +01:00
String firstName = request.getParameter("first_name").trim();
String lastName = request.getParameter("last_name").trim();
String organization = request.getParameter("organization").trim();
String username = request.getParameter("username").trim();
String email =request.getParameter("email").trim();
String confirmEmail = request.getParameter("email_conf").trim();
2017-11-01 11:58:15 +01:00
String password = request.getParameter("password");
String confirmPassword = request.getParameter("password_conf");
2017-10-20 14:31:39 +02:00
if (organization == null){
logger.info("organization is null");
}
if (firstName != null && lastName != null && username != null &&
2017-11-01 11:58:15 +01:00
email.equals(confirmEmail) && password.equals(confirmPassword) ) {
2017-10-20 14:31:39 +02:00
try {
2017-10-25 16:15:07 +02:00
if (username.matches("^[a-zA-Z0-9][a-zA-Z0-9_-]{4,150}") && !ldapActions.usernameExists(username) && !ldapActions.emailExists(email)) {
ldapActions.createZombieUser(username, email, firstName, lastName, organization, password);
2017-11-07 11:56:43 +01:00
logger.info("Zombie user successfully created");
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
UUID verificationCode = UUID.randomUUID();
Date creationDate = new Date();
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
Timestamp timestamp = new Timestamp(creationDate.getTime());
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
if (!verificationActions.verificationEntryExists(username)) {
verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
} else {
verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
}
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
String verificationCodeMsg = "Hello " + username + ",\n" +
"\n" +
"A request has been made to verify your email and activate your OpenAIRE account. To activate your\n" +
"account, you will need to submit your username and this activation code in order to verify that the\n" +
"request was legitimate.\n" +
"\n" +
"The activation code is " + verificationCode.toString() + "\n Thank you";
2017-10-25 16:15:07 +02:00
2017-10-26 15:42:42 +02:00
String verificationCodeSubject = "Activate your OpenAIRE account";
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg);
2017-10-25 16:15:07 +02:00
2017-10-26 15:10:30 +02:00
response.sendRedirect("./activate.jsp");
2017-10-30 15:34:06 +01:00
2017-10-26 15:10:30 +02:00
} else {
if(!username.matches("^[a-zA-Z0-9][a-zA-Z0-9_-]{4,150}")){
if (username.length() < 5) {
request.getSession().setAttribute("username_message", "Minimum username length 5 characters.");
logger.info("Minimum username length 5 characters.");
}
if (username.length() > 150) {
request.getSession().setAttribute("username_message", "Maximum username lenght 150 characters.");
logger.info("Maximum username lenght 150 characters.");
}
}
2017-10-26 15:10:30 +02:00
if (ldapActions.usernameExists(username)) {
request.getSession().setAttribute("username_message", "Username already exists! Choose another one.");
logger.info("Username already exists");
}
if (ldapActions.emailExists(email)) {
request.getSession().setAttribute("email_message", "There is another user with this email.");
logger.info("There is another user with this email");
}
2017-10-30 15:34:06 +01:00
request.getSession().setAttribute("first_name", firstName);
request.getSession().setAttribute("last_name", lastName);
request.getSession().setAttribute("organization", organization);
request.getSession().setAttribute("username", username);
request.getSession().setAttribute("email", email);
request.getSession().setAttribute("email_conf", confirmEmail);
2017-10-26 15:10:30 +02:00
response.sendRedirect("./register.jsp");
}
2017-10-25 16:15:07 +02:00
2017-10-20 14:31:39 +02:00
} catch (Exception e) {
logger.error("LDAP error in creating user", e);
response.sendRedirect("./error.jsp");
}
}
printWriter.close();
}
}