1
|
package eu.dnetlib.openaire.usermanagement;
|
2
|
|
3
|
import eu.dnetlib.openaire.user.utils.EmailSender;
|
4
|
import org.apache.commons.validator.routines.EmailValidator;
|
5
|
import eu.dnetlib.openaire.user.utils.LDAPActions;
|
6
|
import eu.dnetlib.openaire.user.utils.VerificationActions;
|
7
|
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
11
|
|
12
|
import javax.servlet.ServletConfig;
|
13
|
import javax.servlet.ServletException;
|
14
|
import javax.servlet.http.HttpServlet;
|
15
|
import javax.servlet.http.HttpServletRequest;
|
16
|
import javax.servlet.http.HttpServletResponse;
|
17
|
import java.io.IOException;
|
18
|
import java.io.PrintWriter;
|
19
|
import java.sql.Timestamp;
|
20
|
import java.util.Date;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
/**
|
24
|
* Created by sofia on 20/10/2017.
|
25
|
*/
|
26
|
public class RegisterServlet extends HttpServlet {
|
27
|
|
28
|
public void init(ServletConfig config) throws ServletException {
|
29
|
super.init(config);
|
30
|
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
|
31
|
config.getServletContext());
|
32
|
}
|
33
|
|
34
|
@Autowired
|
35
|
private VerificationActions verificationActions;
|
36
|
|
37
|
@Autowired
|
38
|
private EmailSender emailSender;
|
39
|
|
40
|
@Autowired
|
41
|
private LDAPActions ldapActions;
|
42
|
|
43
|
private Logger logger = Logger.getLogger(RegisterServlet.class);
|
44
|
|
45
|
@Override
|
46
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
47
|
response.setContentType("text/html");
|
48
|
PrintWriter printWriter = response.getWriter();
|
49
|
|
50
|
String firstName = request.getParameter("first_name").trim();
|
51
|
String lastName = request.getParameter("last_name").trim();
|
52
|
String organization = request.getParameter("organization").trim();
|
53
|
String username = request.getParameter("username").trim();
|
54
|
String email =request.getParameter("email").trim();
|
55
|
String confirmEmail = request.getParameter("email_conf").trim();
|
56
|
String password = request.getParameter("password");
|
57
|
String confirmPassword = request.getParameter("password_conf");
|
58
|
|
59
|
if (organization == null){
|
60
|
logger.info("organization is null");
|
61
|
}
|
62
|
if (firstName != null && lastName != null && username != null &&
|
63
|
email.equals(confirmEmail) && password.equals(confirmPassword) ) {
|
64
|
|
65
|
try {
|
66
|
|
67
|
if (username.matches("^[a-zA-Z0-9\\.\\_\\-]{4,150}") && !ldapActions.usernameExists(username) && !ldapActions.emailExists(email)
|
68
|
&& !ldapActions.isZombieUsersEmail(email) && !ldapActions.isZombieUsersUsername(username) && EmailValidator.getInstance().isValid(email)) {
|
69
|
|
70
|
ldapActions.createZombieUser(username, email, firstName, lastName, organization, password);
|
71
|
logger.info("Zombie user successfully created");
|
72
|
|
73
|
UUID verificationCode = UUID.randomUUID();
|
74
|
Date creationDate = new Date();
|
75
|
|
76
|
Timestamp timestamp = new Timestamp(creationDate.getTime());
|
77
|
|
78
|
if (!verificationActions.verificationEntryExists(username)) {
|
79
|
verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
|
80
|
|
81
|
} else {
|
82
|
verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
|
83
|
}
|
84
|
|
85
|
String resultPath = UrlConstructor.getRedirectUrl(request, "activate.jsp");
|
86
|
|
87
|
String verificationCodeMsg = "<p>Hello " + username + ",</p>" +
|
88
|
"<p> A request has been made to verify your email and activate your OpenAIRE account. To activate your " +
|
89
|
"account, you will need to submit your username and this activation code in order to verify that the" +
|
90
|
"request was legitimate.</p>" +
|
91
|
"<p>" +
|
92
|
"The activation code is " + verificationCode.toString() +
|
93
|
"</p>" +
|
94
|
"Select the URL below and proceed with activating your password." +
|
95
|
"<p><a href=" + resultPath + ">" + resultPath + "</a></p>" +
|
96
|
"<p>Thank you</p>";
|
97
|
|
98
|
String verificationCodeSubject = "Activate your OpenAIRE account";
|
99
|
|
100
|
emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg);
|
101
|
|
102
|
response.sendRedirect("./activate.jsp");
|
103
|
|
104
|
} else {
|
105
|
|
106
|
if(!username.matches("^[a-zA-Z0-9\\.\\_\\-]{4,150}")) {
|
107
|
|
108
|
if (username.length() < 5) {
|
109
|
request.getSession().setAttribute("username_message", "Minimum username length 5 characters.");
|
110
|
logger.info("Minimum username length 5 characters.");
|
111
|
}
|
112
|
|
113
|
if (username.length() > 150) {
|
114
|
request.getSession().setAttribute("username_message", "Maximum username length 150 characters.");
|
115
|
logger.info("Maximum username length 150 characters.");
|
116
|
}
|
117
|
|
118
|
if (!username.matches("^[a-zA-Z0-9\\.\\_\\-]")) {
|
119
|
request.getSession().setAttribute("username_allowed_chars_message", "You can use letters, numbers, underscores, hyphens and periods.");
|
120
|
logger.info("Only letters, numbers, underscores, hyphens and periods.");
|
121
|
}
|
122
|
|
123
|
if (!username.matches("^[a-zA-Z0-9].*")) {
|
124
|
request.getSession().setAttribute("username_first_char_message", "The username must start with letter or digit.");
|
125
|
logger.info("The username must start with letter or digit.");
|
126
|
}
|
127
|
|
128
|
}
|
129
|
|
130
|
if (ldapActions.usernameExists(username) || ldapActions.isZombieUsersUsername(username)) {
|
131
|
request.getSession().setAttribute("username_message", "Username already exists! Choose another one.");
|
132
|
logger.info("Username already exists");
|
133
|
}
|
134
|
|
135
|
if (ldapActions.emailExists(email)) {
|
136
|
request.getSession().setAttribute("email_message", "There is another user with this email.");
|
137
|
logger.info("There is another user with this email");
|
138
|
}
|
139
|
|
140
|
if (!EmailValidator.getInstance().isValid(email)) {
|
141
|
request.getSession().setAttribute("email_message", "Please enter a valid email.");
|
142
|
logger.info("Invalid email.");
|
143
|
}
|
144
|
|
145
|
if (ldapActions.isZombieUsersEmail(email)) {
|
146
|
request.getSession().setAttribute("email_message", "You have already registered with this email address! Please check your email to activate your account or contact OpenAIRE <a href=\"https://www.openaire.eu/support/helpdesk\">helpdesk</a>.");
|
147
|
logger.info("There is another user with this email");
|
148
|
}
|
149
|
|
150
|
request.getSession().setAttribute("first_name", firstName);
|
151
|
request.getSession().setAttribute("last_name", lastName);
|
152
|
request.getSession().setAttribute("organization", organization);
|
153
|
request.getSession().setAttribute("username", username);
|
154
|
request.getSession().setAttribute("email", email);
|
155
|
request.getSession().setAttribute("email_conf", confirmEmail);
|
156
|
|
157
|
response.sendRedirect("./register.jsp");
|
158
|
}
|
159
|
|
160
|
|
161
|
} catch (Exception e) {
|
162
|
logger.error("LDAP error in creating user", e);
|
163
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp"));
|
164
|
//response.sendRedirect("./error.jsp");
|
165
|
}
|
166
|
}
|
167
|
printWriter.close();
|
168
|
|
169
|
}
|
170
|
}
|
171
|
|