2 * Copyright (C) 2016 Roland Haeder
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.jjobs.beans.user.password;
19 import java.util.Objects;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jjobs.beans.BaseJobsController;
30 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
31 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
32 import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent;
33 import org.mxchange.jusercore.events.user.password_change.UserUpdatedPasswordEvent;
34 import org.mxchange.jusercore.exceptions.UserNotFoundException;
35 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
36 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
37 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
38 import org.mxchange.jusercore.model.user.User;
39 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
40 import org.mxchange.jusercore.model.user.UserUtils;
41 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
44 * A user password (change) controller (bean)
46 * @author Roland Haeder<roland@mxchange.org>
48 @Named ("userPasswordController")
50 public class JobsUserPasswordWebRequestBean extends BaseJobsController implements JobsUserPasswordWebRequestController {
55 private static final long serialVersionUID = 15_267_867_367_501L;
61 private JobsFeaturesWebApplicationController featureController;
66 private final UserSessionBeanRemote userBean;
69 * Login controller (bean)
72 private JobsUserLoginWebSessionController userLoginController;
75 * User password (unencrypted from web form)
77 private String userPassword;
80 * User password repeated (unencrypted from web form)
82 private String userPasswordRepeat;
85 * Event being fired when user's password has been updated
89 private Event<UpdatedUserPasswordEvent> userUpdatedPasswordEvent;
94 public JobsUserPasswordWebRequestBean () {
97 // Get initial context
98 Context context = new InitialContext();
101 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
102 } catch (final NamingException e) {
104 throw new FaceletException(e);
109 public String doChangePassword () {
110 // This method shall only be called if the user is logged-in
111 if (!this.userLoginController.isUserLoggedIn()) {
113 throw new IllegalStateException("User is not logged-in"); //NOI18N
114 } else if (!this.isRequiredChangePasswordSet()) {
115 // Not all required fields are set
116 throw new FaceletException("Not all required fields are set."); //NOI18N
117 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
118 // Password not matching
119 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
120 } else if (!this.featureController.isFeatureEnabled("change_user_password")) { //NOI18N
121 // Editing is not allowed
122 throw new IllegalStateException("User tried to change password."); //NOI18N
123 } else if (!UserUtils.ifPasswordMatches(this.userLoginController.getUserCurrentPassword(), this.userLoginController.getLoggedInUser())) {
124 // Password mismatches
125 this.showFacesMessage("form_user_change_password:userCurrentPassword", "ERROR_USER_CURRENT_PASSWORD_MISMATCHING"); //NOI18N
132 } else if (!Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())) {
133 // Both entered passwords don't match
134 this.showFacesMessage("form_user_change_password:userPasswordRepeat", "ERROR_USER_NEW_PASSWORDS_MISMATCH"); //NOI18N
141 } else if (Objects.equals(this.userLoginController.getUserCurrentPassword(), this.getUserPassword())) {
142 // New password matches current
143 this.showFacesMessage("form_user_change_password:userPassword", "ERROR_USER_NEW_PASSWORD_SAME_AS_CURRENT"); //NOI18N
150 } else if (this.userLoginController.isPasswordInHistory(this.getUserPassword())) {
151 // Is already in list (to old passwords are ignored)
152 this.showFacesMessage("form_user_change_password:userPassword", "ERROR_USER_NEW_PASSWORD_ALREADY_ENTERED"); //NOI18N
162 User user = this.userLoginController.getLoggedInUser();
165 String encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
168 user.setUserEncryptedPassword(encryptedPassword);
171 // All is set, then update password
172 PasswordHistory passwordHistory = this.userBean.updateUserPassword(user);
175 this.userUpdatedPasswordEvent.fire(new UserUpdatedPasswordEvent(passwordHistory));
176 } catch (final UserNotFoundException | UserStatusUnconfirmedException | UserStatusLockedException ex) {
181 throw new FaceletException(ex);
188 return "login_data_saved"; //NOI18N
192 public String getUserPassword () {
193 return this.userPassword;
197 public void setUserPassword (final String userPassword) {
198 this.userPassword = userPassword;
202 public String getUserPasswordRepeat () {
203 return this.userPasswordRepeat;
207 public void setUserPasswordRepeat (final String userPasswordRepeat) {
208 this.userPasswordRepeat = userPasswordRepeat;
212 public boolean isRequiredChangePasswordSet () {
214 return ((this.userLoginController.getUserCurrentPassword() != null) &&
215 (!this.userLoginController.getUserCurrentPassword().isEmpty()) &&
216 (this.getUserPassword() != null) &&
217 (!this.getUserPassword().isEmpty()) &&
218 (this.getUserPasswordRepeat() != null) &&
219 (!this.getUserPasswordRepeat().isEmpty()));
225 private void clear () {
227 this.setUserPassword(null);
228 this.setUserPasswordRepeat(null);