2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.Objects;
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;
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 BaseAddressbookBean 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;
71 * Controller for listing email address changes
74 private AddressbookEmailChangeListWebViewController emailChangeListController;
80 private AddressbookFeaturesWebApplicationController featureController;
83 * Login controller (bean)
86 private AddressbookUserLoginWebSessionController userLoginController;
91 public AddressbookEmailChangeWebRequestBean () {
92 // Call super constructor
97 * Changes logged-in user's email address if the current password matches.
99 * @return Redirect outcome
101 public String doUserChangeEmailAddress () {
102 // This method shall only be called if the user is logged-in
103 if (!this.userLoginController.isUserLoggedIn()) {
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
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
123 final User user = this.userLoginController.getLoggedInUser();
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
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);
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
144 // Create change object, to save EJB calls, the hash is not generated here
145 final ChangeableEmailAddress emailChange = new EmailAddressChange(
147 this.getEmailAddress(),
148 EmailChangeStatus.NEW
152 final String baseUrl = FacesUtils.generateBaseUrl();
155 this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl);
157 // Unset all so the user is forced to re-enter it
161 return "user_login_email_change_queued"; //NOI18N
165 * Getter for email address 1 (changing)
167 * @return Email address
169 public String getEmailAddress () {
170 return this.emailAddress;
174 * Setter for email address 1 (changing)
176 * @param emailAddress Email address 1
178 public void setEmailAddress (final String emailAddress) {
179 this.emailAddress = emailAddress;
183 * Getter for email address 2 (repeat changing)
185 * @return Email address 2
187 public String getEmailAddressRepeat () {
188 return this.emailAddressRepeat;
192 * Setter for email address 2 (repeat changing)
194 * @param emailAddressRepeat Email address 2
196 public void setEmailAddressRepeat (final String emailAddressRepeat) {
197 this.emailAddressRepeat = emailAddressRepeat;
201 public boolean isRequiredChangeEmailAddressSet () {
202 return ((this.getEmailAddress() != null) &&
203 (this.getEmailAddressRepeat() != null));
207 * Clears email address fields so the user has to re-enter them
209 private void clear () {
211 this.setEmailAddress(null);
212 this.setEmailAddressRepeat(null);