]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/register/JobsUserRegisterWebSessionBean.java
dff3b2cc2e71f32438c69266390c98310c4aba01
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / register / JobsUserRegisterWebSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.beans.user.register;
18
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;
50
51 /**
52  * A web bean for user registration
53  * <p>
54  * @author Roland Häder<roland@mxchange.org>
55  */
56 @Named ("userRegistrationController")
57 @SessionScoped
58 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
59
60         /**
61          * Serial number
62          */
63         private static final long serialVersionUID = 47_828_986_719_691_592L;
64
65         /**
66          * User controller
67          */
68         @Inject
69         private JobsAdminUserWebRequestController adminUserController;
70
71         /**
72          * Contact controller
73          */
74         @Inject
75         private JobsContactWebSessionController contactController;
76
77         /**
78          * Features controller
79          */
80         @Inject
81         private JobsFeaturesWebApplicationController featureController;
82
83         /**
84          * Remote register session-scoped bean
85          */
86         private UserRegistrationSessionBeanRemote registerBean;
87
88         /**
89          * User controller
90          */
91         @Inject
92         private JobsUserWebSessionController userController;
93
94         /**
95          * An event being fired when a user password was changed
96          */
97         @Inject
98         @Any
99         private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
100
101         /**
102          * An event being fired when a new user has registered
103          */
104         @Inject
105         @Any
106         private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
107
108         /**
109          * Default constructor
110          */
111         public JobsUserRegisterWebSessionBean () {
112                 // Call super constructor
113                 super();
114         }
115
116         /**
117          * Registers the user, if not found. Otherwise this method should throw an
118          * exception.
119          * <p>
120          * @return Redirection target
121          */
122         public String doFinishRegistration () {
123                 // Is registration enabled?
124                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
125                         // Is not enabled
126                         throw new FaceletException("Registration is disabled."); //NOI18N
127                 }
128
129                 // Get user instance
130                 User user = this.userController.createUserInstance(true);
131
132                 // Null random password means registration requires user-entered password
133                 String randomPassword = null;
134
135                 // Is the user already used?
136                 if (null == user) {
137                         // user must be set
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));
147                         } else {
148                                 // May happen here, reset field
149                                 this.userController.clearUserName();
150                                 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
151                                 return ""; //NOI18N
152                         }
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));
158                         } else {
159                                 // May happen here, reset fields
160                                 this.contactController.clearEmailAddresses();
161                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
162                                 return ""; //NOI18N
163                         }
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
169                         } else {
170                                 // May happen here, reset fields
171                                 this.contactController.clearEmailAddresses();
172                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
173                                 return ""; //NOI18N
174                         }
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);
183
184                                 // Generate (ignored) password-history
185                                 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
186
187                                 // Fire event
188                                 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
189                         }
190                 }
191
192                 // Encrypt password
193                 String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
194
195                 // Set it here
196                 user.setUserEncryptedPassword(encryptedPassword);
197
198                 // Is developer mode?
199                 if (this.isDebugModeEnabled("register")) { //NOI18N
200                         // For debugging/programming only:
201                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
202                 } else {
203                         // No debugging of this part
204                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
205
206                         // Ask EJB for generating a not-existing confirmation key
207                         String confirmKey = this.registerBean.generateConfirmationKey(user);
208
209                         // Set it in user
210                         user.setUserConfirmKey(confirmKey);
211                 }
212
213                 try {
214                         // Get base URL
215                         String baseUrl = FacesUtils.generateBaseUrl();
216
217                         // Call bean
218                         User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
219
220                         // The id number should be set
221                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
222
223                         // Fire event
224                         this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
225
226                         // All fine, redirect to proper page
227                         return "user_register_done"; //NOI18N
228                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
229                         // Continue to throw
230                         throw new FaceletException(ex);
231                 }
232         }
233
234         /**
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
237          * accepted.
238          * <p>
239          * @return Redirect
240          */
241         public String doRegisterMultiPage1 () {
242                 // Is registration enabled?
243                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
244                         // Is not enabled
245                         throw new FaceletException("Registration is disabled."); //NOI18N
246                 }
247
248                 // Get user instance
249                 User user = this.userController.createUserInstance(false);
250
251                 // First check if user is not null and user name is not used + if same email address is entered
252                 if (null == user) {
253                         // user must be set
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
259                         return ""; //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
264                         return ""; //NOI18N
265                 } else if (!this.userController.isSamePasswordEntered()) {
266                         // Is multi-page enabled?
267                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
268                                 // Unset both
269                                 this.userController.clearUserPasswords();
270
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
274                                 return ""; //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);
278
279                                 // Generate (ignored) password-history
280                                 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
281
282                                 // Fire event
283                                 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
284                         }
285                 }
286
287                 // Create half contact instance with email address
288                 Contact contact = new UserContact();
289                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
290
291                 // Set contact in user
292                 user.setUserContact(contact);
293
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
299                         return ""; //NOI18N
300                 }
301
302                 // Now only redirect to next page as the JSF does it
303                 return "user_register_page2"; //NOI18N
304         }
305
306         /**
307          * Post-construction method
308          */
309         @PostConstruct
310         public void init () {
311                 try {
312                         // Get initial context
313                         Context context = new InitialContext();
314
315                         // Try to lookup
316                         this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote"); //NOI18N
317                 } catch (final NamingException ex) {
318                         // Continue to throw
319                         throw new FaceletException(ex);
320                 }
321         }
322
323 }