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;
19 import javax.annotation.PostConstruct;
20 import javax.enterprise.context.SessionScoped;
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 javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jcontacts.contact.UserContact;
31 import org.mxchange.jcoreee.utils.FacesUtils;
32 import org.mxchange.jjobs.beans.BaseJobsController;
33 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
34 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
35 import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController;
36 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
37 import org.mxchange.jusercore.events.user.clear.password.ClearUserPasswordEvent;
38 import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent;
39 import org.mxchange.jusercore.events.user.clear.username.ClearUserNameEvent;
40 import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent;
41 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
42 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
43 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
44 import org.mxchange.jusercore.model.user.User;
45 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
46 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
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;
56 * A web bean for user registration
58 * @author Roland Häder<roland@mxchange.org>
60 @Named ("userRegistrationController")
62 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
67 private static final long serialVersionUID = 47_828_986_719_691_592L;
73 private JobsAdminUserWebRequestController adminUserController;
79 private JobsContactWebSessionController contactController;
85 private JobsFeaturesWebApplicationController featureController;
88 * Remote register session-scoped bean
90 private UserRegistrationSessionBeanRemote registerBean;
96 private JobsUserWebSessionController userController;
99 * An event being fired when a user password was changed
103 private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
106 * An event being fired when a user name should be cleared
110 private Event<ObservableClearUserNameEvent> clearUserNameEvent;
113 * An event being fired when a user password should be cleared
117 private Event<ObservableClearUserPasswordEvent> clearUserPasswordEvent;
120 * An event being fired when a new user has registered
124 private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
127 * Default constructor
129 public JobsUserRegisterWebSessionBean () {
130 // Call super constructor
135 * Registers the user, if not found. Otherwise this method should throw an
138 * @return Redirection target
140 public String doFinishRegistration () {
141 // Is registration enabled?
142 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
144 throw new FaceletException("Registration is disabled."); //NOI18N
148 User user = this.userController.createUserInstance(true);
150 // Null random password means registration requires user-entered password
151 String randomPassword = null;
153 // Is the user already used?
156 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
157 } else if (!this.userController.isRequiredPersonalDataSet()) {
158 // Not all required fields are set
159 throw new FaceletException("Not all required fields are set."); //NOI18N
160 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
161 // Is multi-page enabled?
162 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
163 // User name is already used, should not happen here
164 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
166 // May happen here, fire event
167 this.clearUserNameEvent.fire(new ClearUserNameEvent());
170 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
173 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
174 // Is multi-page enabled?
175 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
176 // Email address has already been taken, should not happen here
177 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
179 // May happen here, reset fields
180 this.contactController.clearEmailAddresses();
181 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
184 } else if (!this.contactController.isSameEmailAddressEntered()) {
185 // Is multi-page enabled?
186 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
187 // Not same email address entered, should not happen here
188 throw new FaceletException(new DataRepeatMismatchException("Email addresses not matching.")); //NOI18N
190 // May happen here, reset fields
191 this.contactController.clearEmailAddresses();
192 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
195 } else if (!this.userController.isSamePasswordEntered()) {
196 // Is multi-page enabled?
197 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
198 // Not same password entered, should no longer happen here
199 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
200 } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
201 // Both passwords are left empty and is allowed, then generate a random password
202 randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
204 // Generate (ignored) password-history
205 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
208 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
213 String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
216 user.setUserEncryptedPassword(encryptedPassword);
218 // Is developer mode?
219 if (this.isDebugModeEnabled("register")) { //NOI18N
220 // For debugging/programming only:
221 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
223 // No debugging of this part
224 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
226 // Ask EJB for generating a not-existing confirmation key
227 String confirmKey = this.registerBean.generateConfirmationKey(user);
230 user.setUserConfirmKey(confirmKey);
235 String baseUrl = FacesUtils.generateBaseUrl();
238 User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
240 // The id number should be set
241 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
244 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
246 // All fine, redirect to proper page
247 return "user_register_done"; //NOI18N
248 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
250 throw new FaceletException(ex);
255 * Handles registration request send from first page. The (maybe) entered
256 * user name and email address is not used and that privacy and T&C are
261 public String doRegisterMultiPage1 () {
262 // Is registration enabled?
263 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
265 throw new FaceletException("Registration is disabled."); //NOI18N
269 User user = this.userController.createUserInstance(false);
271 // First check if user is not null and user name is not used + if same email address is entered
274 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
275 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
276 // User name is already used, so clear it
277 this.clearUserNameEvent.fire(new ClearUserNameEvent());
280 this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
282 } else if (!this.contactController.isSameEmailAddressEntered()) {
283 // Not same email address entered, clear both
284 this.contactController.clearEmailAddresses();
285 this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
287 } else if (!this.userController.isSamePasswordEntered()) {
288 // Is multi-page enabled?
289 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
291 this.clearUserPasswordEvent.fire(new ClearUserPasswordEvent());
293 // Output faces message
294 this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY"); //NOI18N
295 this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY"); //NOI18N
297 } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
298 // Both passwords are left empty and is allowed, then generate a random password
299 String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
301 // Generate (ignored) password-history
302 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
305 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
309 // Create half contact instance with email address
310 Contact contact = new UserContact();
311 contact.setContactEmailAddress(this.contactController.getEmailAddress());
313 // Set contact in user
314 user.setUserContact(contact);
316 // Check if email address is registered
317 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
318 // Email address has already been taken, clear both
319 this.contactController.clearEmailAddresses();
320 this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
324 // Now only redirect to next page as the JSF does it
325 return "user_register_page2"; //NOI18N
329 * Post-construction method
332 public void init () {
334 // Get initial context
335 Context context = new InitialContext();
338 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote"); //NOI18N
339 } catch (final NamingException ex) {
341 throw new FaceletException(ex);