]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/register/JobsUserRegisterWebRequestBean.java
e1afa115226842cac9433d90a3afb3a2aef294a9
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / register / JobsUserRegisterWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.beans.user.register;
18
19 import javax.ejb.EJB;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import org.mxchange.jcontacts.model.contact.Contact;
27 import org.mxchange.jcontacts.model.contact.UserContact;
28 import org.mxchange.jcoreee.utils.FacesUtils;
29 import org.mxchange.jjobs.beans.BaseJobsBean;
30 import org.mxchange.jjobs.beans.contact.JobsContactWebRequestController;
31 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
32 import org.mxchange.jjobs.beans.user.JobsUserWebRequestController;
33 import org.mxchange.jusercore.events.user.clear.password.ClearUserPasswordEvent;
34 import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent;
35 import org.mxchange.jusercore.events.user.clear.username.ClearUserNameEvent;
36 import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent;
37 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
38 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
39 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
40 import org.mxchange.jusercore.model.user.User;
41 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
42 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
44 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
45 import org.mxchange.juserlogincore.events.registration.UserRegisteredEvent;
46 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
47 import org.mxchange.juserlogincore.events.user.password_change.UpdatedUserPasswordEvent;
48 import org.mxchange.juserlogincore.login.UserLoginUtils;
49 import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote;
50
51 /**
52  * A web bean for user registration
53  * <p>
54  * @author Roland Häder<roland@mxchange.org>
55  */
56 @Named ("userRegistrationController")
57 @RequestScoped
58 public class JobsUserRegisterWebRequestBean extends BaseJobsBean implements JobsUserRegisterWebRequestController {
59
60         /**
61          * Serial number
62          */
63         private static final long serialVersionUID = 47_828_986_719_691_592L;
64
65         /**
66          * An event being fired when a user name should be cleared
67          */
68         @Inject
69         @Any
70         private Event<ObservableClearUserNameEvent> clearUserNameEvent;
71
72         /**
73          * An event being fired when a user password should be cleared
74          */
75         @Inject
76         @Any
77         private Event<ObservableClearUserPasswordEvent> clearUserPasswordEvent;
78
79         /**
80          * Contact controller
81          */
82         @Inject
83         private JobsContactWebRequestController contactController;
84
85         /**
86          * Features controller
87          */
88         @Inject
89         private JobsFeaturesWebApplicationController featureController;
90
91         /**
92          * Remote register session-scoped bean
93          */
94         @EJB (lookup = "java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote")
95         private UserRegistrationSessionBeanRemote registerBean;
96
97         /**
98          * User controller
99          */
100         @Inject
101         private JobsUserWebRequestController userController;
102
103         /**
104          * An event being fired when a user password was changed
105          */
106         @Inject
107         @Any
108         private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
109
110         /**
111          * An event being fired when a new user has registered
112          */
113         @Inject
114         @Any
115         private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
116
117         /**
118          * Default constructor
119          */
120         public JobsUserRegisterWebRequestBean () {
121                 // Call super constructor
122                 super();
123         }
124
125         /**
126          * Registers the user, if not found. Otherwise this method should throw an
127          * exception.
128          * <p>
129          * @return Redirection target
130          */
131         public String doFinishRegistration () {
132                 // Is registration enabled?
133                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
134                         // Is not enabled
135                         throw new FaceletException("Registration is disabled."); //NOI18N
136                 }
137
138                 // Get user instance
139                 final User user = this.userController.createUserInstance(true);
140
141                 // Null random password means registration requires user-entered password
142                 String randomPassword = null;
143
144                 // Is the user already used?
145                 if (null == user) {
146                         // user must be set
147                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
148                 } else if (!this.userController.isRequiredPersonalDataSet()) {
149                         // Not all required fields are set
150                         throw new FaceletException("Not all required fields are set."); //NOI18N
151                 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
152                         // Is multi-page enabled?
153                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
154                                 // User name is already used, should not happen here
155                                 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
156                         } else {
157                                 // May happen here, fire event
158                                 this.clearUserNameEvent.fire(new ClearUserNameEvent());
159
160                                 // Output message
161                                 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
162                                 return ""; //NOI18N
163                         }
164                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
165                         // Is multi-page enabled?
166                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
167                                 // Email address has already been taken, should not happen here
168                                 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
169                         } else {
170                                 // May happen here, reset fields
171                                 this.contactController.clearEmailAddresses();
172                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
173                                 return ""; //NOI18N
174                         }
175                 } else if (!this.contactController.isSameEmailAddressEntered()) {
176                         // Is multi-page enabled?
177                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
178                                 // Not same email address entered, should not happen here
179                                 throw new FaceletException(new DataRepeatMismatchException("Email addresses not matching.")); //NOI18N
180                         } else {
181                                 // May happen here, reset fields
182                                 this.contactController.clearEmailAddresses();
183                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
184                                 return ""; //NOI18N
185                         }
186                 } else if (!this.userController.isSamePasswordEntered()) {
187                         // Is multi-page enabled?
188                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
189                                 // Not same password entered, should no longer happen here
190                                 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
191                         } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
192                                 // Both passwords are left empty and is allowed, then generate a random password
193                                 randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
194
195                                 // Generate (ignored) password-history
196                                 final PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
197
198                                 // Fire event
199                                 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
200                         }
201                 }
202
203                 // Encrypt password
204                 final String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
205
206                 // Set it here
207                 user.setUserEncryptedPassword(encryptedPassword);
208
209                 // Is developer mode?
210                 if (this.isDebugModeEnabled("register")) { //NOI18N
211                         // For debugging/programming only:
212                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
213                 } else {
214                         // No debugging of this part
215                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
216
217                         // Ask EJB for generating a not-existing confirmation key
218                         final String confirmKey = this.registerBean.generateConfirmationKey(user);
219
220                         // Set it in user
221                         user.setUserConfirmKey(confirmKey);
222                 }
223
224                 try {
225                         // Get base URL
226                         final String baseUrl = FacesUtils.generateBaseUrl();
227
228                         // Call bean
229                         final User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
230
231                         // The id number should be set
232                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
233
234                         // Fire event
235                         this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
236
237                         // All fine, redirect to proper page
238                         return "user_register_done"; //NOI18N
239                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
240                         // Continue to throw
241                         throw new FaceletException(ex);
242                 }
243         }
244
245         /**
246          * Handles registration request send from first page. The (maybe) entered
247          * user name and email address is not used and that privacy and T&C are
248          * accepted.
249          * <p>
250          * @return Redirect
251          */
252         public String doRegisterMultiPage1 () {
253                 // Is registration enabled?
254                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
255                         // Is not enabled
256                         throw new FaceletException("Registration is disabled."); //NOI18N
257                 }
258
259                 // Get user instance
260                 final User user = this.userController.createUserInstance(false);
261
262                 // First check if user is not null and user name is not used + if same email address is entered
263                 if (null == user) {
264                         // user must be set
265                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
266                 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
267                         // User name is already used, so clear it
268                         this.clearUserNameEvent.fire(new ClearUserNameEvent());
269
270                         // Output message
271                         this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
272                         return ""; //NOI18N
273                 } else if (!this.contactController.isSameEmailAddressEntered()) {
274                         // Not same email address entered, clear both
275                         this.contactController.clearEmailAddresses();
276                         this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
277                         return ""; //NOI18N
278                 } else if (!this.userController.isSamePasswordEntered()) {
279                         // Is multi-page enabled?
280                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
281                                 // Unset both
282                                 this.clearUserPasswordEvent.fire(new ClearUserPasswordEvent());
283
284                                 // Output faces message
285                                 this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY"); //NOI18N
286                                 this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY"); //NOI18N
287                                 return ""; //NOI18N
288                         } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
289                                 // Both passwords are left empty and is allowed, then generate a random password
290                                 String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
291
292                                 // Generate (ignored) password-history
293                                 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
294
295                                 // Fire event
296                                 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
297                         }
298                 }
299
300                 // Create half contact instance with email address
301                 final Contact contact = new UserContact();
302                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
303
304                 // Set contact in user
305                 user.setUserContact(contact);
306
307                 // Check if email address is registered
308                 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
309                         // Email address has already been taken, clear both
310                         this.contactController.clearEmailAddresses();
311                         this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
312                         return ""; //NOI18N
313                 }
314
315                 // Now only redirect to next page as the JSF does it
316                 return "user_register_page2"; //NOI18N
317         }
318
319 }