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

    
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.createUser(username, email, firstName, lastName, organization, password);
68
                     logger.info("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 verificationCodeMsg = "Hello " + username + ",\n" +
83
                             "\n" +
84
                             "A request has been made to verify your email and activate your OpenAIRE account. To activate your\n" +
85
                             "account, you will need to submit your username and this activation code in order to verify that the\n" +
86
                             "request was legitimate.\n" +
87
                             "\n" +
88
                             "The activation code is " + verificationCode.toString() + "\n Thank you";
89

    
90
                     String verificationCodeSubject = "Activate your OpenAIRE account";
91

    
92
                     emailSender.sendEmail(email, verificationCodeSubject, verificationCodeMsg);
93

    
94
                     response.sendRedirect("./activate.jsp");
95

    
96
                 } else {
97

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

    
100
                         if (username.length() < 5) {
101
                             request.getSession().setAttribute("username_message", "Minimum username length 5 characters.");
102
                             logger.info("Minimum username length 5 characters.");
103
                         }
104

    
105
                         if (username.length() > 150) {
106
                             request.getSession().setAttribute("username_message", "Maximum username lenght 150 characters.");
107
                             logger.info("Maximum username lenght 150 characters.");
108
                         }
109
                     }
110

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

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

    
121
                     request.getSession().setAttribute("first_name", firstName);
122
                     request.getSession().setAttribute("last_name", lastName);
123
                     request.getSession().setAttribute("organization", organization);
124
                     request.getSession().setAttribute("username", username);
125
                     request.getSession().setAttribute("email", email);
126
                     request.getSession().setAttribute("email_conf", confirmEmail);
127

    
128
                     response.sendRedirect("./register.jsp");
129
                 }
130

    
131

    
132
            } catch (Exception e) {
133
                logger.error("LDAP error in creating user", e);
134
                response.sendRedirect("./error.jsp");
135
            }
136
        }
137
        printWriter.close();
138

    
139
    }
140
}
141

    
(4-4/7)