1
|
package eu.dnetlib.openaire.usermanagement;
|
2
|
|
3
|
import eu.dnetlib.openaire.user.utils.InputValidator;
|
4
|
import eu.dnetlib.openaire.user.utils.LDAPActions;
|
5
|
import eu.dnetlib.openaire.user.utils.VerificationActions;
|
6
|
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.beans.factory.annotation.Value;
|
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 javax.servlet.http.HttpSession;
|
18
|
import java.io.IOException;
|
19
|
import java.io.PrintWriter;
|
20
|
|
21
|
/**
|
22
|
* Created by sofia on 23/10/2017.
|
23
|
*/
|
24
|
public class ActivationCodeServlet 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
|
private LDAPActions ldapActions;
|
37
|
|
38
|
@Value("${oidc.home}")
|
39
|
private String oidcHomeUrl;
|
40
|
|
41
|
private Logger logger = Logger.getLogger(ActivationCodeServlet.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
|
String formUsername = request.getParameter("username").trim();
|
49
|
String formVerificationCode = request.getParameter("verification_code").trim();
|
50
|
|
51
|
if (InputValidator.isFilled(formUsername) && InputValidator.isFilled(formVerificationCode)) {
|
52
|
if (verificationActions.verificationEntryExists(formUsername) && verificationActions.verificationCodeIsCorrect(formUsername, formVerificationCode)) {
|
53
|
if (!verificationActions.verificationCodeHasExpired(formUsername)) {
|
54
|
HttpSession session = request.getSession();
|
55
|
session.setAttribute("username", formUsername);
|
56
|
session.setAttribute("homeUrl", oidcHomeUrl);
|
57
|
try {
|
58
|
ldapActions.moveUser(formUsername);
|
59
|
} catch (Exception e) {
|
60
|
logger.error("LDAP error in moving user", e);
|
61
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp"));
|
62
|
}
|
63
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "registerSuccess.jsp"));
|
64
|
} else {
|
65
|
logger.info("Verification code has expired!");
|
66
|
response.sendRedirect(UrlConstructor.getRedirectUrl(request, "expiredVerificationCode.jsp"));
|
67
|
}
|
68
|
} else {
|
69
|
logger.info("Username or activation code are not valid!");
|
70
|
request.getSession().setAttribute("message", "Username or activation code are not valid.");
|
71
|
response.sendRedirect("./activate.jsp");
|
72
|
}
|
73
|
} else {
|
74
|
if (!InputValidator.isFilled(formUsername)) {
|
75
|
logger.info("No username");
|
76
|
request.getSession().setAttribute("msg_username_error", "Please enter your username.");
|
77
|
}
|
78
|
if (!InputValidator.isFilled(formVerificationCode)) {
|
79
|
logger.info("No activation code");
|
80
|
request.getSession().setAttribute("msg_activation_code_error", "Please enter your activation code.");
|
81
|
}
|
82
|
response.sendRedirect("./activate.jsp");
|
83
|
}
|
84
|
printWriter.close();
|
85
|
|
86
|
}
|
87
|
|
88
|
public String getOidcHomeUrl() {
|
89
|
return oidcHomeUrl;
|
90
|
}
|
91
|
|
92
|
public void setOidcHomeUrl(String oidcHomeUrl) {
|
93
|
this.oidcHomeUrl = oidcHomeUrl;
|
94
|
}
|
95
|
}
|