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 java.text.MessageFormat;
20 import javax.annotation.PostConstruct;
21 import javax.enterprise.context.SessionScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcontacts.contact.Contact;
31 import org.mxchange.jcontacts.contact.UserContact;
32 import org.mxchange.jcoreee.utils.FacesUtils;
33 import org.mxchange.jjobs.beans.BaseJobsController;
34 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
35 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
36 import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController;
37 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
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.status.UserAccountStatus;
43 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
44 import org.mxchange.juserlogincore.events.registration.UserRegisteredEvent;
45 import org.mxchange.juserlogincore.login.UserLoginUtils;
46 import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote;
49 * A web bean for user registration
51 * @author Roland Häder<roland@mxchange.org>
53 @Named ("userRegistrationController")
55 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
60 private static final long serialVersionUID = 47_828_986_719_691_592L;
66 private JobsAdminUserWebRequestController adminUserController;
72 private JobsContactWebSessionController contactController;
78 private JobsFeaturesWebApplicationController featureController;
81 * Remote register session-scoped bean
83 private UserRegistrationSessionBeanRemote registerBean;
89 private JobsUserWebSessionController userController;
92 * An en event fireable when a new user has registered
96 private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
101 public JobsUserRegisterWebSessionBean () {
102 // Call super constructor
107 public String doFinishRegistration () {
108 // Is registration enabled?
109 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
111 throw new FaceletException("Registration is disabled."); //NOI18N
115 User user = this.userController.createUserInstance(true);
117 // Null random password means registration requires user-entered password
118 String randomPassword = null;
120 // Is the user already used?
123 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
124 } else if (!this.userController.isRequiredPersonalDataSet()) {
125 // Not all required fields are set
126 throw new FaceletException("Not all required fields are set."); //NOI18N
127 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
128 // Is multi-page enabled?
129 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
130 // User name is already used, should not happen here
131 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
133 // May happen here, reset field
134 this.userController.setUserName(null);
135 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
138 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
139 // Is multi-page enabled?
140 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
141 // Email address has already been taken, should not happen here
142 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
144 // May happen here, reset fields
145 this.contactController.setEmailAddress(null);
146 this.contactController.setEmailAddressRepeat(null);
147 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
150 } else if (!this.contactController.isSameEmailAddressEntered()) {
151 // Is multi-page enabled?
152 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
153 // Not same email address entered, should not happen here
154 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
156 // May happen here, reset fields
157 this.contactController.setEmailAddress(null);
158 this.contactController.setEmailAddressRepeat(null);
159 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
162 } else if (!this.userController.isSamePasswordEntered()) {
163 // Is multi-page enabled?
164 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
165 // Not same password entered, should no longer happen here
166 throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
167 } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
168 // Both passwords are left empty and is allowed, then generate a random password
169 randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
171 // Set it in both fields
172 this.userController.setUserPassword(randomPassword);
173 this.userController.setUserPasswordRepeat(randomPassword);
178 String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
181 user.setUserEncryptedPassword(encryptedPassword);
183 // Is developer mode?
184 if (this.isDebugModeEnabled("register")) { //NOI18N
185 // For debugging/programming only:
186 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
188 // No debugging of this part
189 user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
191 // Ask EJB for generating a not-existing confirmation key
192 String confirmKey = this.registerBean.generateConfirmationKey(user);
195 user.setUserConfirmKey(confirmKey);
200 String baseUrl = FacesUtils.generateBaseUrl();
203 User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
205 // The id number should be set
206 assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
209 this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
211 // All fine, redirect to proper page
212 return "user_register_done"; //NOI18N
213 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
215 throw new FaceletException(ex);
220 public String doRegisterMultiPage1 () {
221 // Is registration enabled?
222 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
224 throw new FaceletException("Registration is disabled."); //NOI18N
228 User user = this.userController.createUserInstance(false);
230 // First check if user is not null and user name is not used + if same email address is entered
233 throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
234 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
235 // User name is already used, so clear it
236 this.userController.setUserName(null);
237 this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
239 } else if (!this.contactController.isSameEmailAddressEntered()) {
240 // Not same email address entered, clear both
241 this.contactController.setEmailAddress(null);
242 this.contactController.setEmailAddressRepeat(null);
243 this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
245 } else if (!this.userController.isSamePasswordEntered()) {
246 // Is multi-page enabled?
247 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
249 this.userController.setUserPassword(null);
250 this.userController.setUserPasswordRepeat(null);
252 // Output faces message
253 this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY"); //NOI18N
254 this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY"); //NOI18N
256 } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
257 // Both passwords are left empty and is allowed, then generate a random password
258 String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
260 // Set it in both fields
261 this.userController.setUserPassword(randomPassword);
262 this.userController.setUserPasswordRepeat(randomPassword);
266 // Create half contact instance with email address
267 Contact contact = new UserContact();
268 contact.setContactEmailAddress(this.contactController.getEmailAddress());
270 // Set contact in user
271 user.setUserContact(contact);
273 // Check if email address is registered
274 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
275 // Email address has already been taken, clear both
276 this.contactController.setEmailAddress(null);
277 this.contactController.setEmailAddressRepeat(null);
278 this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
282 // Now only redirect to next page as the JSF does it
283 return "user_register_page2"; //NOI18N
287 * Post-construction method
290 public void init () {
292 // Get initial context
293 Context context = new InitialContext();
296 this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userRegistration!org.mxchange.jusercore.model.user.register.UserRegistrationSessionBeanRemote"); //NOI18N
297 } catch (final NamingException ex) {
299 throw new FaceletException(ex);