diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/RequestToDeleteAccountServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/RequestToDeleteAccountServlet.java new file mode 100644 index 0000000..1c81a37 --- /dev/null +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/RequestToDeleteAccountServlet.java @@ -0,0 +1,167 @@ +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 eu.dnetlib.openaire.user.utils.VerifyRecaptcha; +import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor; +import org.apache.commons.validator.routines.EmailValidator; +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.context.support.SpringBeanAutowiringSupport; + +import javax.mail.MessagingException; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.sql.Timestamp; +import java.util.Date; +import java.util.UUID; + +/** + * Created by sofia on 21/5/2018. + */ +public class RequestToDeleteAccountServlet extends HttpServlet { + + @Autowired + private VerificationActions verificationActions; + + @Autowired + private LDAPActions ldapActions; + + @Autowired + private EmailSender emailSender; + + @Value("${oidc.home}") + private String oidcHomeUrl; + + @Value("${google.recaptcha.secret}") + private String secret; + + @Value("${google.recaptcha.key}") + private String sitekey; + + private static final Logger logger = Logger.getLogger(RequestActivationCodeServlet.class); + + public void init(ServletConfig config) throws ServletException { + super.init(config); + SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, + config.getServletContext()); + config.getServletContext().setAttribute("sitekey", sitekey); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { + String formEmail = request.getParameter("email").trim(); + + String gRecaptchaResponse = request.getParameter("g-recaptcha-response"); + + HttpSession session = request.getSession(); + session.setAttribute("homeUrl", oidcHomeUrl); + + if (formEmail == null) { + request.getSession().setAttribute("message", "Error reading email."); + response.sendRedirect("./requestToDeleteAccount.jsp"); + + } else if (formEmail.isEmpty()) { + request.getSession().setAttribute("message", "Please enter your email."); + response.sendRedirect("./requestToDeleteAccount.jsp"); + + } else if (!EmailValidator.getInstance().isValid(formEmail)) { + request.getSession().setAttribute("message", "Please enter a valid email."); + response.sendRedirect("./requestToDeleteAccount.jsp"); + + } else if (!VerifyRecaptcha.verify(gRecaptchaResponse, secret)) { + request.getSession().setAttribute("reCAPTCHA_message", "You missed the reCAPTCHA validation!"); + response.sendRedirect("./requestToDeleteAccount.jsp"); + + } else { + + try { + + Boolean isRegistered = false; + Boolean isZombie = false; + + if (ldapActions.emailExists(formEmail)) { + logger.info("User with email: " + formEmail + " is activated user!"); + isRegistered = true; + } else if (ldapActions.isZombieUsersEmail(formEmail)) { + logger.info("User with email: " + formEmail + " is zombie user!"); + isZombie = true; + } + + if (!isRegistered && !isZombie) { + request.getSession().setAttribute("message", "There is no user with that email."); + response.sendRedirect("./requestToDeleteAccount.jsp"); + } else { + + String username = null; + + if (isRegistered) { + username = ldapActions.getUsername(formEmail); + } else if (isZombie) { + username = ldapActions.getZombieUsersUserName(formEmail); + } + + UUID verificationCode = UUID.randomUUID(); + Date creationDate = new Date(); + String vCode = verificationCode.toString(); + + Timestamp timestamp = new Timestamp(creationDate.getTime()); + + if (!verificationActions.verificationEntryExists(username)) { + verificationActions.addVerificationEntry(username, vCode, timestamp); + + } else { + verificationActions.updateVerificationEntry(username, vCode, timestamp); + } + + String resultPath = UrlConstructor.getRedirectUrl(request, "verifyToDelete.jsp"); + String resultPathWithVCode = UrlConstructor.getVerificationLink(resultPath, vCode); + + String verificationCodeMsg = "

Hello " + username + ",

" + + "

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

" + + "

" + + "The verification code is " + vCode + + "

" + + "Click the URL below and proceed with deleting your account." + + "

" + resultPathWithVCode + "

" + + "

The verification code is valid for 24 hours.

" + + "

Thank you,

" + + "

OpenAIRE technical team

"; + + String verificationCodeSubject = "Request to delete your OpenAIRE account"; + + emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg); + logger.info("Sending verification code to user: " + formEmail); + + + response.sendRedirect("./verifyToDelete.jsp"); + } + } catch (MessagingException e) { + logger.error("Error in sending email", e); + request.getSession().setAttribute("message", "Error sending email"); + response.sendRedirect("./requestActivationCode.jsp"); + } catch (Exception ldape) { + logger.error("Could not user with email " + formEmail, ldape); + response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp")); + } + } + } + + public String getOidcHomeUrl() { + return oidcHomeUrl; + } + + public void setOidcHomeUrl(String oidcHomeUrl) { + this.oidcHomeUrl = oidcHomeUrl; + } +} diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/VerifyToDeleteServlet.java b/src/main/java/eu/dnetlib/openaire/usermanagement/VerifyToDeleteServlet.java new file mode 100644 index 0000000..5dcebae --- /dev/null +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/VerifyToDeleteServlet.java @@ -0,0 +1,102 @@ +package eu.dnetlib.openaire.usermanagement; + +import eu.dnetlib.openaire.user.utils.InputValidator; +import eu.dnetlib.openaire.user.utils.LDAPActions; +import eu.dnetlib.openaire.user.utils.VerificationActions; +import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor; +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 javax.servlet.http.HttpSession; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * Created by sofia on 21/5/2018. + */ +public class VerifyToDeleteServlet extends HttpServlet { + + public void init(ServletConfig config) throws ServletException { + super.init(config); + SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, + config.getServletContext()); + } + + @Autowired + private VerificationActions verificationActions; + + @Autowired + private LDAPActions ldapActions; + + private Logger logger = Logger.getLogger(VerificationCodeServlet.class); + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("text/html"); + PrintWriter printWriter = response.getWriter(); + + String formUsername = request.getParameter("username").trim(); + String formVerificationCode = request.getParameter("verification_code").trim(); + + try { + if (InputValidator.isFilled(formUsername) && InputValidator.isFilled(formVerificationCode)) { + if (verificationActions.verificationEntryExists(formUsername) && verificationActions.verificationCodeIsCorrect(formUsername, formVerificationCode)) { + if (!verificationActions.verificationCodeHasExpired(formUsername)) { + + Boolean isRegistered = false; + Boolean isZombie = false; + + if (ldapActions.usernameExists(formUsername)) { + logger.info("User " + formUsername + " is activated user!"); + isRegistered = true; + } else if (ldapActions.isZombieUsersUsername(formUsername)) { + logger.info("User " + formUsername + " is zombie user!"); + isZombie = true; + } + + if (!isRegistered && !isZombie) { + request.getSession().setAttribute("message", "Username or verification code are not valid."); + response.sendRedirect("./verifyToDelete.jsp"); + } else { + if (isRegistered) { + ldapActions.deleteUser(formUsername); + } else if (isZombie) { + ldapActions.deleteZombieUser(formUsername); + } + response.sendRedirect(UrlConstructor.getRedirectUrl(request,"successDeleteAccount.jsp")); + + } + } else { + logger.info("Verification code has expired!"); + response.sendRedirect(UrlConstructor.getRedirectUrl(request, "expiredVerificationCode.jsp")); + } + } else { + logger.info("Username or verification code are not valid!"); + request.getSession().setAttribute("message", "Username or verification code are not valid."); + response.sendRedirect("./verifyToDelete.jsp"); + } + } else { + if (!InputValidator.isFilled(formUsername)) { + logger.info("No username"); + request.getSession().setAttribute("msg_username_error", "Please enter your username."); + } + if (!InputValidator.isFilled(formVerificationCode)) { + logger.info("No verification code"); + request.getSession().setAttribute("msg_verification_code_error", "Please enter your verification code."); + } + response.sendRedirect("./verifyToDelete.jsp"); + } + } catch (Exception ldape) { + logger.error("Could not remove user with username " + formUsername, ldape); + response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp")); + } + + printWriter.close(); + } +} diff --git a/src/main/webapp/requestToDeleteAccount.jsp b/src/main/webapp/requestToDeleteAccount.jsp new file mode 100644 index 0000000..a8a0292 --- /dev/null +++ b/src/main/webapp/requestToDeleteAccount.jsp @@ -0,0 +1,123 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: sofia + Date: 21/5/2018 + Time: 1:21 μμ + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + + + + + + + + + OpenAIRE - Request to Delete Account + + + +
+ + +
+
+ +
+
+ + +
+
+
+
+
+
+

Request to delete your account

+
+
+
+ +
+
+

Please enter your email. We will send you an email with a verification code to delete your account.

+
+ +
+
+ + + +
+ ${message} + + +
+
+ ${reCAPTCHA_message} + +
+
+
+
+ +
+
+
+
+ + +
+ +
+
+ +
+
+
+
+
+ + + +
+ + diff --git a/src/main/webapp/successDeleteAccount.jsp b/src/main/webapp/successDeleteAccount.jsp new file mode 100644 index 0000000..b8afb69 --- /dev/null +++ b/src/main/webapp/successDeleteAccount.jsp @@ -0,0 +1,101 @@ +<%-- + Created by IntelliJ IDEA. + User: sofia + Date: 21/5/2018 + Time: 5:07 μμ + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<% if (session.getAttribute("successDeleteAccount") == null) { + String redirectURL = request.getContextPath() + "/error404.jsp"; + response.sendRedirect(redirectURL); + +} else if (session.getAttribute("successDeleteAccount")!=null) { + session.removeAttribute("successDeleteAccount"); +} +%> + + + + + + + + + + + + OpenAIRE - Success + + +
+ + +
+
+ +
+
+ + +
+
+
+
+
+
+ <%--

Forgot Password

--%> +
+
+
+ +
+ +
+

Your account has been successfully deleted!

+

We hope to see you again!

+
+ +
+ +
+
+ +
+
+
+
+
+ + + +
+ + diff --git a/src/main/webapp/verifyToDelete.jsp b/src/main/webapp/verifyToDelete.jsp new file mode 100644 index 0000000..5df8227 --- /dev/null +++ b/src/main/webapp/verifyToDelete.jsp @@ -0,0 +1,130 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: sofia + Date: 21/5/2018 + Time: 2:45 μμ + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + + + + + + + + + OpenAIRE - Verification to delete your account + + +
+ + +
+
+ +
+
+ + +
+
+
+
+
+
+
+
+
+ +
+
+

An email has been sent to your email address. The email contains a verification code, please paste the verification code in the field below to delete your account.

+
+ +
+
+ + + +
+ ${message} + + ${msg_username_error} + + +
+
+ ${msg_verification_code_error} + + +
+
+
+ +
+
+
+
+ + +
+ +
+
+ +
+
+
+
+
+ + + + +
+ +