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.juserlogincore.model.user.register;
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
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;
33 * A session-scoped bean for user registration
35 * @author Roland Häder<roland@mxchange.org>
37 @Stateless (name = "userRegistration", description = "A bean handling the user registration")
38 public class AddressbookUserRegistrationSessionBean extends BaseAddressbookEnterpriseBean implements UserRegistrationSessionBeanRemote {
43 private static final long serialVersionUID = 12_348_958_986_818_627L;
46 * Administrative user bean
48 @EJB (lookup = "java:global/addressbook-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote")
49 private AdminUserSessionBeanRemote adminUserBean;
54 @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
55 private UserSessionBeanRemote userBean;
60 public AddressbookUserRegistrationSessionBean () {
61 // Call super constructor
62 super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
66 public String generateConfirmationKey (final User user) {
68 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
70 // user should not be null
73 throw new NullPointerException("user is null"); //NOI18N
77 final List<User> users = this.userBean.fetchAllUsers();
79 // Init confirmation key
80 String confirmationKey = null;
83 while (confirmationKey == null) {
85 confirmationKey = UserLoginUtils.generatedConfirmationKey(user);
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;
99 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
102 return confirmationKey;
106 public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
108 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
110 // user should not be null
113 throw new NullPointerException("user is null"); //NOI18N
114 } else if (user.getUserContact() == null) {
116 throw new NullPointerException("user.userContact is null"); //NOI18N
117 } else if (user.getUserContact().getContactEmailAddress() == null) {
119 throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
120 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
122 throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
123 } else if (null == baseUrl) {
125 throw new NullPointerException("baseUrl is null"); //NOI18N
126 } else if (baseUrl.isEmpty()) {
128 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
131 // Check if user is registered
132 if (this.userBean.isUserNameRegistered(user)) {
134 throw new UserNameAlreadyRegisteredException(user);
135 } else if (this.userBean.isEmailAddressRegistered(user)) {
137 throw new EmailAddressAlreadyRegisteredException(user);
141 final User addedUser = this.adminUserBean.addUser(user);
143 // Default template is with no random password
144 String templateName = "user_registration"; //NOI18N
147 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
148 // Switch to template with random password exposed
149 templateName = "user_registration_random"; //NOI18N
153 // @TODO: Internationlize the subject line somehow
154 this.sendEmail("Registration", templateName, addedUser, baseUrl, randomPassword); //NOI18N
157 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N