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