Add emailSender at register

This commit is contained in:
Sofia Baltzi 2017-10-25 14:15:07 +00:00
parent e3395073ab
commit 3182ae1f2d
3 changed files with 60 additions and 6 deletions

View File

@ -97,7 +97,7 @@ public class ForgotPasswordServlet extends HttpServlet {
request.getSession().setAttribute("message", "Error sending email."); request.getSession().setAttribute("message", "Error sending email.");
response.sendRedirect("./forgotPassword.jsp"); response.sendRedirect("./forgotPassword.jsp");
} }
} }
} }

View File

@ -1,5 +1,6 @@
package eu.dnetlib.openaire.usermanagement; 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.LDAPActions;
import eu.dnetlib.openaire.user.utils.VerificationActions; import eu.dnetlib.openaire.user.utils.VerificationActions;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -13,6 +14,9 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;
/** /**
* Created by sofia on 20/10/2017. * Created by sofia on 20/10/2017.
@ -28,6 +32,9 @@ public class RegisterServlet extends HttpServlet {
@Autowired @Autowired
private VerificationActions verificationActions; private VerificationActions verificationActions;
@Autowired
private EmailSender emailSender;
@Autowired @Autowired
private LDAPActions ldapActions; private LDAPActions ldapActions;
@ -55,14 +62,55 @@ public class RegisterServlet extends HttpServlet {
if (firstName != null && lastName != null && username != null && if (firstName != null && lastName != null && username != null &&
email.equals(confirmEmail) /*&& password.equals(confirmPassword)*/ ) { email.equals(confirmEmail) /*&& password.equals(confirmPassword)*/ ) {
try { try {
ldapActions.createUser(username, email, firstName, lastName, organization/*, password*/);
logger.info("User successfully created"); // 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 {
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 = "Your OpenAIRE password reset request";
emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg);
response.sendRedirect("./activate.jsp");
// }
} catch (Exception e) { } catch (Exception e) {
logger.error("LDAP error in creating user", e); logger.error("LDAP error in creating user", e);
response.sendRedirect("./error.jsp"); response.sendRedirect("./error.jsp");
} }
} }
response.sendRedirect("./activate.jsp");
printWriter.close(); printWriter.close();
} }

View File

@ -17,7 +17,7 @@
<script src="./js/jquery.js"></script> <script src="./js/jquery.js"></script>
<script src="./js/uikit.js"></script> <script src="./js/uikit.js"></script>
<script src="./js/validation.js"></script> <script src="./js/validation.js"></script>
<script src="./OpenAIRE_files/uikit-icons-max.js"></script> <script src="./js/uikit-icons-max.js"></script>
<link rel="stylesheet" style="text/css" href="./css/theme.css"> <link rel="stylesheet" style="text/css" href="./css/theme.css">
<link rel="stylesheet" style="text/css" href="./css/custom.css"> <link rel="stylesheet" style="text/css" href="./css/custom.css">
<link rel="stylesheet" style="text/css" href="./css/aai-custom.css"> <link rel="stylesheet" style="text/css" href="./css/aai-custom.css">
@ -56,7 +56,7 @@
<div class="uk-width-1-3@m uk-align-center"> <div class="uk-width-1-3@m uk-align-center">
<!-- REGISTER FORM --> <!-- REGISTER FORM -->
<div id="registerForm"> <div id="registerForm">
<form action="register" method="POST" target="GWTBootstrap3_AbstractForm_RepositoryManager_1" role="form" class="m-t" id="register_form"> <form action="register" method="POST" role="form" class="m-t" id="register_form">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<div class="alert alert-success" aria-hidden="true" style="display: none;"></div> <div class="alert alert-success" aria-hidden="true" style="display: none;"></div>
<div class="alert alert-danger" aria-hidden="true" style="display: none;"></div> <div class="alert alert-danger" aria-hidden="true" style="display: none;"></div>
@ -72,11 +72,15 @@
<span class="msg_username_min_lenght uk-text-danger uk-text-small uk-float-left" style="display:none">Minimum username length 5 characters.</span> <span class="msg_username_min_lenght uk-text-danger uk-text-small uk-float-left" style="display:none">Minimum username length 5 characters.</span>
<span class="msg_username_max_lenght uk-text-danger uk-text-small uk-float-left" style="display:none">Maximum username lenght 150 characters.</span> <span class="msg_username_max_lenght uk-text-danger uk-text-small uk-float-left" style="display:none">Maximum username lenght 150 characters.</span>
<span class="msg_username_error uk-text-danger uk-text-small uk-float-left" style="display:none">Please enter your username.</span> <span class="msg_username_error uk-text-danger uk-text-small uk-float-left" style="display:none">Please enter your username.</span>
<%--<span id="username_server_error" class="uk-text-danger uk-text-small uk-float-left">${username_message}</span>--%>
<%--<c:remove var="username_message" scope="session" />--%>
<input id="username" name="username" type="text" placeholder="Username (*)" class="form-control"></div> <input id="username" name="username" type="text" placeholder="Username (*)" class="form-control"></div>
<div class="form-group"> <div class="form-group">
<span class="msg_email_error uk-text-danger uk-text-small uk-float-left" style="display:none">Please enter your email.</span> <span class="msg_email_error uk-text-danger uk-text-small uk-float-left" style="display:none">Please enter your email.</span>
<span class="msg_email_validation_error uk-text-danger uk-text-small uk-float-left" style="display:none">Please enter a valid email.</span> <span class="msg_email_validation_error uk-text-danger uk-text-small uk-float-left" style="display:none">Please enter a valid email.</span>
<span class="msg_email_conf_error uk-text-danger uk-text-small uk-float-left" style="display:none">These emails don't match.</span> <span class="msg_email_conf_error uk-text-danger uk-text-small uk-float-left" style="display:none">These emails don't match.</span>
<%--<span id="email_server_error" class="uk-text-danger uk-text-small uk-float-left">${email_message}</span>--%>
<%--<c:remove var="email_message" scope="session" />--%>
<input id="email" name="email" type="text" placeholder="Email (*)" class="form-control"></div> <input id="email" name="email" type="text" placeholder="Email (*)" class="form-control"></div>
<div class="form-group"> <div class="form-group">
<input id="email_conf" name="email_conf" type="text" placeholder="Confirm email (*)" class="form-control"></div> <input id="email_conf" name="email_conf" type="text" placeholder="Confirm email (*)" class="form-control"></div>
@ -185,12 +189,14 @@
$("#username").focusin(function () { $("#username").focusin(function () {
$(this).removeClass('aai-form-danger'); $(this).removeClass('aai-form-danger');
$(".msg_username_error").fadeOut(); $(".msg_username_error").fadeOut();
// $(".username_server_error").fadeOut();
}); });
$("#email").focusin(function () { $("#email").focusin(function () {
$(this).removeClass('aai-form-danger'); $(this).removeClass('aai-form-danger');
$(".msg_email_error").fadeOut(); $(".msg_email_error").fadeOut();
$(".msg_email_validation_error").fadeOut(); $(".msg_email_validation_error").fadeOut();
// $(".email_server_error").fadeOut();
}); });
$("#email_conf").focusin(function () { $("#email_conf").focusin(function () {