]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / user / FinancialsAdminUserWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
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.jfinancials.beans.user;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import java.util.Objects;
22 import javax.ejb.EJB;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.event.Observes;
26 import javax.enterprise.inject.Any;
27 import javax.faces.FacesException;
28 import javax.faces.application.FacesMessage;
29 import javax.faces.context.FacesContext;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontacts.model.contact.Contact;
33 import org.mxchange.jcoreee.utils.FacesUtils;
34 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
35 import org.mxchange.jfinancials.beans.contact.FinancialsAdminContactWebRequestController;
36 import org.mxchange.jfinancials.beans.contact.FinancialsContactWebRequestController;
37 import org.mxchange.jfinancials.beans.features.FinancialsFeaturesWebApplicationController;
38 import org.mxchange.jfinancials.beans.helper.FinancialsWebViewHelperController;
39 import org.mxchange.jfinancials.beans.user.list.FinancialsUserListWebViewController;
40 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
41 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
42 import org.mxchange.jusercore.events.user.delete.AdminDeletedUserEvent;
43 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
44 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
45 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
46 import org.mxchange.jusercore.events.user.locked.AdminLockedUserEvent;
47 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
48 import org.mxchange.jusercore.events.user.unlocked.AdminUnlockedUserEvent;
49 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
50 import org.mxchange.jusercore.events.user.update.post.AdminPostUserDataUpdatedEvent;
51 import org.mxchange.jusercore.events.user.update.post.ObservableAdminPostUserDataUpdatedEvent;
52 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
53 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
54 import org.mxchange.jusercore.exceptions.UserNotFoundException;
55 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
56 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
57 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
58 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
59 import org.mxchange.jusercore.model.user.LoginUser;
60 import org.mxchange.jusercore.model.user.User;
61 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
62 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
63 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
64 import org.mxchange.juserlogincore.container.login.UserLoginContainer;
65 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
66 import org.mxchange.juserlogincore.exceptions.UserPasswordRepeatMismatchException;
67 import org.mxchange.juserlogincore.utils.UserLoginUtils;
68
69 /**
70  * A user bean (controller)
71  * <p>
72  * @author Roland Häder<roland@mxchange.org>
73  */
74 @Named ("adminUserController")
75 @RequestScoped
76 public class FinancialsAdminUserWebRequestBean extends BaseFinancialsBean implements FinancialsAdminUserWebRequestController {
77
78         /**
79          * Serial number
80          */
81         private static final long serialVersionUID = 542_145_356_001L;
82
83         /**
84          * An event fired when the administrator has added a new user
85          */
86         @Inject
87         @Any
88         private Event<ObservableAdminAddedUserEvent> addedUserEvent;
89
90         /**
91          * Regular contact controller
92          */
93         @Inject
94         private FinancialsAdminContactWebRequestController adminContactController;
95
96         /**
97          * Administrative user EJB
98          */
99         @EJB (lookup = "java:global/jfinancials-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote")
100         private AdminUserSessionBeanRemote adminUserBean;
101
102         /**
103          * Bean helper
104          */
105         @Inject
106         private FinancialsWebViewHelperController beanHelper;
107
108         /**
109          * Contact instance
110          */
111         private Contact contact;
112
113         /**
114          * Regular contact controller
115          */
116         @Inject
117         private FinancialsContactWebRequestController contactController;
118
119         /**
120          * Event being fired when administrator has deleted user
121          */
122         @Inject
123         @Any
124         private Event<ObservableAdminDeletedUserEvent> deleteUserEvent;
125
126         /**
127          * Features controller
128          */
129         @Inject
130         private FinancialsFeaturesWebApplicationController featureController;
131
132         /**
133          * An event fired when the administrator has updated a new user
134          */
135         @Inject
136         @Any
137         private Event<ObservableAdminPostUserDataUpdatedEvent> updatedUserDataEvent;
138
139         /**
140          * General user EJB
141          */
142         @EJB (lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
143         private UserSessionBeanRemote userBean;
144
145         /**
146          * Delete reason
147          */
148         private String userDeleteReason;
149
150         /**
151          * An event fired when the administrator has linked a user with existing
152          * contact data.
153          */
154         @Inject
155         @Any
156         private Event<ObservableAdminLinkedUserEvent> userLinkedEvent;
157
158         /**
159          * Regular user controller
160          */
161         @Inject
162         private FinancialsUserListWebViewController userListController;
163
164         /**
165          * User lock reason
166          */
167         private String userLockReason;
168
169         /**
170          * Event being fired when an administrator has locked a user
171          */
172         @Inject
173         @Any
174         private Event<ObservableAdminLockedUserEvent> userLockedEvent;
175
176         /**
177          * Flag whether user must change password after login
178          */
179         private Boolean userMustChangePassword;
180
181         /**
182          * User name
183          */
184         private String userName;
185
186         /**
187          * User password (clear-text from web form)
188          */
189         private String userPassword;
190
191         /**
192          * User password repeated (clear-text from web form)
193          */
194         private String userPasswordRepeat;
195
196         /**
197          * Whether the user wants a public profile
198          */
199         private ProfileMode userProfileMode;
200
201         /**
202          * Event being fired when administrator unlocks an account
203          */
204         @Inject
205         @Any
206         private Event<ObservableAdminUnlockedUserEvent> userUnlockedEvent;
207
208         /**
209          * Default constructor
210          */
211         public FinancialsAdminUserWebRequestBean () {
212                 // Call super constructor
213                 super();
214         }
215
216         /**
217          * Adds user instance to database by preparing a complete user instance and
218          * sending it to the EJB. The data set in the controller is being verified,
219          * e.g. if the user name or email address is not used yet.
220          * <p>
221          */
222         public void addUser () {
223                 // As the form cannot validate the data (required="true"), check it here
224                 if (this.getUserName() == null) {
225                         // Throw NPE
226                         throw new NullPointerException("userName is null"); //NOI18N
227                 } else if (this.getUserName().isEmpty()) {
228                         // Is empty
229                         throw new IllegalArgumentException("userName is null"); //NOI18N
230                 } else if (this.getContact() == null) {
231                         // No contact instance set, so test required fields: personal title, first name and family name
232                         this.adminContactController.validateContactData();
233                 }
234
235                 // Create new user instance
236                 final User newUser = this.createUserInstance();
237
238                 // Is the user name or email address used already?
239                 // @TODO Add password length check
240                 if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(newUser))) {
241                         // User name is already used
242                         throw new FacesException(new UserNameAlreadyRegisteredException(newUser));
243                 } else if ((this.getContact() == null) && (this.contactController.isEmailAddressRegistered(newUser.getUserContact()))) {
244                         // Email address is already used
245                         this.showFacesMessage("admin_add_user:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
246
247                         // Always clear password
248                         this.setUserPassword(null);
249                         this.setUserPasswordRepeat(null);
250
251                         // Skip it
252                         return;
253                 }
254
255                 // Init variable
256                 final User updatedUser;
257
258                 try {
259                         // Now, that all is set, call EJB
260                         if (this.getContact() instanceof Contact) {
261                                 // Link contact with this user
262                                 updatedUser = this.adminUserBean.linkUser(newUser);
263                         } else {
264                                 // Add new user
265                                 updatedUser = this.adminUserBean.addUser(newUser);
266                         }
267                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
268                         // Throw again
269                         throw new FacesException(ex);
270                 }
271
272                 // Now, that all is set, call EJB
273                 if (this.getContact() instanceof Contact) {
274                         // Fire event
275                         this.userLinkedEvent.fire(new AdminLinkedUserEvent(updatedUser));
276                 } else {
277                         // Fire event
278                         this.addedUserEvent.fire(new AdminAddedUserEvent(updatedUser));
279                 }
280                 // Clear helper
281                 this.setContact(null);
282
283                 // Clear this bean
284                 this.clear();
285         }
286
287         /**
288          * Event observer for new user registrations
289          * <p>
290          * @param event User registration event
291          */
292         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
293                 // Event and contained entity instance should not be null
294                 if (null == event) {
295                         // Throw NPE
296                         throw new NullPointerException("event is null"); //NOI18N
297                 } else if (event.getRegisteredUser() == null) {
298                         // Throw NPE again
299                         throw new NullPointerException("event.user is null"); //NOI18N
300                 } else if (event.getRegisteredUser().getUserId() == null) {
301                         // userId is null
302                         throw new NullPointerException("event.user.userId is null"); //NOI18N
303                 } else if (event.getRegisteredUser().getUserId() < 1) {
304                         // Not avalid id
305                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
306                 }
307
308                 // Get user instance
309                 final User registeredUser = event.getRegisteredUser();
310
311                 // @TODO Nothing to do with the user here?
312                 // Clear all data
313                 this.clear();
314         }
315
316         /**
317          * Deletes given user account
318          */
319         public void deleteUserData () {
320                 // Get user instance
321                 final User user = this.beanHelper.getUser();
322
323                 // Is the user instance valid and CONFIRMED?
324                 if (null == user) {
325                         // Throw NPE
326                         throw new NullPointerException("user is null"); //NOI18N
327                 } else if (user.getUserId() == null) {
328                         // Throw again
329                         throw new NullPointerException("user.userId is null"); //NOI18N
330                 } else if (user.getUserId() < 1) {
331                         // Invalid id number
332                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
333                 }
334
335                 try {
336                         // All fine, delete it
337                         this.adminUserBean.deleteUser(user, this.getUserDeleteReason());
338                 } catch (final UserNotFoundException ex) {
339                         // Should not happen, so throw again
340                         throw new FacesException(ex);
341                 }
342
343                 // Fire event
344                 this.deleteUserEvent.fire(new AdminDeletedUserEvent(user, this.getUserDeleteReason()));
345         }
346
347         /**
348          * Edits currently loaded user's data in database.
349          */
350         public void editUserData () {
351                 // Get user instance
352                 final User user = this.beanHelper.getUser();
353
354                 // Null password means not setting it
355                 String encryptedPassword = null;
356
357                 // Check if user instance is in helper and valid
358                 if (null == user) {
359                         // Throw NPE
360                         throw new NullPointerException("beanHelper.user is null"); //NOI18N
361                 } else if (user.getUserId() == null) {
362                         // Throw NPE again
363                         throw new NullPointerException("beanHelper.user.userId is null"); //NOI18N
364                 } else if (user.getUserId() < 1) {
365                         // Invalid id
366                         throw new IllegalStateException(MessageFormat.format("beanHelper.user.userId={0} is invalid", user.getUserId())); //NOI18N
367                 } else if (this.getUserName() == null) {
368                         // Not all required fields are set
369                         throw new NullPointerException("userName is null"); //NOI18N
370                 } else if (this.getUserName().isEmpty()) {
371                         // Not all required fields are set
372                         throw new IllegalArgumentException("userName is empty"); //NOI18N
373                 } else if (((!this.getUserPassword().isEmpty()) || (!this.getUserPasswordRepeat().isEmpty())) && (!this.isSamePasswordEntered())) {
374                         // Clear password fields
375                         this.setUserPassword(null);
376                         this.setUserPasswordRepeat(null);
377
378                         // Not same password entered
379                         this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_PASSWORD_REPEAT_DIFFERENT", FacesMessage.SEVERITY_INFO); //NOI18N
380                         return;
381                 } else if ((!Objects.equals(user.getUserName(), this.getUserName())) && (this.userBean.ifUserNameExists(this.getUserName()))) {
382                         // Clear all fields
383                         this.clear();
384
385                         // User name already exists
386                         this.showFacesMessage("form_edit_user:userName", "ADMIN_USER_NAME_ALREADY_EXISTS", FacesMessage.SEVERITY_WARN); //NOI18N
387                         return;
388                 } else if (this.isSamePasswordEntered()) {
389                         // Same password entered, create container
390                         if ((Objects.equals(user.getUserMustChangePassword(), this.getUserMustChangePassword())) && (UserLoginUtils.ifPasswordMatches(new UserLoginContainer(user, this.getUserPassword())))) {
391                                 // Clear password fields
392                                 this.setUserPassword(null);
393                                 this.setUserPasswordRepeat(null);
394
395                                 // Same password entered
396                                 this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_ENTERED_SAME_AS_OLD_PASSWORD", FacesMessage.SEVERITY_WARN); //NOI18N
397                                 return;
398                         }
399
400                         // Encrypt password
401                         encryptedPassword = UserLoginUtils.encryptPassword(this.getUserPassword());
402                 }
403
404                 // Set user name and flag
405                 user.setUserName(this.getUserName());
406                 user.setUserMustChangePassword(this.getUserMustChangePassword());
407
408                 // Is a password set?
409                 if (encryptedPassword != null) {
410                         // Set it as well
411                         user.setUserEncryptedPassword(encryptedPassword);
412                 }
413
414                 // Init updated User instance
415                 final User updatedUser;
416
417                 try {
418                         // Call EJB for updating user data
419                         updatedUser = this.userBean.updateUserData(user);
420                 } catch (final UserNotFoundException ex) {
421                         // Throw as cause
422                         throw new FacesException(ex);
423                 }
424
425                 // Fire event
426                 this.updatedUserDataEvent.fire(new AdminPostUserDataUpdatedEvent(updatedUser));
427         }
428
429         /**
430          * Getter for contact instance
431          * <p>
432          * @return Contact instance
433          */
434         public Contact getContact () {
435                 return this.contact;
436         }
437
438         /**
439          * Setter for contact instance
440          * <p>
441          * @param contact Contact instance
442          */
443         public void setContact (final Contact contact) {
444                 this.contact = contact;
445         }
446
447         /**
448          * Getter for user delete reason
449          * <p>
450          * @return User delete reason
451          */
452         public String getUserDeleteReason () {
453                 return this.userDeleteReason;
454         }
455
456         /**
457          * Setter for user delete reason
458          * <p>
459          * @param userDeleteReason User delete reason
460          */
461         public void setUserDeleteReason (final String userDeleteReason) {
462                 this.userDeleteReason = userDeleteReason;
463         }
464
465         /**
466          * Getter for user lock reason
467          * <p>
468          * @return User lock reason
469          */
470         public String getUserLockReason () {
471                 return this.userLockReason;
472         }
473
474         /**
475          * Setter for user lock reason
476          * <p>
477          * @param userLockReason User lock reason
478          */
479         public void setUserLockReason (final String userLockReason) {
480                 this.userLockReason = userLockReason;
481         }
482
483         /**
484          * Getter for flag if user needs to change password
485          * <p>
486          * @return Flag if user needs to change password
487          */
488         public Boolean getUserMustChangePassword () {
489                 return this.userMustChangePassword;
490         }
491
492         /**
493          * Setter for flag if user needs to change password
494          * <p>
495          * @param userMustChangePassword Flag if user needs to change password
496          */
497         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
498                 this.userMustChangePassword = userMustChangePassword;
499         }
500
501         /**
502          * Getter for user name
503          * <p>
504          * @return User name
505          */
506         public String getUserName () {
507                 return this.userName;
508         }
509
510         /**
511          * Setter for user name
512          * <p>
513          * @param userName User name
514          */
515         public void setUserName (final String userName) {
516                 this.userName = userName;
517         }
518
519         /**
520          * Getter for clear-text user password
521          * <p>
522          * @return Clear-text user password
523          */
524         public String getUserPassword () {
525                 return this.userPassword;
526         }
527
528         /**
529          * Setter for clear-text user password
530          * <p>
531          * @param userPassword Clear-text user password
532          */
533         public void setUserPassword (final String userPassword) {
534                 this.userPassword = userPassword;
535         }
536
537         /**
538          * Getter for clear-text user password repeated
539          * <p>
540          * @return Clear-text user password repeated
541          */
542         public String getUserPasswordRepeat () {
543                 return this.userPasswordRepeat;
544         }
545
546         /**
547          * Setter for clear-text user password repeated
548          * <p>
549          * @param userPasswordRepeat Clear-text user password repeated
550          */
551         public void setUserPasswordRepeat (final String userPasswordRepeat) {
552                 this.userPasswordRepeat = userPasswordRepeat;
553         }
554
555         /**
556          * Getter for user profile mode
557          * <p>
558          * @return User profile mode
559          */
560         public ProfileMode getUserProfileMode () {
561                 return this.userProfileMode;
562         }
563
564         /**
565          * Setter for user profile mode
566          * <p>
567          * @param userProfileMode User profile mode
568          */
569         public void setUserProfileMode (final ProfileMode userProfileMode) {
570                 this.userProfileMode = userProfileMode;
571         }
572
573         /**
574          * Locks selected user's account. This method makes sure that a lock reason
575          * is provided that th user later can read on login attempts.
576          * <p>
577          * @return Redirect outcome
578          */
579         public String lockUserAccount () {
580                 // Get user instance
581                 final User user = this.beanHelper.getUser();
582
583                 // Is the user instance valid and CONFIRMED?
584                 if (null == user) {
585                         // Throw NPE
586                         throw new NullPointerException("user is null"); //NOI18N
587                 } else if (user.getUserId() == null) {
588                         // Throw again
589                         throw new NullPointerException("user.userId is null"); //NOI18N
590                 } else if (user.getUserId() < 1) {
591                         // Invalid id number
592                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
593                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
594                         // User account is locked
595                         throw new FacesException(new UserStatusLockedException(user));
596                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
597                         // User account is locked
598                         throw new FacesException(new UserStatusUnconfirmedException(user));
599                 } else if (this.getUserLockReason() == null) {
600                         // Throw NPE again
601                         throw new NullPointerException("userLockReason is null"); //NOI18N
602                 } else if (this.getUserLockReason().isEmpty()) {
603                         // Empty lock reason
604                         throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
605                 }
606
607                 // Init updated user instance
608                 final User updatedUser;
609
610                 try {
611                         // Get base URL
612                         final String baseUrl = FacesUtils.generateBaseUrl();
613
614                         // Call EJB to lock account
615                         updatedUser = this.adminUserBean.lockUserAccount(user, this.getUserLockReason(), baseUrl);
616                 } catch (final UserStatusLockedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
617                         // Throw again
618                         throw new FacesException(ex);
619                 }
620
621                 // Fire event
622                 this.userLockedEvent.fire(new AdminLockedUserEvent(updatedUser));
623
624                 // Clear bean
625                 this.clear();
626
627                 // Should go fine at this point, redirect to user profile
628                 return "admin_show_user"; //NOI18N
629         }
630
631         /**
632          * Unlocks selected user's account. This method makes sure that the account
633          * is locked.
634          * <p>
635          * @return Redirect outcome
636          */
637         public String unlockUserAccount () {
638                 // Get user instance
639                 final User user = this.beanHelper.getUser();
640
641                 // Is the user instance valid and CONFIRMED?
642                 if (null == user) {
643                         // Throw NPE
644                         throw new NullPointerException("user is null"); //NOI18N
645                 } else if (user.getUserId() == null) {
646                         // Throw again
647                         throw new NullPointerException("user.userId is null"); //NOI18N
648                 } else if (user.getUserId() < 1) {
649                         // Invalid id number
650                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
651                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
652                         // User account is locked
653                         throw new FacesException(new UserStatusConfirmedException(user));
654                 } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
655                         // User account is locked
656                         throw new FacesException(new UserStatusUnconfirmedException(user));
657                 }
658
659                 // Init updated user instance
660                 final User updatedUser;
661
662                 try {
663                         // Get base URL
664                         final String baseUrl = FacesUtils.generateBaseUrl();
665
666                         // Call EJB to unlock account
667                         updatedUser = this.adminUserBean.unlockUserAccount(user, baseUrl);
668                 } catch (final UserStatusConfirmedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
669                         // Throw again
670                         throw new FacesException(ex);
671                 }
672
673                 // Fire event
674                 this.userUnlockedEvent.fire(new AdminUnlockedUserEvent(updatedUser));
675
676                 // Clear bean
677                 this.clear();
678
679                 // Should go fine at this point, redirect to user profile
680                 return "admin_show_user"; //NOI18N
681         }
682
683         /**
684          * Clears this bean
685          */
686         private void clear () {
687                 // Clear all data
688                 this.setContact(null);
689                 this.setUserDeleteReason(null);
690                 this.setUserLockReason(null);
691                 this.setUserMustChangePassword(null);
692                 this.setUserName(null);
693                 this.setUserPassword(null);
694                 this.setUserPasswordRepeat(null);
695                 this.setUserProfileMode(null);
696
697         }
698
699         /**
700          * Creates a new user instance from all currently saved data from this bean
701          * <p>
702          * @return New user instance
703          */
704         private User createUserInstance () {
705                 // Init variable for password and contact
706                 final String password;
707                 final Contact userContact;
708
709                 // Is a contact instance in helper set?
710                 if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
711                         // Empty password entered, then generate one
712                         password = UserLoginUtils.createRandomPassword(FinancialsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
713                 } else if (!this.isSamePasswordEntered()) {
714                         // Both passwords don't match
715                         throw new FacesException(new UserPasswordRepeatMismatchException());
716                 } else {
717                         // Both match, so get it from this bean
718                         password = this.getUserPassword();
719                 }
720
721                 // Is contact instance given? Else create one
722                 if (this.getContact() instanceof Contact) {
723                         // Then use it for contact linking
724                         userContact = this.getContact();
725                 } else {
726                         // Create contact instance
727                         userContact = this.contactController.createContactInstance();
728                 }
729
730                 // Create new instance
731                 final User newUser = new LoginUser(
732                                    this.getUserName(),
733                                    this.getUserProfileMode(),
734                                    this.getUserMustChangePassword(),
735                                    UserLoginUtils.encryptPassword(password),
736                                    UserAccountStatus.CONFIRMED,
737                                    userContact
738                    );
739
740                 // Get locale from view-root
741                 final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
742
743                 // Copy user locale
744                 newUser.setUserLocale(locale);
745
746                 // Return it
747                 return newUser;
748         }
749
750         /**
751          * Checks if same password is entered and that they are not empty.
752          * <p>
753          * @return Whether the same password was entered
754          */
755         private boolean isSamePasswordEntered () {
756                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
757         }
758
759 }