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.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 JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
63 private static final long serialVersionUID = 47_828_986_719_691_592L;
69 private JobsAdminUserWebRequestController adminUserController;
75 private JobsContactWebSessionController contactController;
81 private JobsFeaturesWebApplicationController featureController;
84 * Remote register session-scoped bean
86 private UserRegistrationSessionBeanRemote registerBean;
92 private JobsUserWebSessionController userController;
95 * An event being fired when a user password was changed
99 private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
102 * An event being fired when a new user has registered
106 private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
109 * Default constructor
111 public JobsUserRegisterWebSessionBean () {
112 // Call super constructor
117 * Registers the user, if not found. Otherwise this method should throw an
120 * @return Redirection target
122 public String doFinishRegistration () {
123 // Is registration enabled?
124 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
126 throw new FaceletException("Registration is disabled."); //NOI18N
130 User user = this.userController.createUserInstance(true);
132 // Null random password means registration requires user-entered password
133 String randomPassword = null;
135 // Is the user already used?
138 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
139 } else if (!this.userController.isRequiredPersonalDataSet()) {
140 // Not all required fields are set
141 throw new FaceletException("Not all required fields are set."); //NOI18N
142 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
143 // Is multi-page enabled?
144 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
145 // User name is already used, should not happen here
146 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
148 // May happen here, reset field
149 this.userController.clearUserName();
150 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
153 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
154 // Is multi-page enabled?
155 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
156 // Email address has already been taken, should not happen here
157 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
159 // May happen here, reset fields
160 this.contactController.clearEmailAddresses();
161 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
164 } else if (!this.contactController.isSameEmailAddressEntered()) {
165 // Is multi-page enabled?
166 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
167 // Not same email address entered, should not happen here
168 throw new FaceletException(new DataRepeatMismatchException("Email addresses not matching.")); //NOI18N
170 // May happen here, reset fields
171 this.contactController.clearEmailAddresses();
172 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
175 } else if (!this.userController.isSamePasswordEntered()) {
176 // Is multi-page enabled?
177 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
178 // Not same password entered, should no longer happen here
179 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
180 } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
181 // Both passwords are left empty and is allowed, then generate a random password
182 randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
184 // Generate (ignored) password-history
185 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
188 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
193 String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
196 user.setUserEncryptedPassword(encryptedPassword);
198 // Is developer mode?
199 if (this.isDebugModeEnabled("register")) { //NOI18N
200 // For debugging/programming only:
201 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
203 // No debugging of this part
204 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
206 // Ask EJB for generating a not-existing confirmation key
207 String confirmKey = this.registerBean.generateConfirmationKey(user);
210 user.setUserConfirmKey(confirmKey);
215 String baseUrl = FacesUtils.generateBaseUrl();
218 User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
220 // The id number should be set
221 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
224 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
226 // All fine, redirect to proper page
227 return "user_register_done"; //NOI18N
228 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
230 throw new FaceletException(ex);
235 * Handles registration request send from first page. The (maybe) entered
236 * user name and email address is not used and that privacy and T&C are
241 public String doRegisterMultiPage1 () {
242 // Is registration enabled?
243 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
245 throw new FaceletException("Registration is disabled."); //NOI18N
249 User user = this.userController.createUserInstance(false);
251 // First check if user is not null and user name is not used + if same email address is entered
254 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
255 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
256 // User name is already used, so clear it
257 this.userController.clearUserName();
258 this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
260 } else if (!this.contactController.isSameEmailAddressEntered()) {
261 // Not same email address entered, clear both
262 this.contactController.clearEmailAddresses();
263 this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
265 } else if (!this.userController.isSamePasswordEntered()) {
266 // Is multi-page enabled?
267 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
269 this.userController.clearUserPasswords();
271 // Output faces message
272 this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY"); //NOI18N
273 this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY"); //NOI18N
275 } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
276 // Both passwords are left empty and is allowed, then generate a random password
277 String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
279 // Generate (ignored) password-history
280 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
283 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
287 // Create half contact instance with email address
288 Contact contact = new UserContact();
289 contact.setContactEmailAddress(this.contactController.getEmailAddress());
291 // Set contact in user
292 user.setUserContact(contact);
294 // Check if email address is registered
295 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
296 // Email address has already been taken, clear both
297 this.contactController.clearEmailAddresses();
298 this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
302 // Now only redirect to next page as the JSF does it
303 return "user_register_page2"; //NOI18N
307 * Post-construction method
310 public void init () {
312 // Get initial context
313 Context context = new InitialContext();
316 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote"); //NOI18N
317 } catch (final NamingException ex) {
319 throw new FaceletException(ex);