Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement;
2

    
3
import eu.dnetlib.openaire.user.utils.EmailSender;
4
import eu.dnetlib.openaire.user.utils.LDAPActions;
5
import eu.dnetlib.openaire.user.utils.VerificationActions;
6
import eu.dnetlib.openaire.user.utils.VerifyRecaptcha;
7
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
8
import org.apache.commons.validator.routines.EmailValidator;
9
import org.apache.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Value;
12
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
13

    
14
import javax.mail.MessagingException;
15
import javax.servlet.ServletConfig;
16
import javax.servlet.ServletException;
17
import javax.servlet.http.HttpServlet;
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20
import javax.servlet.http.HttpSession;
21
import java.io.IOException;
22
import java.sql.Timestamp;
23
import java.util.Date;
24
import java.util.UUID;
25

    
26
/**
27
 * Created by sofia on 21/5/2018.
28
 */
29
public class RequestToDeleteAccountServlet extends HttpServlet {
30

    
31
    @Autowired
32
    private VerificationActions verificationActions;
33

    
34
    @Autowired
35
    private LDAPActions ldapActions;
36

    
37
    @Autowired
38
    private EmailSender emailSender;
39

    
40
    @Value("${oidc.home}")
41
    private String oidcHomeUrl;
42

    
43
    @Value("${google.recaptcha.secret}")
44
    private String secret;
45

    
46
    @Value("${google.recaptcha.key}")
47
    private String sitekey;
48

    
49
    private static final Logger logger = Logger.getLogger(RequestActivationCodeServlet.class);
50

    
51
    public void init(ServletConfig config) throws ServletException {
52
        super.init(config);
53
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
54
                config.getServletContext());
55
        config.getServletContext().setAttribute("sitekey", sitekey);
56

    
57
    }
58

    
59
    @Override
60
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
61
        String formEmail = request.getParameter("email").trim();
62

    
63
        String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
64

    
65
        HttpSession session = request.getSession();
66
        session.setAttribute("homeUrl", oidcHomeUrl);
67

    
68
        if (formEmail == null) {
69
            request.getSession().setAttribute("message", "Error reading email.");
70
            response.sendRedirect("./requestToDeleteAccount.jsp");
71

    
72
        } else if (formEmail.isEmpty()) {
73
            request.getSession().setAttribute("message", "Please enter your email.");
74
            response.sendRedirect("./requestToDeleteAccount.jsp");
75

    
76
        } else if (!EmailValidator.getInstance().isValid(formEmail)) {
77
            request.getSession().setAttribute("message", "Please enter a valid email.");
78
            response.sendRedirect("./requestToDeleteAccount.jsp");
79

    
80
        } else if (!VerifyRecaptcha.verify(gRecaptchaResponse, secret)) {
81
            request.getSession().setAttribute("reCAPTCHA_message", "You missed the reCAPTCHA validation!");
82
            response.sendRedirect("./requestToDeleteAccount.jsp");
83

    
84
        } else {
85

    
86
            try {
87

    
88
                Boolean isRegistered = false;
89
                Boolean isZombie = false;
90

    
91
                if (ldapActions.emailExists(formEmail)) {
92
                    logger.info("User with email: " + formEmail + " is activated user!");
93
                    isRegistered = true;
94
                } else if (ldapActions.isZombieUsersEmail(formEmail)) {
95
                    logger.info("User with email: " + formEmail + " is zombie user!");
96
                    isZombie = true;
97
                }
98

    
99
                if (!isRegistered && !isZombie) {
100
                    request.getSession().setAttribute("message", "There is no user with that email.");
101
                    response.sendRedirect("./requestToDeleteAccount.jsp");
102
                } else {
103

    
104
                    String username = null;
105

    
106
                    if (isRegistered) {
107
                        username = ldapActions.getUsername(formEmail);
108
                    } else if (isZombie) {
109
                        username = ldapActions.getZombieUsersUserName(formEmail);
110
                    }
111

    
112
                    UUID verificationCode = UUID.randomUUID();
113
                    Date creationDate = new Date();
114
                    String vCode = verificationCode.toString();
115

    
116
                    Timestamp timestamp = new Timestamp(creationDate.getTime());
117

    
118
                    if (!verificationActions.verificationEntryExists(username)) {
119
                        verificationActions.addVerificationEntry(username, vCode, timestamp);
120

    
121
                    } else {
122
                        verificationActions.updateVerificationEntry(username, vCode, timestamp);
123
                    }
124

    
125
                    String resultPath = UrlConstructor.getRedirectUrl(request, "verifyToDelete.jsp");
126
                    String resultPathWithVCode = UrlConstructor.getVerificationLink(resultPath, vCode);
127

    
128
                    String verificationCodeMsg = "<p>Hello " + username + ",</p>" +
129
                            "<p> A request has been made to get a verification code to delete your OpenAIRE account. To delete your " +
130
                            "account, you will need to submit your username and this verification code in order to verify that the " +
131
                            "request was legitimate.</p>" +
132
                            "<p>" +
133
                            "The verification code is " + vCode +
134
                            "</p>" +
135
                            "Click the URL below and proceed with deleting your account." +
136
                            "<p><a href=" + resultPathWithVCode + ">" + resultPathWithVCode + "</a></p>" +
137
                            "<p>The verification code is valid for 24 hours.</p>" +
138
                            "<p>Thank you,</p>" +
139
                            "<p>OpenAIRE technical team</p>";
140

    
141
                    String verificationCodeSubject = "Request to delete your OpenAIRE account";
142

    
143
                    emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
144
                    logger.info("Sending verification code to user: " + formEmail);
145

    
146

    
147
                    response.sendRedirect("./verifyToDelete.jsp");
148
                }
149
            } catch (MessagingException e) {
150
                logger.error("Error in sending email", e);
151
                request.getSession().setAttribute("message", "Error sending email");
152
                response.sendRedirect("./requestActivationCode.jsp");
153
            } catch (Exception ldape) {
154
                logger.error("Could not user with email " + formEmail, ldape);
155
                response.sendRedirect(UrlConstructor.getRedirectUrl(request, "error.jsp"));
156
            }
157
        }
158
    }
159

    
160
    public String getOidcHomeUrl() {
161
        return oidcHomeUrl;
162
    }
163

    
164
    public void setOidcHomeUrl(String oidcHomeUrl) {
165
        this.oidcHomeUrl = oidcHomeUrl;
166
    }
167
}
(13-13/18)