From: Roland Häder Date: Thu, 6 Jul 2017 21:56:04 +0000 (+0200) Subject: Please cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=02c40f99c2aa44d7d26014ae15b1ebfcfc3a57ee;p=pizzaservice-ejb.git Please cherry-pick: - 'login' is to generic, there are maybe other logins around than just for users only Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jusercore/model/login/PizzaUserLoginSessionBean.java b/src/java/org/mxchange/jusercore/model/login/PizzaUserLoginSessionBean.java deleted file mode 100644 index 89c87a6..0000000 --- a/src/java/org/mxchange/jusercore/model/login/PizzaUserLoginSessionBean.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.jusercore.model.login; - -import org.mxchange.jusercore.model.login.user.UserLoginSessionBeanRemote; -import java.text.MessageFormat; -import javax.ejb.EJB; -import javax.ejb.Stateless; -import org.mxchange.jusercore.container.login.LoginContainer; -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.user.register.UserRegistrationSessionBeanRemote; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.UserSessionBeanRemote; -import org.mxchange.jusercore.model.user.UserUtils; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; -import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean; - -/** - * A session EJB for user logins - *

- * @author Roland Häder - */ -@Stateless (name = "login", description = "A bean handling the user login") -public class PizzaUserLoginSessionBean extends BasePizzaDatabaseBean 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 PizzaUserLoginSessionBean () { - // 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 UserUtils.ifPasswordMatches(container, updatedUser); - } - -} diff --git a/src/java/org/mxchange/jusercore/model/user/login/PizzaUserLoginSessionBean.java b/src/java/org/mxchange/jusercore/model/user/login/PizzaUserLoginSessionBean.java new file mode 100644 index 0000000..98180bb --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/user/login/PizzaUserLoginSessionBean.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.jusercore.model.user.login; + +import java.text.MessageFormat; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.jusercore.container.login.LoginContainer; +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.user.UserLoginSessionBeanRemote; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserSessionBeanRemote; +import org.mxchange.jusercore.model.user.UserUtils; +import org.mxchange.jusercore.model.user.register.UserRegistrationSessionBeanRemote; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; +import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean; + +/** + * 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 PizzaUserLoginSessionBean extends BasePizzaDatabaseBean 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 PizzaUserLoginSessionBean () { + // 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 UserUtils.ifPasswordMatches(container, updatedUser); + } + +}