]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/login/JobsUserLoginWebSessionBean.java
Continued a bit: (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / login / JobsUserLoginWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jjobs.beans.login;
18
19 import java.util.Objects;
20 import javax.enterprise.context.SessionScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jjobs.beans.BaseJobsController;
30 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
31 import org.mxchange.jusercore.container.login.LoginContainer;
32 import org.mxchange.jusercore.container.login.UserLoginContainer;
33 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
34 import org.mxchange.jusercore.events.login.UserLoginEvent;
35 import org.mxchange.jusercore.exceptions.UserNotFoundException;
36 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
37 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
38 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
39 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
40 import org.mxchange.jusercore.model.user.User;
41 import org.mxchange.jusercore.model.user.UserUtils;
42 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
44
45 /**
46  * A web bean for user registration
47  * <p>
48  * @author Roland Haeder<roland@mxchange.org>
49  */
50 @Named ("loginController")
51 @SessionScoped
52 public class JobsUserLoginWebSessionBean extends BaseJobsController implements JobsUserLoginWebSessionController {
53
54         /**
55          * Serial number
56          */
57         private static final long serialVersionUID = 47_828_986_719_691_592L;
58
59         /**
60          * Current password
61          */
62         private String currentPassword;
63
64         /**
65          * Logged-in user instance
66          */
67         private User loggedInUser;
68
69         /**
70          * Remote register session bean
71          */
72         private UserLoginSessionBeanRemote loginBean;
73
74         /**
75          * Event fired when user has logged in
76          */
77         @Inject
78         @Any
79         private Event<UserLoggedInEvent> loginEvent;
80
81         /**
82          * Template type for pages that might be displayed in guest area and login
83          * area. Default is guest area.
84          */
85         private String templateType = "guest"; //NOI18N
86
87         /**
88          * User controller
89          */
90         @Inject
91         private JobsUserWebSessionController userController;
92
93         /**
94          * Flag whether the user has logged-in, set only from inside
95          */
96         private boolean userLoggedIn;
97
98         /**
99          * Default constructor
100          */
101         public JobsUserLoginWebSessionBean () {
102                 try {
103                         // Get initial context
104                         Context context = new InitialContext();
105
106                         // Try to lookup
107                         this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/jjobs-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
108                 } catch (final NamingException ex) {
109                         // Continue to throw
110                         throw new FaceletException(ex);
111                 }
112         }
113
114         @Override
115         public String doLogin () {
116                 // Get user instance
117                 User user = this.userController.createUserLogin();
118
119                 // Create login container
120                 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
121
122                 try {
123                         // Call bean
124                         User confirmedUser = this.loginBean.validateUserAccountStatus(container);
125
126                         // All fine here so set it here
127                         this.setLoggedInUser(confirmedUser);
128
129                         // Set template to "login"
130                         this.setTemplateType("login"); //NOI18N
131
132                         // Fire event away. Keep this last before return statement.
133                         this.loginEvent.fire(new UserLoginEvent(confirmedUser));
134
135                         // All fine
136                         return "login"; //NOI18N
137                 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException | UserPasswordMismatchException ex) {
138                         // Throw again
139                         throw new FaceletException(ex);
140                 }
141         }
142
143         @Override
144         public String getCurrentPassword () {
145                 return this.currentPassword;
146         }
147
148         @Override
149         public void setCurrentPassword (final String currentPassword) {
150                 this.currentPassword = currentPassword;
151         }
152
153         @Override
154         public User getLoggedInUser () {
155                 return this.loggedInUser;
156         }
157
158         @Override
159         public void setLoggedInUser (final User loggedInUser) {
160                 this.loggedInUser = loggedInUser;
161         }
162
163         @Override
164         public String getTemplateType () {
165                 return this.templateType;
166         }
167
168         @Override
169         public void setTemplateType (final String templateType) {
170                 this.templateType = templateType;
171         }
172
173         @Override
174         public boolean ifCurrentPasswordMatches () {
175                 // The current password must be set and not empty
176                 if (this.getCurrentPassword() == null) {
177                         // Is not set
178                         throw new NullPointerException("this.currentPassword is null"); //NOI18N
179                 } else if (this.getCurrentPassword().isEmpty()) {
180                         // Is set empty
181                         throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
182                 }
183
184                 // Create "container"
185                 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
186
187                 // Now check if it matches
188                 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
189         }
190
191         @Override
192         public boolean ifUserMustChangePassword () {
193                 return (this.isUserLoggedIn() && this.getLoggedInUser().getUserMustChangePassword());
194         }
195
196         @Override
197         public boolean isInvisible () {
198                 // Check on login
199                 if (!this.isUserLoggedIn()) {
200                         // Not logged in!
201                         throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
202                 }
203
204                 // Check logged-in first, then invisibility
205                 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
206         }
207
208         @Override
209         public boolean isUserLoggedIn () {
210                 // Trace message
211                 // NOISY-DEBUG System.out.println(MessageFormat.format("JobsUserLoginWebSessionBean:isUserLoggedIn: this.loggedInUser={0},this.templateType={1} - CALLED!", this.getLoggedInUser(), this.getTemplateType()));
212
213                 // Compare instance
214                 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
215
216                 // Trace message
217                 // NOISY-DEBUG System.out.println(MessageFormat.format("JobsUserLoginWebSessionBean:isUserLoggedIn: this.userLoggedIn={0} - EXIT!", this.userLoggedIn));
218                 // Return it
219                 return this.userLoggedIn;
220         }
221
222 }