Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement;
2

    
3
import com.unboundid.ldap.sdk.LDAPException;
4
import eu.dnetlib.openaire.user.utils.EmailActions;
5
import eu.dnetlib.openaire.user.utils.LDAPActions;
6
import eu.dnetlib.openaire.user.utils.VerificationActions;
7
import org.apache.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
10

    
11
import javax.servlet.ServletConfig;
12
import javax.servlet.ServletException;
13
import javax.servlet.http.HttpServlet;
14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
16
import java.io.IOException;
17
import java.util.Date;
18
import java.util.UUID;
19

    
20
/**
21
 * Created by kiatrop on 28/9/2017.
22
 */
23

    
24
public class ForgotPasswordServlet 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 LDAPActions ldapActions;
34

    
35
    @Autowired
36
    private VerificationActions verificationActions;
37

    
38
    private EmailActions emailActions;
39

    
40
    private Logger logger = Logger.getLogger(ForgotPasswordServlet.class);
41

    
42

    
43
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
44

    
45
        String formEmail = request.getParameter("email");
46

    
47
        if (formEmail == null) {
48
            request.getSession().setAttribute("message", "Error reading email.");
49
            response.sendRedirect("./forgotPassword.jsp");
50
        }
51

    
52
        try {
53
            String username = ldapActions.getUsername(formEmail);
54

    
55
            if (username == null) {
56
                request.getSession().setAttribute("message", "User does not exist.");
57
                response.sendRedirect("./forgotPassword.jsp");
58

    
59
            } else {
60
                UUID verificationCode = UUID.randomUUID();
61
                Date creationDate = new Date();
62

    
63
                if (verificationActions.verificationEntryExists(username)) {
64
                    verificationActions.addVerificationEntry(username, verificationCode.toString(), creationDate);
65

    
66
                } else {
67
                    verificationActions.updateVerificationEntry(username, verificationCode.toString(), creationDate);
68
                }
69

    
70
                emailActions.sendVerificationCode(formEmail);
71

    
72
            }
73

    
74

    
75
        } catch (LDAPException ldape) {
76
            //TODO create error page
77
            request.getSession().setAttribute("message", "Error sending email.");
78
            response.sendRedirect("./forgotPassword.jsp");
79
        }
80

    
81
        response.setContentType("text/html");
82
        response.sendRedirect("./verify.jsp");
83
    }
84

    
85
}
(1-1/4)