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;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.application.FacesMessage;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jcontacts.model.contact.Contact;
28 import org.mxchange.jcontacts.model.contact.UserContact;
29 import org.mxchange.jcoreee.utils.FacesUtils;
30 import org.mxchange.jjobs.beans.BaseJobsBean;
31 import org.mxchange.jjobs.beans.contact.JobsContactWebRequestController;
32 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
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;
53 * A web bean for user registration
55 * @author Roland Häder<roland@mxchange.org>
57 @Named ("userRegistrationController")
59 public class JobsUserRegisterWebRequestBean extends BaseJobsBean implements JobsUserRegisterWebRequestController {
64 private static final long serialVersionUID = 47_828_986_719_691_592L;
67 * An event being fired when a user name should be cleared
71 private Event<ObservableClearUserNameEvent> clearUserNameEvent;
74 * An event being fired when a user password should be cleared
78 private Event<ObservableClearUserPasswordEvent> clearUserPasswordEvent;
84 private JobsContactWebRequestController contactController;
90 private JobsFeaturesWebApplicationController featureController;
93 * Remote register session-scoped bean
95 @EJB (lookup = "java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote")
96 private UserRegistrationSessionBeanRemote registerBean;
102 private JobsUserWebRequestController userController;
105 * User list controller
108 private JobsUserListWebViewController userListController;
111 * An event being fired when a user password was changed
115 private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
118 * An event being fired when a new user has registered
122 private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
125 * Default constructor
127 public JobsUserRegisterWebRequestBean () {
128 // Call super constructor
133 * Registers the user, if not found. Otherwise this method should throw an
136 * @return Redirection target
138 public String doFinishRegistration () {
139 // Is registration enabled?
140 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
142 throw new FaceletException("Registration is disabled."); //NOI18N
146 final User user = this.userController.createUserInstance(true);
148 // Null random password means registration requires user-entered password
149 String randomPassword = null;
151 // Is the user already used?
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.userListController.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));
164 // May happen here, fire event
165 this.clearUserNameEvent.fire(new ClearUserNameEvent());
168 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
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));
177 // May happen here, reset fields
178 this.contactController.clearEmailAddresses();
179 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
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
188 // May happen here, reset fields
189 this.contactController.clearEmailAddresses();
190 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING", FacesMessage.SEVERITY_INFO); //NOI18N
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);
202 // Generate (ignored) password-history
203 final PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
206 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
211 final String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
214 user.setUserEncryptedPassword(encryptedPassword);
216 // Is developer mode?
217 if (this.isDebugModeEnabled("register")) { //NOI18N
218 // For debugging/programming only:
219 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
221 // No debugging of this part
222 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
224 // Ask EJB for generating a not-existing confirmation key
225 final String confirmKey = this.registerBean.generateConfirmationKey(user);
228 user.setUserConfirmKey(confirmKey);
233 final String baseUrl = FacesUtils.generateBaseUrl();
236 final User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
238 // The id number should be set
239 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
242 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
244 // All fine, redirect to proper page
245 return "user_register_done"; //NOI18N
246 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
248 throw new FaceletException(ex);
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
259 public String doRegisterMultiPage1 () {
260 // Is registration enabled?
261 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
263 throw new FaceletException("Registration is disabled."); //NOI18N
267 final User user = this.userController.createUserInstance(false);
269 // First check if user is not null and user name is not used + if same email address is entered
272 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
273 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(user))) { //NOI18N
274 // User name is already used, so clear it
275 this.clearUserNameEvent.fire(new ClearUserNameEvent());
278 this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED", FacesMessage.SEVERITY_WARN); //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", FacesMessage.SEVERITY_WARN); //NOI18N
285 } else if (!this.userController.isSamePasswordEntered()) {
286 // Is multi-page enabled?
287 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
289 this.clearUserPasswordEvent.fire(new ClearUserPasswordEvent());
291 // Output faces message
292 this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY", FacesMessage.SEVERITY_WARN); //NOI18N
293 this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY", FacesMessage.SEVERITY_WARN); //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);
299 // Generate (ignored) password-history
300 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
303 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
307 // Create half contact instance with email address
308 final Contact contact = new UserContact();
309 contact.setContactEmailAddress(this.contactController.getEmailAddress());
311 // Set contact in user
312 user.setUserContact(contact);
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", FacesMessage.SEVERITY_WARN); //NOI18N
322 // Now only redirect to next page as the JSF does it
323 return "user_register_page2"; //NOI18N