From: Roland Häder Date: Wed, 12 Jul 2017 20:59:03 +0000 (+0200) Subject: Opps ... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7b6dd669876bdeba51ddd5df9cd81f3b14d98684;p=addressbook-ejb.git Opps ... Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/juserlogincore/model/user/login/AddressbookUserLoginSessionBean.java b/src/java/org/mxchange/juserlogincore/model/user/login/AddressbookUserLoginSessionBean.java new file mode 100644 index 0000000..355b283 --- /dev/null +++ b/src/java/org/mxchange/juserlogincore/model/user/login/AddressbookUserLoginSessionBean.java @@ -0,0 +1,151 @@ +/* + * 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.juserlogincore.model.user.login; + +import java.text.MessageFormat; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; +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.juserlogincore.container.login.LoginContainer; +import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; +import org.mxchange.juserlogincore.login.UserLoginUtils; +import org.mxchange.juserlogincore.model.login.user.UserLoginSessionBeanRemote; +import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote; + +/** + * A session EJB for user logins + *

+ * @author Roland Häder + */ +@Stateless (name = "userLogin", description = "A bean handling the user login for Financials project") +public class AddressbookUserLoginSessionBean extends BaseAddressbookDatabaseBean implements UserLoginSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 21_785_978_127_581_965L; + + /** + * Registration EJB + */ + @EJB + private UserRegistrationSessionBeanRemote registerBean; + + /** + * User EJB + */ + @EJB + private UserSessionBeanRemote userBean; + + /** + * Default constructor + */ + public AddressbookUserLoginSessionBean () { + // Call super constructor + super(); + } + + @Override + public User validateUserAccountStatus (final LoginContainer container) throws UserNotFoundException, UserStatusLockedException, UserStatusUnconfirmedException, UserPasswordMismatchException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: container={1} - CALLED!", this.getClass().getSimpleName(), container)); //NOI18N + + // Check some beans + assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N + assert (this.registerBean instanceof UserRegistrationSessionBeanRemote) : "this.registerBean is not set"; //NOI18N + + // user should not be null + if (null == container) { + // Abort here + throw new NullPointerException("container is null"); //NOI18N + } else if (container.getUser() == null) { + // NPE again + throw new NullPointerException("container.user is null"); //NOI18N + } else if (container.getUserPassword() == null) { + // And yet again NPE + throw new NullPointerException("container.userPassword is null"); //NOI18N + } else if (container.getUserPassword().isEmpty()) { + // Empty password is not allowed, hardcoded. + throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N + } + + // Is the account there? + if (!this.registerBean.isUserNameRegistered(container.getUser())) { + // Not registered + throw new UserNotFoundException(container.getUser()); + } + + // Get user instance from persistance + User updatedUser = this.userBean.fillUserData(container.getUser()); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("loginUser: updatedUser={0}", updatedUser)); //NOI18N + + // Is the user account unconfirmed? + if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.UNCONFIRMED)) { + // Is unconfirmed + throw new UserStatusUnconfirmedException(container.getUser()); + } else if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.LOCKED)) { + // Is locked + throw new UserStatusLockedException(container.getUser()); + } else if (!this.isPasswordMatching(container, updatedUser)) { + // Not matcing passwords + throw new UserPasswordMismatchException(container.getUser()); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N + + // Return it + return updatedUser; + } + + /** + * Checks if password matches of both instances. Both user instances must + * not match, the first one is the one from the calling bean/controller, the + * second is the from database. + *

+ * @param container Container instance holding the user instance and + * clear-text password + * @param updatedUser Updated user instance found for given user name + *

+ * @return Whether the password matches + */ + private boolean isPasswordMatching (final LoginContainer container, final User updatedUser) { + // First math both instances + if (null == container) { + // Throw NPE + throw new NullPointerException("container is null"); //NOI18N + } else if (null == updatedUser) { + // Throw NPE + throw new NullPointerException("updatedUser is null"); //NOI18N + } else if (container.getUser().equals(updatedUser)) { + // Both same instance! + throw new IllegalArgumentException(MessageFormat.format("container.user matches updatedUser: {0}", container.getUser())); //NOI18N + } + + // Is it the same same password? + return UserLoginUtils.ifPasswordMatches(container, updatedUser); + } + +} diff --git a/src/java/org/mxchange/juserlogincore/model/user/login/FinancialsUserLoginSessionBean.java b/src/java/org/mxchange/juserlogincore/model/user/login/FinancialsUserLoginSessionBean.java deleted file mode 100644 index 99fb347..0000000 --- a/src/java/org/mxchange/juserlogincore/model/user/login/FinancialsUserLoginSessionBean.java +++ /dev/null @@ -1,151 +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.juserlogincore.model.user.login; - -import java.text.MessageFormat; -import javax.ejb.EJB; -import javax.ejb.Stateless; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.exceptions.UserStatusLockedException; -import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; -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.juserlogincore.container.login.LoginContainer; -import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; -import org.mxchange.juserlogincore.login.UserLoginUtils; -import org.mxchange.juserlogincore.model.login.user.UserLoginSessionBeanRemote; -import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote; - -/** - * A session EJB for user logins - *

- * @author Roland Häder - */ -@Stateless (name = "userLogin", description = "A bean handling the user login for Financials project") -public class FinancialsUserLoginSessionBean extends BaseAddressbookDatabaseBean implements UserLoginSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 21_785_978_127_581_965L; - - /** - * Registration EJB - */ - @EJB - private UserRegistrationSessionBeanRemote registerBean; - - /** - * User EJB - */ - @EJB - private UserSessionBeanRemote userBean; - - /** - * Default constructor - */ - public FinancialsUserLoginSessionBean () { - // Call super constructor - super(); - } - - @Override - public User validateUserAccountStatus (final LoginContainer container) throws UserNotFoundException, UserStatusLockedException, UserStatusUnconfirmedException, UserPasswordMismatchException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: container={1} - CALLED!", this.getClass().getSimpleName(), container)); //NOI18N - - // Check some beans - assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N - assert (this.registerBean instanceof UserRegistrationSessionBeanRemote) : "this.registerBean is not set"; //NOI18N - - // user should not be null - if (null == container) { - // Abort here - throw new NullPointerException("container is null"); //NOI18N - } else if (container.getUser() == null) { - // NPE again - throw new NullPointerException("container.user is null"); //NOI18N - } else if (container.getUserPassword() == null) { - // And yet again NPE - throw new NullPointerException("container.userPassword is null"); //NOI18N - } else if (container.getUserPassword().isEmpty()) { - // Empty password is not allowed, hardcoded. - throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N - } - - // Is the account there? - if (!this.registerBean.isUserNameRegistered(container.getUser())) { - // Not registered - throw new UserNotFoundException(container.getUser()); - } - - // Get user instance from persistance - User updatedUser = this.userBean.fillUserData(container.getUser()); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("loginUser: updatedUser={0}", updatedUser)); //NOI18N - - // Is the user account unconfirmed? - if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.UNCONFIRMED)) { - // Is unconfirmed - throw new UserStatusUnconfirmedException(container.getUser()); - } else if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.LOCKED)) { - // Is locked - throw new UserStatusLockedException(container.getUser()); - } else if (!this.isPasswordMatching(container, updatedUser)) { - // Not matcing passwords - throw new UserPasswordMismatchException(container.getUser()); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N - - // Return it - return updatedUser; - } - - /** - * Checks if password matches of both instances. Both user instances must - * not match, the first one is the one from the calling bean/controller, the - * second is the from database. - *

- * @param container Container instance holding the user instance and - * clear-text password - * @param updatedUser Updated user instance found for given user name - *

- * @return Whether the password matches - */ - private boolean isPasswordMatching (final LoginContainer container, final User updatedUser) { - // First math both instances - if (null == container) { - // Throw NPE - throw new NullPointerException("container is null"); //NOI18N - } else if (null == updatedUser) { - // Throw NPE - throw new NullPointerException("updatedUser is null"); //NOI18N - } else if (container.getUser().equals(updatedUser)) { - // Both same instance! - throw new IllegalArgumentException(MessageFormat.format("container.user matches updatedUser: {0}", container.getUser())); //NOI18N - } - - // Is it the same same password? - return UserLoginUtils.ifPasswordMatches(container, updatedUser); - } - -}