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