]> git.mxchange.org Git - addressbook-ejb.git/blob
427a4087f901569258fcafe8bc0600773f8ae638
[addressbook-ejb.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.juserlogincore.model.user.register;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.ejb.EJB;
23 import javax.ejb.Stateless;
24 import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean;
25 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
26 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
27 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
28 import org.mxchange.jusercore.model.user.User;
29 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
30 import org.mxchange.juserlogincore.login.UserLoginUtils;
31
32 /**
33  * A session-scoped bean for user registration
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 @Stateless (name = "userRegistration", description = "A bean handling the user registration")
38 public class AddressbookUserRegistrationSessionBean extends BaseAddressbookEnterpriseBean implements UserRegistrationSessionBeanRemote {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 12_348_958_986_818_627L;
44
45         /**
46          * Administrative user bean
47          */
48         @EJB (lookup = "java:global/addressbook-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote")
49         private AdminUserSessionBeanRemote adminUserBean;
50
51         /**
52          * Regular user EJB
53          */
54         @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
55         private UserSessionBeanRemote userBean;
56
57         /**
58          * Default constructor
59          */
60         public AddressbookUserRegistrationSessionBean () {
61                 // Call super constructor
62                 super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
63         }
64
65         @Override
66         public String generateConfirmationKey (final User user) {
67                 // Trace message
68                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
69
70                 // user should not be null
71                 if (null == user) {
72                         // Abort here
73                         throw new NullPointerException("user is null"); //NOI18N
74                 }
75
76                 // Fetch whole list
77                 final List<User> users = this.userBean.fetchAllUsers();
78
79                 // Init confirmation key
80                 String confirmationKey = null;
81
82                 // Find a free one
83                 while (confirmationKey == null) {
84                         // Create new one
85                         confirmationKey = UserLoginUtils.generatedConfirmationKey(user);
86
87                         // Check all entries
88                         for (final User currentUser : users) {
89                                 // Does the key match?
90                                 if (Objects.equals(currentUser.getUserConfirmKey(), confirmationKey)) {
91                                         // Set key to null and exit loop
92                                         confirmationKey = null;
93                                         break;
94                                 }
95                         }
96                 }
97
98                 // Log trace message
99                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
100
101                 // Return it
102                 return confirmationKey;
103         }
104
105         @Override
106         public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
107                 // Trace message
108                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
109
110                 // user should not be null
111                 if (null == user) {
112                         // Abort here
113                         throw new NullPointerException("user is null"); //NOI18N
114                 } else if (user.getUserContact() == null) {
115                         // Throw NPE again
116                         throw new NullPointerException("user.userContact is null"); //NOI18N
117                 } else if (user.getUserContact().getContactEmailAddress() == null) {
118                         // Throw NPE again
119                         throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
120                 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
121                         // Is empty
122                         throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
123                 } else if (null == baseUrl) {
124                         // Throw NPE again
125                         throw new NullPointerException("baseUrl is null"); //NOI18N
126                 } else if (baseUrl.isEmpty()) {
127                         // Throw IAE
128                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
129                 }
130
131                 // Check if user is registered
132                 if (this.userBean.isUserNameRegistered(user)) {
133                         // Abort here
134                         throw new UserNameAlreadyRegisteredException(user);
135                 } else if (this.userBean.isEmailAddressRegistered(user)) {
136                         // Abort here
137                         throw new EmailAddressAlreadyRegisteredException(user);
138                 }
139
140                 // Call other EJB
141                 final User addedUser = this.adminUserBean.addUser(user);
142
143                 // Default template is with no random password
144                 String templateName = "user_registration"; //NOI18N
145
146                 // Is password set?
147                 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
148                         // Switch to template with random password exposed
149                         templateName = "user_registration_random"; //NOI18N
150                 }
151
152                 // Send email
153                 // @TODO: Internationlize the subject line somehow
154                 this.sendEmail("Registration", templateName, addedUser, baseUrl, randomPassword); //NOI18N
155
156                 // Trace message
157                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N
158
159                 // Return it
160                 return addedUser;
161         }
162
163 }