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