]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/user/email_address/FinancialsEmailChangeWebRequestBean.java
Don't cherry-pick:
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / user / email_address / FinancialsEmailChangeWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
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.jfinancials.beans.user.email_address;
18
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.ejb.EJB;
22 import javax.enterprise.context.RequestScoped;
23 import javax.faces.FacesException;
24 import javax.faces.application.FacesMessage;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jcontacts.model.contact.Contact;
28 import org.mxchange.jcoreee.utils.FacesUtils;
29 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
30 import org.mxchange.jfinancials.beans.features.FinancialsFeaturesWebApplicationController;
31 import org.mxchange.jfinancials.beans.user.email_address.list.FinancialsEmailChangeListWebViewController;
32 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
33 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
34 import org.mxchange.jusercore.model.email_address.EmailAddressChange;
35 import org.mxchange.jusercore.model.user.User;
36 import org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote;
37 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
38
39 /**
40  * A web session-scoped bean for changing email addresses
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("userEmailChangeController")
45 @RequestScoped
46 public class FinancialsEmailChangeWebRequestBean extends BaseFinancialsBean implements FinancialsEmailChangeWebRequestController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 186_078_724_659_153L;
52
53         /**
54          * Email address 1 (changing)
55          */
56         private String emailAddress;
57
58         /**
59          * Email address 2 (repeat in changing)
60          */
61         private String emailAddressRepeat;
62
63         /**
64          * Remote email change bean
65          */
66         @EJB (lookup = "java:global/jfinancials-ejb/userEmailChange!org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote")
67         private UserEmailChangeSessionBeanRemote emailChangeBean;
68
69         /**
70          * Controller for listing email address changes
71          */
72         @Inject
73         private FinancialsEmailChangeListWebViewController emailChangeListController;
74
75         /**
76          * Features controller
77          */
78         @Inject
79         private FinancialsFeaturesWebApplicationController featureController;
80
81         /**
82          * Login controller (bean)
83          */
84         @Inject
85         private FinancialsUserLoginWebSessionController userLoginController;
86
87         /**
88          * Default constructor
89          */
90         public FinancialsEmailChangeWebRequestBean () {
91                 // Call super constructor
92                 super();
93         }
94
95         /**
96          * Changes logged-in user's email address if the current password matches.
97          * <p>
98          * @return Redirect outcome
99          */
100         public String doUserChangeEmailAddress () {
101                 // This method shall only be called if the user is logged-in
102                 if (!this.userLoginController.isUserLoggedIn()) {
103                         // Not logged-in
104                         throw new IllegalStateException("User is not logged-in"); //NOI18N
105                 } else if (!this.featureController.isFeatureEnabled("user_change_email_address")) { //NOI18N
106                         // Editing is not allowed
107                         throw new IllegalStateException("User tried to change email address"); //NOI18N
108                 } else if (!this.isRequiredChangeEmailAddressSet()) {
109                         // Not all required fields are set
110                         throw new FacesException("Not all required fields are set."); //NOI18N
111                 } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) {
112                         // Email address 1+2 mismatch
113                         this.showFacesMessage("form_user_change_email_address:emailAddressRepeat", "ERROR_USER_EMAIL_ADDRESSES_MISMATCH", FacesMessage.SEVERITY_WARN); //NOI18N
114                         return ""; //NOI18N
115                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
116                         // Password not matching
117                         this.showFacesMessage("form_login_user_change_email_address:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()), FacesMessage.SEVERITY_WARN); //NOI18N
118                         return ""; //NOI18N
119                 }
120
121                 // Get user instance
122                 final User user = this.userLoginController.getLoggedInUser();
123
124                 // It should be there, so run some tests on it
125                 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
126                 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
127                 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
128                 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
129                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
130                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
131
132                 // Check if the email address is already enqueued
133                 if (this.emailChangeListController.isEmailAddressQueued(this.getEmailAddress())) {
134                         // Clear both email addresses
135                         this.setEmailAddress(null);
136                         this.setEmailAddressRepeat(null);
137
138                         // Yes, then abort here
139                         this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED", FacesMessage.SEVERITY_ERROR); //NOI18N
140                         return ""; //NOI18N
141                 }
142
143                 // Create change object, to save EJB calls, the hash is not generated here
144                 final ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress());
145
146                 // Get base URL
147                 final String baseUrl = FacesUtils.generateBaseUrl();
148
149                 // Call EJB
150                 this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl);
151
152                 // Unset all so the user is forced to re-enter it
153                 this.clear();
154
155                 // All fine
156                 return "user_login_email_change_queued"; //NOI18N
157         }
158
159         /**
160          * Getter for email address 1 (changing)
161          * <p>
162          * @return Email address
163          */
164         public String getEmailAddress () {
165                 return this.emailAddress;
166         }
167
168         /**
169          * Setter for email address 1 (changing)
170          * <p>
171          * @param emailAddress Email address 1
172          */
173         public void setEmailAddress (final String emailAddress) {
174                 this.emailAddress = emailAddress;
175         }
176
177         /**
178          * Getter for email address 2 (repeat changing)
179          * <p>
180          * @return Email address 2
181          */
182         public String getEmailAddressRepeat () {
183                 return this.emailAddressRepeat;
184         }
185
186         /**
187          * Setter for email address 2 (repeat changing)
188          * <p>
189          * @param emailAddressRepeat Email address 2
190          */
191         public void setEmailAddressRepeat (final String emailAddressRepeat) {
192                 this.emailAddressRepeat = emailAddressRepeat;
193         }
194
195         @Override
196         public boolean isRequiredChangeEmailAddressSet () {
197                 return ((this.getEmailAddress() != null) &&
198                                 (this.getEmailAddressRepeat() != null));
199         }
200
201         /**
202          * Clears email address fields so the user has to re-enter them
203          */
204         private void clear () {
205                 // Clear fields
206                 this.setEmailAddress(null);
207                 this.setEmailAddressRepeat(null);
208         }
209
210 }