From: Roland Häder Date: Sat, 22 Apr 2017 20:04:26 +0000 (+0200) Subject: Please cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e666b223a426ab472e604af87b37d6042059c325;p=jfinancials-war.git Please cherry-pick: - Let's always call super constructor (not the default one, of course), maybe one day there will be something added - sorted members a bit - some constructors still contain EJB-lookup code, moved to init() (@PostConstruct) method Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jfinancials/beans/BaseFinancialsController.java b/src/java/org/mxchange/jfinancials/beans/BaseFinancialsController.java index 509dde87..28adbde7 100644 --- a/src/java/org/mxchange/jfinancials/beans/BaseFinancialsController.java +++ b/src/java/org/mxchange/jfinancials/beans/BaseFinancialsController.java @@ -17,8 +17,14 @@ package org.mxchange.jfinancials.beans; import java.io.Serializable; +import java.security.Principal; +import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; +import org.mxchange.jusercore.model.user.UserUtils; /** * A general controller @@ -32,6 +38,35 @@ public abstract class BaseFinancialsController implements Serializable { */ private static final long serialVersionUID = 50_837_597_127_567_140L; + /** + * Protected constructor + */ + protected BaseFinancialsController () { + } + + /** + * Determines principal's name or returns null if no principal (security) is + * set. + *

+ * @return Principal's name or null + */ + protected String determinePrincipalName () { + // Get principal + Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal(); + + // Init with null + String principalName = null; + + // Is the principal set? + if (userPrincipal instanceof Principal) { + // Get principal's name + principalName = userPrincipal.getName(); + } + + // Return it + return principalName; + } + /** * Returns given property key or throws an exception if not found. *

