2 * Copyright (C) 2016 - 2020 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.jfinancials.beans.user.confirmlink;
19 import java.text.MessageFormat;
20 import java.util.Objects;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.mxchange.jcoreee.events.helper.clear.HelperCleanupEvent;
29 import org.mxchange.jcoreee.events.helper.clear.ObservableHelperCleanupEvent;
30 import org.mxchange.jcoreee.utils.FacesUtils;
31 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
32 import org.mxchange.jfinancials.beans.user.list.FinancialsUserListWebViewController;
33 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
34 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
35 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
36 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
37 import org.mxchange.jusercore.model.user.User;
38 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
39 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
40 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
41 import org.mxchange.juserlogincore.events.confirmation.UserConfirmedAccountEvent;
44 * A web request bean for confirmation link handling
46 * @author Roland Häder<roland@mxchange.org>
48 @Named ("userConfirmationLinkController")
50 public class FinancialsConfirmationLinkWebRequestBean extends BaseFinancialsBean implements FinancialsConfirmationLinkWebRequestController {
55 private static final long serialVersionUID = 57_637_182_796_370L;
58 * Event being fired when a bean helper should be cleaned
62 private Event<ObservableHelperCleanupEvent> cleanHelperEvent;
67 private String confirmationKey;
72 @EJB (lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
73 private UserSessionBeanRemote userBean;
76 * Event being fired when a user has confirmed the account
80 private Event<ObservableUserConfirmedAccountEvent> userConfirmedEvent;
83 * Event for when a user instance was created
87 private Event<ObservableCreatedUserEvent> userCreatedEvent;
93 private FinancialsUserListWebViewController userListController;
98 public FinancialsConfirmationLinkWebRequestBean () {
99 // Call super constructor
104 public String getConfirmationKey () {
105 return this.confirmationKey;
109 public void setConfirmationKey (final String confirmationKey) {
110 this.confirmationKey = confirmationKey;
114 public void maybeConfirmUserAccount () {
115 // Is the confirmation key set?
116 if (this.getConfirmationKey() == null) {
117 // May be null if not set
119 } else if (this.getConfirmationKey().isEmpty()) {
127 // Then loop through all
128 for (final User currentUser : this.userListController.getAllUsers()) {
129 // Same confirmation key?
130 if (Objects.equals(this.getConfirmationKey(), currentUser.getUserConfirmKey())) {
131 // Found it, then set it and abort loop
137 // Is the user instance null?
138 if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) {
139 // Then clear this bean and the helper
140 this.cleanHelperEvent.fire(new HelperCleanupEvent());
143 this.confirmUserAccount(user);
148 * Tries to confirm the currently set user instance (in helper).
150 * @param user User instance
152 private void confirmUserAccount (final User user) {
156 throw new NullPointerException("user is null"); //NOI18N
157 } else if (user.getUserId() == null) {
159 throw new NullPointerException("user.userId is null"); //NOI18N
160 } else if (user.getUserId() < 1) {
162 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
163 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
164 // Account is already confirmed
165 throw new FacesException(new UserStatusConfirmedException(user));
166 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
167 // Account is already confirmed
168 throw new FacesException(new UserStatusLockedException(user));
169 } else if (user.getUserConfirmKey() == null) {
171 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
172 } else if (user.getUserConfirmKey().isEmpty()) {
174 throw new IllegalArgumentException("user.userConfirmKey is empty"); //NOI18N
177 // Updated user instance
178 final User updatedUser;
182 final String baseUrl = FacesUtils.generateBaseUrl();
185 updatedUser = this.userBean.confirmAccount(user, baseUrl);
186 } catch (final UserStatusConfirmedException | UserStatusLockedException ex) {
187 // Something unexpected happened
188 throw new FacesException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N
191 // Fire event that the user has confirmed account
192 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
195 this.userCreatedEvent.fire(new CreatedUserEvent(updatedUser));