]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/register/JobsUserRegisterWebSessionBean.java
Please cherry-pick:
[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.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;
54
55 /**
56  * A web bean for user registration
57  * <p>
58  * @author Roland Häder<roland@mxchange.org>
59  */
60 @Named ("userRegistrationController")
61 @SessionScoped
62 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
63
64         /**
65          * Serial number
66          */
67         private static final long serialVersionUID = 47_828_986_719_691_592L;
68
69         /**
70          * User controller
71          */
72         @Inject
73         private JobsAdminUserWebRequestController adminUserController;
74
75         /**
76          * Contact controller
77          */
78         @Inject
79         private JobsContactWebSessionController contactController;
80
81         /**
82          * Features controller
83          */
84         @Inject
85         private JobsFeaturesWebApplicationController featureController;
86
87         /**
88          * Remote register session-scoped bean
89          */
90         private UserRegistrationSessionBeanRemote registerBean;
91
92         /**
93          * User controller
94          */
95         @Inject
96         private JobsUserWebSessionController userController;
97
98         /**
99          * An event being fired when a user password was changed
100          */
101         @Inject
102         @Any
103         private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
104
105         /**
106          * An event being fired when a user name should be cleared
107          */
108         @Inject
109         @Any
110         private Event<ObservableClearUserNameEvent> clearUserNameEvent;
111
112         /**
113          * An event being fired when a user password should be cleared
114          */
115         @Inject
116         @Any
117         private Event<ObservableClearUserPasswordEvent> clearUserPasswordEvent;
118
119         /**
120          * An event being fired when a new user has registered
121          */
122         @Inject
123         @Any
124         private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
125
126         /**
127          * Default constructor
128          */
129         public JobsUserRegisterWebSessionBean () {
130                 // Call super constructor
131                 super();
132         }
133
134         /**
135          * Registers the user, if not found. Otherwise this method should throw an
136          * exception.
137          * <p>
138          * @return Redirection target
139          */
140         public String doFinishRegistration () {
141                 // Is registration enabled?
142                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
143                         // Is not enabled
144                         throw new FaceletException("Registration is disabled."); //NOI18N
145                 }
146
147                 // Get user instance
148                 User user = this.userController.createUserInstance(true);
149
150                 // Null random password means registration requires user-entered password
151                 String randomPassword = null;
152
153                 // Is the user already used?
154                 if (null == user) {
155                         // user must be set
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));
165                         } else {
166                                 // May happen here, fire event
167                                 this.clearUserNameEvent.fire(new ClearUserNameEvent());
168
169                                 // Output message
170                                 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
171                                 return ""; //NOI18N
172                         }
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));
178                         } else {
179                                 // May happen here, reset fields
180                                 this.contactController.clearEmailAddresses();
181                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
182                                 return ""; //NOI18N
183                         }
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
189                         } else {
190                                 // May happen here, reset fields
191                                 this.contactController.clearEmailAddresses();
192                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
193                                 return ""; //NOI18N
194                         }
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);
203
204                                 // Generate (ignored) password-history
205                                 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
206
207                                 // Fire event
208                                 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
209                         }
210                 }
211
212                 // Encrypt password
213                 String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
214
215                 // Set it here
216                 user.setUserEncryptedPassword(encryptedPassword);
217
218                 // Is developer mode?
219                 if (this.isDebugModeEnabled("register")) { //NOI18N
220                         // For debugging/programming only:
221                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
222                 } else {
223                         // No debugging of this part
224                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
225
226                         // Ask EJB for generating a not-existing confirmation key
227                         String confirmKey = this.registerBean.generateConfirmationKey(user);
228
229                         // Set it in user
230                         user.setUserConfirmKey(confirmKey);
231                 }
232
233                 try {
234                         // Get base URL
235                         String baseUrl = FacesUtils.generateBaseUrl();
236
237                         // Call bean
238                         User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
239
240                         // The id number should be set
241                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
242
243                         // Fire event
244                         this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
245
246                         // All fine, redirect to proper page
247                         return "user_register_done"; //NOI18N
248                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
249                         // Continue to throw
250                         throw new FaceletException(ex);
251                 }
252         }
253
254         /**
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
257          * accepted.
258          * <p>
259          * @return Redirect
260          */
261         public String doRegisterMultiPage1 () {
262                 // Is registration enabled?
263                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
264                         // Is not enabled
265                         throw new FaceletException("Registration is disabled."); //NOI18N
266                 }
267
268                 // Get user instance
269                 User user = this.userController.createUserInstance(false);
270
271                 // First check if user is not null and user name is not used + if same email address is entered
272                 if (null == user) {
273                         // user must be set
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());
278
279                         // Output message
280                         this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
281                         return ""; //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
286                         return ""; //NOI18N
287                 } else if (!this.userController.isSamePasswordEntered()) {
288                         // Is multi-page enabled?
289                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
290                                 // Unset both
291                                 this.clearUserPasswordEvent.fire(new ClearUserPasswordEvent());
292
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
296                                 return ""; //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);
300
301                                 // Generate (ignored) password-history
302                                 PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
303
304                                 // Fire event
305                                 this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
306                         }
307                 }
308
309                 // Create half contact instance with email address
310                 Contact contact = new UserContact();
311                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
312
313                 // Set contact in user
314                 user.setUserContact(contact);
315
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
321                         return ""; //NOI18N
322                 }
323
324                 // Now only redirect to next page as the JSF does it
325                 return "user_register_page2"; //NOI18N
326         }
327
328         /**
329          * Post-construction method
330          */
331         @PostConstruct
332         public void init () {
333                 try {
334                         // Get initial context
335                         Context context = new InitialContext();
336
337                         // Try to lookup
338                         this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote"); //NOI18N
339                 } catch (final NamingException ex) {
340                         // Continue to throw
341                         throw new FaceletException(ex);
342                 }
343         }
344
345 }