]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java
Please cherry-pick:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / login / AddressbookUserLoginSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jusercore.model.login;
18
19 import java.text.MessageFormat;
20 import javax.ejb.EJB;
21 import javax.ejb.Stateless;
22 import org.mxchange.jcoreee.database.BaseDatabaseBean;
23 import org.mxchange.jusercore.container.login.LoginContainer;
24 import org.mxchange.jusercore.exceptions.UserNotFoundException;
25 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
26 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
27 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
28 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
29 import org.mxchange.jusercore.model.user.User;
30 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
31 import org.mxchange.jusercore.model.user.UserUtils;
32 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
33
34 /**
35  * A session EJB for user logins
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Stateless (name = "login", description = "A bean handling the user login for Addressbook project")
40 public class AddressbookUserLoginSessionBean extends BaseDatabaseBean implements UserLoginSessionBeanRemote {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 21_785_978_127_581_965L;
46
47         /**
48          * Registration EJB
49          */
50         @EJB
51         private UserRegistrationSessionBeanRemote registerBean;
52
53         /**
54          * User EJB
55          */
56         @EJB
57         private UserSessionBeanRemote userBean;
58
59         /**
60          * Default constructor
61          */
62         public AddressbookUserLoginSessionBean () {
63                 // Call super constructor
64                 super();
65         }
66
67         @Override
68         public User validateUserAccountStatus (final LoginContainer container) throws UserNotFoundException, UserStatusLockedException, UserStatusUnconfirmedException, UserPasswordMismatchException {
69                 // Trace message
70                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: container={1} - CALLED!", this.getClass().getSimpleName(), container)); //NOI18N
71
72                 // Check some beans
73                 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
74                 assert (this.registerBean instanceof UserRegistrationSessionBeanRemote) : "this.registerBean is not set"; //NOI18N
75
76                 // user should not be null
77                 if (null == container) {
78                         // Abort here
79                         throw new NullPointerException("container is null"); //NOI18N
80                 } else if (container.getUser() == null) {
81                         // NPE again
82                         throw new NullPointerException("container.user is null"); //NOI18N
83                 } else if (container.getUserPassword() == null) {
84                         // And yet again NPE
85                         throw new NullPointerException("container.userPassword is null"); //NOI18N
86                 } else if (container.getUserPassword().isEmpty()) {
87                         // Empty password is not allowed, hardcoded.
88                         throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N
89                 }
90
91                 // Is the account there?
92                 if (!this.registerBean.isUserNameRegistered(container.getUser())) {
93                         // Not registered
94                         throw new UserNotFoundException(container.getUser());
95                 }
96
97                 // Get user instance from persistance
98                 User updatedUser = this.userBean.fillUserData(container.getUser());
99
100                 // Debug message
101                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("loginUser: updatedUser={0}", updatedUser)); //NOI18N
102
103                 // Is the user account unconfirmed?
104                 if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.UNCONFIRMED)) {
105                         // Is unconfirmed
106                         throw new UserStatusUnconfirmedException(container.getUser());
107                 } else if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.LOCKED)) {
108                         // Is locked
109                         throw new UserStatusLockedException(container.getUser());
110                 } else if (!this.isPasswordMatching(container, updatedUser)) {
111                         // Not matcing passwords
112                         throw new UserPasswordMismatchException(container.getUser());
113                 }
114
115                 // Trace message
116                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
117
118                 // Return it
119                 return updatedUser;
120         }
121
122         /**
123          * Checks if password matches of both instances. Both user instances must
124          * not match, the first one is the one from the calling bean/controller, the
125          * second is the from database.
126          * <p>
127          * @param container Container instance holding the user instance and
128          * clear-text password
129          * @param updatedUser Updated user instance found for given user name
130          * <p>
131          * @return Whether the password matches
132          */
133         private boolean isPasswordMatching (final LoginContainer container, final User updatedUser) {
134                 // First math both instances
135                 if (null == container) {
136                         // Throw NPE
137                         throw new NullPointerException("container is null"); //NOI18N
138                 } else if (null == updatedUser) {
139                         // Throw NPE
140                         throw new NullPointerException("updatedUser is null"); //NOI18N
141                 } else if (container.getUser().equals(updatedUser)) {
142                         // Both same instance!
143                         throw new IllegalArgumentException(MessageFormat.format("container.user matches updatedUser: {0}", container.getUser())); //NOI18N
144                 }
145
146                 // Is it the same same password?
147                 return UserUtils.ifPasswordMatches(container, updatedUser);
148         }
149
150 }