]> git.mxchange.org Git - jfinancials-war.git/blob
ab615016417792ab98070c7bb16804c6653c37da
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.user.confirmlink;
18
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.ejb.EJB;
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;
42
43 /**
44  * A web request bean for confirmation link handling
45  * <p>
46  * @author Roland Häder<roland@mxchange.org>
47  */
48 @Named ("userConfirmationLinkController")
49 @RequestScoped
50 public class FinancialsConfirmationLinkWebRequestBean extends BaseFinancialsBean implements FinancialsConfirmationLinkWebRequestController {
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 57_637_182_796_370L;
56
57         /**
58          * Event being fired when a bean helper should be cleaned
59          */
60         @Inject
61         @Any
62         private Event<ObservableHelperCleanupEvent> cleanHelperEvent;
63
64         /**
65          * Confirmation key
66          */
67         private String confirmationKey;
68
69         /**
70          * Remote user bean
71          */
72         @EJB (lookup = "java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
73         private UserSessionBeanRemote userBean;
74
75         /**
76          * Event being fired when a user has confirmed the account
77          */
78         @Inject
79         @Any
80         private Event<ObservableUserConfirmedAccountEvent> userConfirmedEvent;
81
82         /**
83          * Event for when a user instance was created
84          */
85         @Any
86         @Inject
87         private Event<ObservableCreatedUserEvent> userCreatedEvent;
88
89         /**
90          * User controller
91          */
92         @Inject
93         private FinancialsUserListWebViewController userListController;
94
95         /**
96          * Default constructor
97          */
98         public FinancialsConfirmationLinkWebRequestBean () {
99                 // Call super constructor
100                 super();
101         }
102
103         @Override
104         public String getConfirmationKey () {
105                 return this.confirmationKey;
106         }
107
108         @Override
109         public void setConfirmationKey (final String confirmationKey) {
110                 this.confirmationKey = confirmationKey;
111         }
112
113         @Override
114         public void maybeConfirmUserAccount () {
115                 // Is the confirmation key set?
116                 if (this.getConfirmationKey() == null) {
117                         // May be null if not set
118                         return;
119                 } else if (this.getConfirmationKey().isEmpty()) {
120                         // Is empty string
121                         return;
122                 }
123
124                 // Init instance
125                 User user = null;
126
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
132                                 user = currentUser;
133                                 break;
134                         }
135                 }
136
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());
141                 } else {
142                         // Try to confirm it
143                         this.confirmUserAccount(user);
144                 }
145         }
146
147         /**
148          * Tries to confirm the currently set user instance (in helper).
149          * <p>
150          * @param user User instance
151          */
152         private void confirmUserAccount (final User user) {
153                 // Should be set
154                 if (null == user) {
155                         // Throw NPE
156                         throw new NullPointerException("user is null"); //NOI18N
157                 } else if (user.getUserId() == null) {
158                         // Abort here
159                         throw new NullPointerException("user.userId is null"); //NOI18N
160                 } else if (user.getUserId() < 1) {
161                         // Invalid number
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) {
170                         // Throw NPE
171                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
172                 } else if (user.getUserConfirmKey().isEmpty()) {
173                         // Is empty string
174                         throw new IllegalArgumentException("user.userConfirmKey is empty"); //NOI18N
175                 }
176
177                 // Updated user instance
178                 final User updatedUser;
179
180                 try {
181                         // Get base URL
182                         final String baseUrl = FacesUtils.generateBaseUrl();
183
184                         // Confirm account
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
189                 }
190
191                 // Fire event that the user has confirmed account
192                 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
193
194                 // Fire event
195                 this.userCreatedEvent.fire(new CreatedUserEvent(updatedUser));
196         }
197
198 }