]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java
Please rename/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.JobsContactWebRequestController;
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 JobsContactWebRequestController 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 JobsUserWebRequestController 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                 // As the form cannot validate the data (required="true"), check it here
218                 if (this.getUserName() == null) {
219                         // Throw NPE
220                         throw new NullPointerException("userName is null"); //NOI18N
221                 } else if (this.getUserName().isEmpty()) {
222                         // Is empty
223                         throw new IllegalArgumentException("userName is null"); //NOI18N
224                 } else if (this.getContact() == null) {
225                         // No contact instance set, so test required fields: gender, first name and family name
226                         this.adminContactController.validateContactData();
227                 }
228
229                 // Create new user instance
230                 User newUser = new LoginUser();
231
232                 // Set user name, CONFIRMED and INVISIBLE
233                 newUser.setUserName(this.getUserName());
234                 newUser.setUserMustChangePassword(this.getUserMustChangePassword());
235                 newUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
236                 newUser.setUserProfileMode(ProfileMode.INVISIBLE);
237
238                 // Get locale from view-root
239                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
240
241                 // Copy user locale
242                 newUser.setUserLocale(locale);
243
244                 // Init instance
245                 Contact userContact;
246
247                 // Is a contact instance in helper set?
248                 if (this.getContact() instanceof Contact) {
249                         // Then use it for contact linking
250                         userContact = this.getContact();
251                 } else {
252                         // Create contact instance
253                         userContact = this.contactController.createContactInstance();
254                 }
255
256                 // Set contact in user
257                 newUser.setUserContact(userContact);
258
259                 // Init variable for password
260                 String password = null;
261
262                 // Is the user name or email address used already?
263                 // @TODO Add password length check
264                 if (this.userController.isUserNameRegistered(newUser)) {
265                         // User name is already used
266                         throw new FaceletException(new UserNameAlreadyRegisteredException(newUser));
267                 } else if ((this.getContact() == null) && (this.contactController.isEmailAddressRegistered(newUser.getUserContact()))) {
268                         // Email address is already used
269                         this.showFacesMessage("admin_add_user:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
270
271                         // Always clear password
272                         this.setUserPassword(null);
273                         this.setUserPasswordRepeat(null);
274
275                         // Skip it
276                         return ""; //NOI18N
277                 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
278                         // Empty password entered, then generate one
279                         password = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
280                 } else if (!this.isSamePasswordEntered()) {
281                         // Both passwords don't match
282                         throw new FaceletException(new UserPasswordRepeatMismatchException(newUser));
283                 } else {
284                         // Both match, so get it from this bean
285                         password = this.getUserPassword();
286                 }
287
288                 // The password should not be null and at least 5 characters long
289                 assert (password != null) : "password is null"; //NOI18N
290                 assert (password.length() >= JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
291
292                 // Encrypt password and set it
293                 newUser.setUserEncryptedPassword(UserLoginUtils.encryptPassword(password));
294
295                 try {
296                         // Now, that all is set, call EJB
297                         if (this.getContact() instanceof Contact) {
298                                 // Link contact with this user
299                                 User updatedUser = this.adminUserBean.linkUser(newUser);
300
301                                 // Fire event
302                                 this.userLinkedEvent.fire(new AdminLinkedUserEvent(updatedUser));
303                         } else {
304                                 // Add new contact
305                                 User updatedUser = this.adminUserBean.addUser(newUser);
306
307                                 // Fire event
308                                 this.addedUserEvent.fire(new AdminAddedUserEvent(updatedUser));
309                         }
310                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
311                         // Throw again
312                         throw new FaceletException(ex);
313                 }
314
315                 // Clear helper
316                 this.setContact(null);
317
318                 // Clear this bean
319                 this.clear();
320
321                 // Return to user list (for now)
322                 return "admin_list_user"; //NOI18N
323         }
324
325         /**
326          * Event observer for when a bean helper has successfully created a user
327          * instance, means the user exists. If the user does not exist, this event
328          * should not fire but instead a proper exception must be thrown.
329          * <p>
330          * @param event User created event
331          */
332         public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
333                 // Is the instance valid?
334                 if (null == event) {
335                         // Throw NPE
336                         throw new NullPointerException("event is null"); //NOI18N
337                 } else if (event.getCreatedUser() == null) {
338                         // Throw NPE again
339                         throw new NullPointerException("event.createdUser is null"); //NOI18N
340                 } else if (event.getCreatedUser().getUserId() == null) {
341                         // Throw NPE again
342                         throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
343                 } else if (event.getCreatedUser().getUserId() < 1) {
344                         // Throw NPE again
345                         throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
346                 }
347
348                 // Set whole user
349                 this.setUser(event.getCreatedUser());
350         }
351
352         /**
353          * Event observer for new user registrations
354          * <p>
355          * @param event User registration event
356          */
357         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
358                 // event should not be null
359                 if (null == event) {
360                         // Throw NPE
361                         throw new NullPointerException("event is null"); //NOI18N
362                 } else if (event.getRegisteredUser() == null) {
363                         // Throw NPE again
364                         throw new NullPointerException("event.user is null"); //NOI18N
365                 } else if (event.getRegisteredUser().getUserId() == null) {
366                         // userId is null
367                         throw new NullPointerException("event.user.userId is null"); //NOI18N
368                 } else if (event.getRegisteredUser().getUserId() < 1) {
369                         // Not avalid id
370                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
371                 }
372
373                 // Get user instance
374                 User registeredUser = event.getRegisteredUser();
375
376                 // @TODO Nothing to do with the user here?
377                 // Clear all data
378                 this.clear();
379         }
380
381         /**
382          * Deletes given user account
383          * <p>
384          * @return Redirect outcome
385          */
386         public String deleteUserData () {
387                 // Is the user instance valid and CONFIRMED?
388                 if (this.getUser() == null) {
389                         // Throw NPE
390                         throw new NullPointerException("user is null"); //NOI18N
391                 } else if (this.getUser().getUserId() == null) {
392                         // Throw again
393                         throw new NullPointerException("user.userId is null"); //NOI18N
394                 } else if (this.getUser().getUserId() < 1) {
395                         // Invalid id number
396                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
397                 }
398
399                 try {
400                         // All fine, delete it
401                         this.adminUserBean.deleteUser(this.getUser(), this.getUserDeleteReason());
402                 } catch (final UserNotFoundException ex) {
403                         // Should not happen, so throw again
404                         throw new FaceletException(ex);
405                 }
406
407                 // Fire event
408                 this.deleteUserEvent.fire(new AdminDeletedUserEvent(this.getUser(), this.getUserDeleteReason()));
409
410                 // Redirect
411                 return "admin_list_user"; //NOI18N
412         }
413
414         /**
415          * Edits currently loaded user's data in database.
416          * <p>
417          * @return Redirect outcome
418          */
419         public String editUserData () {
420                 // Null password means not setting it
421                 String encryptedPassword = null;
422
423                 // Check if user instance is in helper and valid
424                 if (this.getUser() == null) {
425                         // Throw NPE
426                         throw new NullPointerException("beanHelper.user is null"); //NOI18N
427                 } else if (this.getUser().getUserId() == null) {
428                         // Throw NPE again
429                         throw new NullPointerException("beanHelper.user.userId is null"); //NOI18N
430                 } else if (this.getUser().getUserId() < 1) {
431                         // Invalid id
432                         throw new IllegalStateException(MessageFormat.format("beanHelper.user.userId={0} is invalid", this.getUser().getUserId())); //NOI18N
433                 } else if (this.getUserName() == null) {
434                         // Not all required fields are set
435                         throw new NullPointerException("this.userName is null"); //NOI18N
436                 } else if (this.getUserName().isEmpty()) {
437                         // Not all required fields are set
438                         throw new IllegalArgumentException("this.userName is empty"); //NOI18N
439                 } else if (((!this.getUserPassword().isEmpty()) || (!this.getUserPasswordRepeat().isEmpty())) && (!this.isSamePasswordEntered())) {
440                         // Clear password fields
441                         this.setUserPassword(null);
442                         this.setUserPasswordRepeat(null);
443
444                         // Not same password entered
445                         this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_PASSWORD_REPEAT_DIFFERENT"); //NOI18N
446                         return ""; //NOI18N
447                 } else if ((!Objects.equals(this.getUser().getUserName(), this.getUserName())) && (this.userBean.ifUserNameExists(this.getUserName()))) {
448                         // Clear all fields
449                         this.clear();
450
451                         // User name already exists
452                         this.showFacesMessage("form_edit_user:userName", "ADMIN_USER_NAME_ALREADY_EXISTS"); //NOI18N
453                         return ""; //NOI18N
454                 } else if (this.isSamePasswordEntered()) {
455                         // Same password entered, create container
456                         if ((Objects.equals(this.getUser().getUserMustChangePassword(), this.getUserMustChangePassword())) && (UserLoginUtils.ifPasswordMatches(new UserLoginContainer(this.getUser(), this.getUserPassword())))) {
457                                 // Clear password fields
458                                 this.setUserPassword(null);
459                                 this.setUserPasswordRepeat(null);
460
461                                 // Same password entered
462                                 this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_ENTERED_SAME_AS_OLD_PASSWORD"); //NOI18N
463                                 return ""; //NOI18N
464                         }
465
466                         // Encrypt password
467                         encryptedPassword = UserLoginUtils.encryptPassword(this.getUserPassword());
468                 }
469
470                 // Set user name and flag
471                 this.getUser().setUserName(this.getUserName());
472                 this.getUser().setUserMustChangePassword(this.getUserMustChangePassword());
473
474                 // Is a password set?
475                 if (encryptedPassword != null) {
476                         // Set it as well
477                         this.getUser().setUserEncryptedPassword(encryptedPassword);
478                 }
479
480                 // Call EJB for updating user data
481                 User updatedUser = this.userBean.updateUserData(this.getUser());
482
483                 // Fire event
484                 this.updatedUserDataEvent.fire(new AdminUpdatedUserDataEvent(updatedUser));
485
486                 // Return to user list (for now)
487                 return "admin_list_user"; //NOI18N
488         }
489
490         /**
491          * Getter for contact instance
492          * <p>
493          * @return Contact instance
494          */
495         public Contact getContact () {
496                 return this.contact;
497         }
498
499         /**
500          * Setter for contact instance
501          * <p>
502          * @param contact Contact instance
503          */
504         public void setContact (final Contact contact) {
505                 this.contact = contact;
506         }
507
508         /**
509          * Getter for user instance
510          * <p>
511          * @return User instance
512          */
513         public User getUser () {
514                 return this.user;
515         }
516
517         /**
518          * Setter for user instance
519          * <p>
520          * @param user User instance
521          */
522         public void setUser (final User user) {
523                 this.user = user;
524         }
525
526         /**
527          * Getter for user delete reason
528          * <p>
529          * @return User delete reason
530          */
531         public String getUserDeleteReason () {
532                 return this.userDeleteReason;
533         }
534
535         /**
536          * Setter for user delete reason
537          * <p>
538          * @param userDeleteReason User delete reason
539          */
540         public void setUserDeleteReason (final String userDeleteReason) {
541                 this.userDeleteReason = userDeleteReason;
542         }
543
544         /**
545          * Getter for user lock reason
546          * <p>
547          * @return User lock reason
548          */
549         public String getUserLockReason () {
550                 return this.userLockReason;
551         }
552
553         /**
554          * Setter for user lock reason
555          * <p>
556          * @param userLockReason User lock reason
557          */
558         public void setUserLockReason (final String userLockReason) {
559                 this.userLockReason = userLockReason;
560         }
561
562         /**
563          * Getter for flag if user needs to change password
564          * <p>
565          * @return Flag if user needs to change password
566          */
567         public Boolean getUserMustChangePassword () {
568                 return this.userMustChangePassword;
569         }
570
571         /**
572          * Setter for flag if user needs to change password
573          * <p>
574          * @param userMustChangePassword Flag if user needs to change password
575          */
576         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
577                 this.userMustChangePassword = userMustChangePassword;
578         }
579
580         /**
581          * Getter for user name
582          * <p>
583          * @return User name
584          */
585         public String getUserName () {
586                 return this.userName;
587         }
588
589         /**
590          * Setter for user name
591          * <p>
592          * @param userName User name
593          */
594         public void setUserName (final String userName) {
595                 this.userName = userName;
596         }
597
598         /**
599          * Getter for clear-text user password
600          * <p>
601          * @return Clear-text user password
602          */
603         public String getUserPassword () {
604                 return this.userPassword;
605         }
606
607         /**
608          * Setter for clear-text user password
609          * <p>
610          * @param userPassword Clear-text user password
611          */
612         public void setUserPassword (final String userPassword) {
613                 this.userPassword = userPassword;
614         }
615
616         /**
617          * Getter for clear-text user password repeated
618          * <p>
619          * @return Clear-text user password repeated
620          */
621         public String getUserPasswordRepeat () {
622                 return this.userPasswordRepeat;
623         }
624
625         /**
626          * Setter for clear-text user password repeated
627          * <p>
628          * @param userPasswordRepeat Clear-text user password repeated
629          */
630         public void setUserPasswordRepeat (final String userPasswordRepeat) {
631                 this.userPasswordRepeat = userPasswordRepeat;
632         }
633
634         /**
635          * Locks selected user's account. This method makes sure that a lock reason
636          * is provided that th user later can read on login attempts.
637          * <p>
638          * @return Redirect outcome
639          */
640         public String lockUserAccount () {
641                 // Is the user instance valid and CONFIRMED?
642                 if (this.getUser() == null) {
643                         // Throw NPE
644                         throw new NullPointerException("this.user is null"); //NOI18N
645                 } else if (this.getUser().getUserId() == null) {
646                         // Throw again
647                         throw new NullPointerException("this.user.userId is null"); //NOI18N
648                 } else if (this.getUser().getUserId() < 1) {
649                         // Invalid id number
650                         throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
651                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.LOCKED) {
652                         // User account is locked
653                         throw new FacesException(new UserStatusLockedException(this.getUser()));
654                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
655                         // User account is locked
656                         throw new FaceletException(new UserStatusUnconfirmedException(this.getUser()));
657                 } else if (this.getUserLockReason() == null) {
658                         // Throw NPE again
659                         throw new NullPointerException("this.userLockReason is null"); //NOI18N
660                 } else if (this.getUserLockReason().isEmpty()) {
661                         // Empty lock reason
662                         throw new IllegalArgumentException("this.userLockReason is empty"); //NOI18N
663                 }
664
665                 // Init updated user instance
666                 User updatedUser;
667
668                 try {
669                         // Get base URL
670                         String baseUrl = FacesUtils.generateBaseUrl();
671
672                         // Call EJB to lock account
673                         updatedUser = this.adminUserBean.lockUserAccount(this.getUser(), this.getUserLockReason(), baseUrl);
674                 } catch (final UserStatusLockedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
675                         // Throw again
676                         throw new FaceletException(ex);
677                 }
678
679                 // Fire event
680                 this.userLockedEvent.fire(new AdminLockedUserEvent(updatedUser));
681
682                 // Clear bean
683                 this.clear();
684
685                 // Should go fine at this point, redirect to user profile
686                 return "admin_show_user"; //NOI18N
687         }
688
689         /**
690          * Unlocks selected user's account. This method makes sure that the account
691          * is locked.
692          * <p>
693          * @return Redirect outcome
694          */
695         public String unlockUserAccount () {
696                 // Is the user instance valid and CONFIRMED?
697                 if (this.getUser() == null) {
698                         // Throw NPE
699                         throw new NullPointerException("this.user is null"); //NOI18N
700                 } else if (this.getUser().getUserId() == null) {
701                         // Throw again
702                         throw new NullPointerException("this.user.userId is null"); //NOI18N
703                 } else if (this.getUser().getUserId() < 1) {
704                         // Invalid id number
705                         throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
706                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
707                         // User account is locked
708                         throw new FacesException(new UserStatusConfirmedException(this.getUser()));
709                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
710                         // User account is locked
711                         throw new FaceletException(new UserStatusUnconfirmedException(this.getUser()));
712                 }
713
714                 // Init updated user instance
715                 User updatedUser;
716
717                 try {
718                         // Get base URL
719                         String baseUrl = FacesUtils.generateBaseUrl();
720
721                         // Call EJB to unlock account
722                         updatedUser = this.adminUserBean.unlockUserAccount(this.getUser(), baseUrl);
723                 } catch (final UserStatusConfirmedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
724                         // Throw again
725                         throw new FaceletException(ex);
726                 }
727
728                 // Fire event
729                 this.userUnlockedEvent.fire(new AdminUnlockedUserEvent(updatedUser));
730
731                 // Clear bean
732                 this.clear();
733
734                 // Should go fine at this point, redirect to user profile
735                 return "admin_show_user"; //NOI18N
736         }
737
738         /**
739          * Clears this bean
740          */
741         private void clear () {
742                 // Clear all data
743                 this.setContact(null);
744                 this.setUserLockReason(null);
745                 this.setUserMustChangePassword(null);
746                 this.setUserName(null);
747         }
748
749         /**
750          * Checks if same password is entered and that they are not empty.
751          * <p>
752          * @return Whether the same password was entered
753          */
754         private boolean isSamePasswordEntered () {
755                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
756         }
757
758 }