2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.user.register;
19 import java.util.Objects;
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.jjobs.beans.BaseJobsBean;
33 import org.mxchange.jjobs.beans.contact.JobsContactWebRequestController;
34 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
35 import org.mxchange.jjobs.beans.user.JobsUserWebRequestController;
36 import org.mxchange.jusercore.events.user.clear.password.ClearUserPasswordEvent;
37 import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent;
38 import org.mxchange.jusercore.events.user.clear.username.ClearUserNameEvent;
39 import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent;
40 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
41 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
42 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
43 import org.mxchange.jusercore.model.user.LoginUser;
44 import org.mxchange.jusercore.model.user.User;
45 import org.mxchange.jusercore.model.user.Users;
46 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
47 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
48 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
49 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
50 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
51 import org.mxchange.juserlogincore.events.registration.UserRegisteredEvent;
52 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
53 import org.mxchange.juserlogincore.events.user.password_change.UpdatedUserPasswordEvent;
54 import org.mxchange.juserlogincore.login.UserLoginUtils;
55 import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote;
58 * A web bean for user registration
60 * @author Roland Häder<roland@mxchange.org>
62 @Named ("userRegistrationController")
64 public class JobsUserRegisterWebRequestBean extends BaseJobsBean implements JobsUserRegisterWebRequestController {
69 private static final long serialVersionUID = 47_828_986_719_691_592L;
75 private JobsContactWebRequestController contactController;
81 private JobsFeaturesWebApplicationController featureController;
84 * Localization controller
87 private FinancialsLocalizationSessionController localizationController;
90 * Remote register session-scoped bean
92 @EJB (lookup = "java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote")
93 private UserRegistrationSessionBeanRemote registerBean;
96 * User list controller
99 private FinancialsUserListWebViewController userListController;
104 private String userName;
107 * User password (clear-text from web form)
109 private String userPassword;
112 * An event being fired when a user password was changed
116 private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
119 * User password repeated (clear-text from web form)
121 private String userPasswordRepeat;
124 * Whether the user wants a public profile
126 private ProfileMode userProfileMode;
129 * An event being fired when a new user has registered
133 private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
136 * Default constructor
138 public JobsUserRegisterWebRequestBean () {
139 // Call super constructor
144 * Event observer for user password changes
146 * @param event Event being fired
148 public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
152 throw new NullPointerException("event is null"); //NOI18N
153 } else if (event.getUserPassword() == null) {
155 throw new NullPointerException("event.userPassword is null"); //NOI18N
156 } else if (event.getUserPassword().isEmpty()) {
158 throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N
162 this.setUserPassword(event.getUserPassword());
163 this.setUserPasswordRepeat(event.getUserPassword());
167 * Registers the user, if not found. Otherwise this method should throw an
170 * @return Redirection target
172 public String doFinishRegistration () {
173 // Is registration enabled?
174 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
176 throw new FacesException("Registration is disabled."); //NOI18N
180 final User user = this.createUserInstance(true);
182 // Null random password means registration requires user-entered password
183 String randomPassword = null;
185 // Is the user already used?
188 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
189 } else if (!this.isRequiredPersonalDataSet()) {
190 // Not all required fields are set
191 throw new FacesException("Not all required fields are set."); //NOI18N
192 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(user))) { //NOI18N
193 // Is multi-page enabled?
194 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
195 // User name is already used, should not happen here
196 throw new FacesException(new UserNameAlreadyRegisteredException(user));
198 // May happen here, clear user name
199 this.clearUserName();
202 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
205 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
206 // Is multi-page enabled?
207 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
208 // Email address has already been taken, should not happen here
209 throw new FacesException(new EmailAddressAlreadyRegisteredException(user));
211 // May happen here, reset fields
212 this.contactController.clearEmailAddresses();
213 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
216 } else if (!this.contactController.isSameEmailAddressEntered()) {
217 // Is multi-page enabled?
218 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
219 // Not same email address entered, should not happen here
220 throw new FacesException(new DataRepeatMismatchException("Email addresses not matching.")); //NOI18N
222 // May happen here, reset fields
223 this.contactController.clearEmailAddresses();
224 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING", FacesMessage.SEVERITY_INFO); //NOI18N
227 } else if (!this.isSamePasswordEntered()) {
228 // Is multi-page enabled?
229 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
230 // Not same password entered, should no longer happen here
231 throw new FacesException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
232 } else if (this.ifBothPasswordsEmptyAllowed()) {
233 // Both passwords are left empty and is allowed, then generate a random password
234 randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
236 // Generate (ignored) password-history
237 final PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
240 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
245 final String encryptedPassword = UserLoginUtils.encryptPassword(this.getUserPassword());
248 user.setUserEncryptedPassword(encryptedPassword);
250 // Is developer mode?
251 if (this.isDebugModeEnabled("register")) { //NOI18N
252 // For debugging/programming only:
253 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
255 // No debugging of this part
256 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
258 // Ask EJB for generating a not-existing confirmation key
259 final String confirmKey = this.registerBean.generateConfirmationKey(user);
262 user.setUserConfirmKey(confirmKey);
267 final String baseUrl = FacesUtils.generateBaseUrl();
270 final User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
272 // The id number should be set
273 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
276 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
278 // All fine, redirect to proper page
279 return "user_register_done"; //NOI18N
280 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
282 throw new FacesException(ex);
287 * Handles registration request send from first page. The (maybe) entered
288 * user name and email address is not used and that privacy and T&C are
293 public String doRegisterMultiPage1 () {
294 // Is registration enabled?
295 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
297 throw new FacesException("Registration is disabled."); //NOI18N
301 final User user = this.createUserInstance(false);
303 // First check if user is not null and user name is not used + if same email address is entered
306 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
307 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(user))) { //NOI18N
308 // User name is already used, so clear it
309 this.clearUserName();
312 this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
314 } else if (!this.contactController.isSameEmailAddressEntered()) {
315 // Not same email address entered, clear both
316 this.contactController.clearEmailAddresses();
317 this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING", FacesMessage.SEVERITY_WARN); //NOI18N
319 } else if (!this.isSamePasswordEntered()) {
320 // Is multi-page enabled?
321 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
323 this.clearUserPasswords();
325 // Output faces message
326 this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY", FacesMessage.SEVERITY_WARN); //NOI18N
327 this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY", FacesMessage.SEVERITY_WARN); //NOI18N
329 } else if (this.ifBothPasswordsEmptyAllowed()) {
330 // Both passwords are left empty and is allowed, then generate a random password
331 final String randomPassword = UserLoginUtils.createRandomPassword(FinancialsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
333 // Generate (ignored) password-history
334 final PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
337 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
341 // Create half contact instance with email address
342 final Contact contact = new UserContact();
343 contact.setContactEmailAddress(this.contactController.getEmailAddress());
345 // Set contact in user
346 user.setUserContact(contact);
348 // Check if email address is registered
349 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
350 // Email address has already been taken, clear both
351 this.contactController.clearEmailAddresses();
352 this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
356 // Now only redirect to next page as the JSF does it
357 return "user_register_page2"; //NOI18N
361 * Getter for user name
365 public String getUserName () {
366 return this.userName;
370 * Setter for user name
372 * @param userName User name
374 public void setUserName (final String userName) {
375 this.userName = userName;
379 * Getter for clear-text user password
381 * @return Clear-text user password
383 public String getUserPassword () {
384 return this.userPassword;
388 * Setter for clear-text user password
390 * @param userPassword Clear-text user password
392 public void setUserPassword (final String userPassword) {
393 this.userPassword = userPassword;
397 * Getter for clear-text user password repeated
399 * @return Clear-text user password repeated
401 public String getUserPasswordRepeat () {
402 return this.userPasswordRepeat;
406 * Setter for clear-text user password repeated
408 * @param userPasswordRepeat Clear-text user password repeated
410 public void setUserPasswordRepeat (final String userPasswordRepeat) {
411 this.userPasswordRepeat = userPasswordRepeat;
415 * Getter for user profile mode
417 * @return User profile mode
419 public ProfileMode getUserProfileMode () {
420 return this.userProfileMode;
424 * Setter for user profile mode
426 * @param userProfileMode User profile mode
428 public void setUserProfileMode (final ProfileMode userProfileMode) {
429 this.userProfileMode = userProfileMode;
433 public boolean isRequiredChangePersonalDataSet () {
434 return ((this.getUserProfileMode() != null) &&
435 (this.getUserName() != null) &&
436 (!this.getUserName().isEmpty()) &&
437 (this.contactController.isRequiredChangePersonalDataSet()));
443 private void clearUserName () {
445 this.setUserName(null);
449 * Clears both user passwords
451 private void clearUserPasswords () {
453 this.setUserPassword(null);
454 this.setUserPasswordRepeat(null);
458 * Creates an instance from all properties
460 * @param createContactData Whether contact data should be created
462 * @return A user instance
464 private User createUserInstance (final boolean createContactData) {
465 // Required personal data must be set
466 assert (this.isRequiredPersonalDataSet()) : "All required personal data must be set before invoking this method."; //NOI18N
468 // Is user name required?
469 if (!this.featureController.isFeatureEnabled("user_login_require_username")) {
471 String randomName = null;
472 boolean isUsernameFree = false;
475 for (final User user : this.userListController.getAllUsers()) {
476 // Loop until a user name is found
477 while ((randomName == null) || (randomName.equals(user.getUserName()))) {
478 // Generate random name
479 randomName = Users.generateRandomUserName();
480 isUsernameFree = true;
483 // Is non-existing username found
484 if (isUsernameFree) {
485 // Also stop looping here
490 // Set it and inivisible profile
491 this.setUserName(randomName);
492 this.setUserProfileMode(ProfileMode.INVISIBLE);
494 // Generate random password
495 final String randomPassword = UserLoginUtils.createRandomPassword(FinancialsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
497 // Set random password
498 this.setUserPassword(randomPassword);
499 this.setUserPasswordRepeat(randomPassword);
502 // Create new user instance
503 final User user = new LoginUser();
505 // Set user name profile mode and locale
506 user.setUserName(this.getUserName());
507 user.setUserProfileMode(this.getUserProfileMode());
508 user.setUserLocale(this.localizationController.getLocale());
510 // Is multiple registration page
511 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
512 // Create contact instance
513 final Contact contact = this.contactController.createContactInstance();
515 // Set contact in user
516 user.setUserContact(contact);
524 * Checks if both user passwords are left empty and if this is enabled
525 * (allowed) in context parameter. If true, the calling bean should create a
526 * random password (preferable with UserUtils.createRandomPassword() and set
527 * it in both user password fields.
529 * @return Whether empty passwords are allowed
531 private boolean ifBothPasswordsEmptyAllowed () {
532 // Check feature first
533 return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N
534 ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
535 ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
539 * Checks whether all required personal data is set
541 * @return Whether the required personal data is set
543 private boolean isRequiredPersonalDataSet () {
544 // Check conditions based on of multi-page registration is enabled
545 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
546 // Multiple registration page
547 return this.contactController.isRequiredPersonalDataSet();
549 // Single registration page
550 return (((this.getUserName() != null) || (!this.featureController.isFeatureEnabled("user_login_require_username"))) && //NOI18N
551 (this.getUserProfileMode() != null) &&
552 (this.contactController.isRequiredPersonalDataSet()) &&
553 (this.getUserPassword() != null) &&
554 (this.getUserPasswordRepeat() != null));
559 * Checks whether same passwords has been entered
561 * @return Whether same passwords has been entered
563 private boolean isSamePasswordEntered () {
564 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));