2 * Copyright (C) 2016, 2017 Roland Häder
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.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;
52 * A web bean for user registration
54 * @author Roland Häder<roland@mxchange.org>
56 @Named ("userRegistrationController")
58 public class JobsUserRegisterWebRequestBean extends BaseJobsBean implements JobsUserRegisterWebRequestController {
63 private static final long serialVersionUID = 47_828_986_719_691_592L;
66 * An event being fired when a user name should be cleared
70 private Event<ObservableClearUserNameEvent> clearUserNameEvent;
73 * An event being fired when a user password should be cleared
77 private Event<ObservableClearUserPasswordEvent> clearUserPasswordEvent;
83 private JobsContactWebRequestController contactController;
89 private JobsFeaturesWebApplicationController featureController;
92 * Remote register session-scoped bean
94 @EJB (lookup = "java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote")
95 private UserRegistrationSessionBeanRemote registerBean;
101 private JobsUserWebRequestController userController;
104 * An event being fired when a user password was changed
108 private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
111 * An event being fired when a new user has registered
115 private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
118 * Default constructor
120 public JobsUserRegisterWebRequestBean () {
121 // Call super constructor
126 * Registers the user, if not found. Otherwise this method should throw an
129 * @return Redirection target
131 public String doFinishRegistration () {
132 // Is registration enabled?
133 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
135 throw new FaceletException("Registration is disabled."); //NOI18N
139 final User user = this.userController.createUserInstance(true);
141 // Null random password means registration requires user-entered password
142 String randomPassword = null;
144 // Is the user already used?
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));
157 // May happen here, fire event
158 this.clearUserNameEvent.fire(new ClearUserNameEvent());
161 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
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));
170 // May happen here, reset fields
171 this.contactController.clearEmailAddresses();
172 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
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
181 // May happen here, reset fields
182 this.contactController.clearEmailAddresses();
183 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
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);
195 // Generate (ignored) password-history
196 final PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
199 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
204 final String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
207 user.setUserEncryptedPassword(encryptedPassword);
209 // Is developer mode?
210 if (this.isDebugModeEnabled("register")) { //NOI18N
211 // For debugging/programming only:
212 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
214 // No debugging of this part
215 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
217 // Ask EJB for generating a not-existing confirmation key
218 final String confirmKey = this.registerBean.generateConfirmationKey(user);
221 user.setUserConfirmKey(confirmKey);
226 final String baseUrl = FacesUtils.generateBaseUrl();
229 final User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
231 // The id number should be set
232 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
235 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
237 // All fine, redirect to proper page
238 return "user_register_done"; //NOI18N
239 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
241 throw new FaceletException(ex);
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
252 public String doRegisterMultiPage1 () {
253 // Is registration enabled?
254 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
256 throw new FaceletException("Registration is disabled."); //NOI18N
260 final User user = this.userController.createUserInstance(false);
262 // First check if user is not null and user name is not used + if same email address is entered
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());
271 this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //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
278 } else if (!this.userController.isSamePasswordEntered()) {
279 // Is multi-page enabled?
280 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
282 this.clearUserPasswordEvent.fire(new ClearUserPasswordEvent());
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
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);
292 // Generate (ignored) password-history
293 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
296 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
300 // Create half contact instance with email address
301 final Contact contact = new UserContact();
302 contact.setContactEmailAddress(this.contactController.getEmailAddress());
304 // Set contact in user
305 user.setUserContact(contact);
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
315 // Now only redirect to next page as the JSF does it
316 return "user_register_page2"; //NOI18N