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").trim();
49
        String lastName = request.getParameter("last_name").trim();
50
        String organization = request.getParameter("organization").trim();
51
        String username = request.getParameter("username").trim();
52
        String email =request.getParameter("email").trim();
53
        String confirmEmail = request.getParameter("email_conf").trim();
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

    
63
            try {
64

    
65
                 if (username.matches("^[a-zA-Z0-9][a-zA-Z0-9_-]{4,150}") && !ldapActions.usernameExists(username) && !ldapActions.emailExists(email)
66
                         && !ldapActions.isZombieUsersEmail(email) && !ldapActions.isZombieUsersUsername(username)) {
67

    
68
                     ldapActions.createZombieUser(username, email, firstName, lastName, organization, password);
69
                     logger.info("Zombie user successfully created");
70

    
71
                     UUID verificationCode = UUID.randomUUID();
72
                     Date creationDate = new Date();
73

    
74
                     Timestamp timestamp = new Timestamp(creationDate.getTime());
75

    
76
                     if (!verificationActions.verificationEntryExists(username)) {
77
                         verificationActions.addVerificationEntry(username, verificationCode.toString(), timestamp);
78

    
79
                     } else {
80
                         verificationActions.updateVerificationEntry(username, verificationCode.toString(), timestamp);
81
                     }
82

    
83
                     String scheme = request.getScheme();
84
                     String serverName = request.getServerName();
85
                     int portNumber = request.getServerPort();
86
                     String contextPath = request.getContextPath();
87

    
88
                     String resultPath = scheme + "://" +serverName + ":" +portNumber + contextPath + "/activate.jsp";
89

    
90
                     String verificationCodeMsg = "<p>Hello " + username + ",</p>" +
91
                             "<p> A request has been made to verify your email and activate your OpenAIRE account. To activate your " +
92
                             "account, you will need to submit your username and this activation code in order to verify that the" +
93
                             "request was legitimate.</p>" +
94
                             "<p>" +
95
                             "The activation code is " + verificationCode.toString() +
96
                             "</p>" +
97
                             "Select the URL below and proceed with activating your password." +
98
                             "<p><a href=" + resultPath + ">" + resultPath + "</a></p>" +
99
                             "<p>Thank you</p>";
100

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

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

    
105
                     response.sendRedirect("./activate.jsp");
106

    
107
                 } else {
108

    
109
                     if(!username.matches("^[a-zA-Z0-9][a-zA-Z0-9_-]{4,150}")){
110

    
111
                         if (username.length() < 5) {
112
                             request.getSession().setAttribute("username_message", "Minimum username length 5 characters.");
113
                             logger.info("Minimum username length 5 characters.");
114
                         }
115

    
116
                         if (username.length() > 150) {
117
                             request.getSession().setAttribute("username_message", "Maximum username lenght 150 characters.");
118
                             logger.info("Maximum username lenght 150 characters.");
119
                         }
120
                     }
121

    
122
                     if (ldapActions.usernameExists(username) || ldapActions.isZombieUsersUsername(username)) {
123
                        request.getSession().setAttribute("username_message", "Username already exists! Choose another one.");
124
                        logger.info("Username already exists");
125
                     }
126

    
127
                     if (ldapActions.emailExists(email)) {
128
                         request.getSession().setAttribute("email_message", "There is another user with this email.");
129
                         logger.info("There is another user with this email");
130
                     }
131

    
132
                     if (ldapActions.isZombieUsersEmail(email)) {
133
                         request.getSession().setAttribute("email_message", "You have already registered with this email address! Please check your email to activate your account or contact OpenAIRE <a href=\"https://www.openaire.eu/support/helpdesk\">helpdesk</a>.");
134
                         logger.info("There is another user with this email");
135
                     }
136

    
137
                     request.getSession().setAttribute("first_name", firstName);
138
                     request.getSession().setAttribute("last_name", lastName);
139
                     request.getSession().setAttribute("organization", organization);
140
                     request.getSession().setAttribute("username", username);
141
                     request.getSession().setAttribute("email", email);
142
                     request.getSession().setAttribute("email_conf", confirmEmail);
143

    
144
                     response.sendRedirect("./register.jsp");
145
                 }
146

    
147

    
148
            } catch (Exception e) {
149
                logger.error("LDAP error in creating user", e);
150
                response.sendRedirect("./error.jsp");
151
            }
152
        }
153
        printWriter.close();
154

    
155
    }
156
}
157

    
(4-4/7)