Implement the ManyToOne relationship for UserLogin and User that is related to Issue 64.
[pithos] / src / gr / ebs / gss / server / Registration.java
1 /*
2  * Copyright 2010 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.server;
20
21 import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 import gr.ebs.gss.client.exceptions.DuplicateNameException;
23 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
24 import gr.ebs.gss.client.exceptions.RpcException;
25 import gr.ebs.gss.server.domain.User;
26 import gr.ebs.gss.server.domain.dto.UserDTO;
27 import gr.ebs.gss.server.ejb.TransactionHelper;
28
29 import java.io.IOException;
30 import java.util.concurrent.Callable;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * The servlet that handles user registration.
40  *
41  * @author past
42  */
43 public class Registration extends BaseServlet {
44         /**
45          * The request parameter name for the acceptance flag.
46          */
47         private static final String ACCEPT_PARAM = "accept";
48
49         /**
50          * The request parameter name for the firstname.
51          */
52         private static final String FIRSTNAME_PARAM = "firstname";
53
54         /**
55          * The request parameter name for the lastname.
56          */
57         private static final String LASTNAME_PARAM = "lastname";
58
59         /**
60          * The request parameter name for the username.
61          */
62         private static final String USERNAME_PARAM = "username";
63
64         /**
65          * The request parameter name for the e-mail.
66          */
67         private static final String EMAIL_PARAM = "email";
68
69         /**
70          * The request parameter name for the password.
71          */
72         private static final String PASSWORD_PARAM = "password";
73
74         /**
75          * The request parameter name for the password confirmation.
76          */
77         private static final String PASSWORD2_PARAM = "password2";
78
79         /**
80          * The serial version UID of the class.
81          */
82         private static final long serialVersionUID = 1L;
83
84         /**
85          * The logger.
86          */
87         private static Log logger = LogFactory.getLog(Registration.class);
88
89         @Override
90         public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
91                 if (getConfiguration().getBoolean("onlyRegisterWithCode"))
92                         response.sendRedirect("invites.jsp");
93                 else
94                         response.sendRedirect("register.jsp");
95         }
96
97         @Override
98         public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
99                 final String firstname = request.getParameter(FIRSTNAME_PARAM);
100                 final String lastname = request.getParameter(LASTNAME_PARAM);
101                 final String email = request.getParameter(EMAIL_PARAM);
102                 final String username = request.getParameter(USERNAME_PARAM);
103                 String password = request.getParameter(PASSWORD_PARAM);
104                 String password2 = request.getParameter(PASSWORD2_PARAM);
105                 String accept = request.getParameter(ACCEPT_PARAM);
106                 response.setContentType("text/html");
107
108                 // Validate input parameters.
109                 if (username == null || username.isEmpty()) {
110                         String error = encode("No username was specified");
111                         String errorUrl = "register.jsp?username=&error=" + error;
112                         errorUrl += "&firstname=" + (firstname == null? "": encode(firstname));
113                         errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
114                         errorUrl += "&email=" + (email == null? "": encode(email));
115                         response.sendRedirect(errorUrl);
116                         return;
117                 } else if (username.indexOf(' ') != -1) {
118                         String error = encode("Spaces in username are not allowed");
119                         String errorUrl = "register.jsp?username=&error=" + error;
120                         errorUrl += "&firstname=" + (firstname == null? "": encode(firstname));
121                         errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
122                         errorUrl += "&email=" + (email == null? "": encode(email));
123                         response.sendRedirect(errorUrl);
124                         return;
125                 } else if (firstname == null || firstname.isEmpty()) {
126                         String error = encode("No firstname was specified");
127                         String errorUrl = "register.jsp?firstname=&error=" + error;
128                         errorUrl += "&username=" + encode(username);
129                         errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
130                         errorUrl += "&email=" + (email == null? "": encode(email));
131                         response.sendRedirect(errorUrl);
132                         return;
133                 } else if (lastname == null || lastname.isEmpty()) {
134                         String error = encode("No lastname was specified");
135                         String errorUrl = "register.jsp?lastname=&error=" + error;
136                         errorUrl += "&username=" + encode(username);
137                         errorUrl += "&firstname=" + encode(firstname);
138                         errorUrl += "&email=" + (email == null? "": encode(email));
139                         response.sendRedirect(errorUrl);
140                         return;
141                 } else if (email == null || email.isEmpty()) {
142                         String error = encode("No e-mail was specified");
143                         String errorUrl = "register.jsp?email=&error=" + error;
144                         errorUrl += "&username=" + encode(username);
145                         errorUrl += "&firstname=" + encode(firstname);
146                         errorUrl += "&lastname=" + encode(lastname);
147                         response.sendRedirect(errorUrl);
148                         return;
149                 } else if (password == null || password.isEmpty()) {
150                         String error = encode("No password was specified");
151                         String errorUrl = "register.jsp?error=" + error;
152                         errorUrl += "&username=" + encode(username);
153                         errorUrl += "&firstname=" + encode(firstname);
154                         errorUrl += "&lastname=" + encode(lastname);
155                         errorUrl += "&email=" + encode(email);
156                         response.sendRedirect(errorUrl);
157                         return;
158                 } else if (!password.equals(password2)) {
159                         String error = encode("Passwords do not match");
160                         String errorUrl = "register.jsp?error=" + error;
161                         errorUrl += "&username=" + encode(username);
162                         errorUrl += "&firstname=" + encode(firstname);
163                         errorUrl += "&lastname=" + encode(lastname);
164                         errorUrl += "&email=" + encode(email);
165                         response.sendRedirect(errorUrl);
166                         return;
167                 } else if (!"on".equalsIgnoreCase(accept)) {
168                         String error = encode("You must accept the terms and conditions");
169                         String errorUrl = "register.jsp?error=" + error;
170                         errorUrl += "&username=" + encode(username);
171                         errorUrl += "&firstname=" + encode(firstname);
172                         errorUrl += "&lastname=" + encode(lastname);
173                         errorUrl += "&email=" + encode(email);
174                         response.sendRedirect(errorUrl);
175                         return;
176                 }
177
178                 User user = null;
179                 try {
180                         user = getService().findUser(username);
181                         if (user != null) {
182                                 String error = encode("The username already exists");
183                                 String errorUrl = "register.jsp?username=&error=" + error;
184                                 errorUrl += "&firstname=" + encode(firstname);
185                                 errorUrl += "&lastname=" + encode(lastname);
186                                 errorUrl += "&email=" + encode(email);
187                                 response.sendRedirect(errorUrl);
188                                 return;
189                         }
190                         try {
191                                 getService().createLdapUser(username, firstname, lastname, email, password);
192                         } catch (Exception e) {
193                                 logger.error(e);
194                                 handleException(response, e.getMessage());
195                                 return;
196                         }
197                         final UserDTO userDto = new TransactionHelper<UserDTO>().tryExecute(new Callable<UserDTO>() {
198                                 @Override
199                                 public UserDTO call() throws Exception {
200                                         return getService().createUser(username, firstname + " " + lastname, email, "", "").getDTO();
201                                 }
202
203                         });
204                         new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
205                                 @Override
206                                 public Void call() throws Exception {
207                                         getService().updateUserPolicyAcceptance(userDto.getId(), true);
208                                         return null;
209                                 }
210
211                         });
212                         response.sendRedirect("registered.jsp");
213                 } catch (RpcException e) {
214                         logger.error(e);
215                         handleException(response, "An error occurred while communicating with the service");
216                 } catch (DuplicateNameException e) {
217                         // Can't happen, but this is more user-friendly than an assert.
218                         logger.error(e);
219                         handleException(response, "The username already exists");
220                 } catch (ObjectNotFoundException e) {
221                         // Can't happen, but this is more user-friendly than an assert.
222                         logger.error(e);
223                         handleException(response, "No username or name was specified");
224                 } catch (Exception e) {
225                         logger.error(e);
226                         handleException(response, e.getMessage());
227                 }
228         }
229
230         private void handleException(HttpServletResponse response, String error) throws IOException {
231                 String errorUrl = "register.jsp?username=&firstname=&lastname=&email=&error=" + encode(error);
232                 response.sendRedirect(errorUrl);
233         }
234 }