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