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