@@ -66,7 +101,7 @@ public abstract class BaseFinancialsController implements Serializable { // Is it null? if (null == contextValue) { // Throw NPE - throw new NullPointerException("parameterKey=" + parameterKey + " is not set."); + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N } // Return it @@ -91,21 +126,45 @@ public abstract class BaseFinancialsController implements Serializable { } // Try to get context parameter - String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N // Is it set and true? - boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N + boolean isEnabled = (Boolean.parseBoolean(contextParameter) == Boolean.TRUE); // Return it return isEnabled; } + /** + * Checks if given password is to weak to be used + *

+ * @param password Clear-text password + *

+ * @return Whether the entered password is to weak + */ + protected boolean isWeakPassword (final String password) { + // Is parameter set? + if (null == password) { + // Throw NPE + throw new NullPointerException("password is null"); //NOI18N + } + + // Get score value + double passwordScore = UserUtils.calculatePasswordScore(password); + + // Is the score within range? + boolean isWeak = (passwordScore <= this.getIntegerContextParameter("min_user_password_score")); //NOI18N + + // Return it + return isWeak; + } + /** * Shows a faces message for given causing exception. The message from the * exception is being inserted into the message. *

* @param clientId Client id to send message to - * @param cause Causing exception + * @param cause Causing exception */ protected void showFacesMessage (final String clientId, final Throwable cause) { // Get context and add message @@ -113,12 +172,47 @@ public abstract class BaseFinancialsController implements Serializable { } /** - * Shows a faces message with given message. + * Shows a faces message with given message (i18n) key. *

* @param clientId Client id to send message to - * @param message Causing exception + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty */ - protected void showFacesMessage (final String clientId, final String message) { + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + + // Get bundle bundle + ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale); + + // Default is i18nKey + String message = i18nKey; + + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); } diff --git a/src/java/org/mxchange/jfinancials/beans/confirmlink/FinancialsConfirmationLinkWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/confirmlink/FinancialsConfirmationLinkWebRequestBean.java index af574493..35d8f270 100644 --- a/src/java/org/mxchange/jfinancials/beans/confirmlink/FinancialsConfirmationLinkWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/confirmlink/FinancialsConfirmationLinkWebRequestBean.java @@ -32,6 +32,7 @@ import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jcoreee.utils.FacesUtils; import org.mxchange.jfinancials.beans.BaseFinancialsController; +import org.mxchange.jfinancials.beans.helper.FinancialsWebRequestHelperController; import org.mxchange.jfinancials.beans.user.FinancialsUserWebSessionController; import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent; import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent; @@ -40,7 +41,6 @@ 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; -import org.mxchange.jfinancials.beans.helper.FinancialsWebRequestHelperController; /** * A web request bean for confirmation link handling @@ -89,6 +89,8 @@ public class FinancialsConfirmationLinkWebRequestBean extends BaseFinancialsCont * Default constructor */ public FinancialsConfirmationLinkWebRequestBean () { + // Call super constructor + super(); } /** diff --git a/src/java/org/mxchange/jfinancials/beans/contact/FinancialsAdminContactWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/contact/FinancialsAdminContactWebRequestBean.java index 0365390d..1377ef26 100644 --- a/src/java/org/mxchange/jfinancials/beans/contact/FinancialsAdminContactWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/contact/FinancialsAdminContactWebRequestBean.java @@ -43,6 +43,7 @@ import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContac import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException; import org.mxchange.jcountry.data.Country; import org.mxchange.jfinancials.beans.BaseFinancialsController; +import org.mxchange.jfinancials.beans.helper.FinancialsWebRequestHelperController; import org.mxchange.jphone.phonenumbers.DialableNumber; import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; import org.mxchange.jphone.phonenumbers.fax.FaxNumber; @@ -51,7 +52,6 @@ import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; import org.mxchange.jphone.phonenumbers.mobile.MobileNumber; import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider; -import org.mxchange.jfinancials.beans.helper.FinancialsWebRequestHelperController; /** * An administrative user bean (controller) @@ -247,6 +247,8 @@ public class FinancialsAdminContactWebRequestBean extends BaseFinancialsControll * Default constructor */ public FinancialsAdminContactWebRequestBean () { + // Call super constructor + super(); } @Override diff --git a/src/java/org/mxchange/jfinancials/beans/contact/FinancialsContactWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/contact/FinancialsContactWebSessionBean.java index 1aeb0cfe..1f12d8e7 100644 --- a/src/java/org/mxchange/jfinancials/beans/contact/FinancialsContactWebSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/contact/FinancialsContactWebSessionBean.java @@ -42,7 +42,7 @@ import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContac import org.mxchange.jcontacts.exceptions.ContactNotFoundException; import org.mxchange.jcountry.data.Country; import org.mxchange.jfinancials.beans.BaseFinancialsController; -import org.mxchange.jfinancials.beans.login.FinancialsUserLoginWebSessionController; +import org.mxchange.jfinancials.beans.login.user.FinancialsUserLoginWebSessionController; import org.mxchange.jfinancials.beans.user.FinancialsUserWebSessionController; import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; import org.mxchange.jphone.phonenumbers.fax.FaxNumber; @@ -234,6 +234,9 @@ public class FinancialsContactWebSessionBean extends BaseFinancialsController im * Default constructor */ public FinancialsContactWebSessionBean () { + // Call super constructor + super(); + // Init lists/maps this.contactList = new LinkedList<>(); this.emailAddressList = new LinkedList<>(); diff --git a/src/java/org/mxchange/jfinancials/beans/contact/phone/FinancialsAdminContactPhoneWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/contact/phone/FinancialsAdminContactPhoneWebRequestBean.java index dc0be5ce..cf05d408 100644 --- a/src/java/org/mxchange/jfinancials/beans/contact/phone/FinancialsAdminContactPhoneWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/contact/phone/FinancialsAdminContactPhoneWebRequestBean.java @@ -17,6 +17,7 @@ package org.mxchange.jfinancials.beans.contact.phone; import java.text.MessageFormat; +import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; import javax.enterprise.event.Observes; @@ -140,20 +141,11 @@ public class FinancialsAdminContactPhoneWebRequestBean extends BaseFinancialsCon * Default constructor */ public FinancialsAdminContactPhoneWebRequestBean () { + // Call super constructor + super(); + // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName()); // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller)); - - // Try it - try { - // Get initial context - Context context = new InitialContext(); - - // Try to lookup the beans - this.adminPhoneBean = (AdminContactsPhoneSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/adminContactPhone!org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote"); //NOI18N - } catch (final NamingException e) { - // Throw again - throw new FaceletException(e); - } } /** @@ -414,6 +406,24 @@ public class FinancialsAdminContactPhoneWebRequestBean extends BaseFinancialsCon return "admin_show_contact"; //NOI18N } + /** + * Post-construction method + */ + @PostConstruct + public void init () { + // Try it + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup the beans + this.adminPhoneBean = (AdminContactsPhoneSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/adminContactPhone!org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote"); //NOI18N + } catch (final NamingException e) { + // Throw again + throw new FaceletException(e); + } + } + @Override public String unlinkFaxContactData () { // Is all data set diff --git a/src/java/org/mxchange/jfinancials/beans/contact/phone/FinancialsContactPhoneWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/contact/phone/FinancialsContactPhoneWebSessionBean.java index ae35fd41..eddc4dc7 100644 --- a/src/java/org/mxchange/jfinancials/beans/contact/phone/FinancialsContactPhoneWebSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/contact/phone/FinancialsContactPhoneWebSessionBean.java @@ -78,6 +78,9 @@ public class FinancialsContactPhoneWebSessionBean extends BaseFinancialsControll * Default constructor */ public FinancialsContactPhoneWebSessionBean () { + // Call super constructor + super(); + // Init lists/maps this.contacts = new HashMap<>(10); } diff --git a/src/java/org/mxchange/jfinancials/beans/country/FinancialsAdminCountryWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/country/FinancialsAdminCountryWebRequestBean.java index 94bde72f..f7447ff6 100644 --- a/src/java/org/mxchange/jfinancials/beans/country/FinancialsAdminCountryWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/country/FinancialsAdminCountryWebRequestBean.java @@ -103,6 +103,8 @@ public class FinancialsAdminCountryWebRequestBean extends BaseFinancialsControll * Default constructor */ public FinancialsAdminCountryWebRequestBean () { + // Call super constructor + super(); } @Override diff --git a/src/java/org/mxchange/jfinancials/beans/country/FinancialsCountryWebApplicationBean.java b/src/java/org/mxchange/jfinancials/beans/country/FinancialsCountryWebApplicationBean.java index aae836a1..cea6d8c2 100644 --- a/src/java/org/mxchange/jfinancials/beans/country/FinancialsCountryWebApplicationBean.java +++ b/src/java/org/mxchange/jfinancials/beans/country/FinancialsCountryWebApplicationBean.java @@ -59,6 +59,8 @@ public class FinancialsCountryWebApplicationBean extends BaseFinancialsControlle * Default constructor */ public FinancialsCountryWebApplicationBean () { + // Call super constructor + super(); } /** diff --git a/src/java/org/mxchange/jfinancials/beans/email_address/FinancialsEmailChangeWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/email_address/FinancialsEmailChangeWebSessionBean.java index 8f37ed45..ded705f1 100644 --- a/src/java/org/mxchange/jfinancials/beans/email_address/FinancialsEmailChangeWebSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/email_address/FinancialsEmailChangeWebSessionBean.java @@ -31,7 +31,7 @@ import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jcoreee.utils.FacesUtils; import org.mxchange.jfinancials.beans.BaseFinancialsController; import org.mxchange.jfinancials.beans.features.FinancialsFeaturesWebApplicationController; -import org.mxchange.jfinancials.beans.login.FinancialsUserLoginWebSessionController; +import org.mxchange.jfinancials.beans.login.user.FinancialsUserLoginWebSessionController; import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress; import org.mxchange.jusercore.model.email_address.EmailAddressChange; @@ -88,6 +88,8 @@ public class FinancialsEmailChangeWebSessionBean extends BaseFinancialsControlle * Default constructor */ public FinancialsEmailChangeWebSessionBean () { + // Call super constructor + super(); } @Override diff --git a/src/java/org/mxchange/jfinancials/beans/features/FinancialsFeatureWebApplicationBean.java b/src/java/org/mxchange/jfinancials/beans/features/FinancialsFeatureWebApplicationBean.java index 03e96e18..4fda84be 100644 --- a/src/java/org/mxchange/jfinancials/beans/features/FinancialsFeatureWebApplicationBean.java +++ b/src/java/org/mxchange/jfinancials/beans/features/FinancialsFeatureWebApplicationBean.java @@ -39,6 +39,8 @@ public class FinancialsFeatureWebApplicationBean extends BaseFinancialsControlle * Default constructor */ public FinancialsFeatureWebApplicationBean () { + // Call super constructor + super(); } /** diff --git a/src/java/org/mxchange/jfinancials/beans/gender/FinancialsGenderWebApplicationBean.java b/src/java/org/mxchange/jfinancials/beans/gender/FinancialsGenderWebApplicationBean.java index ab117590..3990bfac 100644 --- a/src/java/org/mxchange/jfinancials/beans/gender/FinancialsGenderWebApplicationBean.java +++ b/src/java/org/mxchange/jfinancials/beans/gender/FinancialsGenderWebApplicationBean.java @@ -42,6 +42,8 @@ public class FinancialsGenderWebApplicationBean extends BaseFinancialsController * Default constructor */ public FinancialsGenderWebApplicationBean () { + // Call super constructor + super(); } @Override diff --git a/src/java/org/mxchange/jfinancials/beans/helper/FinancialsWebRequestHelperBean.java b/src/java/org/mxchange/jfinancials/beans/helper/FinancialsWebRequestHelperBean.java index eb2e482f..7d39f004 100644 --- a/src/java/org/mxchange/jfinancials/beans/helper/FinancialsWebRequestHelperBean.java +++ b/src/java/org/mxchange/jfinancials/beans/helper/FinancialsWebRequestHelperBean.java @@ -102,6 +102,9 @@ public class FinancialsWebRequestHelperBean implements FinancialsWebRequestHelpe * Default constructor */ public FinancialsWebRequestHelperBean () { + // Call super constructor + super(); + // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName()); // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller)); } diff --git a/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionBean.java b/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionBean.java index 3c22d855..6cd1cb75 100644 --- a/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionBean.java @@ -53,6 +53,8 @@ public class FinancialsLocalizationSessionBean extends BaseFinancialsController * Default constructor */ public FinancialsLocalizationSessionBean () { + // Call super constructor + super(); } /** diff --git a/src/java/org/mxchange/jfinancials/beans/login/FinancialsUserLoginWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/login/FinancialsUserLoginWebSessionBean.java deleted file mode 100644 index 96ffd74e..00000000 --- a/src/java/org/mxchange/jfinancials/beans/login/FinancialsUserLoginWebSessionBean.java +++ /dev/null @@ -1,445 +0,0 @@ -/* - * Copyright (C) 2016 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.jfinancials.beans.login; - -import java.text.MessageFormat; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import javax.annotation.PostConstruct; -import javax.enterprise.context.SessionScoped; -import javax.enterprise.event.Event; -import javax.enterprise.event.Observes; -import javax.enterprise.inject.Any; -import javax.faces.context.FacesContext; -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.jfinancials.beans.BaseFinancialsController; -import org.mxchange.jfinancials.beans.user.FinancialsUserWebSessionController; -import org.mxchange.jusercore.container.login.LoginContainer; -import org.mxchange.jusercore.container.login.UserLoginContainer; -import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent; -import org.mxchange.jusercore.events.login.UserLoggedInEvent; -import org.mxchange.jusercore.events.logout.ObservableUserLogoutEvent; -import org.mxchange.jusercore.events.logout.UserLogoutEvent; -import org.mxchange.jusercore.events.user.password_change.ObservableUpdatedUserPasswordEvent; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; -import org.mxchange.jusercore.exceptions.UserStatusLockedException; -import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; -import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.UserUtils; -import org.mxchange.jusercore.model.user.password_history.PasswordHistory; -import org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote; -import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; - -/** - * A web bean for user registration - *

- * @author Roland Häder - */ -@Named ("userLoginController") -@SessionScoped -public class FinancialsUserLoginWebSessionBean extends BaseFinancialsController implements FinancialsUserLoginWebSessionController { - - /** - * Path name for guest base template - */ - private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest"; - - /** - * Path name for logged-in user base template - */ - private static final String USER_BASE_TEMPLATE_NAME = "login/user/user"; - - /** - * Serial number - */ - private static final long serialVersionUID = 47_828_986_719_691_592L; - - /** - * Template type for pages that might be displayed in guest area and login - * area. - */ - private String baseTemplatePathName; - - /** - * Logged-in user instance - */ - private User loggedInUser; - - /** - * Event fired when user has logged in - */ - @Inject - @Any - private Event loginEvent; - - /** - * User controller - */ - @Inject - private FinancialsUserWebSessionController userController; - - /** - * Current password - */ - private String userCurrentPassword; - - /** - * Flag whether the user has logged-in, set only from inside - */ - private boolean userLoggedIn; - - /** - * Remote register session-scoped bean - */ - private UserLoginSessionBeanRemote userLoginBean; - - /** - * Event fired when user has logged in - */ - @Inject - @Any - private Event userLoginEvent; - - /** - * Event fired when user has logged out - */ - @Inject - @Any - private Event userLogoutEvent; - - /** - * User's password history - */ - private List userPasswordHistory; - - /** - * EJB for user's password history - */ - private UserPasswordHistorySessionBeanRemote userPasswordHistoryBean; - - /** - * Default constructor - */ - public FinancialsUserLoginWebSessionBean () { - // Defaul template is guest - this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME; - } - - /** - * Method being call after user's password has been updated (and history - * entry has been created). - *

- * @param event Event being observed - */ - public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { - // Check parameter - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getPasswordHistory() == null) { - // Throw NPE again - throw new NullPointerException("event.passwordHistory is null"); //NOI18N - } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) { - // ... and again - throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N - } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) { - // Invalid value - throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N - } - - // All fine, so update list - this.updatePasswordHistory(event.getPasswordHistory()); - } - - @Override - public String doAdminLogout () { - // Is a user logged-in? - if (this.isUserLoggedIn()) { - // Call other logout - return this.doUserLogout(); - } - - // Invalidate session - FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); - - // Set template type to guest - this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N - - // Redirect to index - return "index?faces-redirect=true"; //NOI18N - } - - @Override - public String doUserLogin () { - // Get user instance - User user = this.userController.createUserLogin(); - - // Create login container - LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword()); - - try { - // Call bean - User confirmedUser = this.userLoginBean.validateUserAccountStatus(container); - - // All fine here so set it here - this.setLoggedInUser(confirmedUser); - - // Retrieve user's password list - this.userPasswordHistory = this.userPasswordHistoryBean.getUserPasswordHistory(confirmedUser); - - // Set template to "login" - this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N - - // Fire event away. Keep this last before return statement. - this.userLoginEvent.fire(new UserLoggedInEvent(confirmedUser)); - - // Clear this bean - this.clear(); - - // All fine - return "login_user"; //NOI18N - } catch (final UserNotFoundException ex) { - // Show JSF message - this.showFacesMessage("form_user_login:userName", "ERROR_USER_NOT_FOUND"); //NOI18N - return ""; //NOI18N - } catch (final UserStatusLockedException ex) { - this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_LOCKED"); //NOI18N - return ""; //NOI18N - } catch (final UserStatusUnconfirmedException ex) { - this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_UNCONFIRMED"); //NOI18N - return ""; //NOI18N - } catch (final UserPasswordMismatchException ex) { - // Show JSF message - this.showFacesMessage("form_user_login:userPassword", "ERROR_USER_PASSWORD_MISMATCH"); //NOI18N - return ""; //NOI18N - } - } - - @Override - public String doUserLogout () { - // Is loggedInUser set? - if (this.getLoggedInUser() == null) { - // Throw NPE - throw new NullPointerException("this.loggedInUser is null"); //NOI18N - } else if (this.getLoggedInUser().getUserId() == null) { - // Throw again - throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N - } else if (this.getLoggedInUser().getUserId() < 1) { - // Invalid user id - throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N - } - - // Fire event - this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser())); - - // Invalidate session - FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); - - // Unset any user instances - this.setLoggedInUser(null); - this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N - - // Redirect to index - return "index"; //NOI18N - } - - @Override - public String getBaseTemplatePathName () { - return this.baseTemplatePathName; - } - - @Override - public void setBaseTemplatePathName (final String baseTemplatePathName) { - this.baseTemplatePathName = baseTemplatePathName; - } - - @Override - public User getLoggedInUser () { - return this.loggedInUser; - } - - @Override - public void setLoggedInUser (final User loggedInUser) { - this.loggedInUser = loggedInUser; - } - - @Override - public String getUserCurrentPassword () { - return this.userCurrentPassword; - } - - @Override - public void setUserCurrentPassword (final String userCurrentPassword) { - this.userCurrentPassword = userCurrentPassword; - } - - @Override - public List getUserPasswordHistory () { - return Collections.unmodifiableList(this.userPasswordHistory); - } - - @Override - public boolean ifCurrentPasswordMatches () { - // The current password must be set and not empty - if (this.getUserCurrentPassword() == null) { - // Is not set - throw new NullPointerException("this.userCurrentPassword is null"); //NOI18N - } else if (this.getUserCurrentPassword().isEmpty()) { - // Is set empty - throw new IllegalStateException("this.userCurrentPassword is empty."); //NOI18N - } - - // Create "container" - LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getUserCurrentPassword()); - - // Now check if it matches - return UserUtils.ifPasswordMatches(container, this.getLoggedInUser()); - } - - @Override - public boolean ifUserMustChangePassword () { - return ((this.isUserLoggedIn()) && (Objects.equals(this.getLoggedInUser().getUserMustChangePassword(), Boolean.TRUE))); - } - - /** - * Post-construction method - */ - @PostConstruct - public void init () { - try { - // Get initial context - Context context = new InitialContext(); - - // Try to lookup - this.userLoginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N - - // Also find this - this.userPasswordHistoryBean = (UserPasswordHistorySessionBeanRemote) context.lookup("java:global/jfinancials-ejb/userPasswordHistory!org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote"); //NOI18N - - // Defaul template is guest - this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME; - } catch (final NamingException ex) { - // Continue to throw - throw new FaceletException(ex); - } - } - - @Override - public boolean isInvisible () { - // Check on login - if (!this.isUserLoggedIn()) { - // Not logged in! - throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N - } - - // Check logged-in first, then invisibility - return Objects.equals(this.getLoggedInUser().getUserProfileMode(), ProfileMode.INVISIBLE); - } - - @Override - public boolean isPasswordInHistory (final String userPassword) { - // Default is not found - boolean isPasswordInHistory = false; - - // Init variables - int count = 1; - int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N - - // Check all passwords - for (final PasswordHistory entry : this.getUserPasswordHistory()) { - // Is password the same? - if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) { - // Yes, found it - isPasswordInHistory = true; - break; - } else if (count == maxEntries) { - // Maximum reached - break; - } - - // Count up - count++; - } - - // Return status - return isPasswordInHistory; - } - - @Override - public boolean isUserLoggedIn () { - // Compare instance - this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED))); - - // Return it - return this.userLoggedIn; - } - - /** - * Clears this bean - */ - private void clear () { - // Clear all fields - this.setUserCurrentPassword(null); - } - - /** - * Updates password history by adding given entry to it as long as it is not - * there. - *

- * @param passwordHistory Password history entry - */ - private void updatePasswordHistory (final PasswordHistory passwordHistory) { - if (null == passwordHistory) { - // Throw NPE - throw new NullPointerException("passwordHistory is null"); //NOI18N - } else if (passwordHistory.getUserPasswordHistoryId() == null) { - // Throw NPE again - throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N - } else if (passwordHistory.getUserPasswordHistoryId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N - } - - // Is it there? - if (this.userPasswordHistory.contains(passwordHistory)) { - // Excact copy found - return; - } - - // Check all entries - for (final PasswordHistory entry : this.userPasswordHistory) { - // Is same id number? - if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) { - // Found it - return; - } - } - - // Not found, so add it - this.userPasswordHistory.add(passwordHistory); - } - -} diff --git a/src/java/org/mxchange/jfinancials/beans/login/FinancialsUserLoginWebSessionController.java b/src/java/org/mxchange/jfinancials/beans/login/FinancialsUserLoginWebSessionController.java deleted file mode 100644 index 8f108f49..00000000 --- a/src/java/org/mxchange/jfinancials/beans/login/FinancialsUserLoginWebSessionController.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (C) 2016 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.jfinancials.beans.login; - -import java.io.Serializable; -import java.util.List; -import javax.ejb.Local; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.password_history.PasswordHistory; - -/** - * An interface for registration web controllers - *

- * @author Roland Häder - */ -@Local -public interface FinancialsUserLoginWebSessionController extends Serializable { - - /** - * Checks whether given clear-text password is in user's password history. - *

- * @param userPassword Clear-text password - *

- * @return Whether clear-text password is in user's password history - */ - boolean isPasswordInHistory (final String userPassword); - - /** - * Getter for base template type - *

- * @return Template type - */ - String getBaseTemplatePathName (); - - /** - * Setter for base template type - *

- * @param baseTemplatePathName Template type - */ - void setBaseTemplatePathName (final String baseTemplatePathName); - - /** - * Logout for administrator area. If a logged-in user instance exists, it is - * being logged-out, too. - *

- * @return Outcome (should be redirected) - */ - String doAdminLogout (); - - /** - * Logins the user, if the account is found, confirmed and unlocked. - *

- * @return Redirect target - */ - String doUserLogin (); - - /** - * Logout for current user by invalidating the current session. - *

- * @return Outcome (should be redirected) - */ - String doUserLogout (); - - /** - * Getter for logged-in user instance - *

- * @return Logged-in user instance - */ - User getLoggedInUser (); - - /** - * Setter for logged-in user instance - *

- * @param loggedInUser Logged-in user instance - */ - void setLoggedInUser (final User loggedInUser); - - /** - * Checks whether the user is logged-in - *

- * @return Whether the user is logged-in - */ - boolean isUserLoggedIn (); - - /** - * Checks whether the user needs to change password - *

- * @return Whether the user needs to change password - */ - boolean ifUserMustChangePassword (); - - /** - * Whether the currently logged-in user is invisible - *

- * @return Whether the currently logged-in user is invisible - */ - boolean isInvisible (); - - /** - * Setter for current password (clear text) - *

- * @param userCurrentPassword Current password - */ - void setUserCurrentPassword (final String userCurrentPassword); - - /** - * Getter for current password (clear text) - *

- * @return Current password - */ - String getUserCurrentPassword (); - - /** - * Checks whether the (previously entered) current password matches with - * from the user instance. - *

- * @return If current password matches - */ - boolean ifCurrentPasswordMatches (); - - /** - * Getter for user's password history - *

- * @return User's password history - */ - List getUserPasswordHistory (); - -} diff --git a/src/java/org/mxchange/jfinancials/beans/login/user/FinancialsUserLoginWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/login/user/FinancialsUserLoginWebSessionBean.java new file mode 100644 index 00000000..601c8aad --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/login/user/FinancialsUserLoginWebSessionBean.java @@ -0,0 +1,448 @@ +/* + * Copyright (C) 2016 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.jfinancials.beans.login.user; + +import java.text.MessageFormat; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import javax.annotation.PostConstruct; +import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Any; +import javax.faces.context.FacesContext; +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.jfinancials.beans.BaseFinancialsController; +import org.mxchange.jfinancials.beans.user.FinancialsUserWebSessionController; +import org.mxchange.jusercore.container.login.LoginContainer; +import org.mxchange.jusercore.container.login.UserLoginContainer; +import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent; +import org.mxchange.jusercore.events.login.UserLoggedInEvent; +import org.mxchange.jusercore.events.logout.ObservableUserLogoutEvent; +import org.mxchange.jusercore.events.logout.UserLogoutEvent; +import org.mxchange.jusercore.events.user.password_change.ObservableUpdatedUserPasswordEvent; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; +import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserUtils; +import org.mxchange.jusercore.model.user.password_history.PasswordHistory; +import org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote; +import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; + +/** + * A web bean for user registration + *

+ * @author Roland Häder + */ +@Named ("userLoginController") +@SessionScoped +public class FinancialsUserLoginWebSessionBean extends BaseFinancialsController implements FinancialsUserLoginWebSessionController { + + /** + * Path name for guest base template + */ + private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest"; + + /** + * Path name for logged-in user base template + */ + private static final String USER_BASE_TEMPLATE_NAME = "login/user/user"; + + /** + * Serial number + */ + private static final long serialVersionUID = 47_828_986_719_691_592L; + + /** + * Template type for pages that might be displayed in guest area and login + * area. + */ + private String baseTemplatePathName; + + /** + * Logged-in user instance + */ + private User loggedInUser; + + /** + * Event fired when user has logged in + */ + @Inject + @Any + private Event loginEvent; + + /** + * User controller + */ + @Inject + private FinancialsUserWebSessionController userController; + + /** + * Current password + */ + private String userCurrentPassword; + + /** + * Flag whether the user has logged-in, set only from inside + */ + private boolean userLoggedIn; + + /** + * Remote register session-scoped bean + */ + private UserLoginSessionBeanRemote userLoginBean; + + /** + * Event fired when user has logged in + */ + @Inject + @Any + private Event userLoginEvent; + + /** + * Event fired when user has logged out + */ + @Inject + @Any + private Event userLogoutEvent; + + /** + * User's password history + */ + private List userPasswordHistory; + + /** + * EJB for user's password history + */ + private UserPasswordHistorySessionBeanRemote userPasswordHistoryBean; + + /** + * Default constructor + */ + public FinancialsUserLoginWebSessionBean () { + // Call super constructor + super(); + + // Defaul template is guest + this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME; + } + + /** + * Method being call after user's password has been updated (and history + * entry has been created). + *

+ * @param event Event being observed + */ + public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) { + // Check parameter + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getPasswordHistory() == null) { + // Throw NPE again + throw new NullPointerException("event.passwordHistory is null"); //NOI18N + } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) { + // ... and again + throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N + } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) { + // Invalid value + throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N + } + + // All fine, so update list + this.updatePasswordHistory(event.getPasswordHistory()); + } + + @Override + public String doAdminLogout () { + // Is a user logged-in? + if (this.isUserLoggedIn()) { + // Call other logout + return this.doUserLogout(); + } + + // Invalidate session + FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); + + // Set template type to guest + this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N + + // Redirect to index + return "index?faces-redirect=true"; //NOI18N + } + + @Override + public String doUserLogin () { + // Get user instance + User user = this.userController.createUserLogin(); + + // Create login container + LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword()); + + try { + // Call bean + User confirmedUser = this.userLoginBean.validateUserAccountStatus(container); + + // All fine here so set it here + this.setLoggedInUser(confirmedUser); + + // Retrieve user's password list + this.userPasswordHistory = this.userPasswordHistoryBean.getUserPasswordHistory(confirmedUser); + + // Set template to "login" + this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N + + // Fire event away. Keep this last before return statement. + this.userLoginEvent.fire(new UserLoggedInEvent(confirmedUser)); + + // Clear this bean + this.clear(); + + // All fine + return "login_user"; //NOI18N + } catch (final UserNotFoundException ex) { + // Show JSF message + this.showFacesMessage("form_user_login:userName", "ERROR_USER_NOT_FOUND"); //NOI18N + return ""; //NOI18N + } catch (final UserStatusLockedException ex) { + this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_LOCKED"); //NOI18N + return ""; //NOI18N + } catch (final UserStatusUnconfirmedException ex) { + this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_UNCONFIRMED"); //NOI18N + return ""; //NOI18N + } catch (final UserPasswordMismatchException ex) { + // Show JSF message + this.showFacesMessage("form_user_login:userPassword", "ERROR_USER_PASSWORD_MISMATCH"); //NOI18N + return ""; //NOI18N + } + } + + @Override + public String doUserLogout () { + // Is loggedInUser set? + if (this.getLoggedInUser() == null) { + // Throw NPE + throw new NullPointerException("this.loggedInUser is null"); //NOI18N + } else if (this.getLoggedInUser().getUserId() == null) { + // Throw again + throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N + } else if (this.getLoggedInUser().getUserId() < 1) { + // Invalid user id + throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N + } + + // Fire event + this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser())); + + // Invalidate session + FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); + + // Unset any user instances + this.setLoggedInUser(null); + this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N + + // Redirect to index + return "index"; //NOI18N + } + + @Override + public String getBaseTemplatePathName () { + return this.baseTemplatePathName; + } + + @Override + public void setBaseTemplatePathName (final String baseTemplatePathName) { + this.baseTemplatePathName = baseTemplatePathName; + } + + @Override + public User getLoggedInUser () { + return this.loggedInUser; + } + + @Override + public void setLoggedInUser (final User loggedInUser) { + this.loggedInUser = loggedInUser; + } + + @Override + public String getUserCurrentPassword () { + return this.userCurrentPassword; + } + + @Override + public void setUserCurrentPassword (final String userCurrentPassword) { + this.userCurrentPassword = userCurrentPassword; + } + + @Override + public List getUserPasswordHistory () { + return Collections.unmodifiableList(this.userPasswordHistory); + } + + @Override + public boolean ifCurrentPasswordMatches () { + // The current password must be set and not empty + if (this.getUserCurrentPassword() == null) { + // Is not set + throw new NullPointerException("this.userCurrentPassword is null"); //NOI18N + } else if (this.getUserCurrentPassword().isEmpty()) { + // Is set empty + throw new IllegalStateException("this.userCurrentPassword is empty."); //NOI18N + } + + // Create "container" + LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getUserCurrentPassword()); + + // Now check if it matches + return UserUtils.ifPasswordMatches(container, this.getLoggedInUser()); + } + + @Override + public boolean ifUserMustChangePassword () { + return ((this.isUserLoggedIn()) && (Objects.equals(this.getLoggedInUser().getUserMustChangePassword(), Boolean.TRUE))); + } + + /** + * Post-construction method + */ + @PostConstruct + public void init () { + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup + this.userLoginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N + + // Also find this + this.userPasswordHistoryBean = (UserPasswordHistorySessionBeanRemote) context.lookup("java:global/jfinancials-ejb/userPasswordHistory!org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote"); //NOI18N + + // Defaul template is guest + this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME; + } catch (final NamingException ex) { + // Continue to throw + throw new FaceletException(ex); + } + } + + @Override + public boolean isInvisible () { + // Check on login + if (!this.isUserLoggedIn()) { + // Not logged in! + throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N + } + + // Check logged-in first, then invisibility + return Objects.equals(this.getLoggedInUser().getUserProfileMode(), ProfileMode.INVISIBLE); + } + + @Override + public boolean isPasswordInHistory (final String userPassword) { + // Default is not found + boolean isPasswordInHistory = false; + + // Init variables + int count = 1; + int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N + + // Check all passwords + for (final PasswordHistory entry : this.getUserPasswordHistory()) { + // Is password the same? + if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) { + // Yes, found it + isPasswordInHistory = true; + break; + } else if (count == maxEntries) { + // Maximum reached + break; + } + + // Count up + count++; + } + + // Return status + return isPasswordInHistory; + } + + @Override + public boolean isUserLoggedIn () { + // Compare instance + this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED))); + + // Return it + return this.userLoggedIn; + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all fields + this.setUserCurrentPassword(null); + } + + /** + * Updates password history by adding given entry to it as long as it is not + * there. + *

+ * @param passwordHistory Password history entry + */ + private void updatePasswordHistory (final PasswordHistory passwordHistory) { + if (null == passwordHistory) { + // Throw NPE + throw new NullPointerException("passwordHistory is null"); //NOI18N + } else if (passwordHistory.getUserPasswordHistoryId() == null) { + // Throw NPE again + throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N + } else if (passwordHistory.getUserPasswordHistoryId() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N + } + + // Is it there? + if (this.userPasswordHistory.contains(passwordHistory)) { + // Excact copy found + return; + } + + // Check all entries + for (final PasswordHistory entry : this.userPasswordHistory) { + // Is same id number? + if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) { + // Found it + return; + } + } + + // Not found, so add it + this.userPasswordHistory.add(passwordHistory); + } + +} diff --git a/src/java/org/mxchange/jfinancials/beans/login/user/FinancialsUserLoginWebSessionController.java b/src/java/org/mxchange/jfinancials/beans/login/user/FinancialsUserLoginWebSessionController.java new file mode 100644 index 00000000..3ddd5f7b --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/login/user/FinancialsUserLoginWebSessionController.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2016 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.jfinancials.beans.login.user; + +import java.io.Serializable; +import java.util.List; +import javax.ejb.Local; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.password_history.PasswordHistory; + +/** + * An interface for registration web controllers + *

+ * @author Roland Häder + */ +@Local +public interface FinancialsUserLoginWebSessionController extends Serializable { + + /** + * Checks whether given clear-text password is in user's password history. + *

+ * @param userPassword Clear-text password + *

+ * @return Whether clear-text password is in user's password history + */ + boolean isPasswordInHistory (final String userPassword); + + /** + * Getter for base template type + *

+ * @return Template type + */ + String getBaseTemplatePathName (); + + /** + * Setter for base template type + *

+ * @param baseTemplatePathName Template type + */ + void setBaseTemplatePathName (final String baseTemplatePathName); + + /** + * Logout for administrator area. If a logged-in user instance exists, it is + * being logged-out, too. + *

+ * @return Outcome (should be redirected) + */ + String doAdminLogout (); + + /** + * Logins the user, if the account is found, confirmed and unlocked. + *

+ * @return Redirect target + */ + String doUserLogin (); + + /** + * Logout for current user by invalidating the current session. + *

+ * @return Outcome (should be redirected) + */ + String doUserLogout (); + + /** + * Getter for logged-in user instance + *

+ * @return Logged-in user instance + */ + User getLoggedInUser (); + + /** + * Setter for logged-in user instance + *

+ * @param loggedInUser Logged-in user instance + */ + void setLoggedInUser (final User loggedInUser); + + /** + * Checks whether the user is logged-in + *

+ * @return Whether the user is logged-in + */ + boolean isUserLoggedIn (); + + /** + * Checks whether the user needs to change password + *

+ * @return Whether the user needs to change password + */ + boolean ifUserMustChangePassword (); + + /** + * Whether the currently logged-in user is invisible + *

+ * @return Whether the currently logged-in user is invisible + */ + boolean isInvisible (); + + /** + * Setter for current password (clear text) + *

+ * @param userCurrentPassword Current password + */ + void setUserCurrentPassword (final String userCurrentPassword); + + /** + * Getter for current password (clear text) + *

+ * @return Current password + */ + String getUserCurrentPassword (); + + /** + * Checks whether the (previously entered) current password matches with + * from the user instance. + *

+ * @return If current password matches + */ + boolean ifCurrentPasswordMatches (); + + /** + * Getter for user's password history + *

+ * @return User's password history + */ + List getUserPasswordHistory (); + +} diff --git a/src/java/org/mxchange/jfinancials/beans/mobileprovider/FinancialsAdminMobileProviderWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/mobileprovider/FinancialsAdminMobileProviderWebRequestBean.java index 6f6b8f64..9c7a3cad 100644 --- a/src/java/org/mxchange/jfinancials/beans/mobileprovider/FinancialsAdminMobileProviderWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/mobileprovider/FinancialsAdminMobileProviderWebRequestBean.java @@ -94,6 +94,8 @@ public class FinancialsAdminMobileProviderWebRequestBean extends BaseFinancialsC * Default constructor */ public FinancialsAdminMobileProviderWebRequestBean () { + // Call super constructor + super(); } @Override diff --git a/src/java/org/mxchange/jfinancials/beans/mobileprovider/FinancialsMobileProviderWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/mobileprovider/FinancialsMobileProviderWebRequestBean.java index 202abaa4..f1395ba7 100644 --- a/src/java/org/mxchange/jfinancials/beans/mobileprovider/FinancialsMobileProviderWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/mobileprovider/FinancialsMobileProviderWebRequestBean.java @@ -59,6 +59,8 @@ public class FinancialsMobileProviderWebRequestBean extends BaseFinancialsContro * Default constructor */ public FinancialsMobileProviderWebRequestBean () { + // Call super constructor + super(); } /** diff --git a/src/java/org/mxchange/jfinancials/beans/phone/FinancialsAdminPhoneWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/phone/FinancialsAdminPhoneWebRequestBean.java index 7d20d89f..a6acca6a 100644 --- a/src/java/org/mxchange/jfinancials/beans/phone/FinancialsAdminPhoneWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/phone/FinancialsAdminPhoneWebRequestBean.java @@ -196,6 +196,9 @@ public class FinancialsAdminPhoneWebRequestBean extends BaseFinancialsController * Default constructor */ public FinancialsAdminPhoneWebRequestBean () { + // Call super constructor + super(); + // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName()); // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller)); } diff --git a/src/java/org/mxchange/jfinancials/beans/phone/FinancialsPhoneWebApplicationBean.java b/src/java/org/mxchange/jfinancials/beans/phone/FinancialsPhoneWebApplicationBean.java index 2cfcf1b5..b4587a49 100644 --- a/src/java/org/mxchange/jfinancials/beans/phone/FinancialsPhoneWebApplicationBean.java +++ b/src/java/org/mxchange/jfinancials/beans/phone/FinancialsPhoneWebApplicationBean.java @@ -85,17 +85,8 @@ public class FinancialsPhoneWebApplicationBean extends BaseFinancialsController * Default constructor */ public FinancialsPhoneWebApplicationBean () { - // Try it - try { - // Get initial context - Context context = new InitialContext(); - - // Try to lookup the beans - this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N - } catch (final NamingException e) { - // Throw it again - throw new FaceletException(e); - } + // Call super constructor + super(); // Init all lists this.mobileNumbers = new LinkedList<>(); @@ -450,6 +441,18 @@ public class FinancialsPhoneWebApplicationBean extends BaseFinancialsController */ @PostConstruct public void init () { + // Try it + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup the beans + this.phoneBean = (PhoneSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote"); //NOI18N + } catch (final NamingException e) { + // Throw it again + throw new FaceletException(e); + } + // All phone numbers this.allMobileNumbers().addAll(this.phoneBean.allMobileNumbers()); this.allFaxNumbers().addAll(this.phoneBean.allFaxNumbers()); diff --git a/src/java/org/mxchange/jfinancials/beans/profile/FinancialsUserProfileWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/profile/FinancialsUserProfileWebRequestBean.java index 795bd32e..d6920ba9 100644 --- a/src/java/org/mxchange/jfinancials/beans/profile/FinancialsUserProfileWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/profile/FinancialsUserProfileWebRequestBean.java @@ -23,7 +23,7 @@ import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; import org.mxchange.jfinancials.beans.BaseFinancialsController; -import org.mxchange.jfinancials.beans.login.FinancialsUserLoginWebSessionController; +import org.mxchange.jfinancials.beans.login.user.FinancialsUserLoginWebSessionController; import org.mxchange.jfinancials.beans.user.FinancialsUserWebSessionController; import org.mxchange.jusercore.exceptions.UserNotFoundException; import org.mxchange.jusercore.model.user.User; @@ -59,6 +59,8 @@ public class FinancialsUserProfileWebRequestBean extends BaseFinancialsControlle * Default constructor */ public FinancialsUserProfileWebRequestBean () { + // Call super constructor + super(); } /** diff --git a/src/java/org/mxchange/jfinancials/beans/profilemode/FinancialsProfileModeWebApplicationBean.java b/src/java/org/mxchange/jfinancials/beans/profilemode/FinancialsProfileModeWebApplicationBean.java index 7e59be69..26b3d2b0 100644 --- a/src/java/org/mxchange/jfinancials/beans/profilemode/FinancialsProfileModeWebApplicationBean.java +++ b/src/java/org/mxchange/jfinancials/beans/profilemode/FinancialsProfileModeWebApplicationBean.java @@ -40,6 +40,8 @@ public class FinancialsProfileModeWebApplicationBean extends BaseFinancialsContr * Default constructor */ public FinancialsProfileModeWebApplicationBean () { + // Call super constructor + super(); } @Override diff --git a/src/java/org/mxchange/jfinancials/beans/register/FinancialsUserRegisterWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/register/FinancialsUserRegisterWebSessionBean.java index e9ba733d..9e38b14b 100644 --- a/src/java/org/mxchange/jfinancials/beans/register/FinancialsUserRegisterWebSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/register/FinancialsUserRegisterWebSessionBean.java @@ -83,22 +83,24 @@ public class FinancialsUserRegisterWebSessionBean extends BaseFinancialsControll private UserRegistrationSessionBeanRemote registerBean; /** - * An en event fireable when a new user has registered + * User controller */ @Inject - @Any - private Event registeredEvent; + private FinancialsUserWebSessionController userController; /** - * User controller + * An en event fireable when a new user has registered */ @Inject - private FinancialsUserWebSessionController userController; + @Any + private Event userRegisteredEvent; /** * Default constructor */ public FinancialsUserRegisterWebSessionBean () { + // Call super constructor + super(); } @Override @@ -165,7 +167,7 @@ public class FinancialsUserRegisterWebSessionBean extends BaseFinancialsControll assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N // Fire event - this.registeredEvent.fire(new UserRegisteredEvent(registeredUser)); + this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser)); // All fine, redirect to proper page return "register_done"; //NOI18N diff --git a/src/java/org/mxchange/jfinancials/beans/resendlink/FinancialsResendLinkWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/resendlink/FinancialsResendLinkWebSessionBean.java index d3c1330e..96d6b683 100644 --- a/src/java/org/mxchange/jfinancials/beans/resendlink/FinancialsResendLinkWebSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/resendlink/FinancialsResendLinkWebSessionBean.java @@ -75,6 +75,8 @@ public class FinancialsResendLinkWebSessionBean extends BaseFinancialsController * Default constructor */ public FinancialsResendLinkWebSessionBean () { + // Call super constructor + super(); } @Override diff --git a/src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java index 087730c8..88459f4e 100644 --- a/src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java @@ -197,6 +197,8 @@ public class FinancialsAdminUserWebRequestBean extends BaseFinancialsController * Default constructor */ public FinancialsAdminUserWebRequestBean () { + // Call super constructor + super(); } @Override diff --git a/src/java/org/mxchange/jfinancials/beans/user/FinancialsUserWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/user/FinancialsUserWebSessionBean.java index 19fd8110..6b573a60 100644 --- a/src/java/org/mxchange/jfinancials/beans/user/FinancialsUserWebSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/user/FinancialsUserWebSessionBean.java @@ -36,7 +36,7 @@ import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jfinancials.beans.BaseFinancialsController; import org.mxchange.jfinancials.beans.contact.FinancialsContactWebSessionController; import org.mxchange.jfinancials.beans.features.FinancialsFeaturesWebApplicationController; -import org.mxchange.jfinancials.beans.login.FinancialsUserLoginWebSessionController; +import org.mxchange.jfinancials.beans.login.user.FinancialsUserLoginWebSessionController; import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent; import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent; import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent; @@ -149,6 +149,8 @@ public class FinancialsUserWebSessionBean extends BaseFinancialsController imple * Default constructor */ public FinancialsUserWebSessionBean () { + // Call super constructor + super(); } /** diff --git a/src/java/org/mxchange/jfinancials/beans/user/password/FinancialsUserPasswordWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/user/password/FinancialsUserPasswordWebRequestBean.java index 38cdc506..574d6789 100644 --- a/src/java/org/mxchange/jfinancials/beans/user/password/FinancialsUserPasswordWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/user/password/FinancialsUserPasswordWebRequestBean.java @@ -30,7 +30,7 @@ import javax.naming.NamingException; import org.mxchange.jcoreee.utils.FacesUtils; import org.mxchange.jfinancials.beans.BaseFinancialsController; import org.mxchange.jfinancials.beans.features.FinancialsFeaturesWebApplicationController; -import org.mxchange.jfinancials.beans.login.FinancialsUserLoginWebSessionController; +import org.mxchange.jfinancials.beans.login.user.FinancialsUserLoginWebSessionController; import org.mxchange.jusercore.events.user.password_change.ObservableUpdatedUserPasswordEvent; import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent; import org.mxchange.jusercore.exceptions.UserNotFoundException; @@ -99,6 +99,8 @@ public class FinancialsUserPasswordWebRequestBean extends BaseFinancialsControll * Default constructor */ public FinancialsUserPasswordWebRequestBean () { + // Call super constructor + super(); } @Override