]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/juserlogincore/model/user/login/JobsUserLoginSessionBean.java
Please cherry-pick/rename:
[jjobs-ejb.git] / src / java / org / mxchange / juserlogincore / model / user / login / JobsUserLoginSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
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.juserlogincore.model.user.login;
18
19 import java.text.MessageFormat;
20 import javax.ejb.EJB;
21 import javax.ejb.Stateless;
22 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
23 import org.mxchange.jusercore.exceptions.UserNotFoundException;
24 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
25 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
26 import org.mxchange.jusercore.model.user.User;
27 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
28 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
29 import org.mxchange.juserlogincore.container.login.LoginContainer;
30 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
31 import org.mxchange.juserlogincore.login.UserLoginUtils;
32 import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote;
33
34 /**
35  * A session EJB for user logins
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Stateless (name = "userLogin", description = "A bean handling the user login for Jobs project")
40 public class JobsUserLoginSessionBean extends BaseJobsDatabaseBean 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 JobsUserLoginSessionBean () {
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                 // @TODO Rewrite this to use JCache instead
99                 final User updatedUser = this.userBean.fillUserData(container.getUser());
100
101                 // Debug message
102                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("loginUser: updatedUser={0}", updatedUser)); //NOI18N
103
104                 // Is the user account unconfirmed?
105                 if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.UNCONFIRMED)) {
106                         // Is unconfirmed
107                         throw new UserStatusUnconfirmedException(container.getUser());
108                 } else if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.LOCKED)) {
109                         // Is locked
110                         throw new UserStatusLockedException(container.getUser());
111                 } else if (!this.isPasswordMatching(container, updatedUser)) {
112                         // Not matcing passwords
113                         throw new UserPasswordMismatchException(container.getUser());
114                 }
115
116                 // Trace message
117                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
118
119                 // Return it
120                 return updatedUser;
121         }
122
123         /**
124          * Checks if password matches of both instances. Both user instances must
125          * not match, the first one is the one from the calling bean/controller, the
126          * second is the from database.
127          * <p>
128          * @param container   Container instance holding the user instance and
129          *                    clear-text password
130          * @param updatedUser Updated user instance found for given user name
131          * <p>
132          * @return Whether the password matches
133          */
134         private boolean isPasswordMatching (final LoginContainer container, final User updatedUser) {
135                 // First math both instances
136                 if (null == container) {
137                         // Throw NPE
138                         throw new NullPointerException("container is null"); //NOI18N
139                 } else if (null == updatedUser) {
140                         // Throw NPE
141                         throw new NullPointerException("updatedUser is null"); //NOI18N
142                 } else if (container.getUser().equals(updatedUser)) {
143                         // Both same instance!
144                         throw new IllegalArgumentException(MessageFormat.format("container.user matches updatedUser: {0}", container.getUser())); //NOI18N
145                 }
146
147                 // Is it the same same password?
148                 return UserLoginUtils.ifPasswordMatches(container, updatedUser);
149         }
150
151 }