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