]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/login/JobsUserLoginWebSessionBean.java
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.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 ex) {
233                         // Show JSF message
234                         this.showFacesMessage("form_user_login:userName", "ERROR_USER_NOT_FOUND"); //NOI18N
235                         return ""; //NOI18N
236                 } catch (final UserStatusLockedException ex) {
237                         this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_LOCKED"); //NOI18N
238                         return ""; //NOI18N
239                 } catch (final UserStatusUnconfirmedException ex) {
240                         this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_UNCONFIRMED"); //NOI18N
241                         return ""; //NOI18N
242                 } catch (final UserPasswordMismatchException ex) {
243                         // Show JSF message
244                         this.showFacesMessage("form_user_login:userPassword", "ERROR_USER_PASSWORD_MISMATCH"); //NOI18N
245                         return ""; //NOI18N
246                 }
247         }
248
249         @Override
250         public String doUserLogout () {
251                 // Is loggedInUser set?
252                 if (this.getLoggedInUser() == null) {
253                         // Throw NPE
254                         throw new NullPointerException("this.loggedInUser is null"); //NOI18N
255                 } else if (this.getLoggedInUser().getUserId() == null) {
256                         // Throw again
257                         throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
258                 } else if (this.getLoggedInUser().getUserId() < 1) {
259                         // Invalid user id
260                         throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
261                 }
262
263                 // Fire event
264                 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
265
266                 // Invalidate session
267                 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
268
269                 // Unset any user instances
270                 this.setLoggedInUser(null);
271                 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
272
273                 // Redirect to index
274                 return "index"; //NOI18N
275         }
276
277         @Override
278         public String getBaseTemplatePathName () {
279                 return this.baseTemplatePathName;
280         }
281
282         @Override
283         public void setBaseTemplatePathName (final String baseTemplatePathName) {
284                 this.baseTemplatePathName = baseTemplatePathName;
285         }
286
287         @Override
288         public String getCurrentPassword () {
289                 return this.currentPassword;
290         }
291
292         @Override
293         public void setCurrentPassword (final String currentPassword) {
294                 this.currentPassword = currentPassword;
295         }
296
297         @Override
298         public User getLoggedInUser () {
299                 return this.loggedInUser;
300         }
301
302         @Override
303         public void setLoggedInUser (final User loggedInUser) {
304                 this.loggedInUser = loggedInUser;
305         }
306
307         @Override
308         public List<PasswordHistory> getUserPasswordHistory () {
309                 return Collections.unmodifiableList(this.userPasswordHistory);
310         }
311
312         @Override
313         public boolean ifCurrentPasswordMatches () {
314                 // The current password must be set and not empty
315                 if (this.getCurrentPassword() == null) {
316                         // Is not set
317                         throw new NullPointerException("this.currentPassword is null"); //NOI18N
318                 } else if (this.getCurrentPassword().isEmpty()) {
319                         // Is set empty
320                         throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
321                 }
322
323                 // Create "container"
324                 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
325
326                 // Now check if it matches
327                 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
328         }
329
330         @Override
331         public boolean ifUserMustChangePassword () {
332                 return ((this.isUserLoggedIn()) && (Objects.equals(this.getLoggedInUser().getUserMustChangePassword(), Boolean.TRUE)));
333         }
334
335         @Override
336         public boolean isInvisible () {
337                 // Check on login
338                 if (!this.isUserLoggedIn()) {
339                         // Not logged in!
340                         throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
341                 }
342
343                 // Check logged-in first, then invisibility
344                 return Objects.equals(this.getLoggedInUser().getUserProfileMode(), ProfileMode.INVISIBLE);
345         }
346
347         @Override
348         public boolean isPasswordInHistory (final String userPassword) {
349                 // Default is not found
350                 boolean isPasswordInHistory = false;
351
352                 // Init variables
353                 int count = 1;
354                 int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N
355
356                 // Check all passwords
357                 for (final PasswordHistory entry : this.getUserPasswordHistory()) {
358                         // Is password the same?
359                         if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) {
360                                 // Yes, found it
361                                 isPasswordInHistory = true;
362                                 break;
363                         } else if (count == maxEntries) {
364                                 // Maximum reached
365                                 break;
366                         }
367
368                         // Count up
369                         count++;
370                 }
371
372                 // Return status
373                 return isPasswordInHistory;
374         }
375
376         @Override
377         public boolean isUserLoggedIn () {
378                 // Compare instance
379                 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
380
381                 // Return it
382                 return this.userLoggedIn;
383         }
384
385         /**
386          * Clears this bean
387          */
388         private void clear () {
389                 // Clear all fields
390                 this.setCurrentPassword(null);
391         }
392
393         /**
394          * Updates password history by adding given entry to it as long as it is not
395          * there.
396          * <p>
397          * @param passwordHistory Password history entry
398          */
399         private void updatePasswordHistory (final PasswordHistory passwordHistory) {
400                 if (null == passwordHistory) {
401                         // Throw NPE
402                         throw new NullPointerException("passwordHistory is null"); //NOI18N
403                 } else if (passwordHistory.getUserPasswordHistoryId() == null) {
404                         // Throw NPE again
405                         throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N
406                 } else if (passwordHistory.getUserPasswordHistoryId() < 1) {
407                         // Invalid id
408                         throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N
409                 }
410
411                 // Is it there?
412                 if (this.userPasswordHistory.contains(passwordHistory)) {
413                         // Excact copy found
414                         return;
415                 }
416
417                 // Check all entries
418                 for (final PasswordHistory entry : this.userPasswordHistory) {
419                         // Is same id number?
420                         if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) {
421                                 // Found it
422                                 return;
423                         }
424                 }
425
426                 // Not found, so add it
427                 this.userPasswordHistory.add(passwordHistory);
428         }
429
430 }