*/
package org.mxchange.jjobs.beans.user.register;
+import java.util.Objects;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
+import javax.enterprise.event.Observes;
import javax.enterprise.inject.Any;
import javax.faces.application.FacesMessage;
import javax.faces.view.facelets.FaceletException;
import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
+import org.mxchange.jusercore.model.user.LoginUser;
import org.mxchange.jusercore.model.user.User;
+import org.mxchange.jusercore.model.user.Users;
import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
+import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
import org.mxchange.jusercore.model.user.status.UserAccountStatus;
import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
import org.mxchange.juserlogincore.events.registration.UserRegisteredEvent;
*/
private static final long serialVersionUID = 47_828_986_719_691_592L;
- /**
- * An event being fired when a user name should be cleared
- */
- @Inject
- @Any
- private Event<ObservableClearUserNameEvent> clearUserNameEvent;
-
- /**
- * An event being fired when a user password should be cleared
- */
- @Inject
- @Any
- private Event<ObservableClearUserPasswordEvent> clearUserPasswordEvent;
-
/**
* Contact controller
*/
@Inject
private JobsFeaturesWebApplicationController featureController;
+ /**
+ * Localization controller
+ */
+ @Inject
+ private FinancialsLocalizationSessionController localizationController;
+
/**
* Remote register session-scoped bean
*/
private UserRegistrationSessionBeanRemote registerBean;
/**
- * User controller
+ * User list controller
*/
@Inject
- private JobsUserWebRequestController userController;
+ private FinancialsUserListWebViewController userListController;
/**
- * User list controller
+ * User name
*/
- @Inject
- private JobsUserListWebViewController userListController;
+ private String userName;
+
+ /**
+ * User password (clear-text from web form)
+ */
+ private String userPassword;
/**
* An event being fired when a user password was changed
@Any
private Event<ObservableUpdatedUserPasswordEvent> userPasswordChangedEvent;
+ /**
+ * User password repeated (clear-text from web form)
+ */
+ private String userPasswordRepeat;
+
+ /**
+ * Whether the user wants a public profile
+ */
+ private ProfileMode userProfileMode;
+
/**
* An event being fired when a new user has registered
*/
super();
}
+ /**
+ * Event observer for user password changes
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterUserPasswordChangedEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
+ // Is it valid?
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getUserPassword() == null) {
+ // Throw NPE
+ throw new NullPointerException("event.userPassword is null"); //NOI18N
+ } else if (event.getUserPassword().isEmpty()) {
+ // Throw NPE
+ throw new IllegalArgumentException("event.userPassword is empty"); //NOI18N
+ }
+
+ // Set it here
+ this.setUserPassword(event.getUserPassword());
+ this.setUserPasswordRepeat(event.getUserPassword());
+ }
+
/**
* Registers the user, if not found. Otherwise this method should throw an
* exception.
}
// Get user instance
- final User user = this.userController.createUserInstance(true);
+ final User user = this.createUserInstance(true);
// Null random password means registration requires user-entered password
String randomPassword = null;
if (null == user) {
// user must be set
throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
- } else if (!this.userController.isRequiredPersonalDataSet()) {
+ } else if (!this.isRequiredPersonalDataSet()) {
// Not all required fields are set
throw new FaceletException("Not all required fields are set."); //NOI18N
} else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(user))) { //NOI18N
// User name is already used, should not happen here
throw new FaceletException(new UserNameAlreadyRegisteredException(user));
} else {
- // May happen here, fire event
- this.clearUserNameEvent.fire(new ClearUserNameEvent());
+ // May happen here, clear user name
+ this.clearUserName();
// Output message
this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING", FacesMessage.SEVERITY_INFO); //NOI18N
return ""; //NOI18N
}
- } else if (!this.userController.isSamePasswordEntered()) {
+ } else if (!this.isSamePasswordEntered()) {
// Is multi-page enabled?
if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
// Not same password entered, should no longer happen here
throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
- } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
+ } else if (this.ifBothPasswordsEmptyAllowed()) {
// Both passwords are left empty and is allowed, then generate a random password
randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
}
// Encrypt password
- final String encryptedPassword = UserLoginUtils.encryptPassword(this.userController.getUserPassword());
+ final String encryptedPassword = UserLoginUtils.encryptPassword(this.getUserPassword());
// Set it here
user.setUserEncryptedPassword(encryptedPassword);
}
// Get user instance
- final User user = this.userController.createUserInstance(false);
+ final User user = this.createUserInstance(false);
// First check if user is not null and user name is not used + if same email address is entered
if (null == user) {
throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
} else if ((this.featureController.isFeatureEnabled("user_login_require_user_name")) && (this.userListController.isUserNameRegistered(user))) { //NOI18N
// User name is already used, so clear it
- this.clearUserNameEvent.fire(new ClearUserNameEvent());
+ this.clearUserName();
// Output message
this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED", FacesMessage.SEVERITY_WARN); //NOI18N
this.contactController.clearEmailAddresses();
this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING", FacesMessage.SEVERITY_WARN); //NOI18N
return ""; //NOI18N
- } else if (!this.userController.isSamePasswordEntered()) {
+ } else if (!this.isSamePasswordEntered()) {
// Is multi-page enabled?
if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
- // Unset both
- this.clearUserPasswordEvent.fire(new ClearUserPasswordEvent());
+ // Clear passwords
+ this.clearUserPasswords();
// Output faces message
this.showFacesMessage("form_register_page1:userPassword", "ERROR_USER_PASSWORD_EMPTY", FacesMessage.SEVERITY_WARN); //NOI18N
this.showFacesMessage("form_register_page1:userPasswordRepeat", "ERROR_USER_PASSWORD_REPEAT_EMPTY", FacesMessage.SEVERITY_WARN); //NOI18N
return ""; //NOI18N
- } else if (this.userController.ifBothPasswordsEmptyAllowed()) {
+ } else if (this.ifBothPasswordsEmptyAllowed()) {
// Both passwords are left empty and is allowed, then generate a random password
- String randomPassword = UserLoginUtils.createRandomPassword(JobsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
+ final String randomPassword = UserLoginUtils.createRandomPassword(FinancialsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
// Generate (ignored) password-history
- PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
+ final PasswordHistory passwordHistory = new UserPasswordHistory(randomPassword, user);
// Fire event
this.userPasswordChangedEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, randomPassword));
return "user_register_page2"; //NOI18N
}
+ /**
+ * Getter for user name
+ * <p>
+ * @return User name
+ */
+ public String getUserName () {
+ return this.userName;
+ }
+
+ /**
+ * Setter for user name
+ * <p>
+ * @param userName User name
+ */
+ public void setUserName (final String userName) {
+ this.userName = userName;
+ }
+
+ /**
+ * Getter for clear-text user password
+ * <p>
+ * @return Clear-text user password
+ */
+ public String getUserPassword () {
+ return this.userPassword;
+ }
+
+ /**
+ * Setter for clear-text user password
+ * <p>
+ * @param userPassword Clear-text user password
+ */
+ public void setUserPassword (final String userPassword) {
+ this.userPassword = userPassword;
+ }
+
+ /**
+ * Getter for clear-text user password repeated
+ * <p>
+ * @return Clear-text user password repeated
+ */
+ public String getUserPasswordRepeat () {
+ return this.userPasswordRepeat;
+ }
+
+ /**
+ * Setter for clear-text user password repeated
+ * <p>
+ * @param userPasswordRepeat Clear-text user password repeated
+ */
+ public void setUserPasswordRepeat (final String userPasswordRepeat) {
+ this.userPasswordRepeat = userPasswordRepeat;
+ }
+
+ /**
+ * Getter for user profile mode
+ * <p>
+ * @return User profile mode
+ */
+ public ProfileMode getUserProfileMode () {
+ return this.userProfileMode;
+ }
+
+ /**
+ * Setter for user profile mode
+ * <p>
+ * @param userProfileMode User profile mode
+ */
+ public void setUserProfileMode (final ProfileMode userProfileMode) {
+ this.userProfileMode = userProfileMode;
+ }
+
+ @Override
+ public boolean isRequiredChangePersonalDataSet () {
+ return ((this.getUserProfileMode() != null) &&
+ (this.getUserName() != null) &&
+ (!this.getUserName().isEmpty()) &&
+ (this.contactController.isRequiredChangePersonalDataSet()));
+ }
+
+ /**
+ * Clears user name
+ */
+ private void clearUserName () {
+ // Clear it
+ this.setUserName(null);
+ }
+
+ /**
+ * Clears both user passwords
+ */
+ private void clearUserPasswords () {
+ // Clear both
+ this.setUserPassword(null);
+ this.setUserPasswordRepeat(null);
+ }
+
+ /**
+ * Creates an instance from all properties
+ * <p>
+ * @param createContactData Whether contact data should be created
+ * <p>
+ * @return A user instance
+ */
+ private User createUserInstance (final boolean createContactData) {
+ // Required personal data must be set
+ assert (this.isRequiredPersonalDataSet()) : "All required personal data must be set before invoking this method."; //NOI18N
+
+ // Is user name required?
+ if (!this.featureController.isFeatureEnabled("user_login_require_username")) {
+ // Init variables
+ String randomName = null;
+ boolean isUsernameFree = false;
+
+ // Get full list
+ for (final User user : this.userListController.getAllUsers()) {
+ // Loop until a user name is found
+ while ((randomName == null) || (randomName.equals(user.getUserName()))) {
+ // Generate random name
+ randomName = Users.generateRandomUserName();
+ isUsernameFree = true;
+ }
+
+ // Is non-existing username found
+ if (isUsernameFree) {
+ // Also stop looping here
+ break;
+ }
+ }
+
+ // Set it and inivisible profile
+ this.setUserName(randomName);
+ this.setUserProfileMode(ProfileMode.INVISIBLE);
+
+ // Generate random password
+ final String randomPassword = UserLoginUtils.createRandomPassword(FinancialsUserWebRequestController.MINIMUM_PASSWORD_LENGTH);
+
+ // Set random password
+ this.setUserPassword(randomPassword);
+ this.setUserPasswordRepeat(randomPassword);
+ }
+
+ // Create new user instance
+ final User user = new LoginUser();
+
+ // Set user name profile mode and locale
+ user.setUserName(this.getUserName());
+ user.setUserProfileMode(this.getUserProfileMode());
+ user.setUserLocale(this.localizationController.getLocale());
+
+ // Is multiple registration page
+ if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
+ // Create contact instance
+ final Contact contact = this.contactController.createContactInstance();
+
+ // Set contact in user
+ user.setUserContact(contact);
+ }
+
+ // Return it
+ return user;
+ }
+
+ /**
+ * Checks if both user passwords are left empty and if this is enabled
+ * (allowed) in context parameter. If true, the calling bean should create a
+ * random password (preferable with UserUtils.createRandomPassword() and set
+ * it in both user password fields.
+ * <p>
+ * @return Whether empty passwords are allowed
+ */
+ private boolean ifBothPasswordsEmptyAllowed () {
+ // Check feature first
+ return ((this.featureController.isFeatureEnabled("allow_user_registration_empty_password")) && //NOI18N
+ ((this.getUserPassword() == null) || (this.getUserPassword().isEmpty())) &&
+ ((this.getUserPasswordRepeat() == null) || (this.getUserPasswordRepeat().isEmpty())));
+ }
+
+ /**
+ * Checks whether all required personal data is set
+ * <p>
+ * @return Whether the required personal data is set
+ */
+ private boolean isRequiredPersonalDataSet () {
+ // Check conditions based on of multi-page registration is enabled
+ if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
+ // Multiple registration page
+ return this.contactController.isRequiredPersonalDataSet();
+ } else {
+ // Single registration page
+ return (((this.getUserName() != null) || (!this.featureController.isFeatureEnabled("user_login_require_username"))) && //NOI18N
+ (this.getUserProfileMode() != null) &&
+ (this.contactController.isRequiredPersonalDataSet()) &&
+ (this.getUserPassword() != null) &&
+ (this.getUserPasswordRepeat() != null));
+ }
+ }
+
+ /**
+ * Checks whether same passwords has been entered
+ * <p>
+ * @return Whether same passwords has been entered
+ */
+ private boolean isSamePasswordEntered () {
+ return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
+ }
+
}