package eu.dnetlib.openaire.usermanagement; import eu.dnetlib.openaire.user.utils.EmailSender; 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; import java.sql.Timestamp; import java.util.Date; import java.util.UUID; /** * 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; @Autowired private EmailSender emailSender; @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(); String firstName = request.getParameter("first_name"); String lastName = request.getParameter("last_name"); String organization = request.getParameter("organization"); String username = request.getParameter("username"); String email =request.getParameter("email"); String confirmEmail = request.getParameter("email_conf"); //String password = request.getParameter("password"); //String confirmPassword = request.getParameter("password_conf"); if (organization == null){ logger.info("organization is null"); } if (firstName != null && lastName != null && username != null && email.equals(confirmEmail) /*&& password.equals(confirmPassword)*/ ) { try { // if (ldapActions.usernameExists(username)) { // request.getSession().setAttribute("username_message", "Username already exists! Choose another one."); // logger.info("Username already exists"); // response.sendRedirect("./register.jsp"); // // } else 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"); // response.sendRedirect("./register.jsp"); // // } else { if (username.matches("^[a-zA-Z0-9][a-zA-Z0-9_-]{4,150}") && !ldapActions.usernameExists(username) && !ldapActions.emailExists(email)) { ldapActions.createUser(username, email, firstName, lastName, organization/*, password*/); logger.info("User successfully created"); UUID verificationCode = UUID.randomUUID(); Date creationDate = new Date(); Timestamp timestamp = new Timestamp(creationDate.getTime()); if (!verificationActions.verificationEntryExists(username)) { verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp); } else { verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp); } 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"; String verificationCodeSubject = "Activate your OpenAIRE account"; emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg); response.sendRedirect("./activate.jsp"); // } } 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."); } } 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"); } response.sendRedirect("./register.jsp"); } } catch (Exception e) { logger.error("LDAP error in creating user", e); response.sendRedirect("./error.jsp"); } } printWriter.close(); } }