]> 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 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;
47
48 /**
49  * A web bean for user registration
50  * <p>
51  * @author Roland Häder<roland@mxchange.org>
52  */
53 @Named ("userRegistrationController")
54 @SessionScoped
55 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
56
57         /**
58          * Serial number
59          */
60         private static final long serialVersionUID = 47_828_986_719_691_592L;
61
62         /**
63          * User controller
64          */
65         @Inject
66         private JobsAdminUserWebRequestController adminUserController;
67
68         /**
69          * Contact controller
70          */
71         @Inject
72         private JobsContactWebSessionController contactController;
73
74         /**
75          * Features controller
76          */
77         @Inject
78         private JobsFeaturesWebApplicationController featureController;
79
80         /**
81          * Remote register session-scoped bean
82          */
83         private UserRegistrationSessionBeanRemote registerBean;
84
85         /**
86          * User controller
87          */
88         @Inject
89         private JobsUserWebSessionController userController;
90
91         /**
92          * An en event fireable when a new user has registered
93          */
94         @Inject
95         @Any
96         private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
97
98         /**
99          * Default constructor
100          */
101         public JobsUserRegisterWebSessionBean () {
102                 // Call super constructor
103                 super();
104         }
105
106         @Override
107         public String doFinishRegistration () {
108                 // Is registration enabled?
109                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
110                         // Is not enabled
111                         throw new FaceletException("Registration is disabled."); //NOI18N
112                 }
113
114                 // Get user instance
115                 User user = this.userController.createUserInstance(true);
116
117                 // Null random password means registration requires user-entered password
118                 String randomPassword = null;
119
120                 // Is the user already used?
121                 if (null == user) {
122                         // user must be set
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_login_require_user_name")) && (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));
132                         } else {
133                                 // May happen here, reset field
134                                 this.userController.setUserName(null);
135                                 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
136                                 return ""; //NOI18N
137                         }
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));
143                         } else {
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
148                                 return ""; //NOI18N
149                         }
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
155                         } else {
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
160                                 return ""; //NOI18N
161                         }
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);
170
171                                 // Set it in both fields
172                                 this.userController.setUserPassword(randomPassword);
173                                 this.userController.setUserPasswordRepeat(randomPassword);
174                         }
175                 }
176
177                 // Encrypt password
178                 String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
179
180                 // Set it here
181                 user.setUserEncryptedPassword(encryptedPassword);
182
183                 // Is developer mode?
184                 if (this.isDebugModeEnabled("register")) { //NOI18N
185                         // For debugging/programming only:
186                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
187                 } else {
188                         // No debugging of this part
189                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
190
191                         // Ask EJB for generating a not-existing confirmation key
192                         String confirmKey = this.registerBean.generateConfirmationKey(user);
193
194                         // Set it in user
195                         user.setUserConfirmKey(confirmKey);
196                 }
197
198                 try {
199                         // Get base URL
200                         String baseUrl = FacesUtils.generateBaseUrl();
201
202                         // Call bean
203                         User registeredUser = this.registerBean.registerUser(user, baseUrl, randomPassword);
204
205                         // The id number should be set
206                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
207
208                         // Fire event
209                         this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
210
211                         // All fine, redirect to proper page
212                         return "user_register_done"; //NOI18N
213                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
214                         // Continue to throw
215                         throw new FaceletException(ex);
216                 }
217         }
218
219         @Override
220         public String doRegisterMultiPage1 () {
221                 // Is registration enabled?
222                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
223                         // Is not enabled
224                         throw new FaceletException("Registration is disabled."); //NOI18N
225                 }
226
227                 // Get user instance
228                 User user = this.userController.createUserInstance(false);
229
230                 // First check if user is not null and user name is not used + if same email address is entered
231                 if (null == user) {
232                         // user must be set
233                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
234                 } else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (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
238                         return ""; //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
244                         return ""; //NOI18N
245                 } else if (!this.userController.isSamePasswordEntered()) {
246                         // Is multi-page enabled?
247                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
248                                 // Unset both
249                                 this.userController.setUserPassword(null);
250                                 this.userController.setUserPasswordRepeat(null);
251
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
255                                 return ""; //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);
259
260                                 // Set it in both fields
261                                 this.userController.setUserPassword(randomPassword);
262                                 this.userController.setUserPasswordRepeat(randomPassword);
263                         }
264                 }
265
266                 // Create half contact instance with email address
267                 Contact contact = new UserContact();
268                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
269
270                 // Set contact in user
271                 user.setUserContact(contact);
272
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
279                         return ""; //NOI18N
280                 }
281
282                 // Now only redirect to next page as the JSF does it
283                 return "user_register_page2"; //NOI18N
284         }
285
286         /**
287          * Post-construction method
288          */
289         @PostConstruct
290         public void init () {
291                 try {
292                         // Get initial context
293                         Context context = new InitialContext();
294
295                         // Try to lookup
296                         this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote"); //NOI18N
297                 } catch (final NamingException ex) {
298                         // Continue to throw
299                         throw new FaceletException(ex);
300                 }
301         }
302
303 }