]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java
5e6537a7cba0b0dd5f521b4c7e68acc3992de596
[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.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.jjobs.beans.user.list.JobsUserListWebViewController;
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.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.login.UserLoginUtils;
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                 // Init variable
261                 final User updatedUser;
262
263                 try {
264                         // Now, that all is set, call EJB
265                         if (this.getContact() instanceof Contact) {
266                                 // Link contact with this user
267                                 updatedUser = this.adminUserBean.linkUser(newUser);
268                         } else {
269                                 // Add new user
270                                 updatedUser = this.adminUserBean.addUser(newUser);
271                         }
272                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
273                         // Throw again
274                         throw new FacesException(ex);
275                 }
276
277                 // Now, that all is set, call EJB
278                 if (this.getContact() instanceof Contact) {
279                         // Fire event
280                         this.userLinkedEvent.fire(new AdminLinkedUserEvent(updatedUser));
281                 } else {
282                         // Fire event
283                         this.addedUserEvent.fire(new AdminAddedUserEvent(updatedUser));
284                 }
285                 // Clear helper
286                 this.setContact(null);
287
288                 // Clear this bean
289                 this.clear();
290         }
291
292         /**
293          * Event observer for when a bean helper has successfully created a user
294          * instance, means the user exists. If the user does not exist, this event
295          * should not fire but instead a proper exception must be thrown.
296          * <p>
297          * @param event User created event
298          */
299         public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
300                 // Is the instance valid?
301                 if (null == event) {
302                         // Throw NPE
303                         throw new NullPointerException("event is null"); //NOI18N
304                 } else if (event.getCreatedUser() == null) {
305                         // Throw NPE again
306                         throw new NullPointerException("event.createdUser is null"); //NOI18N
307                 } else if (event.getCreatedUser().getUserId() == null) {
308                         // Throw NPE again
309                         throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
310                 } else if (event.getCreatedUser().getUserId() < 1) {
311                         // Throw NPE again
312                         throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
313                 }
314
315                 // Set whole user
316                 this.setUser(event.getCreatedUser());
317         }
318
319         /**
320          * Event observer for new user registrations
321          * <p>
322          * @param event User registration event
323          */
324         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
325                 // Event and contained entity instance should not be null
326                 if (null == event) {
327                         // Throw NPE
328                         throw new NullPointerException("event is null"); //NOI18N
329                 } else if (event.getRegisteredUser() == null) {
330                         // Throw NPE again
331                         throw new NullPointerException("event.user is null"); //NOI18N
332                 } else if (event.getRegisteredUser().getUserId() == null) {
333                         // userId is null
334                         throw new NullPointerException("event.user.userId is null"); //NOI18N
335                 } else if (event.getRegisteredUser().getUserId() < 1) {
336                         // Not avalid id
337                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
338                 }
339
340                 // Get user instance
341                 final User registeredUser = event.getRegisteredUser();
342
343                 // @TODO Nothing to do with the user here?
344                 // Clear all data
345                 this.clear();
346         }
347
348         /**
349          * Deletes given user account
350          */
351         public void deleteUserData () {
352                 // Is the user instance valid and CONFIRMED?
353                 if (this.getUser() == null) {
354                         // Throw NPE
355                         throw new NullPointerException("user is null"); //NOI18N
356                 } else if (this.getUser().getUserId() == null) {
357                         // Throw again
358                         throw new NullPointerException("user.userId is null"); //NOI18N
359                 } else if (this.getUser().getUserId() < 1) {
360                         // Invalid id number
361                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
362                 }
363
364                 try {
365                         // All fine, delete it
366                         this.adminUserBean.deleteUser(this.getUser(), this.getUserDeleteReason());
367                 } catch (final UserNotFoundException ex) {
368                         // Should not happen, so throw again
369                         throw new FacesException(ex);
370                 }
371
372                 // Fire event
373                 this.deleteUserEvent.fire(new AdminDeletedUserEvent(this.getUser(), this.getUserDeleteReason()));
374         }
375
376         /**
377          * Edits currently loaded user's data in database.
378          */
379         public void editUserData () {
380                 // Null password means not setting it
381                 String encryptedPassword = null;
382
383                 // Check if user instance is in helper and valid
384                 if (this.getUser() == null) {
385                         // Throw NPE
386                         throw new NullPointerException("beanHelper.user is null"); //NOI18N
387                 } else if (this.getUser().getUserId() == null) {
388                         // Throw NPE again
389                         throw new NullPointerException("beanHelper.user.userId is null"); //NOI18N
390                 } else if (this.getUser().getUserId() < 1) {
391                         // Invalid id
392                         throw new IllegalStateException(MessageFormat.format("beanHelper.user.userId={0} is invalid", this.getUser().getUserId())); //NOI18N
393                 } else if (this.getUserName() == null) {
394                         // Not all required fields are set
395                         throw new NullPointerException("this.userName is null"); //NOI18N
396                 } else if (this.getUserName().isEmpty()) {
397                         // Not all required fields are set
398                         throw new IllegalArgumentException("this.userName is empty"); //NOI18N
399                 } else if (((!this.getUserPassword().isEmpty()) || (!this.getUserPasswordRepeat().isEmpty())) && (!this.isSamePasswordEntered())) {
400                         // Clear password fields
401                         this.setUserPassword(null);
402                         this.setUserPasswordRepeat(null);
403
404                         // Not same password entered
405                         this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_PASSWORD_REPEAT_DIFFERENT", FacesMessage.SEVERITY_INFO); //NOI18N
406                         return;
407                 } else if ((!Objects.equals(this.getUser().getUserName(), this.getUserName())) && (this.userBean.ifUserNameExists(this.getUserName()))) {
408                         // Clear all fields
409                         this.clear();
410
411                         // User name already exists
412                         this.showFacesMessage("form_edit_user:userName", "ADMIN_USER_NAME_ALREADY_EXISTS", FacesMessage.SEVERITY_WARN); //NOI18N
413                         return;
414                 } else if (this.isSamePasswordEntered()) {
415                         // Same password entered, create container
416                         if ((Objects.equals(this.getUser().getUserMustChangePassword(), this.getUserMustChangePassword())) && (UserLoginUtils.ifPasswordMatches(new UserLoginContainer(this.getUser(), this.getUserPassword())))) {
417                                 // Clear password fields
418                                 this.setUserPassword(null);
419                                 this.setUserPasswordRepeat(null);
420
421                                 // Same password entered
422                                 this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_ENTERED_SAME_AS_OLD_PASSWORD", FacesMessage.SEVERITY_WARN); //NOI18N
423                                 return;
424                         }
425
426                         // Encrypt password
427                         encryptedPassword = UserLoginUtils.encryptPassword(this.getUserPassword());
428                 }
429
430                 // Set user name and flag
431                 this.getUser().setUserName(this.getUserName());
432                 this.getUser().setUserMustChangePassword(this.getUserMustChangePassword());
433
434                 // Is a password set?
435                 if (encryptedPassword != null) {
436                         // Set it as well
437                         this.getUser().setUserEncryptedPassword(encryptedPassword);
438                 }
439
440                 // Init updated User instance
441                 final User updatedUser;
442
443                 try {
444                         // Call EJB for updating user data
445                         updatedUser = this.userBean.updateUserData(this.getUser());
446                 } catch (final UserNotFoundException ex) {
447                         // Throw as cause
448                         throw new FacesException(ex);
449                 }
450
451                 // Fire event
452                 this.updatedUserDataEvent.fire(new AdminPostUserDataUpdatedEvent(updatedUser));
453         }
454
455         /**
456          * Getter for contact instance
457          * <p>
458          * @return Contact instance
459          */
460         public Contact getContact () {
461                 return this.contact;
462         }
463
464         /**
465          * Setter for contact instance
466          * <p>
467          * @param contact Contact instance
468          */
469         public void setContact (final Contact contact) {
470                 this.contact = contact;
471         }
472
473         /**
474          * Getter for user instance
475          * <p>
476          * @return User instance
477          */
478         public User getUser () {
479                 return this.user;
480         }
481
482         /**
483          * Setter for user instance
484          * <p>
485          * @param user User instance
486          */
487         public void setUser (final User user) {
488                 this.user = user;
489         }
490
491         /**
492          * Getter for user delete reason
493          * <p>
494          * @return User delete reason
495          */
496         public String getUserDeleteReason () {
497                 return this.userDeleteReason;
498         }
499
500         /**
501          * Setter for user delete reason
502          * <p>
503          * @param userDeleteReason User delete reason
504          */
505         public void setUserDeleteReason (final String userDeleteReason) {
506                 this.userDeleteReason = userDeleteReason;
507         }
508
509         /**
510          * Getter for user lock reason
511          * <p>
512          * @return User lock reason
513          */
514         public String getUserLockReason () {
515                 return this.userLockReason;
516         }
517
518         /**
519          * Setter for user lock reason
520          * <p>
521          * @param userLockReason User lock reason
522          */
523         public void setUserLockReason (final String userLockReason) {
524                 this.userLockReason = userLockReason;
525         }
526
527         /**
528          * Getter for flag if user needs to change password
529          * <p>
530          * @return Flag if user needs to change password
531          */
532         public Boolean getUserMustChangePassword () {
533                 return this.userMustChangePassword;
534         }
535
536         /**
537          * Setter for flag if user needs to change password
538          * <p>
539          * @param userMustChangePassword Flag if user needs to change password
540          */
541         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
542                 this.userMustChangePassword = userMustChangePassword;
543         }
544
545         /**
546          * Getter for user name
547          * <p>
548          * @return User name
549          */
550         public String getUserName () {
551                 return this.userName;
552         }
553
554         /**
555          * Setter for user name
556          * <p>
557          * @param userName User name
558          */
559         public void setUserName (final String userName) {
560                 this.userName = userName;
561         }
562
563         /**
564          * Getter for clear-text user password
565          * <p>
566          * @return Clear-text user password
567          */
568         public String getUserPassword () {
569                 return this.userPassword;
570         }
571
572         /**
573          * Setter for clear-text user password
574          * <p>
575          * @param userPassword Clear-text user password
576          */
577         public void setUserPassword (final String userPassword) {
578                 this.userPassword = userPassword;
579         }
580
581         /**
582          * Getter for clear-text user password repeated
583          * <p>
584          * @return Clear-text user password repeated
585          */
586         public String getUserPasswordRepeat () {
587                 return this.userPasswordRepeat;
588         }
589
590         /**
591          * Setter for clear-text user password repeated
592          * <p>
593          * @param userPasswordRepeat Clear-text user password repeated
594          */
595         public void setUserPasswordRepeat (final String userPasswordRepeat) {
596                 this.userPasswordRepeat = userPasswordRepeat;
597         }
598
599         /**
600          * Getter for user profile mode
601          * <p>
602          * @return User profile mode
603          */
604         public ProfileMode getUserProfileMode () {
605                 return this.userProfileMode;
606         }
607
608         /**
609          * Setter for user profile mode
610          * <p>
611          * @param userProfileMode User profile mode
612          */
613         public void setUserProfileMode (final ProfileMode userProfileMode) {
614                 this.userProfileMode = userProfileMode;
615         }
616
617         /**
618          * Locks selected user's account. This method makes sure that a lock reason
619          * is provided that th user later can read on login attempts.
620          * <p>
621          * @return Redirect outcome
622          */
623         public String lockUserAccount () {
624                 // Is the user instance valid and CONFIRMED?
625                 if (this.getUser() == null) {
626                         // Throw NPE
627                         throw new NullPointerException("this.user is null"); //NOI18N
628                 } else if (this.getUser().getUserId() == null) {
629                         // Throw again
630                         throw new NullPointerException("this.user.userId is null"); //NOI18N
631                 } else if (this.getUser().getUserId() < 1) {
632                         // Invalid id number
633                         throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
634                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.LOCKED) {
635                         // User account is locked
636                         throw new FacesException(new UserStatusLockedException(this.getUser()));
637                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
638                         // User account is locked
639                         throw new FacesException(new UserStatusUnconfirmedException(this.getUser()));
640                 } else if (this.getUserLockReason() == null) {
641                         // Throw NPE again
642                         throw new NullPointerException("this.userLockReason is null"); //NOI18N
643                 } else if (this.getUserLockReason().isEmpty()) {
644                         // Empty lock reason
645                         throw new IllegalArgumentException("this.userLockReason is empty"); //NOI18N
646                 }
647
648                 // Init updated user instance
649                 final User updatedUser;
650
651                 try {
652                         // Get base URL
653                         final String baseUrl = FacesUtils.generateBaseUrl();
654
655                         // Call EJB to lock account
656                         updatedUser = this.adminUserBean.lockUserAccount(this.getUser(), this.getUserLockReason(), baseUrl);
657                 } catch (final UserStatusLockedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
658                         // Throw again
659                         throw new FacesException(ex);
660                 }
661
662                 // Fire event
663                 this.userLockedEvent.fire(new AdminLockedUserEvent(updatedUser));
664
665                 // Clear bean
666                 this.clear();
667
668                 // Should go fine at this point, redirect to user profile
669                 return "admin_show_user"; //NOI18N
670         }
671
672         /**
673          * Unlocks selected user's account. This method makes sure that the account
674          * is locked.
675          * <p>
676          * @return Redirect outcome
677          */
678         public String unlockUserAccount () {
679                 // Is the user instance valid and CONFIRMED?
680                 if (this.getUser() == null) {
681                         // Throw NPE
682                         throw new NullPointerException("this.user is null"); //NOI18N
683                 } else if (this.getUser().getUserId() == null) {
684                         // Throw again
685                         throw new NullPointerException("this.user.userId is null"); //NOI18N
686                 } else if (this.getUser().getUserId() < 1) {
687                         // Invalid id number
688                         throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
689                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
690                         // User account is locked
691                         throw new FacesException(new UserStatusConfirmedException(this.getUser()));
692                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
693                         // User account is locked
694                         throw new FacesException(new UserStatusUnconfirmedException(this.getUser()));
695                 }
696
697                 // Init updated user instance
698                 final User updatedUser;
699
700                 try {
701                         // Get base URL
702                         final String baseUrl = FacesUtils.generateBaseUrl();
703
704                         // Call EJB to unlock account
705                         updatedUser = this.adminUserBean.unlockUserAccount(this.getUser(), baseUrl);
706                 } catch (final UserStatusConfirmedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
707                         // Throw again
708                         throw new FacesException(ex);
709                 }
710
711                 // Fire event
712                 this.userUnlockedEvent.fire(new AdminUnlockedUserEvent(updatedUser));
713
714                 // Clear bean
715                 this.clear();
716
717                 // Should go fine at this point, redirect to user profile
718                 return "admin_show_user"; //NOI18N
719         }
720
721         /**
722          * Clears this bean
723          */
724         private void clear () {
725                 // Clear all data
726                 this.setContact(null);
727                 this.setUserDeleteReason(null);
728                 this.setUser(null);
729                 this.setUserLockReason(null);
730                 this.setUserMustChangePassword(null);
731                 this.setUserName(null);
732                 this.setUserPassword(null);
733                 this.setUserPasswordRepeat(null);
734                 this.setUserProfileMode(null);
735
736         }
737
738         /**
739          * Creates a new user instance from all currently saved data from this bean
740          * <p>
741          * @return New user instance
742          */
743         private User createUserInstance () {
744                 // Init variable for password
745                 String password = null;
746
747                 // Init instance
748                 final Contact userContact;
749
750                 // Is a contact instance in helper set?
751                 if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
752                         // Empty password entered, then generate one
753                         password = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
754                 } else if (!this.isSamePasswordEntered()) {
755                         // Both passwords don't match
756                         throw new FacesException(new UserPasswordRepeatMismatchException());
757                 } else {
758                         // Both match, so get it from this bean
759                         password = this.getUserPassword();
760                 }
761
762                 // The password should not be null and at least 5 characters long
763                 assert (password != null) : "password is null"; //NOI18N
764                 assert (password.length() >= JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
765
766                 // Is contact instance given? Else create one
767                 if (this.getContact() instanceof Contact) {
768                         // Then use it for contact linking
769                         userContact = this.getContact();
770                 } else {
771                         // Create contact instance
772                         userContact = this.contactController.createContactInstance();
773                 }
774
775                 // Create new instance
776                 final User newUser = new LoginUser(
777                                    this.getUserName(),
778                                    this.getUserProfileMode(),
779                                    this.getUserMustChangePassword(),
780                                    UserLoginUtils.encryptPassword(password),
781                                    UserAccountStatus.CONFIRMED,
782                                    userContact
783                    );
784
785                 // Get locale from view-root
786                 final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
787
788                 // Copy user locale
789                 newUser.setUserLocale(locale);
790
791                 // Return it
792                 return newUser;
793         }
794
795         /**
796          * Checks if same password is entered and that they are not empty.
797          * <p>
798          * @return Whether the same password was entered
799          */
800         private boolean isSamePasswordEntered () {
801                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
802         }
803
804 }