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