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 org.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
9

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

    
21
/**
22
 * Created by sofia on 20/10/2017.
23
 */
24
public class RegisterServlet 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 EmailSender emailSender;
37

    
38
    @Autowired
39
    private LDAPActions ldapActions;
40

    
41
    private Logger logger = Logger.getLogger(RegisterServlet.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 firstName = request.getParameter("first_name");
49
        String lastName = request.getParameter("last_name");
50
        String organization = request.getParameter("organization");
51
        String username = request.getParameter("username");
52
        String email =request.getParameter("email");
53
        String confirmEmail = request.getParameter("email_conf");
54
        //String password = request.getParameter("password");
55
        //String confirmPassword = request.getParameter("password_conf");
56

    
57
        if (organization == null){
58
            logger.info("organization is null");
59
        }
60
        if (firstName != null && lastName != null &&  username != null &&
61
                email.equals(confirmEmail) /*&& password.equals(confirmPassword)*/ ) {
62
            try {
63

    
64
//                if (ldapActions.usernameExists(username)) {
65
//                    request.getSession().setAttribute("username_message", "Username already exists! Choose another one.");
66
//                    logger.info("Username already exists");
67
//                    response.sendRedirect("./register.jsp");
68
//
69
//                } else if (ldapActions.emailExists(email)) {
70
//                    request.getSession().setAttribute("email_message", "There is another user with this email.");
71
//                    logger.info("There is another user with this email");
72
//                    response.sendRedirect("./register.jsp");
73
//
74
//                } else {
75

    
76
                 if (!ldapActions.usernameExists(username) && !ldapActions.emailExists(email)) {
77

    
78
                     ldapActions.createUser(username, email, firstName, lastName, organization/*, password*/);
79
                     logger.info("User successfully created");
80

    
81
                     UUID verificationCode = UUID.randomUUID();
82
                     Date creationDate = new Date();
83

    
84
                     Timestamp timestamp = new Timestamp(creationDate.getTime());
85

    
86
                     if (!verificationActions.verificationEntryExists(username)) {
87
                         verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
88

    
89
                     } else {
90
                         verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
91
                     }
92

    
93
                     String verificationCodeMsg = "Hello " + username + ",\n" +
94
                             "\n" +
95
                             "A request has been made to verify your email and activate your OpenAIRE account. To activate your\n" +
96
                             "account, you will need to submit your username and this activation code in order to verify that the\n" +
97
                             "request was legitimate.\n" +
98
                             "\n" +
99
                             "The activation code is " + verificationCode.toString() + "\n Thank you";
100

    
101
                     String verificationCodeSubject = "Activate your OpenAIRE account";
102

    
103
                     emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg);
104

    
105

    
106
                     response.sendRedirect("./activate.jsp");
107
//                }
108
                 } else {
109

    
110
                     if (ldapActions.usernameExists(username)) {
111
                        request.getSession().setAttribute("username_message", "Username already exists! Choose another one.");
112
                        logger.info("Username already exists");
113
                     }
114

    
115
                     if (ldapActions.emailExists(email)) {
116
                         request.getSession().setAttribute("email_message", "There is another user with this email.");
117
                         logger.info("There is another user with this email");
118
                     }
119

    
120
                     response.sendRedirect("./register.jsp");
121
                 }
122

    
123

    
124
            } catch (Exception e) {
125
                logger.error("LDAP error in creating user", e);
126
                response.sendRedirect("./error.jsp");
127
            }
128
        }
129
        printWriter.close();
130

    
131
    }
132
}
133

    
(4-4/7)