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