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.jcoreee.utils.FacesUtils;
30 import org.mxchange.jjobs.beans.BaseJobsController;
31 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
32 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
33 import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent;
34 import org.mxchange.jusercore.events.user.password_change.UserUpdatedPasswordEvent;
35 import org.mxchange.jusercore.exceptions.UserNotFoundException;
36 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
37 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
38 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
39 import org.mxchange.jusercore.model.user.User;
40 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
41 import org.mxchange.jusercore.model.user.UserUtils;
42 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
45 * A user password (change) controller (bean)
47 * @author Roland Haeder<roland@mxchange.org>
49 @Named ("userPasswordController")
51 public class JobsUserPasswordWebRequestBean extends BaseJobsController implements JobsUserPasswordWebRequestController {
56 private static final long serialVersionUID = 15_267_867_367_501L;
62 private JobsFeaturesWebApplicationController featureController;
67 private final UserSessionBeanRemote userBean;
70 * Login controller (bean)
73 private JobsUserLoginWebSessionController userLoginController;
76 * User password (unencrypted from web form)
78 private String userPassword;
81 * User password repeated (unencrypted from web form)
83 private String userPasswordRepeat;
86 * Event being fired when user's password has been updated
90 private Event<UpdatedUserPasswordEvent> userUpdatedPasswordEvent;
95 public JobsUserPasswordWebRequestBean () {
98 // Get initial context
99 Context context = new InitialContext();
102 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
103 } catch (final NamingException e) {
105 throw new FaceletException(e);
110 public String doChangePassword () {
111 // This method shall only be called if the user is logged-in
112 if (!this.userLoginController.isUserLoggedIn()) {
114 throw new IllegalStateException("User is not logged-in"); //NOI18N
115 } else if (!this.isRequiredChangePasswordSet()) {
116 // Not all required fields are set
117 throw new FaceletException("Not all required fields are set."); //NOI18N
118 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
119 // Password not matching
120 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
121 } else if (!this.featureController.isFeatureEnabled("change_user_password")) { //NOI18N
122 // Editing is not allowed
123 throw new IllegalStateException("User tried to change password."); //NOI18N
124 } else if (!UserUtils.ifPasswordMatches(this.userLoginController.getUserCurrentPassword(), this.userLoginController.getLoggedInUser())) {
125 // Password mismatches
126 this.showFacesMessage("form_user_change_password:userCurrentPassword", "ERROR_USER_CURRENT_PASSWORD_MISMATCHING"); //NOI18N
129 this.userLoginController.setUserCurrentPassword(null);
134 } else if (!Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())) {
135 // Both entered passwords don't match
136 this.showFacesMessage("form_user_change_password:userPasswordRepeat", "ERROR_USER_NEW_PASSWORDS_MISMATCH"); //NOI18N
139 this.userLoginController.setUserCurrentPassword(null);
144 } else if (Objects.equals(this.userLoginController.getUserCurrentPassword(), this.getUserPassword())) {
145 // New password matches current
146 this.showFacesMessage("form_user_change_password:userPassword", "ERROR_USER_NEW_PASSWORD_SAME_AS_CURRENT"); //NOI18N
149 this.userLoginController.setUserCurrentPassword(null);
154 } else if (this.userLoginController.isPasswordInHistory(this.getUserPassword())) {
155 // Is already in list (to old passwords are ignored)
156 this.showFacesMessage("form_user_change_password:userPassword", "ERROR_USER_NEW_PASSWORD_ALREADY_ENTERED"); //NOI18N
159 this.userLoginController.setUserCurrentPassword(null);
167 User user = this.userLoginController.getLoggedInUser();
170 String encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
173 user.setUserMustChangePassword(Boolean.FALSE);
174 user.setUserEncryptedPassword(encryptedPassword);
178 String baseUrl = FacesUtils.generateBaseUrl();
180 // All is set, then update password
181 PasswordHistory passwordHistory = this.userBean.updateUserPassword(user, baseUrl);
184 this.userUpdatedPasswordEvent.fire(new UserUpdatedPasswordEvent(passwordHistory));
185 } catch (final UserNotFoundException | UserStatusUnconfirmedException | UserStatusLockedException ex) {
187 this.userLoginController.setUserCurrentPassword(null);
191 throw new FaceletException(ex);
198 return "login_data_saved"; //NOI18N
202 public String getUserPassword () {
203 return this.userPassword;
207 public void setUserPassword (final String userPassword) {
208 this.userPassword = userPassword;
212 public String getUserPasswordRepeat () {
213 return this.userPasswordRepeat;
217 public void setUserPasswordRepeat (final String userPasswordRepeat) {
218 this.userPasswordRepeat = userPasswordRepeat;
222 public boolean isRequiredChangePasswordSet () {
224 return ((this.userLoginController.getUserCurrentPassword() != null) &&
225 (!this.userLoginController.getUserCurrentPassword().isEmpty()) &&
226 (this.getUserPassword() != null) &&
227 (!this.getUserPassword().isEmpty()) &&
228 (this.getUserPasswordRepeat() != null) &&
229 (!this.getUserPasswordRepeat().isEmpty()));
235 private void clear () {
237 this.setUserPassword(null);
238 this.setUserPasswordRepeat(null);