]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/user/register/FinancialsUserRegisterWebRequestBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / user / register / FinancialsUserRegisterWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2022 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.jfinancials.beans.user.register;
18
19 import java.util.Objects;
20 import javax.ejb.EJB;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.event.Observes;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.faces.application.FacesMessage;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontacts.model.contact.Contact;
30 import org.mxchange.jcontacts.model.contact.UserContact;
31 import org.mxchange.jcoreee.utils.FacesUtils;
32 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
33 import org.mxchange.jfinancials.beans.contact.FinancialsContactWebRequestController;
34 import org.mxchange.jfinancials.beans.features.FinancialsFeaturesWebApplicationController;
35 import org.mxchange.jfinancials.beans.localization.FinancialsLocalizationSessionController;
36 import org.mxchange.jfinancials.beans.user.FinancialsUserWebRequestController;
37 import org.mxchange.jfinancials.beans.user.list.FinancialsUserListWebViewController;
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.LoginUser;
42 import org.mxchange.jusercore.model.user.User;
43 import org.mxchange.jusercore.model.user.Users;
44 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
45 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
46 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
47 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
48 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
49 import org.mxchange.juserlogincore.events.registration.UserRegisteredEvent;
50 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
51 import org.mxchange.juserlogincore.events.user.password_change.UpdatedUserPasswordEvent;
52 import org.mxchange.juserlogincore.login.UserLoginUtils;
53 import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote;
54
55 /**
56  * A web bean for user registration
57  * <p>
58  * @author Roland Häder<roland@mxchange.org>
59  */
60 @Named ("userRegistrationController")
61 @RequestScoped
62 public class FinancialsUserRegisterWebRequestBean extends BaseFinancialsBean implements FinancialsUserRegisterWebRequestController {
63
64         /**
65          * Serial number
66          */
67         private static final long serialVersionUID = 47_828_986_719_691_592L;
68
69         /**
70          * Contact controller
71          */
72         @Inject
73         private FinancialsContactWebRequestController contactController;
74
75         /**
76          * Features controller
77          */
78         @Inject
79         private FinancialsFeaturesWebApplicationController featureController;
80
81         /**
82          * Localization controller
83          */
84         @Inject
85         private FinancialsLocalizationSessionController localizationController;
86
87         /**
88          * Remote register session-scoped bean
89          */
90         @EJB (lookup = "java:global/jfinancials-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote")
91         private UserRegistrationSessionBeanRemote registerBean;
92
93         /**
94          * User list controller
95          */
96         @Inject
97         private FinancialsUserListWebViewController userListController;
98
99         /**
100          * User name
101          */
102         private String userName;
103
104         /**
105          * User password (clear-text from web form)
106          */
107         private String userPassword;
108
109         /**
110          * An event being fired when a user password was changed
111          */
112         @Inject
113         @Any
114         private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
115
116         /**
117          * User password repeated (clear-text from web form)
118          */
119         private String userPasswordRepeat;
120
121         /**
122          * Whether the user wants a public profile
123          */
124         private ProfileMode userProfileMode;
125
126         /**
127          * An event being fired when a new user has registered
128          */
129         @Inject
130         @Any
131         private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
132
133         /**
134          * Default constructor
135          */
136         public FinancialsUserRegisterWebRequestBean () {
137                 // Call super constructor
138                 super();
139         }
140
141         /**
142          * Event observer for user password changes
143          * <p>
144          * @param event Event being fired
145          */
146         public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
147                 // Is it valid?
148                 if (null == event) {
149                         // Throw NPE
150                         throw new NullPointerException("event is null"); //NOI18N
151                 } else if (event.getUserPassword() == null) {
152                         // Throw NPE
153                         throw new NullPointerException("event.userPassword is null"); //NOI18N
154                 } else if (event.getUserPassword().isEmpty()) {
155                         // Throw NPE
156                         throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N
157                 }
158
159                 // Set it here
160                 this.setUserPassword(event.getUserPassword());
161                 this.setUserPasswordRepeat(event.getUserPassword());
162         }
163
164         /**
165          * Registers the user, if not found. Otherwise this method should throw an
166          * exception.
167          * <p>
168          * @return Redirection target
169          */
170         public String doFinishRegistration () {
171                 // Is registration enabled?
172                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
173                         // Is not enabled
174                         throw new FacesException("Registration is disabled."); //NOI18N
175                 }
176
177                 // Get user instance
178                 final User user = this.createUserInstance(true);
179
180                 // Null random password means registration requires user-entered password
181                 String randomPassword = null;
182
183                 // Is the user already used?
184                 if (null == user) {
185                         // user must be set
186                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
187                 } else if (!this.isRequiredPersonalDataSet()) {
188                         // Not all required fields are set
189                         throw new FacesException("Not all required fields are set."); //NOI18N
190                 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(user))) { //NOI18N
191                         // Is multi-page enabled?
192                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
193                                 // User name is already used, should not happen here
194                                 throw new FacesException(new UserNameAlreadyRegisteredException(user));
195                         } else {
196                                 // May happen here, clear user name
197                                 this.clearUserName();
198
199                                 // Output message
200                                 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
201                                 return ""; //NOI18N
202                         }
203                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
204                         // Is multi-page enabled?
205                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
206                                 // Email address has already been taken, should not happen here
207                                 throw new FacesException(new EmailAddressAlreadyRegisteredException(user));
208                         } else {
209                                 // May happen here, reset fields
210                                 this.contactController.clearEmailAddresses();
211                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
212                                 return ""; //NOI18N
213                         }
214                 } else if (!this.contactController.isSameEmailAddressEntered()) {
215                         // Is multi-page enabled?
216                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
217                                 // Not same email address entered, should not happen here
218                                 throw new FacesException(new DataRepeatMismatchException("Email addresses not matching.")); //NOI18N
219                         } else {
220                                 // May happen here, reset fields
221                                 this.contactController.clearEmailAddresses();
222                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING", FacesMessage.SEVERITY_INFO); //NOI18N
223                                 return ""; //NOI18N
224                         }
225                 } else if (!this.isSamePasswordEntered()) {
226                         // Is multi-page enabled?
227                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
228                                 // Not same password entered, should no longer happen here
229                                 throw new FacesException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
230                         } else if (this.ifBothPasswordsEmptyAllowed()) {
231                                 // Both passwords are left empty and is allowed, then generate a random password
232                                 randomPassword = UserLoginUtils.createRandomPassword(FinancialsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
233
234                                 // Generate (ignored) password-history
235                                 final PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
236
237                                 // Fire event
238                                 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
239                         }
240                 }
241
242                 // Encrypt password
243                 final String encryptedPassword = UserLoginUtils.encryptPassword(this.getUserPassword());
244
245                 // Set it here
246                 user.setUserEncryptedPassword(encryptedPassword);
247
248                 // Is developer mode?
249                 if (this.isDebugModeEnabled("register")) { //NOI18N
250                         // For debugging/programming only:
251                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
252                 } else {
253                         // No debugging of this part
254                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
255
256                         // Ask EJB for generating a not-existing confirmation key
257                         final String confirmKey = this.registerBean.generateConfirmationKey(user);
258
259                         // Set it in user
260                         user.setUserConfirmKey(confirmKey);
261                 }
262
263                 // Init variable
264                 final User registeredUser;
265
266                 try {
267                         // Get base URL
268                         final String baseUrl = FacesUtils.generateBaseUrl();
269
270                         // Call bean
271                         registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
272
273                         // The id number should be set
274                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
275                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
276                         // Continue to throw
277                         throw new FacesException(ex);
278                 }
279
280                 // Fire event
281                 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
282
283                 // All fine, redirect to proper page
284                 return "user_register_done"; //NOI18N
285         }
286
287         /**
288          * Handles registration request send from first page. The (maybe) entered
289          * user name and email address is not used and that privacy and T&C are
290          * accepted.
291          * <p>
292          * @return Redirect
293          */
294         public String doRegisterMultiPage1 () {
295                 // Is registration enabled?
296                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
297                         // Is not enabled
298                         throw new FacesException("Registration is disabled."); //NOI18N
299                 }
300
301                 // Get user instance
302                 final User user = this.createUserInstance(false);
303
304                 // First check if user is not null and user name is not used + if same email address is entered
305                 if (null == user) {
306                         // user must be set
307                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
308                 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(user))) { //NOI18N
309                         // User name is already used, so clear it
310                         this.clearUserName();
311
312                         // Output message
313                         this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
314                         return ""; //NOI18N
315                 } else if (!this.contactController.isSameEmailAddressEntered()) {
316                         // Not same email address entered, clear both
317                         this.contactController.clearEmailAddresses();
318                         this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING", FacesMessage.SEVERITY_WARN); //NOI18N
319                         return ""; //NOI18N
320                 } else if (!this.isSamePasswordEntered()) {
321                         // Is multi-page enabled?
322                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
323                                 // Clear passwords
324                                 this.clearUserPasswords();
325
326                                 // Output faces message
327                                 this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY", FacesMessage.SEVERITY_WARN); //NOI18N
328                                 this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY", FacesMessage.SEVERITY_WARN); //NOI18N
329                                 return ""; //NOI18N
330                         } else if (this.ifBothPasswordsEmptyAllowed()) {
331                                 // Both passwords are left empty and is allowed, then generate a random password
332                                 final String randomPassword = UserLoginUtils.createRandomPassword(FinancialsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
333
334                                 // Generate (ignored) password-history
335                                 final PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
336
337                                 // Fire event
338                                 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
339                         }
340                 }
341
342                 // Create half contact instance with email address
343                 final Contact contact = new UserContact();
344                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
345
346                 // Set contact in user
347                 user.setUserContact(contact);
348
349                 // Check if email address is registered
350                 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
351                         // Email address has already been taken, clear both
352                         this.contactController.clearEmailAddresses();
353                         this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
354                         return ""; //NOI18N
355                 }
356
357                 // Now only redirect to next page as the JSF does it
358                 return "user_register_page2"; //NOI18N
359         }
360
361         /**
362          * Getter for user name
363          * <p>
364          * @return User name
365          */
366         public String getUserName () {
367                 return this.userName;
368         }
369
370         /**
371          * Setter for user name
372          * <p>
373          * @param userName User name
374          */
375         public void setUserName (final String userName) {
376                 this.userName = userName;
377         }
378
379         /**
380          * Getter for clear-text user password
381          * <p>
382          * @return Clear-text user password
383          */
384         public String getUserPassword () {
385                 return this.userPassword;
386         }
387
388         /**
389          * Setter for clear-text user password
390          * <p>
391          * @param userPassword Clear-text user password
392          */
393         public void setUserPassword (final String userPassword) {
394                 this.userPassword = userPassword;
395         }
396
397         /**
398          * Getter for clear-text user password repeated
399          * <p>
400          * @return Clear-text user password repeated
401          */
402         public String getUserPasswordRepeat () {
403                 return this.userPasswordRepeat;
404         }
405
406         /**
407          * Setter for clear-text user password repeated
408          * <p>
409          * @param userPasswordRepeat Clear-text user password repeated
410          */
411         public void setUserPasswordRepeat (final String userPasswordRepeat) {
412                 this.userPasswordRepeat = userPasswordRepeat;
413         }
414
415         /**
416          * Getter for user profile mode
417          * <p>
418          * @return User profile mode
419          */
420         public ProfileMode getUserProfileMode () {
421                 return this.userProfileMode;
422         }
423
424         /**
425          * Setter for user profile mode
426          * <p>
427          * @param userProfileMode User profile mode
428          */
429         public void setUserProfileMode (final ProfileMode userProfileMode) {
430                 this.userProfileMode = userProfileMode;
431         }
432
433         @Override
434         public boolean isRequiredChangePersonalDataSet () {
435                 return ((this.getUserProfileMode() != null) &&
436                                 (this.getUserName() != null) &&
437                                 (!this.getUserName().isEmpty()) &&
438                                 (this.contactController.isRequiredChangePersonalDataSet()));
439         }
440
441         /**
442          * Clears user name
443          */
444         private void clearUserName () {
445                 // Clear it
446                 this.setUserName(null);
447         }
448
449         /**
450          * Clears both user passwords
451          */
452         private void clearUserPasswords () {
453                 // Clear both
454                 this.setUserPassword(null);
455                 this.setUserPasswordRepeat(null);
456         }
457
458         /**
459          * Creates an instance from all properties
460          * <p>
461          * @param createContactData Whether contact data should be created
462          * <p>
463          * @return A user instance
464          */
465         private User createUserInstance (final boolean createContactData) {
466                 // Required personal data must be set
467                 assert (this.isRequiredPersonalDataSet()) : "All required personal data must be set before invoking this method."; //NOI18N
468
469                 // Is user name required?
470                 if (!this.featureController.isFeatureEnabled("user_login_require_username")) {
471                         // Init variables
472                         String randomName = null;
473                         boolean isUsernameFree = false;
474
475                         // Get full list
476                         for (final User user : this.userListController.getAllUsers()) {
477                                 // Loop until a user name is found
478                                 while ((randomName == null) || (randomName.equals(user.getUserName()))) {
479                                         // Generate random name
480                                         randomName = Users.generateRandomUserName();
481                                         isUsernameFree = true;
482                                 }
483
484                                 // Is non-existing username found
485                                 if (isUsernameFree) {
486                                         // Also stop looping here
487                                         break;
488                                 }
489                         }
490
491                         // Set it and inivisible profile
492                         this.setUserName(randomName);
493                         this.setUserProfileMode(ProfileMode.INVISIBLE);
494
495                         // Generate random password
496                         final String randomPassword = UserLoginUtils.createRandomPassword(FinancialsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
497
498                         // Set random password
499                         this.setUserPassword(randomPassword);
500                         this.setUserPasswordRepeat(randomPassword);
501                 }
502
503                 // Create new user instance
504                 final User user = new LoginUser();
505
506                 // Set user name profile mode and locale
507                 user.setUserName(this.getUserName());
508                 user.setUserProfileMode(this.getUserProfileMode());
509                 user.setUserLocale(this.localizationController.getLocale());
510
511                 // Is multiple registration page
512                 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
513                         // Create contact instance
514                         final Contact contact = this.contactController.createContactInstance();
515
516                         // Set contact in user
517                         user.setUserContact(contact);
518                 }
519
520                 // Return it
521                 return user;
522         }
523
524         /**
525          * Checks if both user passwords are left empty and if this is enabled
526          * (allowed) in context parameter. If true, the calling bean should create a
527          * random password (preferable with UserUtils.createRandomPassword() and set
528          * it in both user password fields.
529          * <p>
530          * @return Whether empty passwords are allowed
531          */
532         private boolean ifBothPasswordsEmptyAllowed () {
533                 // Check feature first
534                 return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N
535                                 ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
536                                 ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
537         }
538
539         /**
540          * Checks whether all required personal data is set
541          * <p>
542          * @return Whether the required personal data is set
543          */
544         private boolean isRequiredPersonalDataSet () {
545                 // Check conditions based on of multi-page registration is enabled
546                 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
547                         // Multiple registration page
548                         return this.contactController.isRequiredPersonalDataSet();
549                 } else {
550                         // Single registration page
551                         return (((this.getUserName() != null) || (!this.featureController.isFeatureEnabled("user_login_require_username"))) && //NOI18N
552                                         (this.getUserProfileMode() != null) &&
553                                         (this.contactController.isRequiredPersonalDataSet()) &&
554                                         (this.getUserPassword() != null) &&
555                                         (this.getUserPasswordRepeat() != null));
556                 }
557         }
558
559         /**
560          * Checks whether same passwords has been entered
561          * <p>
562          * @return Whether same passwords has been entered
563          */
564         private boolean isSamePasswordEntered () {
565                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
566         }
567
568 }