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