From: Roland Häder Date: Thu, 22 Jun 2017 19:59:50 +0000 (+0200) Subject: Don't cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f09656223dacc1eb53eb96d8194ccdd70137c2b2;p=addressbook-war.git Don't cherry-pick: - moved bean to proper package as it is user-specific registration Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/addressbook/beans/register/AddressbookUserRegisterWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/register/AddressbookUserRegisterWebSessionBean.java deleted file mode 100644 index 017d267c..00000000 --- a/src/java/org/mxchange/addressbook/beans/register/AddressbookUserRegisterWebSessionBean.java +++ /dev/null @@ -1,237 +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.register; - -import java.text.MessageFormat; -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.contact.AddressbookContactWebSessionController; -import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; -import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController; -import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; -import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jcontacts.contact.UserContact; -import org.mxchange.jcoreee.utils.FacesUtils; -import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent; -import org.mxchange.jusercore.events.registration.UserRegisteredEvent; -import org.mxchange.jusercore.exceptions.DataRepeatMismatchException; -import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; -import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; -import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.UserUtils; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; - -/** - * A web bean for user registration - *

- * @author Roland Häder - */ -@Named ("registerController") -@SessionScoped -public class AddressbookUserRegisterWebSessionBean extends BaseAddressbookController implements AddressbookUserRegisterWebSessionController { - - /** - * Serial number - */ - private static final long serialVersionUID = 47_828_986_719_691_592L; - - /** - * Administrative user controller - */ - @Inject - private AddressbookAdminUserWebRequestController adminUserController; - - /** - * Contact controller - */ - @Inject - private AddressbookContactWebSessionController contactController; - - /** - * Features controller - */ - @Inject - private AddressbookFeaturesWebApplicationController featureController; - - /** - * Remote register session-scoped bean - */ - private UserRegistrationSessionBeanRemote registerBean; - - /** - * User controller - */ - @Inject - private AddressbookUserWebSessionController userController; - - /** - * An en event fireable when a new user has registered - */ - @Inject - @Any - private Event userRegisteredEvent; - - /** - * Default constructor - */ - public AddressbookUserRegisterWebSessionBean () { - // Call super constructor - super(); - } - - @Override - public String doFinishRegistration () { - // Is registration enabled? - if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N - // Is not enabled - throw new FaceletException("Registration is disabled."); //NOI18N - } - - // Get user instance - User user = this.userController.createUserInstance(true); - - // Is the user already used? - if (null == user) { - // user must be set - throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N - } else if (!this.userController.isRequiredPersonalDataSet()) { - // Not all required fields are set - throw new FaceletException("Not all required fields are set."); //NOI18N - } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N - // User name is already used - throw new FaceletException(new UserNameAlreadyRegisteredException(user)); - } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { - // Email address has already been taken - throw new FaceletException(new EmailAddressAlreadyRegisteredException(user)); - } else if (!this.contactController.isSameEmailAddressEntered()) { - // Not same email address entered - throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N - } else if (!this.userController.isSamePasswordEntered()) { - // Not same password entered - throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N - } - - // Encrypt password - String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword()); - - // Set it here - user.setUserEncryptedPassword(encryptedPassword); - - // Is developer mode? - if (this.isDebugModeEnabled("register")) { //NOI18N - // For debugging/programming only: - user.setUserAccountStatus(UserAccountStatus.CONFIRMED); - } else { - // No debugging of this part - user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED); - - // Ask EJB for generating a not-existing confirmation key - String confirmKey = this.registerBean.generateConfirmationKey(user); - - // Set it in user - user.setUserConfirmKey(confirmKey); - } - - try { - // Get base URL - String baseUrl = FacesUtils.generateBaseUrl(); - - // Call bean - User registeredUser = this.registerBean.registerUser(user, baseUrl); - - // The id number should be set - assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N - - // Fire event - this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser)); - - // All fine, redirect to proper page - return "register_done"; //NOI18N - } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) { - // Continue to throw - throw new FaceletException(ex); - } - } - - @Override - public String doRegisterMultiPage1 () { - // Is registration enabled? - if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N - // Is not enabled - throw new FaceletException("Registration is disabled."); //NOI18N - } - - // Get user instance - User user = this.userController.createUserInstance(false); - - // First check if user is not null and user name is not used + if same email address is entered - if (null == user) { - // user must be set - throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N - } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N - // User name is already used - throw new FaceletException(new UserNameAlreadyRegisteredException(user)); - } else if (!this.contactController.isSameEmailAddressEntered()) { - // Not same email address entered - throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N - } - - // Create half contact instance with email address - Contact contact = new UserContact(); - contact.setContactEmailAddress(this.contactController.getEmailAddress()); - - // Set contact in user - user.setUserContact(contact); - - // Check if email address is registered - if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { - // Email address has already been taken - throw new FaceletException(new EmailAddressAlreadyRegisteredException(user)); - } - - // Now only redirect to next page as the JSF does it - return "user_register_page2"; //NOI18N - } - - /** - * Post-construction method - */ - @PostConstruct - public void init () { - try { - // Get initial context - Context context = new InitialContext(); - - // Try to lookup - this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/addressbook-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw - throw new FaceletException(ex); - } - } - -} diff --git a/src/java/org/mxchange/addressbook/beans/register/AddressbookUserRegisterWebSessionController.java b/src/java/org/mxchange/addressbook/beans/register/AddressbookUserRegisterWebSessionController.java deleted file mode 100644 index 82b40266..00000000 --- a/src/java/org/mxchange/addressbook/beans/register/AddressbookUserRegisterWebSessionController.java +++ /dev/null @@ -1,45 +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.register; - -import java.io.Serializable; - -/** - * An interface for registration web controllers - *

- * @author Roland Häder - */ -public interface AddressbookUserRegisterWebSessionController extends Serializable { - - /** - * Registers the user, if not found. Otherwise this method should throw an - * exception. - *

- * @return Redirection target - */ - String doFinishRegistration (); - - /** - * Handles registration request send from first page. The (maybe) entered - * user name and email address is not used and that privacy and T&C are - * accepted. - *

- * @return Redirect - */ - String doRegisterMultiPage1 (); - -} diff --git a/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionBean.java new file mode 100644 index 00000000..07ff07aa --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionBean.java @@ -0,0 +1,237 @@ +/* + * 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.register; + +import java.text.MessageFormat; +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.contact.AddressbookContactWebSessionController; +import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController; +import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController; +import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcontacts.contact.UserContact; +import org.mxchange.jcoreee.utils.FacesUtils; +import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent; +import org.mxchange.jusercore.events.registration.UserRegisteredEvent; +import org.mxchange.jusercore.exceptions.DataRepeatMismatchException; +import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; +import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserUtils; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; + +/** + * A web bean for user registration + *

+ * @author Roland Häder + */ +@Named ("registerController") +@SessionScoped +public class AddressbookUserRegisterWebSessionBean extends BaseAddressbookController implements AddressbookUserRegisterWebSessionController { + + /** + * Serial number + */ + private static final long serialVersionUID = 47_828_986_719_691_592L; + + /** + * Administrative user controller + */ + @Inject + private AddressbookAdminUserWebRequestController adminUserController; + + /** + * Contact controller + */ + @Inject + private AddressbookContactWebSessionController contactController; + + /** + * Features controller + */ + @Inject + private AddressbookFeaturesWebApplicationController featureController; + + /** + * Remote register session-scoped bean + */ + private UserRegistrationSessionBeanRemote registerBean; + + /** + * User controller + */ + @Inject + private AddressbookUserWebSessionController userController; + + /** + * An en event fireable when a new user has registered + */ + @Inject + @Any + private Event userRegisteredEvent; + + /** + * Default constructor + */ + public AddressbookUserRegisterWebSessionBean () { + // Call super constructor + super(); + } + + @Override + public String doFinishRegistration () { + // Is registration enabled? + if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N + // Is not enabled + throw new FaceletException("Registration is disabled."); //NOI18N + } + + // Get user instance + User user = this.userController.createUserInstance(true); + + // Is the user already used? + if (null == user) { + // user must be set + throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N + } else if (!this.userController.isRequiredPersonalDataSet()) { + // Not all required fields are set + throw new FaceletException("Not all required fields are set."); //NOI18N + } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N + // User name is already used + throw new FaceletException(new UserNameAlreadyRegisteredException(user)); + } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { + // Email address has already been taken + throw new FaceletException(new EmailAddressAlreadyRegisteredException(user)); + } else if (!this.contactController.isSameEmailAddressEntered()) { + // Not same email address entered + throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N + } else if (!this.userController.isSamePasswordEntered()) { + // Not same password entered + throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N + } + + // Encrypt password + String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword()); + + // Set it here + user.setUserEncryptedPassword(encryptedPassword); + + // Is developer mode? + if (this.isDebugModeEnabled("register")) { //NOI18N + // For debugging/programming only: + user.setUserAccountStatus(UserAccountStatus.CONFIRMED); + } else { + // No debugging of this part + user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED); + + // Ask EJB for generating a not-existing confirmation key + String confirmKey = this.registerBean.generateConfirmationKey(user); + + // Set it in user + user.setUserConfirmKey(confirmKey); + } + + try { + // Get base URL + String baseUrl = FacesUtils.generateBaseUrl(); + + // Call bean + User registeredUser = this.registerBean.registerUser(user, baseUrl); + + // The id number should be set + assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N + + // Fire event + this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser)); + + // All fine, redirect to proper page + return "register_done"; //NOI18N + } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) { + // Continue to throw + throw new FaceletException(ex); + } + } + + @Override + public String doRegisterMultiPage1 () { + // Is registration enabled? + if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N + // Is not enabled + throw new FaceletException("Registration is disabled."); //NOI18N + } + + // Get user instance + User user = this.userController.createUserInstance(false); + + // First check if user is not null and user name is not used + if same email address is entered + if (null == user) { + // user must be set + throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N + } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N + // User name is already used + throw new FaceletException(new UserNameAlreadyRegisteredException(user)); + } else if (!this.contactController.isSameEmailAddressEntered()) { + // Not same email address entered + throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N + } + + // Create half contact instance with email address + Contact contact = new UserContact(); + contact.setContactEmailAddress(this.contactController.getEmailAddress()); + + // Set contact in user + user.setUserContact(contact); + + // Check if email address is registered + if (this.contactController.isEmailAddressRegistered(user.getUserContact())) { + // Email address has already been taken + throw new FaceletException(new EmailAddressAlreadyRegisteredException(user)); + } + + // Now only redirect to next page as the JSF does it + return "user_register_page2"; //NOI18N + } + + /** + * Post-construction method + */ + @PostConstruct + public void init () { + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup + this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/addressbook-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw + throw new FaceletException(ex); + } + } + +} diff --git a/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionController.java b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionController.java new file mode 100644 index 00000000..6fc80871 --- /dev/null +++ b/src/java/org/mxchange/addressbook/beans/user/register/AddressbookUserRegisterWebSessionController.java @@ -0,0 +1,45 @@ +/* + * 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.register; + +import java.io.Serializable; + +/** + * An interface for registration web controllers + *

+ * @author Roland Häder + */ +public interface AddressbookUserRegisterWebSessionController extends Serializable { + + /** + * Registers the user, if not found. Otherwise this method should throw an + * exception. + *

+ * @return Redirection target + */ + String doFinishRegistration (); + + /** + * Handles registration request send from first page. The (maybe) entered + * user name and email address is not used and that privacy and T&C are + * accepted. + *

+ * @return Redirect + */ + String doRegisterMultiPage1 (); + +}