2 * Copyright (C) 2016, 2020 Free Software Foundation
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.user.login;
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Objects;
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.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.addressbook.beans.BaseAddressbookController;
32 import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController;
33 import org.mxchange.jusercore.exceptions.UserNotFoundException;
34 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
35 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
38 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote;
39 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
40 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
41 import org.mxchange.juserlogincore.container.login.LoginContainer;
42 import org.mxchange.juserlogincore.container.login.UserLoginContainer;
43 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
44 import org.mxchange.juserlogincore.events.login.UserLoggedInEvent;
45 import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent;
46 import org.mxchange.juserlogincore.events.logout.UserLogoutEvent;
47 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
48 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
49 import org.mxchange.juserlogincore.login.UserLoginUtils;
50 import org.mxchange.juserlogincore.model.user.login.UserLoginSessionBeanRemote;
53 * A web bean for user registration
55 * @author Roland Häder<roland@mxchange.org>
57 @Named ("userLoginController")
59 public class AddressbookUserLoginWebSessionBean extends BaseAddressbookController implements AddressbookUserLoginWebSessionController {
62 * Path name for guest base template
64 private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest"; //NOI18N
67 * Path name for logged-in user base template
69 private static final String USER_BASE_TEMPLATE_NAME = "login/user/user"; //NOI18N
74 private static final long serialVersionUID = 47_828_986_719_691_592L;
77 * Template type for pages that might be displayed in guest area and login
80 private String baseTemplatePathName;
83 * Logged-in user instance
85 private User loggedInUser;
91 private AddressbookUserWebRequestController userController;
96 private String userCurrentPassword;
99 * Flag whether the user has logged-in, set only from inside
101 private boolean userLoggedIn;
104 * Remote register session-scoped bean
106 @EJB (lookup = "java:global/addressbook-ejb/userLogin!org.mxchange.juserlogincore.model.user.login.UserLoginSessionBeanRemote")
107 private UserLoginSessionBeanRemote userLoginBean;
110 * Event fired when user has logged in
114 private Event<ObservableUserLoggedInEvent> userLoginEvent;
117 * Event fired when user has logged out
121 private Event<ObservableUserLogoutEvent> userLogoutEvent;
124 * User's password history
126 private List<PasswordHistory> userPasswordHistory;
129 * EJB for user's password history
131 @EJB (lookup = "java:global/addressbook-ejb/userPasswordHistory!org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote")
132 private UserPasswordHistorySessionBeanRemote userPasswordHistoryBean;
135 * Default constructor
137 public AddressbookUserLoginWebSessionBean () {
138 // Call super constructor
141 // Defaul template is guest
142 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
146 * Method being call after user's password has been updated (and history
147 * entry has been created).
149 * @param event Event being observed
151 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
155 throw new NullPointerException("event is null"); //NOI18N
156 } else if (event.getPasswordHistory() == null) {
158 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
159 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
161 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
162 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
164 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
167 // All fine, so update list
168 this.updatePasswordHistory(event.getPasswordHistory());
172 * Logout for administrator area. If a logged-in user instance exists, it is
173 * being logged-out, too.
175 * @return Outcome (should be redirected)
177 public String doAdminLogout () {
178 // Is a user logged-in?
179 if (this.isUserLoggedIn()) {
181 return this.doUserLogout();
184 // Invalidate session
185 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
187 // Set template type to guest
188 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
191 return "index?faces-redirect=true"; //NOI18N
195 * Logins the user, if the account is found, confirmed and unlocked.
197 * @return Redirect target
199 public String doUserLogin () {
201 final User user = this.userController.createUserLogin();
203 // Create login container
204 final LoginContainer loginContainer = new UserLoginContainer(user, this.userController.getUserPassword());
208 final User confirmedUser = this.userLoginBean.validateUserAccountStatus(loginContainer);
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 * Logout for current user by invalidating the current session.
247 * @return Outcome (should be redirected)
249 public String doUserLogout () {
250 // Is loggedInUser set?
251 if (this.getLoggedInUser() == null) {
253 throw new NullPointerException("this.loggedInUser is null"); //NOI18N
254 } else if (this.getLoggedInUser().getUserId() == null) {
256 throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
257 } else if (this.getLoggedInUser().getUserId() < 1) {
259 throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
263 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
265 // Invalidate session
266 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
268 // Unset any user instances
269 this.setLoggedInUser(null);
270 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
273 return "index"; //NOI18N
277 public String getBaseTemplatePathName () {
278 return this.baseTemplatePathName;
282 public void setBaseTemplatePathName (final String baseTemplatePathName) {
283 this.baseTemplatePathName = baseTemplatePathName;
287 public User getLoggedInUser () {
288 return this.loggedInUser;
292 public void setLoggedInUser (final User loggedInUser) {
293 this.loggedInUser = loggedInUser;
297 * Getter for current password (clear text)
299 * @return Current password
301 public String getUserCurrentPassword () {
302 return this.userCurrentPassword;
306 * Setter for current password (clear text)
308 * @param userCurrentPassword Current password
310 public void setUserCurrentPassword (final String userCurrentPassword) {
311 this.userCurrentPassword = userCurrentPassword;
315 public List<PasswordHistory> getUserPasswordHistory () {
316 return Collections.unmodifiableList(this.userPasswordHistory);
320 public boolean ifCurrentPasswordMatches () {
321 // The current password must be set and not empty
322 if (this.getUserCurrentPassword() == null) {
324 throw new NullPointerException("this.userCurrentPassword is null"); //NOI18N
325 } else if (this.getUserCurrentPassword().isEmpty()) {
327 throw new IllegalStateException("this.userCurrentPassword is empty."); //NOI18N
330 // Create "container"
331 final LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getUserCurrentPassword());
333 // Now check if it matches
334 return UserLoginUtils.ifPasswordMatches(container, this.getLoggedInUser());
338 public boolean ifUserMustChangePassword () {
339 return ((this.isUserLoggedIn()) && (Objects.equals(this.getLoggedInUser().getUserMustChangePassword(), Boolean.TRUE)));
343 public boolean isInvisible () {
345 if (!this.isUserLoggedIn()) {
347 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
350 // Check logged-in first, then invisibility
351 return Objects.equals(this.getLoggedInUser().getUserProfileMode(), ProfileMode.INVISIBLE);
355 public boolean isPasswordInHistory (final String userPassword) {
356 // Default is not found
357 boolean isPasswordInHistory = false;
361 final int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N
363 // Check all passwords
364 for (final PasswordHistory entry : this.getUserPasswordHistory()) {
365 // Is password the same?
366 if (UserLoginUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) {
368 isPasswordInHistory = true;
370 } else if (count == maxEntries) {
380 return isPasswordInHistory;
384 public boolean isUserLoggedIn () {
386 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
389 return this.userLoggedIn;
395 private void clear () {
397 this.setUserCurrentPassword(null);
401 * Updates password history by adding given entry to it as long as it is not
404 * @param passwordHistory Password history entry
406 private void updatePasswordHistory (final PasswordHistory passwordHistory) {
407 if (null == passwordHistory) {
409 throw new NullPointerException("passwordHistory is null"); //NOI18N
410 } else if (passwordHistory.getUserPasswordHistoryId() == null) {
412 throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N
413 } else if (passwordHistory.getUserPasswordHistoryId() < 1) {
415 throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N
419 if (this.userPasswordHistory.contains(passwordHistory)) {
425 for (final PasswordHistory entry : this.userPasswordHistory) {
426 // Is same id number?
427 if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) {
433 // Not found, so add it
434 this.userPasswordHistory.add(passwordHistory);