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