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