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.pizzaapplication.beans.user.password;
19 import java.util.Objects;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jcoreee.utils.FacesUtils;
28 import org.mxchange.jusercore.exceptions.UserNotFoundException;
29 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
30 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
31 import org.mxchange.jusercore.model.user.User;
32 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
33 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
34 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
35 import org.mxchange.juserlogincore.events.user.password_change.UpdatedUserPasswordEvent;
36 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
37 import org.mxchange.juserlogincore.login.UserLoginUtils;
38 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
39 import org.mxchange.pizzaapplication.beans.features.PizzaFeaturesWebApplicationController;
40 import org.mxchange.pizzaapplication.beans.user.login.PizzaUserLoginWebSessionController;
43 * A user password (change) bean (controller)
45 * @author Roland Häder<roland@mxchange.org>
47 @Named ("userPasswordController")
49 public class PizzaUserPasswordWebRequestBean extends BasePizzaBean implements PizzaUserPasswordWebRequestController {
54 private static final long serialVersionUID = 15_267_867_367_501L;
60 private PizzaFeaturesWebApplicationController featureController;
65 @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
66 private UserSessionBeanRemote userBean;
69 * Current password (for confirmation of password change)
71 private String userCurrentPassword;
74 * Login bean (controller)
77 private PizzaUserLoginWebSessionController userLoginController;
80 * User password (clear-text from web form)
82 private String userPassword;
85 * User password repeated (clear-text from web form)
87 private String userPasswordRepeat;
90 * Event being fired when user's password has been updated
94 private Event<ObservableUpdatedUserPasswordEvent> userUpdatedPasswordEvent;
99 public PizzaUserPasswordWebRequestBean () {
100 // Call super constructor
105 * Changes logged-in user's password. It must not match with current
106 * password and should not appear in password history list for X
107 * (configurable) entries.
109 * @return Redirect outcome
111 public String doChangePassword () {
112 // This method shall only be called if the user is logged-in
113 if (!this.userLoginController.isUserLoggedIn()) {
115 throw new IllegalStateException("User is not logged-in"); //NOI18N
116 } else if (!this.isRequiredChangePasswordSet()) {
117 // Not all required fields are set
118 throw new FaceletException("Not all required fields are set."); //NOI18N
119 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
120 // Password not matching
121 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
122 } else if (!this.featureController.isFeatureEnabled("change_user_password")) { //NOI18N
123 // Editing is not allowed
124 throw new IllegalStateException("User tried to change password."); //NOI18N
125 } else if (!UserLoginUtils.ifPasswordMatches(this.getUserCurrentPassword(), this.userLoginController.getLoggedInUser())) {
126 // Password mismatches
127 this.showFacesMessage("form_user_change_password:userCurrentPassword", "Entered current password does not matched stored password."); //NOI18N
134 } else if (!Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())) {
135 // Both entered passwords don't match
136 this.showFacesMessage("form_user_change_password:userPasswordRepeat", "Entered new passwords mismatch."); //NOI18N
143 } else if (Objects.equals(this.getUserCurrentPassword(), this.getUserPassword())) {
144 // New password matches current
145 this.showFacesMessage("form_user_change_password:userPassword", "Entered new password is same as current password."); //NOI18N
152 } else if (this.userLoginController.isPasswordInHistory(this.getUserPassword())) {
153 // Is already in list (to old passwords are ignored)
154 this.showFacesMessage("form_user_change_password:userPassword", "Entered new password is has already been used some time ago."); //NOI18N
164 final User user = this.userLoginController.getLoggedInUser();
167 final String encryptedPassword = UserLoginUtils.encryptPassword(this.getUserPassword());
170 user.setUserEncryptedPassword(encryptedPassword);
174 final String baseUrl = FacesUtils.generateBaseUrl();
176 // All is set, then update password
177 PasswordHistory passwordHistory = this.userBean.updateUserPassword(user, baseUrl);
180 this.userUpdatedPasswordEvent.fire(new UpdatedUserPasswordEvent(passwordHistory, this.getUserPassword()));
181 } catch (final UserNotFoundException | UserStatusUnconfirmedException | UserStatusLockedException ex) {
186 throw new FaceletException(ex);
193 return "login_data_saved"; //NOI18N
197 * Getter for current clear-text user password
199 * @return Current clear-text user password
201 public String getUserCurrentPassword () {
202 return this.userCurrentPassword;
206 * Setter for current clear-text user password
208 * @param userCurrentPassword Current clear-text user password
210 public void setUserCurrentPassword (final String userCurrentPassword) {
211 this.userCurrentPassword = userCurrentPassword;
215 * Getter for clear-text user password
217 * @return Clear-text user password
219 public String getUserPassword () {
220 return this.userPassword;
224 * Setter for clear-text user password
226 * @param userPassword Clear-text user password
228 public void setUserPassword (final String userPassword) {
229 this.userPassword = userPassword;
233 * Getter for clear-text user password repeated
235 * @return Clear-text user password repeated
237 public String getUserPasswordRepeat () {
238 return this.userPasswordRepeat;
242 * Setter for clear-text user password repeated
244 * @param userPasswordRepeat Clear-text user password repeated
246 public void setUserPasswordRepeat (final String userPasswordRepeat) {
247 this.userPasswordRepeat = userPasswordRepeat;
251 public boolean isRequiredChangePasswordSet () {
253 return ((this.getUserCurrentPassword() != null) &&
254 (!this.getUserCurrentPassword().isEmpty()) &&
255 (this.getUserPassword() != null) &&
256 (!this.getUserPassword().isEmpty()) &&
257 (this.getUserPasswordRepeat() != null) &&
258 (!this.getUserPasswordRepeat().isEmpty()));
264 private void clear () {
266 this.setUserPassword(null);
267 this.setUserPasswordRepeat(null);