2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.user.email_address;
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;
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;
41 * A web session-scoped bean for changing email addresses
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("userEmailChangeController")
47 public class AddressbookEmailChangeWebRequestBean extends BaseAddressbookController implements AddressbookEmailChangeWebRequestController {
52 private static final long serialVersionUID = 186_078_724_659_153L;
55 * Email address 1 (changing)
57 private String emailAddress;
60 * Email address 2 (repeat in changing)
62 private String emailAddressRepeat;
65 * Remote email change bean
67 @EJB (lookup = "java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote")
68 private UserEmailChangeSessionBeanRemote emailChangeBean;
74 private AddressbookFeaturesWebApplicationController featureController;
77 * Local list of already queued email addresses
80 @Cached (cacheName = "queuedEmailCache")
81 private transient Cache<String, Boolean> queuedEmailCache;
84 * Login controller (bean)
87 private AddressbookUserLoginWebSessionController userLoginController;
92 public AddressbookEmailChangeWebRequestBean () {
93 // Call super constructor
98 * Changes logged-in user's email address if the current password matches.
100 * @return Redirect outcome
102 public String doUserChangeEmailAddress () {
103 // This method shall only be called if the user is logged-in
104 if (!this.userLoginController.isUserLoggedIn()) {
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
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
124 User user = this.userLoginController.getLoggedInUser();
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
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);
140 // Yes, then abort here
141 this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N
145 // Create change object, to save EJB calls, the hash is not generated here
146 ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress());
149 String baseUrl = FacesUtils.generateBaseUrl();
152 this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl);
154 // Unset all so the user is forced to re-enter it
158 return "user_login_email_change_queued"; //NOI18N
162 * Getter for email address 1 (changing)
164 * @return Email address
166 public String getEmailAddress () {
167 return this.emailAddress;
171 * Setter for email address 1 (changing)
173 * @param emailAddress Email address 1
175 public void setEmailAddress (final String emailAddress) {
176 this.emailAddress = emailAddress;
180 * Getter for email address 2 (repeat changing)
182 * @return Email address 2
184 public String getEmailAddressRepeat () {
185 return this.emailAddressRepeat;
189 * Setter for email address 2 (repeat changing)
191 * @param emailAddressRepeat Email address 2
193 public void setEmailAddressRepeat (final String emailAddressRepeat) {
194 this.emailAddressRepeat = emailAddressRepeat;
201 public void init () {
203 if (!this.queuedEmailCache.iterator().hasNext()) {
205 List<String> list = this.emailChangeBean.allQueuedAddresses();
208 for (final Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
210 final String next = iterator.next();
213 this.queuedEmailCache.put(next, Boolean.TRUE);
219 public boolean isRequiredChangeEmailAddressSet () {
220 return ((this.getEmailAddress() != null) &&
221 (this.getEmailAddressRepeat() != null));
225 * Clears email address fields so the user has to re-enter them
227 private void clear () {
229 this.setEmailAddress(null);
230 this.setEmailAddressRepeat(null);
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.
238 * @param emailAddress Email address to verify
240 * @return Whether the email address in field emailAddress is already queued
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
248 if (this.queuedEmailCache.containsKey(emailAddress)) {
254 boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress);
259 this.queuedEmailCache.put(emailAddress, Boolean.TRUE);