]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/password/JobsUserPasswordWebRequestBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / password / JobsUserPasswordWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.beans.user.password;
18
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;
42
43 /**
44  * A user password (change) controller (bean)
45  * <p>
46  * @author Roland Haeder<roland@mxchange.org>
47  */
48 @Named ("userPasswordController")
49 @RequestScoped
50 public class JobsUserPasswordWebRequestBean extends BaseJobsController implements JobsUserPasswordWebRequestController {
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 15_267_867_367_501L;
56
57         /**
58          * Features controller
59          */
60         @Inject
61         private JobsFeaturesWebApplicationController featureController;
62
63         /**
64          * Remote user bean
65          */
66         private final UserSessionBeanRemote userBean;
67
68         /**
69          * Login controller (bean)
70          */
71         @Inject
72         private JobsUserLoginWebSessionController userLoginController;
73
74         /**
75          * User password (unencrypted from web form)
76          */
77         private String userPassword;
78
79         /**
80          * User password repeated (unencrypted from web form)
81          */
82         private String userPasswordRepeat;
83
84         /**
85          * Event being fired when user's password has been updated
86          */
87         @Any
88         @Inject
89         private Event<UpdatedUserPasswordEvent> userUpdatedPasswordEvent;
90
91         /**
92          * Default constructor
93          */
94         public JobsUserPasswordWebRequestBean () {
95                 // Try it
96                 try {
97                         // Get initial context
98                         Context context = new InitialContext();
99
100                         // Try to lookup
101                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
102                 } catch (final NamingException e) {
103                         // Throw again
104                         throw new FaceletException(e);
105                 }
106         }
107
108         @Override
109         public String doChangePassword () {
110                 // This method shall only be called if the user is logged-in
111                 if (!this.userLoginController.isUserLoggedIn()) {
112                         // Not logged-in
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
126
127                         // Clear bean
128                         this.clear();
129
130                         // No redirect
131                         return ""; //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
135
136                         // Clear bean
137                         this.clear();
138
139                         // No redirect
140                         return ""; //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
144
145                         // Clear bean
146                         this.clear();
147
148                         // No redirect
149                         return ""; //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
153
154                         // Clear bean
155                         this.clear();
156
157                         // No redirect
158                         return ""; //NOI18N
159                 }
160
161                 // Get user instance
162                 User user = this.userLoginController.getLoggedInUser();
163
164                 // Encrypt password
165                 String encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
166
167                 // Set it in user
168                 user.setUserMustChangePassword(Boolean.FALSE);
169                 user.setUserEncryptedPassword(encryptedPassword);
170
171                 try {
172                         // All is set, then update password
173                         PasswordHistory passwordHistory = this.userBean.updateUserPassword(user);
174
175                         // Fire event
176                         this.userUpdatedPasswordEvent.fire(new UserUpdatedPasswordEvent(passwordHistory));
177                 } catch (final UserNotFoundException | UserStatusUnconfirmedException | UserStatusLockedException ex) {
178                         // Clear bean
179                         this.clear();
180
181                         // Throw again
182                         throw new FaceletException(ex);
183                 }
184
185                 // Clear bean
186                 this.clear();
187
188                 // Return outcome
189                 return "login_data_saved"; //NOI18N
190         }
191
192         @Override
193         public String getUserPassword () {
194                 return this.userPassword;
195         }
196
197         @Override
198         public void setUserPassword (final String userPassword) {
199                 this.userPassword = userPassword;
200         }
201
202         @Override
203         public String getUserPasswordRepeat () {
204                 return this.userPasswordRepeat;
205         }
206
207         @Override
208         public void setUserPasswordRepeat (final String userPasswordRepeat) {
209                 this.userPasswordRepeat = userPasswordRepeat;
210         }
211
212         @Override
213         public boolean isRequiredChangePasswordSet () {
214                 // Is all data set?
215                 return ((this.userLoginController.getUserCurrentPassword() != null) &&
216                                 (!this.userLoginController.getUserCurrentPassword().isEmpty()) &&
217                                 (this.getUserPassword() != null) &&
218                                 (!this.getUserPassword().isEmpty()) &&
219                                 (this.getUserPasswordRepeat() != null) &&
220                                 (!this.getUserPasswordRepeat().isEmpty()));
221         }
222
223         /**
224          * Clears this bean
225          */
226         private void clear () {
227                 // Clear all data
228                 this.setUserPassword(null);
229                 this.setUserPasswordRepeat(null);
230         }
231
232 }