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

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

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

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

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

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

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

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

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

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

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

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

    
106
                 } else {
107

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

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

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

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

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

    
131
                     request.getSession().setAttribute("first_name", firstName);
132
                     request.getSession().setAttribute("last_name", lastName);
133
                     request.getSession().setAttribute("organization", organization);
134
                     request.getSession().setAttribute("username", username);
135
                     request.getSession().setAttribute("email", email);
136
                     request.getSession().setAttribute("email_conf", confirmEmail);
137

    
138
                     response.sendRedirect("./register.jsp");
139
                 }
140

    
141

    
142
            } catch (Exception e) {
143
                logger.error("LDAP error in creating user", e);
144
                response.sendRedirect("./error.jsp");
145
            }
146
        }
147
        printWriter.close();
148

    
149
    }
150
}
151

    
(4-4/7)