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.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.addressbook.beans.BaseAddressbookController;
35 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
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 Häder<roland@mxchange.org>
60 @Named ("loginController")
62 public class AddressbookUserLoginWebSessionBean extends BaseAddressbookController implements AddressbookUserLoginWebSessionController {
65 * Path name for guest base template
67 private static final String GUEST_BASE_TEMPLATE_NAME = "guest/guest"; //NOI18N
70 * Path name for logged-in user base template
72 private static final String USER_BASE_TEMPLATE_NAME = "login/user/user"; //NOI18N
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;
99 private AddressbookUserWebSessionController userController;
102 * Flag whether the user has logged-in, set only from inside
104 private boolean userLoggedIn;
109 private UserLoginSessionBeanRemote userLoginBean;
112 * Event fired when user has logged in
116 private Event<UserLoggedInEvent> userLoginEvent;
119 * Event fired when user has logged out
123 private Event<ObserveableUserLogoutEvent> userLogoutEvent;
126 * User's password history
128 private List<PasswordHistory> userPasswordHistory;
131 * EJB for user's password history
133 private UserPasswordHistorySessionBeanRemote userPasswordHistoryBean;
136 * Default constructor
138 public AddressbookUserLoginWebSessionBean () {
140 // Get initial context
141 Context context = new InitialContext();
144 this.userLoginBean = (UserLoginSessionBeanRemote) context.lookup("java:global/addressbook-ejb/login!org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote"); //NOI18N
147 this.userPasswordHistoryBean = (UserPasswordHistorySessionBeanRemote) context.lookup("java:global/addressbook-ejb/userPasswordHistory!org.mxchange.jusercore.model.user.password_history.UserPasswordHistorySessionBeanRemote"); //NOI18N
149 // Defaul template is guest
150 this.baseTemplatePathName = GUEST_BASE_TEMPLATE_NAME;
151 } catch (final NamingException ex) {
153 throw new FaceletException(ex);
158 public void afterUserUpdatedPasswordEvent (@Observes final UpdatedUserPasswordEvent event) {
162 throw new NullPointerException("event is null"); //NOI18N
163 } else if (event.getPasswordHistory() == null) {
165 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
166 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
168 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
169 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
171 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
174 // All fine, so update list
175 this.updatePasswordHistory(event.getPasswordHistory());
179 public String doAdminLogout () {
180 // Is a user logged-in?
181 if (this.isUserLoggedIn()) {
183 return this.doUserLogout();
186 // Invalidate session
187 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
189 // Set template type to guest
190 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
193 return "index?faces-redirect=true"; //NOI18N
197 public String doUserLogin () {
199 User user = this.userController.createUserLogin();
201 // Create login container
202 LoginContainer container = new UserLoginContainer(user, this.userController.getUserPassword());
206 User confirmedUser = this.userLoginBean.validateUserAccountStatus(container);
208 // All fine here so set it here
209 this.setLoggedInUser(confirmedUser);
211 // Retrieve user's password list
212 this.userPasswordHistory = this.userPasswordHistoryBean.getUserPasswordHistory(confirmedUser);
214 // Set template to "login"
215 this.setBaseTemplatePathName(USER_BASE_TEMPLATE_NAME); //NOI18N
217 // Fire event away. Keep this last before return statement.
218 this.userLoginEvent.fire(new UserLoginEvent(confirmedUser));
224 return "login"; //NOI18N
225 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException ex) {
227 this.showFacesMessage("form_user_login:userName", ex); //NOI18N
229 } catch (final UserPasswordMismatchException ex) {
231 this.showFacesMessage("form_user_login:userPassword", ex); //NOI18N
237 public String doUserLogout () {
238 // Is loggedInUser set?
239 if (this.getLoggedInUser() == null) {
241 throw new NullPointerException("this.loggedInUser is null"); //NOI18N
242 } else if (this.getLoggedInUser().getUserId() == null) {
244 throw new NullPointerException("this.loggedInUser.userId is null"); //NOI18N
245 } else if (this.getLoggedInUser().getUserId() < 1) {
247 throw new IllegalStateException(MessageFormat.format("this.loggedInUser.userId={0} is not valid.", this.getLoggedInUser().getUserId())); //NOI18N
251 this.userLogoutEvent.fire(new UserLogoutEvent(this.getLoggedInUser()));
253 // Invalidate session
254 FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
256 // Unset any user instances
257 this.setLoggedInUser(null);
258 this.setBaseTemplatePathName(GUEST_BASE_TEMPLATE_NAME); //NOI18N
261 return "index"; //NOI18N
265 public String getBaseTemplatePathName () {
266 return this.baseTemplatePathName;
270 public void setBaseTemplatePathName (final String baseTemplatePathName) {
271 this.baseTemplatePathName = baseTemplatePathName;
275 public String getCurrentPassword () {
276 return this.currentPassword;
280 public void setCurrentPassword (final String currentPassword) {
281 this.currentPassword = currentPassword;
285 public User getLoggedInUser () {
286 return this.loggedInUser;
290 public void setLoggedInUser (final User loggedInUser) {
291 this.loggedInUser = loggedInUser;
295 public List<PasswordHistory> getUserPasswordHistory () {
296 return Collections.unmodifiableList(this.userPasswordHistory);
300 public boolean ifCurrentPasswordMatches () {
301 // The current password must be set and not empty
302 if (this.getCurrentPassword() == null) {
304 throw new NullPointerException("this.currentPassword is null"); //NOI18N
305 } else if (this.getCurrentPassword().isEmpty()) {
307 throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
310 // Create "container"
311 LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
313 // Now check if it matches
314 return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
318 public boolean ifUserMustChangePassword () {
319 return (this.isUserLoggedIn() && this.getLoggedInUser().getUserMustChangePassword());
323 public boolean isInvisible () {
325 if (!this.isUserLoggedIn()) {
327 throw new IllegalStateException("isInvisible() has been invoked for a guest."); //NOI18N
330 // Check logged-in first, then invisibility
331 return this.getLoggedInUser().getUserProfileMode().equals(ProfileMode.INVISIBLE);
335 public boolean isPasswordInHistory (final String userPassword) {
336 // Default is not found
337 boolean isPasswordInHistory = false;
341 int maxEntries = this.getIntegerContextParameter("max_user_password_history"); //NOI18N
343 // Check all passwords
344 for (final PasswordHistory entry : this.getUserPasswordHistory()) {
345 // Is password the same?
346 if (UserUtils.ifPasswordMatches(userPassword, entry.getUserPasswordHistoryUser())) {
348 isPasswordInHistory = true;
350 } else if (count == maxEntries) {
360 return isPasswordInHistory;
364 public boolean isUserLoggedIn () {
365 this.userLoggedIn = ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
368 return this.userLoggedIn;
374 private void clear () {
376 this.setCurrentPassword(null);
380 * Updates password history by adding given entry to it as long as it is not
383 * @param passwordHistory Password history entry
385 private void updatePasswordHistory (final PasswordHistory passwordHistory) {
386 if (null == passwordHistory) {
388 throw new NullPointerException("passwordHistory is null"); //NOI18N
389 } else if (passwordHistory.getUserPasswordHistoryId() == null) {
391 throw new NullPointerException("passwordHistory.userPasswordHistoryId is null"); //NOI18N
392 } else if (passwordHistory.getUserPasswordHistoryId() < 1) {
394 throw new IllegalArgumentException(MessageFormat.format("passwordHistory.userPasswordHistoryId={0} is not valid.", passwordHistory.getUserPasswordHistoryId())); //NOI18N
398 if (this.userPasswordHistory.contains(passwordHistory)) {
404 for (final PasswordHistory entry : this.userPasswordHistory) {
405 // Is same id number?
406 if (Objects.equals(entry.getUserPasswordHistoryId(), passwordHistory.getUserPasswordHistoryId())) {
412 // Not found, so add it
413 this.userPasswordHistory.add(passwordHistory);