From: Roland Häder Date: Fri, 23 Jun 2017 21:27:37 +0000 (+0200) Subject: They are all user-related controllers/beans X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=0e592d182756c4c239c6d3e4884651a06f905dc3;p=addressbook-war.git They are all user-related controllers/beans Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/addressbook/beans/confirmlink/AddressbookConfirmationLinkWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/confirmlink/AddressbookConfirmationLinkWebRequestBean.java deleted file mode 100644 index 93662298..00000000 --- a/src/java/org/mxchange/addressbook/beans/confirmlink/AddressbookConfirmationLinkWebRequestBean.java +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.beans.confirmlink; - -import java.text.MessageFormat; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; -import javax.annotation.PostConstruct; -import javax.enterprise.context.RequestScoped; -import javax.enterprise.event.Event; -import javax.enterprise.inject.Any; -import javax.faces.view.facelets.FaceletException; -import javax.inject.Inject; -import javax.inject.Named; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.helper.AddressbookWebRequestHelperController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; -import org.mxchange.jcoreee.utils.FacesUtils; -import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent; -import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent; -import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; -import org.mxchange.jusercore.exceptions.UserStatusLockedException; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.UserSessionBeanRemote; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; - -/** - * A web request bean for confirmation link handling - *

- * @author Roland Häder - */ -@Named ("confirmationLinkController") -@RequestScoped -public class AddressbookConfirmationLinkWebRequestBean extends BaseAddressbookController implements AddressbookConfirmationLinkWebRequestController { - - /** - * Serial number - */ - private static final long serialVersionUID = 57_637_182_796_370L; - - /** - * Bean helper instance - */ - @Inject - private AddressbookWebRequestHelperController beanHelper; - - /** - * Confirmation key - */ - private String confirmationKey; - - /** - * Remote user bean - */ - private UserSessionBeanRemote userBean; - - /** - * Event being fired when a user has confirmed the account - */ - @Inject - @Any - private Event userConfirmedEvent; - - /** - * User controller - */ - @Inject - private AddressbookUserWebSessionController userController; - - /** - * Default constructor - */ - public AddressbookConfirmationLinkWebRequestBean () { - // Call super constructor - super(); - } - - @Override - public String getConfirmationKey () { - return this.confirmationKey; - } - - @Override - public void setConfirmationKey (final String confirmationKey) { - this.confirmationKey = confirmationKey; - } - - /** - * Post-construction method - */ - @PostConstruct - public void init () { - // Try it - try { - // Get initial context - Context context = new InitialContext(); - - // Try to lookup - this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N - } catch (final NamingException e) { - // Throw again - throw new FaceletException(e); - } - } - - @Override - public void maybeConfirmUserAccount () { - // Is the confirmation key set? - if (this.getConfirmationKey() == null) { - // May be null if not set - return; - } else if (this.getConfirmationKey().isEmpty()) { - // Is empty string - return; - } - - // Now try to find the user in user list, first get the whole list - List users = this.userController.allUsers(); - - // Get iterator from it - Iterator iterator = users.iterator(); - - // Init instance - User user = null; - - // Then loop through all - while (iterator.hasNext()) { - // Get next user - User next = iterator.next(); - - // Same confirmation key? - if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) { - // Found it, then set it and abort loop - user = next; - break; - } - } - - // Is the user instance null? - if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) { - // Then clear this bean and the helper - this.beanHelper.setUser(null); - } else { - // Set user ... - this.beanHelper.setUser(user); - - // ... and copy it to the controller - this.beanHelper.copyUserToController(); - - // Try to confirm it - this.confirmUserAccount(); - } - } - - /** - * Tries to confirm the currently set user instance (in helper). - */ - private void confirmUserAccount () { - // Get user instance - User user = this.beanHelper.getUser(); - - // Should be set - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); - } else if (user.getUserId() == null) { - // Abort here - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid number - throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N - } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { - // Account is already confirmed - throw new FaceletException(new UserStatusConfirmedException(user)); - } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { - // Account is already confirmed - throw new FaceletException(new UserStatusLockedException(user)); - } else if (user.getUserConfirmKey() == null) { - // Throw NPE - throw new NullPointerException("user.userConfirmKey is null"); //NOI18N - } - - // Updated user instance - User updatedUser; - - try { - // Get base URL - String baseUrl = FacesUtils.generateBaseUrl(); - - // Confirm account - updatedUser = this.userBean.confirmAccount(user, baseUrl); - } catch (final UserStatusConfirmedException | UserStatusLockedException ex) { - // Something unexpected happened - throw new FaceletException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N - } - - // Fire event that the user has confirmed account - this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser)); - - // Set it again in helper - this.beanHelper.setUser(updatedUser); - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/confirmlink/AddressbookConfirmationLinkWebRequestController.java b/src/java/org/mxchange/addressbook/beans/confirmlink/AddressbookConfirmationLinkWebRequestController.java deleted file mode 100644 index 9a187d5f..00000000 --- a/src/java/org/mxchange/addressbook/beans/confirmlink/AddressbookConfirmationLinkWebRequestController.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.beans.confirmlink; - -import java.io.Serializable; - -/** - * An interface for an email change controller - *

- * @author Roland Häder - */ -public interface AddressbookConfirmationLinkWebRequestController extends Serializable { - - /** - * Getter for confirmation key - *

- * @return Confirmation key - */ - String getConfirmationKey (); - - /** - * Setter for confirmation key - *

- * @param confirmationKey Confirmation key - */ - void setConfirmationKey (final String confirmationKey); - - /** - * Tries to lookup the user by currently set confirmation key and if found - * tries to confirm it. If no user is found, the instance beanHelper.user is - * set to null. Other methods or JSF pages should then respond on this - * accordingly. - */ - void maybeConfirmUserAccount (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/email_address/AddressbookEmailChangeWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/email_address/AddressbookEmailChangeWebSessionBean.java deleted file mode 100644 index 0127ca19..00000000 --- a/src/java/org/mxchange/addressbook/beans/email_address/AddressbookEmailChangeWebSessionBean.java +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.beans.email_address; - -import java.text.MessageFormat; -import java.util.List; -import java.util.Objects; -import javax.annotation.PostConstruct; -import javax.enterprise.context.SessionScoped; -import javax.faces.view.facelets.FaceletException; -import javax.inject.Inject; -import javax.inject.Named; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; -import org.mxchange.addressbook.beans.login.user.AddressbookUserLoginWebSessionController; -import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jcoreee.utils.FacesUtils; -import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; -import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress; -import org.mxchange.jusercore.model.email_address.EmailAddressChange; -import org.mxchange.jusercore.model.email_address.UserEmailChangeSessionBeanRemote; -import org.mxchange.jusercore.model.user.User; - -/** - * A web session-scoped bean for changing email addresses - *

- * @author Roland Häder - */ -@Named ("userEmailChangeController") -@SessionScoped -public class AddressbookEmailChangeWebSessionBean extends BaseAddressbookController implements AddressbookEmailChangeWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 186_078_724_659_153L; - - /** - * Email address 1 (changing) - */ - private String emailAddress; - - /** - * Email address 2 (repeat in changing) - */ - private String emailAddressRepeat; - - /** - * Local list of already queued email addresses - */ - private List emailAddresses; - - /** - * Remote email change bean - */ - private UserEmailChangeSessionBeanRemote emailChangeBean; - - /** - * Features controller - */ - @Inject - private AddressbookFeaturesWebApplicationController featureController; - - /** - * Login controller (bean) - */ - @Inject - private AddressbookUserLoginWebSessionController userLoginController; - - /** - * Default constructor - */ - public AddressbookEmailChangeWebSessionBean () { - // Call super constructor - super(); - } - - @Override - public String doUserChangeEmailAddress () { - // This method shall only be called if the user is logged-in - if (!this.userLoginController.isUserLoggedIn()) { - // Not logged-in - throw new IllegalStateException("User is not logged-in"); //NOI18N - } else if (!this.featureController.isFeatureEnabled("user_change_email_address")) { //NOI18N - // Editing is not allowed - throw new IllegalStateException("User tried to change email address"); //NOI18N - } else if (!this.isRequiredChangeEmailAddressSet()) { - // Not all required fields are set - throw new FaceletException("Not all required fields are set."); //NOI18N - } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) { - // Email address 1+2 mismatch - this.showFacesMessage("form_user_change_email_address:emailAddressRepeat", "ERROR_USER_EMAIL_ADDRESSES_MISMATCH"); //NOI18N - return ""; //NOI18N - } else if (!this.userLoginController.ifCurrentPasswordMatches()) { - // Password not matching - this.showFacesMessage("form_login_user_change_email_address:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N - return ""; //NOI18N - } - - // Get user instance - User user = this.userLoginController.getLoggedInUser(); - - // It should be there, so run some tests on it - assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N - assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N - assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N - assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N - assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N - assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N - - // Check if the email address is already enqueued - if (this.isEmailAddressQueued(this.getEmailAddress())) { - // Clear both email addresses - this.setEmailAddress(null); - this.setEmailAddressRepeat(null); - - // Yes, then abort here - this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N - return ""; //NOI18N - } - - // Create change object, to save EJB calls, the hash is not generated here - ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress()); - - // Get base URL - String baseUrl = FacesUtils.generateBaseUrl(); - - // Call EJB - this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl); - - // Unset all so the user is forced to re-enter it - this.clear(); - - // All fine - return "user_login_email_change_queued"; //NOI18N - } - - @Override - public String getEmailAddress () { - return this.emailAddress; - } - - @Override - public void setEmailAddress (final String emailAddress) { - this.emailAddress = emailAddress; - } - - @Override - public String getEmailAddressRepeat () { - return this.emailAddressRepeat; - } - - @Override - public void setEmailAddressRepeat (final String emailAddressRepeat) { - this.emailAddressRepeat = emailAddressRepeat; - } - - /** - * Post-construction - */ - @PostConstruct - public void init () { - // Try it - try { - // Get initial context - Context context = new InitialContext(); - - // Try to lookup - this.emailChangeBean = (UserEmailChangeSessionBeanRemote) context.lookup("java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote"); //NOI18N - } catch (final NamingException e) { - // Throw again - throw new FaceletException(e); - } - - // Init list - this.emailAddresses = this.emailChangeBean.allQueuedAddresses(); - } - - @Override - public boolean isRequiredChangeEmailAddressSet () { - return ((this.getEmailAddress() != null) && - (this.getEmailAddressRepeat() != null)); - } - - /** - * Clears email address fields so the user has to re-enter them - */ - private void clear () { - // Clear fields - this.setEmailAddress(null); - this.setEmailAddressRepeat(null); - } - - /** - * Checks if given email address has already been queued. First a local list - * is being checked, if not found, the EJB is called. Only if found, the - * result is "cached" in the list. - *

- * @param emailAddress Email address to verify - *

- * @return Whether the email address in field emailAddress is already queued - */ - private boolean isEmailAddressQueued (final String emailAddress) { - // It should be there - assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N - assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N - - // Check list - if (this.emailAddresses.contains(emailAddress)) { - // Okay, found it - return true; - } - - // Check EJB - boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress); - - // Is it there? - if (isQueued) { - // Add to list - this.emailAddresses.add(emailAddress); - } - - // Return status - return isQueued; - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/email_address/AddressbookEmailChangeWebSessionController.java b/src/java/org/mxchange/addressbook/beans/email_address/AddressbookEmailChangeWebSessionController.java deleted file mode 100644 index 65f0573d..00000000 --- a/src/java/org/mxchange/addressbook/beans/email_address/AddressbookEmailChangeWebSessionController.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.beans.email_address; - -import java.io.Serializable; - -/** - * An interface for an email change controller - *

- * @author Roland Häder - */ -public interface AddressbookEmailChangeWebSessionController extends Serializable { - - /** - * Getter for email address 1 (changing) - *

- * @return Email address - */ - String getEmailAddress (); - - /** - * Setter for email address 1 (changing) - *

- * @param emailAddress Email address 1 - */ - void setEmailAddress (final String emailAddress); - - /** - * Getter for email address 2 (repeat changing) - *

- * @return Email address 2 - */ - String getEmailAddressRepeat (); - - /** - * Setter for email address 2 (repeat changing) - *

- * @param emailAddressRepeat Email address 2 - */ - void setEmailAddressRepeat (final String emailAddressRepeat); - - /** - * Checks whether all required are set for changing email address - *

- * @return Whether the required personal data is set - */ - boolean isRequiredChangeEmailAddressSet (); - - /** - * Changes logged-in user's email address if the current password matches. - *

- * @return New target page - */ - String doUserChangeEmailAddress (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkWebSessionBean.java deleted file mode 100644 index a6c5cb8c..00000000 --- a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkWebSessionBean.java +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.beans.resendlink; - -import javax.annotation.PostConstruct; -import javax.enterprise.context.SessionScoped; -import javax.enterprise.event.Event; -import javax.enterprise.inject.Any; -import javax.faces.view.facelets.FaceletException; -import javax.inject.Inject; -import javax.inject.Named; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import org.mxchange.addressbook.beans.BaseAddressbookController; -import org.mxchange.addressbook.beans.localization.AddressbookLocalizationSessionController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; -import org.mxchange.jcoreee.utils.FacesUtils; -import org.mxchange.jusercore.events.resendlink.ObservableUserResendLinkAccountEvent; -import org.mxchange.jusercore.events.resendlink.UserResendLinkAccountEvent; -import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; -import org.mxchange.jusercore.exceptions.UserStatusLockedException; -import org.mxchange.jusercore.model.resendlink.ResendLinkSessionBeanRemote; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; - -/** - * A web session-scoped bean for resending confirmation link - *

- * @author Roland Häder - */ -@Named ("resendController") -@SessionScoped -public class AddressbookResendLinkWebSessionBean extends BaseAddressbookController implements AddressbookResendLinkWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 186_078_724_659_153L; - - /** - * Email address - */ - private String emailAddress; - - /** - * Localization controller - */ - @Inject - private AddressbookLocalizationSessionController localizationController; - - /** - * EJB for resending confirmation link - */ - private ResendLinkSessionBeanRemote resendLinkBean; - - /** - * Regular user controller - */ - @Inject - private AddressbookUserWebSessionController userController; - - /** - * Event being fired after confirmation link is being sent - */ - @Inject - @Any - private Event userResendLinkEvent; - - /** - * Default constructor - */ - public AddressbookResendLinkWebSessionBean () { - // Call super constructor - super(); - } - - @Override - public String doResendLink () { - // The email address should not be empty as the JSF validates this - if (this.getEmailAddress() == null) { - // Throw NPE - throw new NullPointerException("this.emailAddress is null"); //NOI18N - } - - // Init user instance - User user; - - try { - // Is the email address really not used? - user = this.userController.lookupUserByEmailAddress(this.getEmailAddress()); - } catch (final UserEmailAddressNotFoundException ex) { - // Always clear bean - this.clear(); - - // Not found, should not happen as the registered validator should find it - this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N - return ""; //NOI18N - } - - // Is the user account already confirmed? - if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { - // Always clear bean - this.clear(); - - // Then abort here - this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N - return ""; //NOI18N - } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { - // Always clear bean - this.clear(); - - // User account is locked - this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N - return ""; //NOI18N - } else if (user.getUserConfirmKey() == null) { - // Status is UNCONFIRMED but confirmation key is NULL - throw new NullPointerException("user.userConfirmKey is null"); //NOI18N - } - - try { - // Get base URL - String baseUrl = FacesUtils.generateBaseUrl(); - - // Call EJB and return redirect target - this.resendLinkBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl); - } catch (final UserNotFoundException ex) { - // User not found - this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N - return ""; //NOI18N - } catch (final UserStatusLockedException | UserStatusConfirmedException ex) { - // Output message, this should not happen as the confirmation key is being removed - this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N - return ""; //NOI18N - } - - // Fire event - this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(user)); - - // Clear this bean - this.clear(); - - // Return redirect target - return "user_resend_done"; //NOI18N - } - - @Override - public String getEmailAddress () { - return this.emailAddress; - } - - @Override - public void setEmailAddress (final String emailAddress) { - this.emailAddress = emailAddress; - } - - /** - * Post-construction method - */ - @PostConstruct - public void init () { - // Try it - try { - // Get initial context - Context context = new InitialContext(); - - // Try to lookup - this.resendLinkBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/addressbook-ejb/resendLink!org.mxchange.jusercore.model.resendlink.ResendLinkSessionBeanRemote"); //NOI18N - } catch (final NamingException e) { - // Throw again - throw new FaceletException(e); - } - } - - /** - * Clears email address fields so the user has to re-enter them - */ - private void clear () { - // Clear fields - this.setEmailAddress(null); - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkWebSessionController.java b/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkWebSessionController.java deleted file mode 100644 index 69616e5e..00000000 --- a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkWebSessionController.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.addressbook.beans.resendlink; - -import java.io.Serializable; - -/** - * An interface for an email change controller - *

- * @author Roland Häder - */ -public interface AddressbookResendLinkWebSessionController extends Serializable { - - /** - * Getter for email address 1 (changing) - *

- * @return Email address - */ - String getEmailAddress (); - - /** - * Setter for email address 1 (changing) - *

- * @param emailAddress Email address 1 - */ - void setEmailAddress (final String emailAddress); - - /** - * Resends (new) confirmation link to given email address, if found. - * Otherwise an exception is thrown. On success a redirect takes place. - *

- * @return Redirection target - */ - String doResendLink (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestBean.java b/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestBean.java new file mode 100644 index 00000000..27bd6dbc --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestBean.java @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.beans.user.confirmlink; + +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import javax.annotation.PostConstruct; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.helper.AddressbookWebRequestHelperController; +import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; +import org.mxchange.jcoreee.utils.FacesUtils; +import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent; +import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent; +import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserSessionBeanRemote; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; + +/** + * A web request bean for confirmation link handling + *

+ * @author Roland Häder + */ +@Named ("confirmationLinkController") +@RequestScoped +public class AddressbookConfirmationLinkWebRequestBean extends BaseAddressbookController implements AddressbookConfirmationLinkWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 57_637_182_796_370L; + + /** + * Bean helper instance + */ + @Inject + private AddressbookWebRequestHelperController beanHelper; + + /** + * Confirmation key + */ + private String confirmationKey; + + /** + * Remote user bean + */ + private UserSessionBeanRemote userBean; + + /** + * Event being fired when a user has confirmed the account + */ + @Inject + @Any + private Event userConfirmedEvent; + + /** + * User controller + */ + @Inject + private AddressbookUserWebSessionController userController; + + /** + * Default constructor + */ + public AddressbookConfirmationLinkWebRequestBean () { + // Call super constructor + super(); + } + + @Override + public String getConfirmationKey () { + return this.confirmationKey; + } + + @Override + public void setConfirmationKey (final String confirmationKey) { + this.confirmationKey = confirmationKey; + } + + /** + * Post-construction method + */ + @PostConstruct + public void init () { + // Try it + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup + this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N + } catch (final NamingException e) { + // Throw again + throw new FaceletException(e); + } + } + + @Override + public void maybeConfirmUserAccount () { + // Is the confirmation key set? + if (this.getConfirmationKey() == null) { + // May be null if not set + return; + } else if (this.getConfirmationKey().isEmpty()) { + // Is empty string + return; + } + + // Now try to find the user in user list, first get the whole list + List users = this.userController.allUsers(); + + // Get iterator from it + Iterator iterator = users.iterator(); + + // Init instance + User user = null; + + // Then loop through all + while (iterator.hasNext()) { + // Get next user + User next = iterator.next(); + + // Same confirmation key? + if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) { + // Found it, then set it and abort loop + user = next; + break; + } + } + + // Is the user instance null? + if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) { + // Then clear this bean and the helper + this.beanHelper.setUser(null); + } else { + // Set user ... + this.beanHelper.setUser(user); + + // ... and copy it to the controller + this.beanHelper.copyUserToController(); + + // Try to confirm it + this.confirmUserAccount(); + } + } + + /** + * Tries to confirm the currently set user instance (in helper). + */ + private void confirmUserAccount () { + // Get user instance + User user = this.beanHelper.getUser(); + + // Should be set + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); + } else if (user.getUserId() == null) { + // Abort here + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N + } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { + // Account is already confirmed + throw new FaceletException(new UserStatusConfirmedException(user)); + } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { + // Account is already confirmed + throw new FaceletException(new UserStatusLockedException(user)); + } else if (user.getUserConfirmKey() == null) { + // Throw NPE + throw new NullPointerException("user.userConfirmKey is null"); //NOI18N + } + + // Updated user instance + User updatedUser; + + try { + // Get base URL + String baseUrl = FacesUtils.generateBaseUrl(); + + // Confirm account + updatedUser = this.userBean.confirmAccount(user, baseUrl); + } catch (final UserStatusConfirmedException | UserStatusLockedException ex) { + // Something unexpected happened + throw new FaceletException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N + } + + // Fire event that the user has confirmed account + this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser)); + + // Set it again in helper + this.beanHelper.setUser(updatedUser); + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestController.java b/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestController.java new file mode 100644 index 00000000..798f967f --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/confirmlink/AddressbookConfirmationLinkWebRequestController.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.beans.user.confirmlink; + +import java.io.Serializable; + +/** + * An interface for an email change controller + *

+ * @author Roland Häder + */ +public interface AddressbookConfirmationLinkWebRequestController extends Serializable { + + /** + * Getter for confirmation key + *

+ * @return Confirmation key + */ + String getConfirmationKey (); + + /** + * Setter for confirmation key + *

+ * @param confirmationKey Confirmation key + */ + void setConfirmationKey (final String confirmationKey); + + /** + * Tries to lookup the user by currently set confirmation key and if found + * tries to confirm it. If no user is found, the instance beanHelper.user is + * set to null. Other methods or JSF pages should then respond on this + * accordingly. + */ + void maybeConfirmUserAccount (); + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionBean.java new file mode 100644 index 00000000..10a23bc2 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionBean.java @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.beans.user.email_address; + +import java.text.MessageFormat; +import java.util.List; +import java.util.Objects; +import javax.annotation.PostConstruct; +import javax.enterprise.context.SessionScoped; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; +import org.mxchange.addressbook.beans.login.user.AddressbookUserLoginWebSessionController; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcoreee.utils.FacesUtils; +import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; +import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress; +import org.mxchange.jusercore.model.email_address.EmailAddressChange; +import org.mxchange.jusercore.model.email_address.UserEmailChangeSessionBeanRemote; +import org.mxchange.jusercore.model.user.User; + +/** + * A web session-scoped bean for changing email addresses + *

+ * @author Roland Häder + */ +@Named ("userEmailChangeController") +@SessionScoped +public class AddressbookEmailChangeWebSessionBean extends BaseAddressbookController implements AddressbookEmailChangeWebSessionController { + + /** + * Serial number + */ + private static final long serialVersionUID = 186_078_724_659_153L; + + /** + * Email address 1 (changing) + */ + private String emailAddress; + + /** + * Email address 2 (repeat in changing) + */ + private String emailAddressRepeat; + + /** + * Local list of already queued email addresses + */ + private List emailAddresses; + + /** + * Remote email change bean + */ + private UserEmailChangeSessionBeanRemote emailChangeBean; + + /** + * Features controller + */ + @Inject + private AddressbookFeaturesWebApplicationController featureController; + + /** + * Login controller (bean) + */ + @Inject + private AddressbookUserLoginWebSessionController userLoginController; + + /** + * Default constructor + */ + public AddressbookEmailChangeWebSessionBean () { + // Call super constructor + super(); + } + + @Override + public String doUserChangeEmailAddress () { + // This method shall only be called if the user is logged-in + if (!this.userLoginController.isUserLoggedIn()) { + // Not logged-in + throw new IllegalStateException("User is not logged-in"); //NOI18N + } else if (!this.featureController.isFeatureEnabled("user_change_email_address")) { //NOI18N + // Editing is not allowed + throw new IllegalStateException("User tried to change email address"); //NOI18N + } else if (!this.isRequiredChangeEmailAddressSet()) { + // Not all required fields are set + throw new FaceletException("Not all required fields are set."); //NOI18N + } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) { + // Email address 1+2 mismatch + this.showFacesMessage("form_user_change_email_address:emailAddressRepeat", "ERROR_USER_EMAIL_ADDRESSES_MISMATCH"); //NOI18N + return ""; //NOI18N + } else if (!this.userLoginController.ifCurrentPasswordMatches()) { + // Password not matching + this.showFacesMessage("form_login_user_change_email_address:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N + return ""; //NOI18N + } + + // Get user instance + User user = this.userLoginController.getLoggedInUser(); + + // It should be there, so run some tests on it + assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N + assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N + assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N + assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N + assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N + assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N + + // Check if the email address is already enqueued + if (this.isEmailAddressQueued(this.getEmailAddress())) { + // Clear both email addresses + this.setEmailAddress(null); + this.setEmailAddressRepeat(null); + + // Yes, then abort here + this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N + return ""; //NOI18N + } + + // Create change object, to save EJB calls, the hash is not generated here + ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress()); + + // Get base URL + String baseUrl = FacesUtils.generateBaseUrl(); + + // Call EJB + this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl); + + // Unset all so the user is forced to re-enter it + this.clear(); + + // All fine + return "user_login_email_change_queued"; //NOI18N + } + + @Override + public String getEmailAddress () { + return this.emailAddress; + } + + @Override + public void setEmailAddress (final String emailAddress) { + this.emailAddress = emailAddress; + } + + @Override + public String getEmailAddressRepeat () { + return this.emailAddressRepeat; + } + + @Override + public void setEmailAddressRepeat (final String emailAddressRepeat) { + this.emailAddressRepeat = emailAddressRepeat; + } + + /** + * Post-construction + */ + @PostConstruct + public void init () { + // Try it + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup + this.emailChangeBean = (UserEmailChangeSessionBeanRemote) context.lookup("java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote"); //NOI18N + } catch (final NamingException e) { + // Throw again + throw new FaceletException(e); + } + + // Init list + this.emailAddresses = this.emailChangeBean.allQueuedAddresses(); + } + + @Override + public boolean isRequiredChangeEmailAddressSet () { + return ((this.getEmailAddress() != null) && + (this.getEmailAddressRepeat() != null)); + } + + /** + * Clears email address fields so the user has to re-enter them + */ + private void clear () { + // Clear fields + this.setEmailAddress(null); + this.setEmailAddressRepeat(null); + } + + /** + * Checks if given email address has already been queued. First a local list + * is being checked, if not found, the EJB is called. Only if found, the + * result is "cached" in the list. + *

+ * @param emailAddress Email address to verify + *

+ * @return Whether the email address in field emailAddress is already queued + */ + private boolean isEmailAddressQueued (final String emailAddress) { + // It should be there + assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N + assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N + + // Check list + if (this.emailAddresses.contains(emailAddress)) { + // Okay, found it + return true; + } + + // Check EJB + boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress); + + // Is it there? + if (isQueued) { + // Add to list + this.emailAddresses.add(emailAddress); + } + + // Return status + return isQueued; + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionController.java b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionController.java new file mode 100644 index 00000000..a1021957 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/email_address/AddressbookEmailChangeWebSessionController.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.beans.user.email_address; + +import java.io.Serializable; + +/** + * An interface for an email change controller + *

+ * @author Roland Häder + */ +public interface AddressbookEmailChangeWebSessionController extends Serializable { + + /** + * Getter for email address 1 (changing) + *

+ * @return Email address + */ + String getEmailAddress (); + + /** + * Setter for email address 1 (changing) + *

+ * @param emailAddress Email address 1 + */ + void setEmailAddress (final String emailAddress); + + /** + * Getter for email address 2 (repeat changing) + *

+ * @return Email address 2 + */ + String getEmailAddressRepeat (); + + /** + * Setter for email address 2 (repeat changing) + *

+ * @param emailAddressRepeat Email address 2 + */ + void setEmailAddressRepeat (final String emailAddressRepeat); + + /** + * Checks whether all required are set for changing email address + *

+ * @return Whether the required personal data is set + */ + boolean isRequiredChangeEmailAddressSet (); + + /** + * Changes logged-in user's email address if the current password matches. + *

+ * @return New target page + */ + String doUserChangeEmailAddress (); + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionBean.java new file mode 100644 index 00000000..463c20e1 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionBean.java @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.beans.user.resendlink; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.addressbook.beans.BaseAddressbookController; +import org.mxchange.addressbook.beans.localization.AddressbookLocalizationSessionController; +import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; +import org.mxchange.jcoreee.utils.FacesUtils; +import org.mxchange.jusercore.events.resendlink.ObservableUserResendLinkAccountEvent; +import org.mxchange.jusercore.events.resendlink.UserResendLinkAccountEvent; +import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.model.resendlink.ResendLinkSessionBeanRemote; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; + +/** + * A web session-scoped bean for resending confirmation link + *

+ * @author Roland Häder + */ +@Named ("resendController") +@SessionScoped +public class AddressbookResendLinkWebSessionBean extends BaseAddressbookController implements AddressbookResendLinkWebSessionController { + + /** + * Serial number + */ + private static final long serialVersionUID = 186_078_724_659_153L; + + /** + * Email address + */ + private String emailAddress; + + /** + * Localization controller + */ + @Inject + private AddressbookLocalizationSessionController localizationController; + + /** + * EJB for resending confirmation link + */ + private ResendLinkSessionBeanRemote resendLinkBean; + + /** + * Regular user controller + */ + @Inject + private AddressbookUserWebSessionController userController; + + /** + * Event being fired after confirmation link is being sent + */ + @Inject + @Any + private Event userResendLinkEvent; + + /** + * Default constructor + */ + public AddressbookResendLinkWebSessionBean () { + // Call super constructor + super(); + } + + @Override + public String doResendLink () { + // The email address should not be empty as the JSF validates this + if (this.getEmailAddress() == null) { + // Throw NPE + throw new NullPointerException("this.emailAddress is null"); //NOI18N + } + + // Init user instance + User user; + + try { + // Is the email address really not used? + user = this.userController.lookupUserByEmailAddress(this.getEmailAddress()); + } catch (final UserEmailAddressNotFoundException ex) { + // Always clear bean + this.clear(); + + // Not found, should not happen as the registered validator should find it + this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N + return ""; //NOI18N + } + + // Is the user account already confirmed? + if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { + // Always clear bean + this.clear(); + + // Then abort here + this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N + return ""; //NOI18N + } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { + // Always clear bean + this.clear(); + + // User account is locked + this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N + return ""; //NOI18N + } else if (user.getUserConfirmKey() == null) { + // Status is UNCONFIRMED but confirmation key is NULL + throw new NullPointerException("user.userConfirmKey is null"); //NOI18N + } + + try { + // Get base URL + String baseUrl = FacesUtils.generateBaseUrl(); + + // Call EJB and return redirect target + this.resendLinkBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl); + } catch (final UserNotFoundException ex) { + // User not found + this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N + return ""; //NOI18N + } catch (final UserStatusLockedException | UserStatusConfirmedException ex) { + // Output message, this should not happen as the confirmation key is being removed + this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N + return ""; //NOI18N + } + + // Fire event + this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(user)); + + // Clear this bean + this.clear(); + + // Return redirect target + return "user_resend_done"; //NOI18N + } + + @Override + public String getEmailAddress () { + return this.emailAddress; + } + + @Override + public void setEmailAddress (final String emailAddress) { + this.emailAddress = emailAddress; + } + + /** + * Post-construction method + */ + @PostConstruct + public void init () { + // Try it + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup + this.resendLinkBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/addressbook-ejb/resendLink!org.mxchange.jusercore.model.resendlink.ResendLinkSessionBeanRemote"); //NOI18N + } catch (final NamingException e) { + // Throw again + throw new FaceletException(e); + } + } + + /** + * Clears email address fields so the user has to re-enter them + */ + private void clear () { + // Clear fields + this.setEmailAddress(null); + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionController.java b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionController.java new file mode 100644 index 00000000..5f1d38b4 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/resendlink/AddressbookResendLinkWebSessionController.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.beans.user.resendlink; + +import java.io.Serializable; + +/** + * An interface for an email change controller + *

+ * @author Roland Häder + */ +public interface AddressbookResendLinkWebSessionController extends Serializable { + + /** + * Getter for email address 1 (changing) + *

+ * @return Email address + */ + String getEmailAddress (); + + /** + * Setter for email address 1 (changing) + *

+ * @param emailAddress Email address 1 + */ + void setEmailAddress (final String emailAddress); + + /** + * Resends (new) confirmation link to given email address, if found. + * Otherwise an exception is thrown. On success a redirect takes place. + *

+ * @return Redirection target + */ + String doResendLink (); + +}