2 * Copyright (C) 2016, 2017 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.user;
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-scoped 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 // Call super constructor
150 // Defaul template is guest
151 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
155 * Method being call after user's password has been updated (and history
156 * entry has been created).
158 * @param event Event being observed
160 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
164 throw new NullPointerException("event is null"); //NOI18N
165 } else if (event.getPasswordHistory() == null) {
167 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
168 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
170 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
171 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
173 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
176 // All fine, so update list
177 this.updatePasswordHistory(event.getPasswordHistory());
181 public String doAdminLogout () {
182 // Is a user logged-in?
183 if (this.isUserLoggedIn()) {
185 return this.doUserLogout();
188 // Invalidate session
189 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
191 // Set template type to guest
192 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
195 return "index?faces-redirect=true"; //NOI18N
199 public String doUserLogin () {
201 User user = this.userController.createUserLogin();
203 // Create login container
204 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
208 User confirmedUser = this.userLoginBean.validateUserAccountStatus(container);
210 // All fine here so set it here
211 this.setLoggedInUser(confirmedUser);
213 // Retrieve user's password list
214 this.userPasswordHistory = this.userPasswordHistoryBean.getUserPasswordHistory(confirmedUser);
216 // Set template to "login"
217 this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N
219 // Fire event away. Keep this last before return statement.
220 this.userLoginEvent.fire(new UserLoggedInEvent(confirmedUser));
226 return "login_user"; //NOI18N
227 } catch (final UserNotFoundException ex) {
229 this.showFacesMessage("form_user_login:userName", "ERROR_USER_NOT_FOUND"); //NOI18N
231 } catch (final UserStatusLockedException ex) {
232 this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_LOCKED"); //NOI18N
234 } catch (final UserStatusUnconfirmedException ex) {
235 this.showFacesMessage("form_user_login:userName", "ERROR_USER_STATUS_UNCONFIRMED"); //NOI18N
237 } catch (final UserPasswordMismatchException ex) {
239 this.showFacesMessage("form_user_login:userPassword", "ERROR_USER_PASSWORD_MISMATCH"); //NOI18N
245 public String doUserLogout () {
246 // Is loggedInUser set?
247 if (this.getLoggedInUser() == null) {
249 throw new NullPointerException("this.loggedInUser is null"); //NOI18N
250 } else if (this.getLoggedInUser().getUserId() == null) {
252 throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
253 } else if (this.getLoggedInUser().getUserId() < 1) {
255 throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
259 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
261 // Invalidate session
262 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
264 // Unset any user instances
265 this.setLoggedInUser(null);
266 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
269 return "index"; //NOI18N
273 public String getBaseTemplatePathName () {
274 return this.baseTemplatePathName;
278 public void setBaseTemplatePathName (final String baseTemplatePathName) {
279 this.baseTemplatePathName = baseTemplatePathName;
283 public User getLoggedInUser () {
284 return this.loggedInUser;
288 public void setLoggedInUser (final User loggedInUser) {
289 this.loggedInUser = loggedInUser;
293 public String getUserCurrentPassword () {
294 return this.userCurrentPassword;
298 public void setUserCurrentPassword (final String userCurrentPassword) {
299 this.userCurrentPassword = userCurrentPassword;
303 public List<PasswordHistory> getUserPasswordHistory () {
304 return Collections.unmodifiableList(this.userPasswordHistory);
308 public boolean ifCurrentPasswordMatches () {
309 // The current password must be set and not empty
310 if (this.getUserCurrentPassword() == null) {
312 throw new NullPointerException("this.userCurrentPassword is null"); //NOI18N
313 } else if (this.getUserCurrentPassword().isEmpty()) {
315 throw new IllegalStateException("this.userCurrentPassword is empty."); //NOI18N
318 // Create "container"
319 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getUserCurrentPassword());
321 // Now check if it matches
322 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
326 public boolean ifUserMustChangePassword () {
327 return ((this.isUserLoggedIn()) && (Objects.equals(this.getLoggedInUser().getUserMustChangePassword(), Boolean.TRUE)));
331 * Post-construction method
334 public void init () {
336 // Get initial context
337 Context context = new InitialContext();
340 this.userLoginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/addressbook-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
343 this.userPasswordHistoryBean = (UserPasswordHistorySessionBeanRemote) context.lookup("java:global/addressbook-ejb/userPasswordHistory!org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote"); //NOI18N
345 // Defaul template is guest
346 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
347 } catch (final NamingException ex) {
349 throw new FaceletException(ex);
354 public boolean isInvisible () {
356 if (!this.isUserLoggedIn()) {
358 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
361 // Check logged-in first, then invisibility
362 return Objects.equals(this.getLoggedInUser().getUserProfileMode(), ProfileMode.INVISIBLE);
366 public boolean isPasswordInHistory (final String userPassword) {
367 // Default is not found
368 boolean isPasswordInHistory = false;
372 int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N
374 // Check all passwords
375 for (final PasswordHistory entry : this.getUserPasswordHistory()) {
376 // Is password the same?
377 if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) {
379 isPasswordInHistory = true;
381 } else if (count == maxEntries) {
391 return isPasswordInHistory;
395 public boolean isUserLoggedIn () {
397 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
400 return this.userLoggedIn;
406 private void clear () {
408 this.setUserCurrentPassword(null);
412 * Updates password history by adding given entry to it as long as it is not
415 * @param passwordHistory Password history entry
417 private void updatePasswordHistory (final PasswordHistory passwordHistory) {
418 if (null == passwordHistory) {
420 throw new NullPointerException("passwordHistory is null"); //NOI18N
421 } else if (passwordHistory.getUserPasswordHistoryId() == null) {
423 throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N
424 } else if (passwordHistory.getUserPasswordHistoryId() < 1) {
426 throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N
430 if (this.userPasswordHistory.contains(passwordHistory)) {
436 for (final PasswordHistory entry : this.userPasswordHistory) {
437 // Is same id number?
438 if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) {
444 // Not found, so add it
445 this.userPasswordHistory.add(passwordHistory);