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