Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement;
2

    
3
import com.unboundid.ldap.sdk.LDAPException;
4
import eu.dnetlib.openaire.user.utils.EmailSender;
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.RequestDispatcher;
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 java.io.IOException;
18
import java.sql.Timestamp;
19
import java.util.Date;
20
import java.util.UUID;
21

    
22
/**
23
 * Created by kiatrop on 28/9/2017.
24
 */
25

    
26
public class ForgotPasswordServlet extends HttpServlet {
27

    
28
    public void init(ServletConfig config) throws ServletException {
29
        super.init(config);
30
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
31
                config.getServletContext());
32
    }
33

    
34
    @Autowired
35
    private LDAPActions ldapActions;
36

    
37
    @Autowired
38
    private VerificationActions verificationActions;
39

    
40
    @Autowired
41
    private EmailSender emailSender;
42

    
43
    private Logger logger = Logger.getLogger(ForgotPasswordServlet.class);
44

    
45

    
46
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
47

    
48
        String formEmail = request.getParameter("email");
49

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

    
55
        try {
56

    
57
            String username = ldapActions.getUsername(formEmail);
58
            if (username == null) {
59
                request.getSession().setAttribute("message", "User does not exist.");
60
                response.sendRedirect("./forgotPassword.jsp");
61

    
62
            } else {
63
                UUID verificationCode = UUID.randomUUID();
64
                Date creationDate = new Date();
65

    
66
                Timestamp timestamp = new Timestamp(creationDate.getTime());
67

    
68
                if (!verificationActions.verificationEntryExists(username)) {
69
                    verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
70

    
71
                } else {
72
                    verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
73
                }
74

    
75
                String verificationCodeMsg = "Hello,\n" +
76
                        "\n" +
77
                        "A request has been made to reset your OpenAIRE account password. To reset your\n" +
78
                        "password, you will need to submit this verification code in order to verify that the\n" +
79
                        "request was legitimate.\n" +
80
                        "\n" +
81
                        "The verification code is " + verificationCode.toString() + "\n Thank you";
82

    
83
                String verificationCodeSubject = "Your OpenAIRE password reset request";
84

    
85
                emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
86
            }
87

    
88
        } catch (LDAPException ldape) {
89
            //TODO create error page
90
            request.getSession().setAttribute("message", "Error sending email.");
91
            response.sendRedirect("./forgotPassword.jsp");
92
        }
93

    
94
        response.setContentType("text/html");
95
//        try {
96
////            request.getSession().setAttribute("email", formEmail);
97
////            request.getSession().setAttribute("username", ldapActions.getUsername(formEmail));
98
////            request.setAttribute("email", formEmail);
99
//            request.setAttribute("username", ldapActions.getUsername(formEmail));
100
//            RequestDispatcher rd = request.getRequestDispatcher("./verify.jsp");
101
//            rd.forward(request, response);
102
////            RequestDispatcher rd = request.getRequestDispatcher("ForgotPasswordServlet");
103
////            rd.forward(request, response);
104
////
105
////            logger.info("Stelnwwww");
106
////
107
//        } catch (LDAPException e) {
108
//            e.printStackTrace();
109
//            logger.info("LDAP error" + e);
110
//            request.getSession().setAttribute("message", "Error getting username.");
111
//            response.sendRedirect("./forgotPassword.jsp");
112
//        } catch (ServletException ex) {
113
//            ex.printStackTrace();
114
//            logger.info("Dispacher error" + ex);
115
//        }
116

    
117
        response.sendRedirect("./verify.jsp");
118
    }
119

    
120
}
(1-1/4)