]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/password/JobsUserPasswordWebRequestBean.java
d5cc6da01fdb8620d2776574428c0bdac40495b2
[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) bean (controller)
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          * Current password (for confirmation of password change)
70          */
71         private String userCurrentPassword;
72
73         /**
74          * Login bean (controller)
75          */
76         @Inject
77         private JobsUserLoginWebSessionController userLoginController;
78
79         /**
80          * User password (unencrypted from web form)
81          */
82         private String userPassword;
83
84         /**
85          * User password repeated (unencrypted from web form)
86          */
87         private String userPasswordRepeat;
88
89         /**
90          * Event being fired when user's password has been updated
91          */
92         @Any
93         @Inject
94         private Event<UpdatedUserPasswordEvent> userUpdatedPasswordEvent;
95
96         /**
97          * Default constructor
98          */
99         public JobsUserPasswordWebRequestBean () {
100                 // Try it
101                 try {
102                         // Get initial context
103                         Context context = new InitialContext();
104
105                         // Try to lookup
106                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
107                 } catch (final NamingException e) {
108                         // Throw again
109                         throw new FaceletException(e);
110                 }
111         }
112
113         @Override
114         public String doChangePassword () {
115                 // This method shall only be called if the user is logged-in
116                 if (!this.userLoginController.isUserLoggedIn()) {
117                         // Not logged-in
118                         throw new IllegalStateException("User is not logged-in"); //NOI18N
119                 } else if (!this.isRequiredChangePasswordSet()) {
120                         // Not all required fields are set
121                         throw new FaceletException("Not all required fields are set."); //NOI18N
122                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
123                         // Password not matching
124                         throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
125                 } else if (!this.featureController.isFeatureEnabled("change_user_password")) { //NOI18N
126                         // Editing is not allowed
127                         throw new IllegalStateException("User tried to change password."); //NOI18N
128                 } else if (!UserUtils.ifPasswordMatches(this.getUserCurrentPassword(), this.userLoginController.getLoggedInUser())) {
129                         // Password mismatches
130                         this.showFacesMessage("form_user_change_password:userCurrentPassword", "Entered current password does not matched stored password."); //NOI18N
131
132                         // Clear bean
133                         this.clear();
134
135                         // No redirect
136                         return ""; //NOI18N
137                 } else if (!Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())) {
138                         // Both entered passwords don't match
139                         this.showFacesMessage("form_user_change_password:userPasswordRepeat", "Entered new passwords mismatch."); //NOI18N
140
141                         // Clear bean
142                         this.clear();
143
144                         // No redirect
145                         return ""; //NOI18N
146                 } else if (Objects.equals(this.getUserCurrentPassword(), this.getUserPassword())) {
147                         // New password matches current
148                         this.showFacesMessage("form_user_change_password:userPassword", "Entered new password is same as current password."); //NOI18N
149
150                         // Clear bean
151                         this.clear();
152
153                         // No redirect
154                         return ""; //NOI18N
155                 } else if (this.userLoginController.isPasswordInHistory(this.getUserPassword())) {
156                         // Is already in list (to old passwords are ignored)
157                         this.showFacesMessage("form_user_change_password:userPassword", "Entered new password is has already been used some time ago."); //NOI18N
158
159                         // Clear bean
160                         this.clear();
161
162                         // No redirect
163                         return ""; //NOI18N
164                 }
165
166                 // Get user instance
167                 User user = this.userLoginController.getLoggedInUser();
168
169                 // Encrypt password
170                 String encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
171
172                 // Set it in user
173                 user.setUserEncryptedPassword(encryptedPassword);
174
175                 try {
176                         // All is set, then update password
177                         PasswordHistory passwordHistory = this.userBean.updateUserPassword(user);
178
179                         // Fire event
180                         this.userUpdatedPasswordEvent.fire(new UserUpdatedPasswordEvent(passwordHistory));
181                 } catch (final UserNotFoundException | UserStatusUnconfirmedException | UserStatusLockedException ex) {
182                         // Clear bean
183                         this.clear();
184
185                         // Throw again
186                         throw new FaceletException(ex);
187                 }
188
189                 // Clear bean
190                 this.clear();
191
192                 // Return outcome
193                 return "login_data_saved"; //NOI18N
194         }
195
196         @Override
197         public String getUserCurrentPassword () {
198                 return this.userCurrentPassword;
199         }
200
201         @Override
202         public void setUserCurrentPassword (final String userCurrentPassword) {
203                 this.userCurrentPassword = userCurrentPassword;
204         }
205
206         @Override
207         public String getUserPassword () {
208                 return this.userPassword;
209         }
210
211         @Override
212         public void setUserPassword (final String userPassword) {
213                 this.userPassword = userPassword;
214         }
215
216         @Override
217         public String getUserPasswordRepeat () {
218                 return this.userPasswordRepeat;
219         }
220
221         @Override
222         public void setUserPasswordRepeat (final String userPasswordRepeat) {
223                 this.userPasswordRepeat = userPasswordRepeat;
224         }
225
226         public boolean isRequiredChangePasswordSet () {
227                 // Is all data set?
228                 return ((this.getUserCurrentPassword() != null) &&
229                                 (!this.getUserCurrentPassword().isEmpty()) &&
230                                 (this.getUserPassword() != null) &&
231                                 (!this.getUserPassword().isEmpty()) &&
232                                 (this.getUserPasswordRepeat() != null) &&
233                                 (!this.getUserPasswordRepeat().isEmpty()));
234         }
235
236         /**
237          * Clears this bean
238          */
239         private void clear () {
240                 // Clear all data
241                 this.setUserPassword(null);
242                 this.setUserPasswordRepeat(null);
243         }
244
245 }