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").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(); 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 (username.matches("^[a-zA-Z0-9][a-zA-Z0-9_-]{4,150}") && !ldapActions.usernameExists(username) && !ldapActions.emailExists(email) && !ldapActions.isZombieUsersEmail(email) && !ldapActions.isZombieUsersUsername(username)) { ldapActions.createZombieUser(username, email, firstName, lastName, organization, password); logger.info("Zombie 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 scheme = request.getScheme(); String serverName = request.getServerName(); int portNumber = request.getServerPort(); String contextPath = request.getContextPath(); String resultPath; if (portNumber == 80) { resultPath = scheme + "://" + serverName + contextPath + "/activate.jsp"; } else { resultPath = scheme + "://" +serverName + ":" +portNumber + contextPath + "/activate.jsp"; } String verificationCodeMsg = "

Hello " + username + ",

" + "

A request has been made to verify your email and activate your OpenAIRE account. To activate your " + "account, you will need to submit your username and this activation code in order to verify that the" + "request was legitimate.

" + "

" + "The activation code is " + verificationCode.toString() + "

" + "Select the URL below and proceed with activating your password." + "

" + resultPath + "

" + "

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) || ldapActions.isZombieUsersUsername(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"); } if (ldapActions.isZombieUsersEmail(email)) { request.getSession().setAttribute("email_message", "You have already registered with this email address! Please check your email to activate your account or contact OpenAIRE helpdesk."); logger.info("There is another user with this email"); } 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); response.sendRedirect("./register.jsp"); } } catch (Exception e) { logger.error("LDAP error in creating user", e); response.sendRedirect("./error.jsp"); } } printWriter.close(); } }