X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjusercore%2Fmodel%2Fuser%2FAddressbookAdminUserSessionBean.java;h=642c3fc5d3f536c6b430d134a7dff5f39ba1e841;hb=76ba082608c21b4108825e4af63a9c476bd32e3d;hp=9352fe244e56dbbfcc2fe5485f56feee50ab3578;hpb=c4fb853be0b32b8237a56f658d26f8b3462d94a6;p=addressbook-mailer-ejb.git diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java index 9352fe2..642c3fc 100644 --- a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Cho-Time GmbH + * Copyright (C) 2016, 2017 Roland Häder * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -19,19 +19,28 @@ package org.mxchange.jusercore.model.user; import java.text.MessageFormat; import java.util.GregorianCalendar; import javax.ejb.EJB; +import javax.ejb.EJBException; import javax.ejb.Stateless; +import javax.mail.Address; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; /** * An administrative user EJB *

- * @author Roland Haeder + * @author Roland Häder */ -@Stateless (name = "user", description = "A bean handling the user data") +@Stateless (name = "adminUser", description = "A bean handling the user data") public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean implements AdminUserSessionBeanRemote { /** @@ -100,6 +109,51 @@ public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean return user; } + @Override + public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Id is set + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.userBean.ifUserExists(user)) { + // Name already found + throw new UserNotFoundException(user); + } + + // Get a managed instance + User managedUser = this.getManagedUser(user); + + // Should be found! + assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N + + // Delete it + this.getEntityManager().remove(managedUser); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N + } + @Override public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { // Trace message @@ -151,4 +205,152 @@ public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean return user; } + @Override + public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Id is set + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Id is set + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.userBean.ifUserExists(user)) { + // Name already found + throw new UserNotFoundException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { + // Account is locked + throw new UserStatusLockedException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) { + // Account is unconfirmed + throw new UserStatusUnconfirmedException(user); + } else if (null == userLockReason) { + // Throw NPE again + throw new NullPointerException("userLockReason is null"); //NOI18N + } else if (userLockReason.isEmpty()) { + // Is empty + throw new IllegalArgumentException("userLockReason is empty"); //NOI18N + } + + // Remove contact instance as this is not updated + user.setUserContact(null); + + // Set as locked, set timestamp and lock reason + user.setUserAccountStatus(UserAccountStatus.LOCKED); + user.setUserLastLocked(new GregorianCalendar()); + user.setUserLastLockedReason(userLockReason); + + // Update user + User updatedUser = this.userBean.updateUserData(user); + + // @TODO Create user lock history entry + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send out email + // @TODO externalize subject line + this.sendEmail("Account locked", "account_locked", emailAddress, updatedUser, baseUrl); //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N + + // Return detached (and updated) user + return updatedUser; + } + + @Override + public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Id is set + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Id is set + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.userBean.ifUserExists(user)) { + // Name already found + throw new UserNotFoundException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { + // Account is confirmed + throw new UserStatusConfirmedException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) { + // Account is unconfirmed + throw new UserStatusUnconfirmedException(user); + } + + // Remove contact instance as this is not updated + user.setUserContact(null); + + // Unlock account + user.setUserAccountStatus(UserAccountStatus.CONFIRMED); + + // Update user + User updatedUser = this.userBean.updateUserData(user); + + // @TODO Create user lock history entry + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send out email + // @TODO externalize subject line + this.sendEmail("Account unlocked", "account_unlocked", emailAddress, updatedUser, baseUrl); //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N + + // Return changed account + return updatedUser; + } + }