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.List;
21 import java.util.Objects;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.SessionScoped;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.addressbook.beans.BaseAddressbookController;
31 import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController;
32 import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController;
33 import org.mxchange.jcontacts.contact.Contact;
34 import org.mxchange.jcoreee.utils.FacesUtils;
35 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
36 import org.mxchange.jusercore.model.email_address.EmailAddressChange;
37 import org.mxchange.jusercore.model.user.User;
38 import org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote;
39 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
42 * A web session-scoped bean for changing email addresses
44 * @author Roland Häder<roland@mxchange.org>
46 @Named ("userEmailChangeController")
48 public class AddressbookEmailChangeWebSessionBean extends BaseAddressbookController implements AddressbookEmailChangeWebSessionController {
53 private static final long serialVersionUID = 186_078_724_659_153L;
56 * Email address 1 (changing)
58 private String emailAddress;
61 * Email address 2 (repeat in changing)
63 private String emailAddressRepeat;
66 * Local list of already queued email addresses
68 private List<String> emailAddresses;
71 * Remote email change bean
73 private UserEmailChangeSessionBeanRemote emailChangeBean;
79 private AddressbookFeaturesWebApplicationController featureController;
82 * Login controller (bean)
85 private AddressbookUserLoginWebSessionController userLoginController;
90 public AddressbookEmailChangeWebSessionBean () {
91 // Call super constructor
96 * Changes logged-in user's email address if the current password matches.
98 * @return Redirect outcome
100 public String doUserChangeEmailAddress () {
101 // This method shall only be called if the user is logged-in
102 if (!this.userLoginController.isUserLoggedIn()) {
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 FaceletException("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"); //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())); //NOI18N
122 User user = this.userLoginController.getLoggedInUser();
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
132 // Check if the email address is already enqueued
133 if (this.isEmailAddressQueued(this.getEmailAddress())) {
134 // Clear both email addresses
135 this.setEmailAddress(null);
136 this.setEmailAddressRepeat(null);
138 // Yes, then abort here
139 this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N
143 // Create change object, to save EJB calls, the hash is not generated here
144 ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress());
147 String baseUrl = FacesUtils.generateBaseUrl();
150 this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl);
152 // Unset all so the user is forced to re-enter it
156 return "user_login_email_change_queued"; //NOI18N
160 * Getter for email address 1 (changing)
162 * @return Email address
164 public String getEmailAddress () {
165 return this.emailAddress;
169 * Setter for email address 1 (changing)
171 * @param emailAddress Email address 1
173 public void setEmailAddress (final String emailAddress) {
174 this.emailAddress = emailAddress;
178 * Getter for email address 2 (repeat changing)
180 * @return Email address 2
182 public String getEmailAddressRepeat () {
183 return this.emailAddressRepeat;
187 * Setter for email address 2 (repeat changing)
189 * @param emailAddressRepeat Email address 2
191 public void setEmailAddressRepeat (final String emailAddressRepeat) {
192 this.emailAddressRepeat = emailAddressRepeat;
199 public void init () {
202 // Get initial context
203 Context context = new InitialContext();
206 this.emailChangeBean = (UserEmailChangeSessionBeanRemote) context.lookup("java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote"); //NOI18N
207 } catch (final NamingException e) {
209 throw new FaceletException(e);
213 this.emailAddresses = this.emailChangeBean.allQueuedAddresses();
217 public boolean isRequiredChangeEmailAddressSet () {
218 return ((this.getEmailAddress() != null) &&
219 (this.getEmailAddressRepeat() != null));
223 * Clears email address fields so the user has to re-enter them
225 private void clear () {
227 this.setEmailAddress(null);
228 this.setEmailAddressRepeat(null);
232 * Checks if given email address has already been queued. First a local list
233 * is being checked, if not found, the EJB is called. Only if found, the
234 * result is "cached" in the list.
236 * @param emailAddress Email address to verify
238 * @return Whether the email address in field emailAddress is already queued
240 private boolean isEmailAddressQueued (final String emailAddress) {
241 // It should be there
242 assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N
243 assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N
246 if (this.emailAddresses.contains(emailAddress)) {
252 boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress);
257 this.emailAddresses.add(emailAddress);