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 = (String)request.getAttribute("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 userEmail = ldapActions.getUsername(formEmail);
54

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

    
59
            } else {
60
                String username = ldapActions.getUsername(userEmail);
61
                UUID verificationCode = UUID.randomUUID();
62
                Date creationDate = new Date();
63

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

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

    
71
                emailActions.sendVerificationCode(userEmail);
72

    
73
            }
74

    
75

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

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

    
86
}
(1-1/4)