2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.user;
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
29 import javax.enterprise.context.RequestScoped;
30 import javax.enterprise.event.Event;
31 import javax.enterprise.event.Observes;
32 import javax.enterprise.inject.Any;
33 import javax.faces.context.FacesContext;
34 import javax.faces.view.facelets.FaceletException;
35 import javax.inject.Inject;
36 import javax.inject.Named;
37 import org.mxchange.jcontacts.model.contact.Contact;
38 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
39 import org.mxchange.jjobs.beans.BaseJobsController;
40 import org.mxchange.jjobs.beans.contact.JobsContactWebRequestController;
41 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
42 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
43 import org.mxchange.jjobs.beans.user.login.JobsUserLoginWebSessionController;
44 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
45 import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent;
46 import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent;
47 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
48 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
49 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
50 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
51 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
52 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
53 import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent;
54 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
55 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
56 import org.mxchange.jusercore.exceptions.UserNotFoundException;
57 import org.mxchange.jusercore.model.user.LoginUser;
58 import org.mxchange.jusercore.model.user.User;
59 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
60 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
61 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
62 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
63 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
64 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
65 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
66 import org.mxchange.juserlogincore.login.UserLoginUtils;
69 * A user controller (bean)
71 * @author Roland Häder<roland@mxchange.org>
73 @Named ("userController")
75 public class JobsUserWebRequestBean extends BaseJobsController implements JobsUserWebRequestController {
80 private static final long serialVersionUID = 542_145_347_916L;
83 * General contact controller
86 private JobsContactWebRequestController contactController;
92 private JobsFeaturesWebApplicationController featureController;
97 private Locale locale;
100 * Localization controller
103 private JobsLocalizationSessionController localizationController;
106 * Event being fired when user updated personal data
110 private Event<ObservableUpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
115 @EJB (lookup = "java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
116 private UserSessionBeanRemote userBean;
119 * A list of all user profiles
122 @NamedCache (cacheName = "userCache")
123 private Cache<Long, User> userCache;
131 * Login controller (bean)
134 private JobsUserLoginWebSessionController userLoginController;
139 private String userName;
145 @NamedCache (cacheName = "userNameCache")
146 private Cache<Long, String> userNameCache;
149 * User password (clear-text from web form)
151 private String userPassword;
154 * User password repeated (clear-text from web form)
156 private String userPasswordRepeat;
159 * Whether the user wants a public profile
161 private ProfileMode userProfileMode;
164 * Default constructor
166 public JobsUserWebRequestBean () {
167 // Call super constructor
172 * Event observer for newly added users by administrator
174 * @param event Event being fired
176 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
177 // event should not be null
180 throw new NullPointerException("event is null"); //NOI18N
181 } else if (event.getAddedUser() == null) {
183 throw new NullPointerException("event.addedUser is null"); //NOI18N
184 } else if (event.getAddedUser().getUserId() == null) {
186 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
187 } else if (event.getAddedUser().getUserId() < 1) {
189 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
193 this.updateList(event.getAddedUser());
199 this.setUserId(event.getAddedUser().getUserId());
203 * Event observer for deleted user accounts (by administrator)
205 * @param event Event being fired
207 public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) {
208 // event should not be null
211 throw new NullPointerException("event is null"); //NOI18N
212 } else if (event.getDeletedUser() == null) {
214 throw new NullPointerException("event.deletedUser is null"); //NOI18N
215 } else if (event.getDeletedUser().getUserId() == null) {
217 throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
218 } else if (event.getDeletedUser().getUserId() < 1) {
220 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
224 this.removeFromList(event.getDeletedUser());
231 * Event observer for linked users with existing contact data
233 * @param event Event being fired
235 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
236 // event should not be null
239 throw new NullPointerException("event is null"); //NOI18N
240 } else if (event.getLinkedUser() == null) {
242 throw new NullPointerException("event.linkedUser is null"); //NOI18N
243 } else if (event.getLinkedUser().getUserId() == null) {
245 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
246 } else if (event.getLinkedUser().getUserId() < 1) {
248 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
252 this.updateList(event.getLinkedUser());
258 this.setUserId(event.getLinkedUser().getUserId());
262 * Event observer for locked users
264 * @param event Event being fired
266 public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
267 // event should not be null
270 throw new NullPointerException("event is null"); //NOI18N
271 } else if (event.getLockedUser() == null) {
273 throw new NullPointerException("event.lockedUser is null"); //NOI18N
274 } else if (event.getLockedUser().getUserId() == null) {
276 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
277 } else if (event.getLockedUser().getUserId() < 1) {
279 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
283 this.updateList(event.getLockedUser());
287 * Event observer for unlocked users
289 * @param event Event being fired
291 public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
292 // event should not be null
295 throw new NullPointerException("event is null"); //NOI18N
296 } else if (event.getUnlockedUser() == null) {
298 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
299 } else if (event.getUnlockedUser().getUserId() == null) {
301 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
302 } else if (event.getUnlockedUser().getUserId() < 1) {
304 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
308 this.updateList(event.getUnlockedUser());
312 * Event observer for updated user data by administrator
314 * @param event Event being updated
316 public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
317 // event should not be null
320 throw new NullPointerException("event is null"); //NOI18N
321 } else if (event.getUpdatedUser() == null) {
323 throw new NullPointerException("event.updatedUser is null"); //NOI18N
324 } else if (event.getUpdatedUser().getUserId() == null) {
326 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
327 } else if (event.getUpdatedUser().getUserId() < 1) {
329 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
333 this.updateList(event.getUpdatedUser());
340 * Event observer for when a bean helper has successfully created a user
341 * instance, means the user exists. If the user does not exist, this event
342 * should not fire but instead a proper exception must be thrown.
344 * @param event User created event
346 public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
347 // Is the instance valid?
350 throw new NullPointerException("event is null"); //NOI18N
351 } else if (event.getCreatedUser() == null) {
353 throw new NullPointerException("event.createdUser is null"); //NOI18N
354 } else if (event.getCreatedUser().getUserId() == null) {
356 throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
357 } else if (event.getCreatedUser().getUserId() < 1) {
359 throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
363 final User user = event.getCreatedUser();
365 // Set all fields here
370 * Observer method for events being fired when the application's locale has
373 * @param event Event being fired
375 public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
376 // Is the parameter valid?
379 throw new NullPointerException("event is null");
380 } else if (event.getLocale() == null) {
382 throw new NullPointerException("event.locale is null");
386 this.setLocale(event.getLocale());
390 * Event observer when user confirmed account.
392 * @param event Event being fired
394 public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
395 // event should not be null
398 throw new NullPointerException("event is null"); //NOI18N
399 } else if (event.getConfirmedUser() == null) {
401 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
402 } else if (event.getConfirmedUser().getUserId() == null) {
404 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
405 } else if (event.getConfirmedUser().getUserId() < 1) {
407 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
411 this.updateList(event.getConfirmedUser());
415 * Event observer for logged-in user
417 * @param event Event instance
419 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
420 // event should not be null
423 throw new NullPointerException("event is null"); //NOI18N
424 } else if (event.getLoggedInUser() == null) {
426 throw new NullPointerException("event.registeredUser is null"); //NOI18N
427 } else if (event.getLoggedInUser().getUserId() == null) {
429 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
430 } else if (event.getLoggedInUser().getUserId() < 1) {
432 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
435 // "Cache" user instance
436 final User loggedInUser = event.getLoggedInUser();
438 // Copy all data to this bean
439 this.copyUser(loggedInUser);
443 * Event observer for user password changes
445 * @param event Event being fired
447 public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
451 throw new NullPointerException("event is null"); //NOI18N
452 } else if (event.getUserPassword() == null) {
454 throw new NullPointerException("event.userPassword is null"); //NOI18N
455 } else if (event.getUserPassword().isEmpty()) {
457 throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N
461 this.setUserPassword(event.getUserPassword());
462 this.setUserPasswordRepeat(event.getUserPassword());
466 * Event observer for new user registrations
468 * @param event User registration event
470 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
471 // event should not be null
474 throw new NullPointerException("event is null"); //NOI18N
475 } else if (event.getRegisteredUser() == null) {
477 throw new NullPointerException("event.registeredUser is null"); //NOI18N
478 } else if (event.getRegisteredUser().getUserId() == null) {
480 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
481 } else if (event.getRegisteredUser().getUserId() < 1) {
483 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
487 final User registeredUser = event.getRegisteredUser();
489 // Copy all data from registered->user
490 this.copyUser(registeredUser);
496 this.updateList(registeredUser);
499 this.addUserName(registeredUser);
502 this.setUserId(registeredUser.getUserId());
506 * Method being call after user's password has been updated (and history
507 * entry has been created).
509 * @param event Event being observed
511 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
515 throw new NullPointerException("event is null"); //NOI18N
516 } else if (event.getPasswordHistory() == null) {
518 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
519 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
521 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
522 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
524 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
528 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
532 * Listens to fired event when user updated personal data
534 * @param event Event being fired
536 public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
540 throw new NullPointerException("event is null"); //NOI18N
541 } else if (event.getUpdatedUser() == null) {
543 throw new NullPointerException("event.updatedUser is null"); //NOI18N
544 } else if (event.getUpdatedUser().getUserId() == null) {
546 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
547 } else if (event.getUpdatedUser().getUserId() < 1) {
549 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
553 this.updateList(event.getUpdatedUser());
557 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
558 public List<User> allUsers () {
560 final List<User> list = new LinkedList<>();
563 final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator();
566 while (iterator.hasNext()) {
568 final Cache.Entry<Long, User> next = iterator.next();
571 list.add(next.getValue());
579 * Event observer for when a user name should be cleared
581 * @param event Event being fired
583 public void clearUserNameEvent (@Observes final ObservableClearUserNameEvent event) {
587 throw new NullPointerException("event is null");
591 this.clearUserName();
595 * Event observer for when both user passwords should be cleared
597 * @param event Event being fired
599 public void clearUserPasswordEvent (@Observes final ObservableClearUserPasswordEvent event) {
603 throw new NullPointerException("event is null");
607 this.clearUserPasswords();
611 public User createUserInstance (final boolean createContactData) {
612 // Required personal data must be set
613 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
615 // Create new user instance
616 final User user = new LoginUser();
618 // Is user name required?
619 if (!this.isUserNameRequired()) {
620 // Generate pseudo-random user name
621 String randomName = this.userBean.generateRandomUserName();
623 // Set it and inivisible profile
624 this.setUserName(randomName);
625 this.setUserProfileMode(ProfileMode.INVISIBLE);
627 // Generate random password
628 String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
630 // Set random password
631 this.setUserPassword(randomPassword);
632 this.setUserPasswordRepeat(randomPassword);
635 // Set user name profile mode and locale
636 user.setUserName(this.getUserName());
637 user.setUserProfileMode(this.getUserProfileMode());
638 user.setUserLocale(this.getLocale());
640 // Is multiple registration page
641 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
642 // Create contact instance
643 final Contact contact = this.contactController.createContactInstance();
645 // Set contact in user
646 user.setUserContact(contact);
654 public User createUserLogin () {
656 if (this.getUserName() == null) {
658 throw new NullPointerException("userName is null"); //NOI18N
659 } else if (this.getUserName().isEmpty()) {
661 throw new IllegalStateException("userName is empty."); //NOI18N
664 // Create new user instance
665 final User user = new LoginUser();
667 // Update all data ...
668 user.setUserName(this.getUserName());
670 // Return the new instance
675 public String doChangePersonalData () {
676 // This method shall only be called if the user is logged-in
677 if (!this.userLoginController.isUserLoggedIn()) {
679 throw new IllegalStateException("User is not logged-in"); //NOI18N
680 } else if (!this.isRequiredChangePersonalDataSet()) {
681 // Not all required fields are set
682 throw new FaceletException("Not all required fields are set."); //NOI18N
683 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
684 // Password not matching
685 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
686 } else if (!this.featureController.isFeatureEnabled("change_user_personal_data")) { //NOI18N
687 // Editing is not allowed
688 throw new IllegalStateException("User tried to edit personal data."); //NOI18N
692 final User user = this.userLoginController.getLoggedInUser();
694 // Copy contact data to contact instance
695 this.contactController.updateContactDataFromController(user.getUserContact());
697 // It should be there, so run some tests on it
698 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
699 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
700 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
701 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
702 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
703 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
706 user.setUserProfileMode(this.getUserProfileMode());
708 // Send it to the EJB
709 final User updatedUser = this.userBean.updateUserPersonalData(user);
712 this.updatedPersonalDataEvent.fire(new UpdatedUserPersonalDataEvent(updatedUser));
715 return "user_contact_data_saved"; //NOI18N
723 public Long getUserId () {
730 * @param userId User id
732 public void setUserId (final Long userId) {
733 this.userId = userId;
737 * Getter for user name
741 public String getUserName () {
742 return this.userName;
746 * Setter for user name
748 * @param userName User name
750 public void setUserName (final String userName) {
751 this.userName = userName;
755 public String getUserPassword () {
756 return this.userPassword;
760 * Setter for clear-text user password
762 * @param userPassword Clear-text user password
764 public void setUserPassword (final String userPassword) {
765 this.userPassword = userPassword;
769 * Getter for clear-text user password repeated
771 * @return Clear-text user password repeated
773 public String getUserPasswordRepeat () {
774 return this.userPasswordRepeat;
778 * Setter for clear-text user password repeated
780 * @param userPasswordRepeat Clear-text user password repeated
782 public void setUserPasswordRepeat (final String userPasswordRepeat) {
783 this.userPasswordRepeat = userPasswordRepeat;
787 * Getter for user profile mode
789 * @return User profile mode
791 public ProfileMode getUserProfileMode () {
792 return this.userProfileMode;
796 * Setter for user profile mode
798 * @param userProfileMode User profile mode
800 public void setUserProfileMode (final ProfileMode userProfileMode) {
801 this.userProfileMode = userProfileMode;
805 public boolean ifBothPasswordsEmptyAllowed () {
806 // Check feature first
807 return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N
808 ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
809 ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
813 * Post-initialization of this class
816 public void init () {
818 if (!this.userCache.iterator().hasNext()) {
820 final List<User> list = this.userBean.allUsers();
823 for (final Iterator<User> iterator = list.iterator(); iterator.hasNext();) {
825 final User next = iterator.next();
828 this.userCache.put(next.getUserId(), next);
829 this.userNameCache.put(next.getUserId(), next.getUserName());
835 public boolean isContactFound (final Contact contact) {
836 // The contact must be valid
837 if (null == contact) {
839 throw new NullPointerException("contact is null"); //NOI18N
840 } else if (contact.getContactId() == null) {
842 throw new NullPointerException("contact.contactId is null"); //NOI18N
843 } else if (contact.getContactId() < 1) {
845 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
848 // Default is not found
849 boolean isFound = false;
852 final Iterator<User> iterator = this.allUsers().iterator();
854 // Loop through all entries
855 while (iterator.hasNext()) {
857 final User next = iterator.next();
859 // Compare both objects
860 if (Objects.equals(contact, next.getUserContact())) {
872 public boolean isPublicUserProfileEnabled () {
873 // Get context parameter
874 final String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
877 final boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
879 // This requires user names being enabled, too.
880 if ((isEnabled) && (!this.isUserNameRequired())) {
881 // Not valid state, users must be able to modify their profile, especially when it is public
882 throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
890 public boolean isRequiredChangePersonalDataSet () {
891 return ((this.getUserProfileMode() != null) &&
892 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
893 (this.contactController.isRequiredChangePersonalDataSet()));
897 public boolean isRequiredPersonalDataSet () {
898 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
899 // Multiple registration page
900 return this.contactController.isRequiredPersonalDataSet();
902 // Single registration page
903 return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
904 (this.getUserProfileMode() != null) &&
905 (this.contactController.isRequiredPersonalDataSet()) &&
906 (this.getUserPassword() != null) &&
907 (this.getUserPasswordRepeat() != null));
912 public boolean isSamePasswordEntered () {
913 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
917 public boolean isUserIdEmpty () {
918 return ((this.getUserId() == null) || (this.getUserId() == 0));
922 public boolean isUserNameRegistered (final User user) {
923 return ((this.userNameCache instanceof List) && (this.userNameCache.containsKey(user.getUserId())));
927 public boolean isUserNameRequired () {
928 // Get context parameter
929 final String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_login_require_user_name"); //NOI18N
932 final boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
939 public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
940 // Parameter must be valid
941 if (null == emailAddress) {
943 throw new NullPointerException("emailAddress is null"); //NOI18N
944 } else if (emailAddress.isEmpty()) {
946 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
952 // Try to lookup it in visible user list
953 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
955 final Cache.Entry<Long, User> next = iterator.next();
957 // Contact should be set
958 if (next.getValue().getUserContact() == null) {
960 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getKey())); //NOI18N
961 } else if (next.getValue().getUserContact().getContactEmailAddress() == null) {
962 // Email address should be set
963 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getKey())); //NOI18N
966 // Is the email address found?
967 if (Objects.equals(next.getValue().getUserContact().getContactEmailAddress(), emailAddress)) {
968 // Copy to other variable
969 user = next.getValue();
976 // Not visible for the current user
977 throw new UserEmailAddressNotFoundException(emailAddress);
985 public User lookupUserById (final Long userId) throws UserNotFoundException {
986 // Parameter must be valid
987 if (null == userId) {
989 throw new NullPointerException("userId is null"); //NOI18N
990 } else if (userId < 1) {
992 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
998 // Try to lookup it in visible user list
999 for (final Iterator<Cache.Entry<Long, User>> iterator = this.userCache.iterator(); iterator.hasNext();) {
1001 final Cache.Entry<Long, User> next = iterator.next();
1003 // Is the user id found?
1004 if (Objects.equals(next.getKey(), userId)) {
1005 // Copy to other variable
1006 user = next.getValue();
1011 // Is it still null?
1013 // Not visible for the current user
1014 throw new UserNotFoundException(userId);
1022 * Adds user's name to bean's internal list. It also updates the public user
1023 * list if the user has decided to have a public account,
1025 * @param user User instance
1027 private void addUserName (final User user) {
1028 // Make sure the entry is not added yet
1029 if (this.userNameCache.containsKey(user.getUserId())) {
1031 throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
1035 this.userNameCache.put(user.getUserId(), user.getUserName());
1041 private void clear () {
1044 this.setUserId(null);
1045 this.setUserProfileMode(null);
1048 this.clearUserName();
1049 this.clearUserPasswords();
1050 this.setLocale(null);
1056 private void clearUserName () {
1058 this.setUserName(null);
1062 * Clears both user passwords
1064 private void clearUserPasswords () {
1066 this.setUserPassword(null);
1067 this.setUserPasswordRepeat(null);
1071 * Copies given user into the controller
1073 * @param user User instance
1075 private void copyUser (final User user) {
1076 // Make sure the instance is valid
1079 throw new NullPointerException("user is null"); //NOI18N
1080 } else if (user.getUserContact() == null) {
1082 throw new NullPointerException("user.userContact is null"); //NOI18N
1087 this.setUserId(user.getUserId());
1088 this.setUserProfileMode(user.getUserProfileMode());
1092 * Getter for locale instance
1094 * @return Locale instance
1096 private Locale getLocale () {
1101 * Setter for locale instance
1103 * @param locale Locale instance
1105 private void setLocale (final Locale locale) {
1106 this.locale = locale;
1110 * Removes user from all lists
1112 * @param user User to remove
1114 private void removeFromList (final User user) {
1115 // The user should be valid
1118 throw new NullPointerException("user is null"); //NOI18N
1119 } else if (user.getUserId() == null) {
1121 throw new NullPointerException("user.userId is null"); //NOI18N
1122 } else if (user.getUserId() < 1) {
1124 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1127 // Remove it from lists
1128 this.userCache.remove(user.getUserId());
1130 // Remove name from list
1131 this.userNameCache.remove(user.getUserId());
1135 * Updates list with given user instance
1137 * @param user User instance
1139 private void updateList (final User user) {
1140 // The user should be valid
1143 throw new NullPointerException("user is null"); //NOI18N
1144 } else if (user.getUserId() == null) {
1146 throw new NullPointerException("user.userId is null"); //NOI18N
1147 } else if (user.getUserId() < 1) {
1149 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
1150 } else if (user.getUserContact() == null) {
1152 throw new NullPointerException("user.userContact is null"); //NOI18N
1153 } else if (user.getUserContact().getContactId() == null) {
1155 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
1156 } else if (user.getUserContact().getContactId() < 1) {
1158 throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N
1162 this.userCache.put(user.getUserId(), user);