]> git.mxchange.org Git - addressbook-war.git/blob
67a7c0c38b1e6a6e32647f611e3ee661f8ecba46
[addressbook-war.git] /
1 /*
2  * Copyright (C) 2016, 2017 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.addressbook.beans.user.email_address;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.annotation.PostConstruct;
23 import javax.ejb.EJB;
24 import javax.enterprise.context.SessionScoped;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.mxchange.addressbook.beans.BaseAddressbookController;
29 import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController;
30 import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController;
31 import org.mxchange.jcontacts.model.contact.Contact;
32 import org.mxchange.jcoreee.utils.FacesUtils;
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 @SessionScoped
46 public class AddressbookEmailChangeWebSessionBean extends BaseAddressbookController implements AddressbookEmailChangeWebSessionController {
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          * Local list of already queued email addresses
65          */
66         private List<String> emailAddresses;
67
68         /**
69          * Remote email change bean
70          */
71         @EJB (lookup = "java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote")
72         private UserEmailChangeSessionBeanRemote emailChangeBean;
73
74         /**
75          * Features controller
76          */
77         @Inject
78         private AddressbookFeaturesWebApplicationController featureController;
79
80         /**
81          * Login controller (bean)
82          */
83         @Inject
84         private AddressbookUserLoginWebSessionController userLoginController;
85
86         /**
87          * Default constructor
88          */
89         public AddressbookEmailChangeWebSessionBean () {
90                 // Call super constructor
91                 super();
92         }
93
94         /**
95          * Changes logged-in user's email address if the current password matches.
96          * <p>
97          * @return Redirect outcome
98          */
99         public String doUserChangeEmailAddress () {
100                 // This method shall only be called if the user is logged-in
101                 if (!this.userLoginController.isUserLoggedIn()) {
102                         // Not logged-in
103                         throw new IllegalStateException("User is not logged-in"); //NOI18N
104                 } else if (!this.featureController.isFeatureEnabled("user_change_email_address")) { //NOI18N
105                         // Editing is not allowed
106                         throw new IllegalStateException("User tried to change email address"); //NOI18N
107                 } else if (!this.isRequiredChangeEmailAddressSet()) {
108                         // Not all required fields are set
109                         throw new FaceletException("Not all required fields are set."); //NOI18N
110                 } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) {
111                         // Email address 1+2 mismatch
112                         this.showFacesMessage("form_user_change_email_address:emailAddressRepeat", "ERROR_USER_EMAIL_ADDRESSES_MISMATCH"); //NOI18N
113                         return ""; //NOI18N
114                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
115                         // Password not matching
116                         this.showFacesMessage("form_login_user_change_email_address:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
117                         return ""; //NOI18N
118                 }
119
120                 // Get user instance
121                 User user = this.userLoginController.getLoggedInUser();
122
123                 // It should be there, so run some tests on it
124                 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
125                 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
126                 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
127                 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
128                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
129                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
130
131                 // Check if the email address is already enqueued
132                 if (this.isEmailAddressQueued(this.getEmailAddress())) {
133                         // Clear both email addresses
134                         this.setEmailAddress(null);
135                         this.setEmailAddressRepeat(null);
136
137                         // Yes, then abort here
138                         this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N
139                         return ""; //NOI18N
140                 }
141
142                 // Create change object, to save EJB calls, the hash is not generated here
143                 ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress());
144
145                 // Get base URL
146                 String baseUrl = FacesUtils.generateBaseUrl();
147
148                 // Call EJB
149                 this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl);
150
151                 // Unset all so the user is forced to re-enter it
152                 this.clear();
153
154                 // All fine
155                 return "user_login_email_change_queued"; //NOI18N
156         }
157
158         /**
159          * Getter for email address 1 (changing)
160          * <p>
161          * @return Email address
162          */
163         public String getEmailAddress () {
164                 return this.emailAddress;
165         }
166
167         /**
168          * Setter for email address 1 (changing)
169          * <p>
170          * @param emailAddress Email address 1
171          */
172         public void setEmailAddress (final String emailAddress) {
173                 this.emailAddress = emailAddress;
174         }
175
176         /**
177          * Getter for email address 2 (repeat changing)
178          * <p>
179          * @return Email address 2
180          */
181         public String getEmailAddressRepeat () {
182                 return this.emailAddressRepeat;
183         }
184
185         /**
186          * Setter for email address 2 (repeat changing)
187          * <p>
188          * @param emailAddressRepeat Email address 2
189          */
190         public void setEmailAddressRepeat (final String emailAddressRepeat) {
191                 this.emailAddressRepeat = emailAddressRepeat;
192         }
193
194         /**
195          * Post-construction
196          */
197         @PostConstruct
198         public void init () {
199                 // Init list
200                 this.emailAddresses = this.emailChangeBean.allQueuedAddresses();
201         }
202
203         @Override
204         public boolean isRequiredChangeEmailAddressSet () {
205                 return ((this.getEmailAddress() != null) &&
206                                 (this.getEmailAddressRepeat() != null));
207         }
208
209         /**
210          * Clears email address fields so the user has to re-enter them
211          */
212         private void clear () {
213                 // Clear fields
214                 this.setEmailAddress(null);
215                 this.setEmailAddressRepeat(null);
216         }
217
218         /**
219          * Checks if given email address has already been queued. First a local list
220          * is being checked, if not found, the EJB is called. Only if found, the
221          * result is "cached" in the list.
222          * <p>
223          * @param emailAddress Email address to verify
224          * <p>
225          * @return Whether the email address in field emailAddress is already queued
226          */
227         private boolean isEmailAddressQueued (final String emailAddress) {
228                 // It should be there
229                 assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N
230                 assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N
231
232                 // Check list
233                 if (this.emailAddresses.contains(emailAddress)) {
234                         // Okay, found it
235                         return true;
236                 }
237
238                 // Check EJB
239                 boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress);
240
241                 // Is it there?
242                 if (isQueued) {
243                         // Add to list
244                         this.emailAddresses.add(emailAddress);
245                 }
246
247                 // Return status
248                 return isQueued;
249         }
250
251 }