2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.login;
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;
56 * A web bean for user registration
58 * @author Roland Haeder<roland@mxchange.org>
60 @Named ("loginController")
62 public class JobsUserLoginWebSessionBean extends BaseJobsController implements JobsUserLoginWebSessionController {
65 * Path name for guest base template
67 private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest";
70 * Path name for logged-in user base template
72 private static final String USER_BASE_TEMPLATE_NAME = "login/user/user";
77 private static final long serialVersionUID = 47_828_986_719_691_592L;
80 * Template type for pages that might be displayed in guest area and login
83 private String baseTemplatePathName;
88 private String currentPassword;
91 * Logged-in user instance
93 private User loggedInUser;
96 * Remote register session bean
98 private UserLoginSessionBeanRemote loginBean;
101 * Event fired when user has logged in
105 private Event<UserLoggedInEvent> loginEvent;
111 private JobsUserWebSessionController userController;
114 * Flag whether the user has logged-in, set only from inside
116 private boolean userLoggedIn;
119 * Event fired when user has logged in
123 private Event<UserLoggedInEvent> userLoginEvent;
126 * Event fired when user has logged out
130 private Event<ObserveableUserLogoutEvent> userLogoutEvent;
133 * User's password history
135 private List<PasswordHistory> userPasswordHistory;
138 * EJB for user's password history
140 private UserPasswordHistorySessionBeanRemote userPasswordHistoryBean;
143 * Default constructor
145 public JobsUserLoginWebSessionBean () {
147 // Get initial context
148 Context context = new InitialContext();
151 this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/addressbook-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
154 this.userPasswordHistoryBean = (UserPasswordHistorySessionBeanRemote) context.lookup(""); //NOI18N
156 // Defaul template is guest
157 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
158 } catch (final NamingException ex) {
160 throw new FaceletException(ex);
165 public void afterUserUpdatedPasswordEvent (@Observes final UpdatedUserPasswordEvent event) {
169 throw new NullPointerException("event is null"); //NOI18N
170 } else if (event.getPasswordHistory() == null) {
172 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
173 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
175 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
176 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
178 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
181 // All fine, so update list
182 this.updatePasswordHistory(event.getPasswordHistory());
186 public String doAdminLogout () {
187 // Is a user logged-in?
188 if (this.isUserLoggedIn()) {
190 return this.doUserLogout();
193 // Invalidate session
194 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
196 // Set template type to guest
197 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
200 return "index?faces-redirect=true"; //NOI18N
204 public String doUserLogin () {
206 User user = this.userController.createUserLogin();
208 // Create login container
209 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
213 User confirmedUser = this.loginBean.validateUserAccountStatus(container);
215 // All fine here so set it here
216 this.setLoggedInUser(confirmedUser);
218 // Retrieve user's password list
219 this.userPasswordHistory = this.userPasswordHistoryBean.getUserPasswordHistory(confirmedUser);
221 // Set template to "login"
222 this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N
224 // Fire event away. Keep this last before return statement.
225 this.userLoginEvent.fire(new UserLoginEvent(confirmedUser));
231 return "login"; //NOI18N
232 } catch (final UserNotFoundException ex) {
234 this.showFacesMessage("form_user_login:userName", "ERROR_USER_NOT_FOUND"); //NOI18N
236 } catch (final UserStatusLockedException ex) {
237 this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_LOCKED"); //NOI18N
239 } catch (final UserStatusUnconfirmedException ex) {
240 this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_UNCONFIRMED"); //NOI18N
242 } catch (final UserPasswordMismatchException ex) {
244 this.showFacesMessage("form_user_login:userPassword", "ERROR_USER_PASSWORD_MISMATCH"); //NOI18N
250 public String doUserLogout () {
251 // Is loggedInUser set?
252 if (this.getLoggedInUser() == null) {
254 throw new NullPointerException("this.loggedInUser is null"); //NOI18N
255 } else if (this.getLoggedInUser().getUserId() == null) {
257 throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
258 } else if (this.getLoggedInUser().getUserId() < 1) {
260 throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
264 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
266 // Invalidate session
267 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
269 // Unset any user instances
270 this.setLoggedInUser(null);
271 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
274 return "index"; //NOI18N
278 public String getBaseTemplatePathName () {
279 return this.baseTemplatePathName;
283 public void setBaseTemplatePathName (final String baseTemplatePathName) {
284 this.baseTemplatePathName = baseTemplatePathName;
288 public String getCurrentPassword () {
289 return this.currentPassword;
293 public void setCurrentPassword (final String currentPassword) {
294 this.currentPassword = currentPassword;
298 public User getLoggedInUser () {
299 return this.loggedInUser;
303 public void setLoggedInUser (final User loggedInUser) {
304 this.loggedInUser = loggedInUser;
308 public List<PasswordHistory> getUserPasswordHistory () {
309 return Collections.unmodifiableList(this.userPasswordHistory);
313 public boolean ifCurrentPasswordMatches () {
314 // The current password must be set and not empty
315 if (this.getCurrentPassword() == null) {
317 throw new NullPointerException("this.currentPassword is null"); //NOI18N
318 } else if (this.getCurrentPassword().isEmpty()) {
320 throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
323 // Create "container"
324 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
326 // Now check if it matches
327 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
331 public boolean ifUserMustChangePassword () {
332 return (this.isUserLoggedIn() && this.getLoggedInUser().getUserMustChangePassword());
336 public boolean isInvisible () {
338 if (!this.isUserLoggedIn()) {
340 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
343 // Check logged-in first, then invisibility
344 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
348 public boolean isPasswordInHistory (final String userPassword) {
349 // Default is not found
350 boolean isPasswordInHistory = false;
354 int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N
356 // Check all passwords
357 for (final PasswordHistory entry : this.getUserPasswordHistory()) {
358 // Is password the same?
359 if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) {
361 isPasswordInHistory = true;
363 } else if (count == maxEntries) {
373 return isPasswordInHistory;
377 public boolean isUserLoggedIn () {
379 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
382 return this.userLoggedIn;
388 private void clear () {
390 this.setCurrentPassword(null);
394 * Updates password history by adding given entry to it as long as it is not
397 * @param passwordHistory Password history entry
399 private void updatePasswordHistory (final PasswordHistory passwordHistory) {
400 if (null == passwordHistory) {
402 throw new NullPointerException("passwordHistory is null"); //NOI18N
403 } else if (passwordHistory.getUserPasswordHistoryId() == null) {
405 throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N
406 } else if (passwordHistory.getUserPasswordHistoryId() < 1) {
408 throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N
412 if (this.userPasswordHistory.contains(passwordHistory)) {
418 for (final PasswordHistory entry : this.userPasswordHistory) {
419 // Is same id number?
420 if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) {
426 // Not found, so add it
427 this.userPasswordHistory.add(passwordHistory);