]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/login/JobsUserLoginWebSessionBean.java
re-added doUserLogout() + removed doLogout() as it was to generic
[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.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.enterprise.context.SessionScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.event.Observes;
26 import javax.enterprise.inject.Any;
27 import javax.faces.context.FacesContext;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jjobs.beans.BaseJobsController;
35 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
36 import org.mxchange.jusercore.container.login.LoginContainer;
37 import org.mxchange.jusercore.container.login.UserLoginContainer;
38 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
39 import org.mxchange.jusercore.events.login.UserLoginEvent;
40 import org.mxchange.jusercore.events.logout.ObserveableUserLogoutEvent;
41 import org.mxchange.jusercore.events.logout.UserLogoutEvent;
42 import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent;
43 import org.mxchange.jusercore.exceptions.UserNotFoundException;
44 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
45 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
46 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
47 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
48 import org.mxchange.jusercore.model.user.User;
49 import org.mxchange.jusercore.model.user.UserUtils;
50 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
51 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote;
52 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
53 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
54
55 /**
56  * A web bean for user registration
57  * <p>
58  * @author Roland Haeder<roland@mxchange.org>
59  */
60 @Named ("loginController")
61 @SessionScoped
62 public class JobsUserLoginWebSessionBean extends BaseJobsController implements JobsUserLoginWebSessionController {
63
64         /**
65          * Path name for guest base template
66          */
67         private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest";
68
69         /**
70          * Path name for logged-in user base template
71          */
72         private static final String USER_BASE_TEMPLATE_NAME = "login/user/user";
73
74         /**
75          * Serial number
76          */
77         private static final long serialVersionUID = 47_828_986_719_691_592L;
78
79         /**
80          * Template type for pages that might be displayed in guest area and login
81          * area.
82          */
83         private String baseTemplatePathName;
84
85         /**
86          * Current password
87          */
88         private String currentPassword;
89
90         /**
91          * Logged-in user instance
92          */
93         private User loggedInUser;
94
95         /**
96          * Remote register session bean
97          */
98         private UserLoginSessionBeanRemote loginBean;
99
100         /**
101          * Event fired when user has logged in
102          */
103         @Inject
104         @Any
105         private Event<UserLoggedInEvent> loginEvent;
106
107         /**
108          * User controller
109          */
110         @Inject
111         private JobsUserWebSessionController userController;
112
113         /**
114          * Flag whether the user has logged-in, set only from inside
115          */
116         private boolean userLoggedIn;
117
118         /**
119          * Event fired when user has logged in
120          */
121         @Inject
122         @Any
123         private Event<UserLoggedInEvent> userLoginEvent;
124
125         /**
126          * Event fired when user has logged out
127          */
128         @Inject
129         @Any
130         private Event<ObserveableUserLogoutEvent> userLogoutEvent;
131
132         /**
133          * User's password history
134          */
135         private List<PasswordHistory> userPasswordHistory;
136
137         /**
138          * EJB for user's password history
139          */
140         private UserPasswordHistorySessionBeanRemote userPasswordHistoryBean;
141
142         /**
143          * Default constructor
144          */
145         public JobsUserLoginWebSessionBean () {
146                 try {
147                         // Get initial context
148                         Context context = new InitialContext();
149
150                         // Try to lookup
151                         this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/addressbook-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
152
153                         // Also find this
154                         this.userPasswordHistoryBean = (UserPasswordHistorySessionBeanRemote) context.lookup(""); //NOI18N
155
156                         // Defaul template is guest
157                         this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
158                 } catch (final NamingException ex) {
159                         // Continue to throw
160                         throw new FaceletException(ex);
161                 }
162         }
163
164         @Override
165         public void afterUserUpdatedPasswordEvent (@Observes final UpdatedUserPasswordEvent event) {
166                 // Check parameter
167                 if (null == event) {
168                         // Throw NPE
169                         throw new NullPointerException("event is null"); //NOI18N
170                 } else if (event.getPasswordHistory() == null) {
171                         // Throw NPE again
172                         throw new NullPointerException("event.passwordHistory is null"); //NOI18N
173                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
174                         // ... and again
175                         throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
176                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
177                         // Invalid value
178                         throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
179                 }
180
181                 // All fine, so update list
182                 this.updatePasswordHistory(event.getPasswordHistory());
183         }
184
185         @Override
186         public String doAdminLogout () {
187                 // Is a user logged-in?
188                 if (this.isUserLoggedIn()) {
189                         // Call other logout
190                         return this.doUserLogout();
191                 }
192
193                 // Invalidate session
194                 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
195
196                 // Set template type to guest
197                 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
198
199                 // Redirect to index
200                 return "index?faces-redirect=true"; //NOI18N
201         }
202
203         @Override
204         public String doUserLogin () {
205                 // Get user instance
206                 User user = this.userController.createUserLogin();
207
208                 // Create login container
209                 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
210
211                 try {
212                         // Call bean
213                         User confirmedUser = this.loginBean.validateUserAccountStatus(container);
214
215                         // All fine here so set it here
216                         this.setLoggedInUser(confirmedUser);
217
218                         // Retrieve user's password list
219                         this.userPasswordHistory = this.userPasswordHistoryBean.getUserPasswordHistory(confirmedUser);
220
221                         // Set template to "login"
222                         this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N
223
224                         // Fire event away. Keep this last before return statement.
225                         this.userLoginEvent.fire(new UserLoginEvent(confirmedUser));
226
227                         // Clear this bean
228                         this.clear();
229
230                         // All fine
231                         return "login"; //NOI18N
232                 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException ex) {
233                         // Show JSF message
234                         this.showFacesMessage("form_user_login:userName", ex); //NOI18N
235                         return ""; //NOI18N
236                 } catch (final UserPasswordMismatchException ex) {
237                         // Show JSF message
238                         this.showFacesMessage("form_user_login:userPassword", ex); //NOI18N
239                         return ""; //NOI18N
240                 }
241         }
242
243         @Override
244         public String doUserLogout () {
245                 // Is loggedInUser set?
246                 if (this.getLoggedInUser() == null) {
247                         // Throw NPE
248                         throw new NullPointerException("this.loggedInUser is null"); //NOI18N
249                 } else if (this.getLoggedInUser().getUserId() == null) {
250                         // Throw again
251                         throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
252                 } else if (this.getLoggedInUser().getUserId() < 1) {
253                         // Invalid user id
254                         throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
255                 }
256
257                 // Fire event
258                 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
259
260                 // Invalidate session
261                 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
262
263                 // Unset any user instances
264                 this.setLoggedInUser(null);
265                 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
266
267                 // Redirect to index
268                 return "index"; //NOI18N
269         }
270
271         @Override
272         public String getBaseTemplatePathName () {
273                 return this.baseTemplatePathName;
274         }
275
276         @Override
277         public void setBaseTemplatePathName (final String baseTemplatePathName) {
278                 this.baseTemplatePathName = baseTemplatePathName;
279         }
280
281         @Override
282         public String getCurrentPassword () {
283                 return this.currentPassword;
284         }
285
286         @Override
287         public void setCurrentPassword (final String currentPassword) {
288                 this.currentPassword = currentPassword;
289         }
290
291         @Override
292         public User getLoggedInUser () {
293                 return this.loggedInUser;
294         }
295
296         @Override
297         public void setLoggedInUser (final User loggedInUser) {
298                 this.loggedInUser = loggedInUser;
299         }
300
301         @Override
302         public List<PasswordHistory> getUserPasswordHistory () {
303                 return Collections.unmodifiableList(this.userPasswordHistory);
304         }
305
306         @Override
307         public boolean ifCurrentPasswordMatches () {
308                 // The current password must be set and not empty
309                 if (this.getCurrentPassword() == null) {
310                         // Is not set
311                         throw new NullPointerException("this.currentPassword is null"); //NOI18N
312                 } else if (this.getCurrentPassword().isEmpty()) {
313                         // Is set empty
314                         throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
315                 }
316
317                 // Create "container"
318                 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
319
320                 // Now check if it matches
321                 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
322         }
323
324         @Override
325         public boolean ifUserMustChangePassword () {
326                 return (this.isUserLoggedIn() && this.getLoggedInUser().getUserMustChangePassword());
327         }
328
329         @Override
330         public boolean isInvisible () {
331                 // Check on login
332                 if (!this.isUserLoggedIn()) {
333                         // Not logged in!
334                         throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
335                 }
336
337                 // Check logged-in first, then invisibility
338                 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
339         }
340
341         @Override
342         public boolean isPasswordInHistory (final String userPassword) {
343                 // Default is not found
344                 boolean isPasswordInHistory = false;
345
346                 // Init variables
347                 int count = 1;
348                 int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N
349
350                 // Check all passwords
351                 for (final PasswordHistory entry : this.getUserPasswordHistory()) {
352                         // Is password the same?
353                         if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) {
354                                 // Yes, found it
355                                 isPasswordInHistory = true;
356                                 break;
357                         } else if (count == maxEntries) {
358                                 // Maximum reached
359                                 break;
360                         }
361
362                         // Count up
363                         count++;
364                 }
365
366                 // Return status
367                 return isPasswordInHistory;
368         }
369
370         @Override
371         public boolean isUserLoggedIn () {
372                 // Compare instance
373                 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
374
375                 // Return it
376                 return this.userLoggedIn;
377         }
378
379         /**
380          * Clears this bean
381          */
382         private void clear () {
383                 // Clear all fields
384                 this.setCurrentPassword(null);
385         }
386
387         /**
388          * Updates password history by adding given entry to it as long as it is not
389          * there.
390          * <p>
391          * @param passwordHistory Password history entry
392          */
393         private void updatePasswordHistory (final PasswordHistory passwordHistory) {
394                 if (null == passwordHistory) {
395                         // Throw NPE
396                         throw new NullPointerException("passwordHistory is null"); //NOI18N
397                 } else if (passwordHistory.getUserPasswordHistoryId() == null) {
398                         // Throw NPE again
399                         throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N
400                 } else if (passwordHistory.getUserPasswordHistoryId() < 1) {
401                         // Invalid id
402                         throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N
403                 }
404
405                 // Is it there?
406                 if (this.userPasswordHistory.contains(passwordHistory)) {
407                         // Excact copy found
408                         return;
409                 }
410
411                 // Check all entries
412                 for (final PasswordHistory entry : this.userPasswordHistory) {
413                         // Is same id number?
414                         if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) {
415                                 // Found it
416                                 return;
417                         }
418                 }
419
420                 // Not found, so add it
421                 this.userPasswordHistory.add(passwordHistory);
422         }
423
424 }