/*
- * Copyright (C) 2016 - 2020 Free Software Foundation
+ * Copyright (C) 2016 - 2024 Free Software Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
package org.mxchange.juserlogincore.model.user.register;
import java.text.MessageFormat;
+import java.util.List;
import java.util.Objects;
import javax.ejb.EJB;
import javax.ejb.Stateless;
-import javax.persistence.NoResultException;
-import javax.persistence.Query;
-import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
-import org.mxchange.jcontacts.model.contact.Contact;
+import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean;
import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
-import org.mxchange.jusercore.model.user.LoginUser;
import org.mxchange.jusercore.model.user.User;
import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
-import org.mxchange.juserlogincore.login.UserLoginUtils;
+import org.mxchange.juserlogincore.utils.UserLoginUtils;
/**
* A session-scoped bean for user registration
* @author Roland Häder<roland@mxchange.org>
*/
@Stateless (name = "userRegistration", description = "A bean handling the user registration")
-public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatabaseBean implements UserRegistrationSessionBeanRemote {
+public class AddressbookUserRegistrationSessionBean extends BaseAddressbookEnterpriseBean implements UserRegistrationSessionBeanRemote {
/**
* Serial number
/**
* Administrative user bean
*/
- @EJB
+ @EJB (lookup = "java:global/addressbook-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote")
private AdminUserSessionBeanRemote adminUserBean;
/**
* Regular user EJB
*/
- @EJB
+ @EJB (lookup = "java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
private UserSessionBeanRemote userBean;
/**
throw new NullPointerException("user is null"); //NOI18N
}
- // Create named instance
- final Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
+ // Fetch whole list
+ final List<User> users = this.userBean.fetchAllUsers();
// Init confirmation key
String confirmationKey = null;
// Find a free one
while (confirmationKey == null) {
// Create new one
- final String key = UserLoginUtils.generatedConfirmationKey(user);
-
- // Set key as parameter
- query.setParameter("confirmKey", key); //NOI18N
-
- // Try it
- try {
- // Get contact instance
- final Contact contact = (Contact) query.getSingleResult();
-
- // Warning message
- this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
- } catch (final NoResultException ex) {
- // Not found, normal case
- confirmationKey = key;
- break;
+ confirmationKey = UserLoginUtils.generatedConfirmationKey(user);
+
+ // Check all entries
+ for (final User currentUser : users) {
+ // Does the key match?
+ if (Objects.equals(currentUser.getUserConfirmKey(), confirmationKey)) {
+ // Set key to null and exit loop
+ confirmationKey = null;
+ break;
+ }
}
}
return confirmationKey;
}
- @Override
- public boolean isEmailAddressRegistered (final User user) {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
-
- // Check bean
- assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
-
- // user should not be null
- if (null == user) {
- // Abort here
- throw new NullPointerException("user is null"); //NOI18N
- }
-
- // Call other bean
- return this.userBean.isEmailAddressRegistered(user);
- }
-
- @Override
- public boolean isUserNameRegistered (final User user) {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
-
- // Check bean
- assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
-
- // user should not be null
- if (null == user) {
- // Abort here
- throw new NullPointerException("user is null"); //NOI18N
- }
-
- // Call other bean
- return this.userBean.isUserNameRegistered(user);
- }
-
@Override
public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
// Trace message
} else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
// Is empty
throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
+ } else if (null == baseUrl) {
+ // Throw NPE again
+ throw new NullPointerException("baseUrl is null"); //NOI18N
+ } else if (baseUrl.isEmpty()) {
+ // Throw IAE
+ throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
}
// Check if user is registered
- if (this.isUserNameRegistered(user)) {
+ if (this.userBean.isUserNameRegistered(user)) {
// Abort here
throw new UserNameAlreadyRegisteredException(user);
- } else if (this.isEmailAddressRegistered(user)) {
+ } else if (this.userBean.isEmailAddressRegistered(user)) {
// Abort here
throw new EmailAddressAlreadyRegisteredException(user);
}
// Is password set?
if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
- // Switch to other template
+ // Switch to template with random password exposed
templateName = "user_registration_random"; //NOI18N
}