]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/password/JobsUserPasswordWebRequestBean.java
7aeadf35efc2e6eb7d00980df76d6bc1c09b935d
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / password / JobsUserPasswordWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
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.annotation.PostConstruct;
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 javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcoreee.utils.FacesUtils;
31 import org.mxchange.jjobs.beans.BaseJobsController;
32 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
33 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
34 import org.mxchange.jusercore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
35 import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent;
36 import org.mxchange.jusercore.exceptions.UserNotFoundException;
37 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
38 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
39 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
40 import org.mxchange.jusercore.model.user.User;
41 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
42 import org.mxchange.jusercore.model.user.UserUtils;
43 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
44
45 /**
46  * A user password (change) controller (bean)
47  * <p>
48  * @author Roland Häder<roland@mxchange.org>
49  */
50 @Named ("userPasswordController")
51 @RequestScoped
52 public class JobsUserPasswordWebRequestBean extends BaseJobsController implements JobsUserPasswordWebRequestController {
53
54         /**
55          * Serial number
56          */
57         private static final long serialVersionUID = 15_267_867_367_501L;
58
59         /**
60          * Features controller
61          */
62         @Inject
63         private JobsFeaturesWebApplicationController featureController;
64
65         /**
66          * Remote user bean
67          */
68         private UserSessionBeanRemote userBean;
69
70         /**
71          * Current password (for confirmation of password change)
72          */
73         private String userCurrentPassword;
74
75         /**
76          * Login bean (controller)
77          */
78         @Inject
79         private JobsUserLoginWebSessionController userLoginController;
80
81         /**
82          * User password (clear-text from web form)
83          */
84         private String userPassword;
85
86         /**
87          * User password repeated (clear-text from web form)
88          */
89         private String userPasswordRepeat;
90
91         /**
92          * Event being fired when user's password has been updated
93          */
94         @Any
95         @Inject
96         private Event<ObservableUpdatedUserPasswordEvent> userUpdatedPasswordEvent;
97
98         /**
99          * Default constructor
100          */
101         public JobsUserPasswordWebRequestBean () {
102         }
103
104         @Override
105         public String doChangePassword () {
106                 // This method shall only be called if the user is logged-in
107                 if (!this.userLoginController.isUserLoggedIn()) {
108                         // Not logged-in
109                         throw new IllegalStateException("User is not logged-in"); //NOI18N
110                 } else if (!this.isRequiredChangePasswordSet()) {
111                         // Not all required fields are set
112                         throw new FaceletException("Not all required fields are set."); //NOI18N
113                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
114                         // Password not matching
115                         throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
116                 } else if (!this.featureController.isFeatureEnabled("change_user_password")) { //NOI18N
117                         // Editing is not allowed
118                         throw new IllegalStateException("User tried to change password."); //NOI18N
119                 } else if (!UserUtils.ifPasswordMatches(this.getUserCurrentPassword(), this.userLoginController.getLoggedInUser())) {
120                         // Password mismatches
121                         this.showFacesMessage("form_user_change_password:userCurrentPassword", "Entered current password does not matched stored password."); //NOI18N
122
123                         // Clear bean
124                         this.clear();
125
126                         // No redirect
127                         return ""; //NOI18N
128                 } else if (!Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())) {
129                         // Both entered passwords don't match
130                         this.showFacesMessage("form_user_change_password:userPasswordRepeat", "Entered new passwords mismatch."); //NOI18N
131
132                         // Clear bean
133                         this.clear();
134
135                         // No redirect
136                         return ""; //NOI18N
137                 } else if (Objects.equals(this.getUserCurrentPassword(), this.getUserPassword())) {
138                         // New password matches current
139                         this.showFacesMessage("form_user_change_password:userPassword", "Entered new password is same as current password."); //NOI18N
140
141                         // Clear bean
142                         this.clear();
143
144                         // No redirect
145                         return ""; //NOI18N
146                 } else if (this.userLoginController.isPasswordInHistory(this.getUserPassword())) {
147                         // Is already in list (to old passwords are ignored)
148                         this.showFacesMessage("form_user_change_password:userPassword", "Entered new password is has already been used some time ago."); //NOI18N
149
150                         // Clear bean
151                         this.clear();
152
153                         // No redirect
154                         return ""; //NOI18N
155                 }
156
157                 // Get user instance
158                 User user = this.userLoginController.getLoggedInUser();
159
160                 // Encrypt password
161                 String encryptedPassword = UserUtils.encryptPassword(this.getUserPassword());
162
163                 // Set it in user
164                 user.setUserEncryptedPassword(encryptedPassword);
165
166                 try {
167                         // Get base URL
168                         String baseUrl = FacesUtils.generateBaseUrl();
169
170                         // All is set, then update password
171                         PasswordHistory passwordHistory = this.userBean.updateUserPassword(user, baseUrl);
172
173                         // Fire event
174                         this.userUpdatedPasswordEvent.fire(new UpdatedUserPasswordEvent(passwordHistory));
175                 } catch (final UserNotFoundException | UserStatusUnconfirmedException | UserStatusLockedException ex) {
176                         // Clear bean
177                         this.clear();
178
179                         // Throw again
180                         throw new FaceletException(ex);
181                 }
182
183                 // Clear bean
184                 this.clear();
185
186                 // Return outcome
187                 return "login_data_saved"; //NOI18N
188         }
189
190         @Override
191         public String getUserCurrentPassword () {
192                 return this.userCurrentPassword;
193         }
194
195         @Override
196         public void setUserCurrentPassword (final String userCurrentPassword) {
197                 this.userCurrentPassword = userCurrentPassword;
198         }
199
200         @Override
201         public String getUserPassword () {
202                 return this.userPassword;
203         }
204
205         @Override
206         public void setUserPassword (final String userPassword) {
207                 this.userPassword = userPassword;
208         }
209
210         @Override
211         public String getUserPasswordRepeat () {
212                 return this.userPasswordRepeat;
213         }
214
215         @Override
216         public void setUserPasswordRepeat (final String userPasswordRepeat) {
217                 this.userPasswordRepeat = userPasswordRepeat;
218         }
219
220         /**
221          * Post-initialization of this class
222          */
223         @PostConstruct
224         public void init () {
225                 // Try it
226                 try {
227                         // Get initial context
228                         Context context = new InitialContext();
229
230                         // Try to lookup
231                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
232                 } catch (final NamingException e) {
233                         // Throw again
234                         throw new FaceletException(e);
235                 }
236         }
237
238         @Override
239         public boolean isRequiredChangePasswordSet () {
240                 // Is all data set?
241                 return ((this.getUserCurrentPassword() != null) &&
242                                 (!this.getUserCurrentPassword().isEmpty()) &&
243                                 (this.getUserPassword() != null) &&
244                                 (!this.getUserPassword().isEmpty()) &&
245                                 (this.getUserPasswordRepeat() != null) &&
246                                 (!this.getUserPasswordRepeat().isEmpty()));
247         }
248
249         /**
250          * Clears this bean
251          */
252         private void clear () {
253                 // Clear all data
254                 this.setUserPassword(null);
255                 this.setUserPasswordRepeat(null);
256         }
257
258 }