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.addressbook.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.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.addressbook.beans.BaseAddressbookController;
36 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
37 import org.mxchange.jusercore.container.login.LoginContainer;
38 import org.mxchange.jusercore.container.login.UserLoginContainer;
39 import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
40 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
41 import org.mxchange.jusercore.events.logout.ObservableUserLogoutEvent;
42 import org.mxchange.jusercore.events.logout.UserLogoutEvent;
43 import org.mxchange.jusercore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
44 import org.mxchange.jusercore.exceptions.UserNotFoundException;
45 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
46 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
47 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
48 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
49 import org.mxchange.jusercore.model.user.User;
50 import org.mxchange.jusercore.model.user.UserUtils;
51 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
52 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote;
53 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
54 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
57 * A web bean for user registration
59 * @author Roland Häder<roland@mxchange.org>
61 @Named ("userLoginController")
63 public class AddressbookUserLoginWebSessionBean extends BaseAddressbookController implements AddressbookUserLoginWebSessionController {
66 * Path name for guest base template
68 private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest";
71 * Path name for logged-in user base template
73 private static final String USER_BASE_TEMPLATE_NAME = "login/user/user";
78 private static final long serialVersionUID = 47_828_986_719_691_592L;
81 * Template type for pages that might be displayed in guest area and login
84 private String baseTemplatePathName;
87 * Logged-in user instance
89 private User loggedInUser;
92 * Event fired when user has logged in
96 private Event<ObservableUserLoggedInEvent> loginEvent;
102 private AddressbookUserWebSessionController userController;
107 private String userCurrentPassword;
110 * Flag whether the user has logged-in, set only from inside
112 private boolean userLoggedIn;
115 * Remote register session bean
117 private UserLoginSessionBeanRemote userLoginBean;
120 * Event fired when user has logged in
124 private Event<ObservableUserLoggedInEvent> userLoginEvent;
127 * Event fired when user has logged out
131 private Event<ObservableUserLogoutEvent> userLogoutEvent;
134 * User's password history
136 private List<PasswordHistory> userPasswordHistory;
139 * EJB for user's password history
141 private UserPasswordHistorySessionBeanRemote userPasswordHistoryBean;
144 * Default constructor
146 public AddressbookUserLoginWebSessionBean () {
147 // Defaul template is guest
148 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
152 * Method being call after user's password has been updated (and history
153 * entry has been created).
155 * @param event Event being observed
157 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
161 throw new NullPointerException("event is null"); //NOI18N
162 } else if (event.getPasswordHistory() == null) {
164 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
165 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
167 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
168 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
170 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
173 // All fine, so update list
174 this.updatePasswordHistory(event.getPasswordHistory());
178 public String doAdminLogout () {
179 // Is a user logged-in?
180 if (this.isUserLoggedIn()) {
182 return this.doUserLogout();
185 // Invalidate session
186 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
188 // Set template type to guest
189 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
192 return "index?faces-redirect=true"; //NOI18N
196 public String doUserLogin () {
198 User user = this.userController.createUserLogin();
200 // Create login container
201 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
205 User confirmedUser = this.userLoginBean.validateUserAccountStatus(container);
207 // All fine here so set it here
208 this.setLoggedInUser(confirmedUser);
210 // Retrieve user's password list
211 this.userPasswordHistory = this.userPasswordHistoryBean.getUserPasswordHistory(confirmedUser);
213 // Set template to "login"
214 this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N
216 // Fire event away. Keep this last before return statement.
217 this.userLoginEvent.fire(new UserLoggedInEvent(confirmedUser));
223 return "login_user"; //NOI18N
224 } catch (final UserNotFoundException ex) {
226 this.showFacesMessage("form_user_login:userName", "ERROR_USER_NOT_FOUND"); //NOI18N
228 } catch (final UserStatusLockedException ex) {
229 this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_LOCKED"); //NOI18N
231 } catch (final UserStatusUnconfirmedException ex) {
232 this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_UNCONFIRMED"); //NOI18N
234 } catch (final UserPasswordMismatchException ex) {
236 this.showFacesMessage("form_user_login:userPassword", "ERROR_USER_PASSWORD_MISMATCH"); //NOI18N
242 public String doUserLogout () {
243 // Is loggedInUser set?
244 if (this.getLoggedInUser() == null) {
246 throw new NullPointerException("this.loggedInUser is null"); //NOI18N
247 } else if (this.getLoggedInUser().getUserId() == null) {
249 throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
250 } else if (this.getLoggedInUser().getUserId() < 1) {
252 throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
256 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
258 // Invalidate session
259 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
261 // Unset any user instances
262 this.setLoggedInUser(null);
263 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
266 return "index"; //NOI18N
270 public String getBaseTemplatePathName () {
271 return this.baseTemplatePathName;
275 public void setBaseTemplatePathName (final String baseTemplatePathName) {
276 this.baseTemplatePathName = baseTemplatePathName;
280 public User getLoggedInUser () {
281 return this.loggedInUser;
285 public void setLoggedInUser (final User loggedInUser) {
286 this.loggedInUser = loggedInUser;
290 public String getUserCurrentPassword () {
291 return this.userCurrentPassword;
295 public void setUserCurrentPassword (final String userCurrentPassword) {
296 this.userCurrentPassword = userCurrentPassword;
300 public List<PasswordHistory> getUserPasswordHistory () {
301 return Collections.unmodifiableList(this.userPasswordHistory);
305 public boolean ifCurrentPasswordMatches () {
306 // The current password must be set and not empty
307 if (this.getUserCurrentPassword() == null) {
309 throw new NullPointerException("this.userCurrentPassword is null"); //NOI18N
310 } else if (this.getUserCurrentPassword().isEmpty()) {
312 throw new IllegalStateException("this.userCurrentPassword is empty."); //NOI18N
315 // Create "container"
316 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getUserCurrentPassword());
318 // Now check if it matches
319 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
323 public boolean ifUserMustChangePassword () {
324 return ((this.isUserLoggedIn()) && (Objects.equals(this.getLoggedInUser().getUserMustChangePassword(), Boolean.TRUE)));
328 * Post-construction method
331 public void init () {
333 // Get initial context
334 Context context = new InitialContext();
337 this.userLoginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/addressbook-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
340 this.userPasswordHistoryBean = (UserPasswordHistorySessionBeanRemote) context.lookup("java:global/addressbook-ejb/userPasswordHistory!org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote"); //NOI18N
342 // Defaul template is guest
343 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
344 } catch (final NamingException ex) {
346 throw new FaceletException(ex);
351 public boolean isInvisible () {
353 if (!this.isUserLoggedIn()) {
355 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
358 // Check logged-in first, then invisibility
359 return Objects.equals(this.getLoggedInUser().getUserProfileMode(), ProfileMode.INVISIBLE);
363 public boolean isPasswordInHistory (final String userPassword) {
364 // Default is not found
365 boolean isPasswordInHistory = false;
369 int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N
371 // Check all passwords
372 for (final PasswordHistory entry : this.getUserPasswordHistory()) {
373 // Is password the same?
374 if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) {
376 isPasswordInHistory = true;
378 } else if (count == maxEntries) {
388 return isPasswordInHistory;
392 public boolean isUserLoggedIn () {
394 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
397 return this.userLoggedIn;
403 private void clear () {
405 this.setUserCurrentPassword(null);
409 * Updates password history by adding given entry to it as long as it is not
412 * @param passwordHistory Password history entry
414 private void updatePasswordHistory (final PasswordHistory passwordHistory) {
415 if (null == passwordHistory) {
417 throw new NullPointerException("passwordHistory is null"); //NOI18N
418 } else if (passwordHistory.getUserPasswordHistoryId() == null) {
420 throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N
421 } else if (passwordHistory.getUserPasswordHistoryId() < 1) {
423 throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N
427 if (this.userPasswordHistory.contains(passwordHistory)) {
433 for (final PasswordHistory entry : this.userPasswordHistory) {
434 // Is same id number?
435 if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) {
441 // Not found, so add it
442 this.userPasswordHistory.add(passwordHistory);