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