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