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.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.addressbook.beans.BaseAddressbookBean;
29 import org.mxchange.addressbook.beans.user.list.AddressbookUserListWebViewController;
30 import org.mxchange.jcoreee.events.helper.clear.HelperCleanupEvent;
31 import org.mxchange.jcoreee.events.helper.clear.ObservableHelperCleanupEvent;
32 import org.mxchange.jcoreee.utils.FacesUtils;
33 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
34 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
35 import org.mxchange.jusercore.exceptions.UserNotFoundException;
36 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
37 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
38 import org.mxchange.jusercore.model.user.User;
39 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
40 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
41 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
42 import org.mxchange.juserlogincore.events.confirmation.UserConfirmedAccountEvent;
45 * A web request bean for confirmation link handling
47 * @author Roland Häder<roland@mxchange.org>
49 @Named ("userConfirmationLinkController")
51 public class AddressbookConfirmationLinkWebRequestBean extends BaseAddressbookBean implements AddressbookConfirmationLinkWebRequestController {
56 private static final long serialVersionUID = 57_637_182_796_370L;
59 * Event being fired when a bean helper should be cleaned
63 private Event<ObservableHelperCleanupEvent> cleanHelperEvent;
68 private String confirmationKey;
73 @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
74 private UserSessionBeanRemote userBean;
77 * Event being fired when a user has confirmed the account
81 private Event<ObservableUserConfirmedAccountEvent> userConfirmedEvent;
84 * Event for when a user instance was created
88 private Event<ObservableCreatedUserEvent> userCreatedEvent;
94 private AddressbookUserListWebViewController userListController;
99 public AddressbookConfirmationLinkWebRequestBean () {
100 // Call super constructor
105 public String getConfirmationKey () {
106 return this.confirmationKey;
110 public void setConfirmationKey (final String confirmationKey) {
111 this.confirmationKey = confirmationKey;
115 public void maybeConfirmUserAccount () {
116 // Is the confirmation key set?
117 if (this.getConfirmationKey() == null) {
118 // May be null if not set
120 } else if (this.getConfirmationKey().isEmpty()) {
128 // Then loop through all
129 for (final User currentUser : this.userListController.getAllUsers()) {
130 // Same confirmation key?
131 if (Objects.equals(this.getConfirmationKey(), currentUser.getUserConfirmKey())) {
132 // Found it, then set it and abort loop
138 // Is the user instance null?
139 if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) {
140 // Then clear this bean and the helper
141 this.cleanHelperEvent.fire(new HelperCleanupEvent());
144 this.confirmUserAccount(user);
149 * Tries to confirm the currently set user instance (in helper).
151 * @param user User instance
153 private void confirmUserAccount (final User user) {
157 throw new NullPointerException("user is null"); //NOI18N
158 } else if (user.getUserId() == null) {
160 throw new NullPointerException("user.userId is null"); //NOI18N
161 } else if (user.getUserId() < 1) {
163 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
164 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
165 // Account is already confirmed
166 throw new FacesException(new UserStatusConfirmedException(user));
167 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
168 // Account is already confirmed
169 throw new FacesException(new UserStatusLockedException(user));
170 } else if (user.getUserConfirmKey() == null) {
172 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
173 } else if (user.getUserConfirmKey().isEmpty()) {
175 throw new IllegalArgumentException("user.userConfirmKey is empty"); //NOI18N
178 // Updated user instance
179 final User updatedUser;
183 final String baseUrl = FacesUtils.generateBaseUrl();
186 updatedUser = this.userBean.confirmAccount(user, baseUrl);
187 } catch (final UserStatusConfirmedException | UserStatusLockedException | UserNotFoundException ex) {
188 // Something unexpected happened
189 throw new FacesException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N
192 // Fire event that the user has confirmed account
193 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
196 this.userCreatedEvent.fire(new CreatedUserEvent(updatedUser));