2 * Copyright (C) 2016 Roland Häder
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.pizzaapplication.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.jusercore.container.login.LoginContainer;
35 import org.mxchange.jusercore.container.login.UserLoginContainer;
36 import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
37 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
38 import org.mxchange.jusercore.events.logout.ObservableUserLogoutEvent;
39 import org.mxchange.jusercore.events.logout.UserLogoutEvent;
40 import org.mxchange.jusercore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
41 import org.mxchange.jusercore.exceptions.UserNotFoundException;
42 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
43 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
44 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
45 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
46 import org.mxchange.jusercore.model.user.User;
47 import org.mxchange.jusercore.model.user.UserUtils;
48 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
49 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote;
50 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
51 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
52 import org.mxchange.pizzaapplication.beans.BasePizzaController;
53 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
56 * A web bean for user registration
58 * @author Roland Häder<roland@mxchange.org>
60 @Named ("userLoginController")
62 public class PizzaUserLoginWebSessionBean extends BasePizzaController implements PizzaUserLoginWebSessionController {
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;
86 * Logged-in user instance
88 private User loggedInUser;
91 * Remote register session bean
93 private UserLoginSessionBeanRemote loginBean;
96 * Event fired when user has logged in
100 private Event<ObservableUserLoggedInEvent> loginEvent;
106 private PizzaUserWebSessionController userController;
111 private String userCurrentPassword;
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<ObservableUserLoggedInEvent> userLoginEvent;
126 * Event fired when user has logged out
130 private Event<ObservableUserLogoutEvent> 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 PizzaUserLoginWebSessionBean () {
147 // Get initial context
148 Context context = new InitialContext();
151 this.loginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/jjobs-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
154 this.userPasswordHistoryBean = (UserPasswordHistorySessionBeanRemote) context.lookup("java:global/jjobs-ejb/userPasswordHistory!org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote"); //NOI18N
156 // Defaul template is guest
157 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
158 } catch (final NamingException ex) {
160 throw new FaceletException(ex);
165 * Method being call after user's password has been updated (and history
166 * entry has been created).
168 * @param event Event being observed
170 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
174 throw new NullPointerException("event is null"); //NOI18N
175 } else if (event.getPasswordHistory() == null) {
177 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
178 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
180 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
181 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
183 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
186 // All fine, so update list
187 this.updatePasswordHistory(event.getPasswordHistory());
191 public String doAdminLogout () {
192 // Is a user logged-in?
193 if (this.isUserLoggedIn()) {
195 return this.doUserLogout();
198 // Invalidate session
199 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
201 // Set template type to guest
202 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
205 return "index?faces-redirect=true"; //NOI18N
209 public String doUserLogin () {
211 User user = this.userController.createUserLogin();
213 // Create login container
214 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
218 User confirmedUser = this.loginBean.validateUserAccountStatus(container);
220 // All fine here so set it here
221 this.setLoggedInUser(confirmedUser);
223 // Retrieve user's password list
224 this.userPasswordHistory = this.userPasswordHistoryBean.getUserPasswordHistory(confirmedUser);
226 // Set template to "login"
227 this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N
229 // Fire event away. Keep this last before return statement.
230 this.userLoginEvent.fire(new UserLoggedInEvent(confirmedUser));
236 return "login_user"; //NOI18N
237 } catch (final UserNotFoundException ex) {
239 this.showFacesMessage("form_user_login:userName", "ERROR_USER_NOT_FOUND"); //NOI18N
241 } catch (final UserStatusLockedException ex) {
242 this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_LOCKED"); //NOI18N
244 } catch (final UserStatusUnconfirmedException ex) {
245 this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_UNCONFIRMED"); //NOI18N
247 } catch (final UserPasswordMismatchException ex) {
249 this.showFacesMessage("form_user_login:userPassword", "ERROR_USER_PASSWORD_MISMATCH"); //NOI18N
255 public String doUserLogout () {
256 // Is loggedInUser set?
257 if (this.getLoggedInUser() == null) {
259 throw new NullPointerException("this.loggedInUser is null"); //NOI18N
260 } else if (this.getLoggedInUser().getUserId() == null) {
262 throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
263 } else if (this.getLoggedInUser().getUserId() < 1) {
265 throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
269 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
271 // Invalidate session
272 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
274 // Unset any user instances
275 this.setLoggedInUser(null);
276 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
279 return "index"; //NOI18N
283 public String getBaseTemplatePathName () {
284 return this.baseTemplatePathName;
288 public void setBaseTemplatePathName (final String baseTemplatePathName) {
289 this.baseTemplatePathName = baseTemplatePathName;
293 public User getLoggedInUser () {
294 return this.loggedInUser;
298 public void setLoggedInUser (final User loggedInUser) {
299 this.loggedInUser = loggedInUser;
303 public String getUserCurrentPassword () {
304 return this.userCurrentPassword;
308 public void setUserCurrentPassword (final String userCurrentPassword) {
309 this.userCurrentPassword = userCurrentPassword;
313 public List<PasswordHistory> getUserPasswordHistory () {
314 return Collections.unmodifiableList(this.userPasswordHistory);
318 public boolean ifCurrentPasswordMatches () {
319 // The current password must be set and not empty
320 if (this.getUserCurrentPassword() == null) {
322 throw new NullPointerException("this.userCurrentPassword is null"); //NOI18N
323 } else if (this.getUserCurrentPassword().isEmpty()) {
325 throw new IllegalStateException("this.userCurrentPassword is empty."); //NOI18N
328 // Create "container"
329 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getUserCurrentPassword());
331 // Now check if it matches
332 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
336 public boolean ifUserMustChangePassword () {
337 return ((this.isUserLoggedIn()) && (Objects.equals(this.getLoggedInUser().getUserMustChangePassword(), Boolean.TRUE)));
341 public boolean isInvisible () {
343 if (!this.isUserLoggedIn()) {
345 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
348 // Check logged-in first, then invisibility
349 return Objects.equals(this.getLoggedInUser().getUserProfileMode(), ProfileMode.INVISIBLE);
353 public boolean isPasswordInHistory (final String userPassword) {
354 // Default is not found
355 boolean isPasswordInHistory = false;
359 int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N
361 // Check all passwords
362 for (final PasswordHistory entry : this.getUserPasswordHistory()) {
363 // Is password the same?
364 if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) {
366 isPasswordInHistory = true;
368 } else if (count == maxEntries) {
378 return isPasswordInHistory;
382 public boolean isUserLoggedIn () {
384 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
387 return this.userLoggedIn;
393 private void clear () {
395 this.setUserCurrentPassword(null);
399 * Updates password history by adding given entry to it as long as it is not
402 * @param passwordHistory Password history entry
404 private void updatePasswordHistory (final PasswordHistory passwordHistory) {
405 if (null == passwordHistory) {
407 throw new NullPointerException("passwordHistory is null"); //NOI18N
408 } else if (passwordHistory.getUserPasswordHistoryId() == null) {
410 throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N
411 } else if (passwordHistory.getUserPasswordHistoryId() < 1) {
413 throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N
417 if (this.userPasswordHistory.contains(passwordHistory)) {
423 for (final PasswordHistory entry : this.userPasswordHistory) {
424 // Is same id number?
425 if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) {
431 // Not found, so add it
432 this.userPasswordHistory.add(passwordHistory);