From c68c8e3cd95534278b31ff71621a04935d05f781 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Mon, 6 May 2024 12:03:32 +0200 Subject: [PATCH] Add new command to send notifications for user registration status --- com.woltlab.wcf/userNotificationEvent.xml | 9 ++- .../lib/form/RegisterActivationForm.class.php | 4 + .../files/lib/form/RegisterForm.class.php | 42 +--------- .../RegistrationNotification.class.php | 66 ++++++++++++++++ ...dActivationUserNotificationEvent.class.php | 2 +- ...tionSuccessUserNotificationEvent.class.php | 76 +++++++++++++++++++ 6 files changed, 159 insertions(+), 40 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/user/command/RegistrationNotification.class.php create mode 100644 wcfsetup/install/files/lib/system/user/notification/event/UserRegistrationSuccessUserNotificationEvent.class.php diff --git a/com.woltlab.wcf/userNotificationEvent.xml b/com.woltlab.wcf/userNotificationEvent.xml index f5bca57435..6d92886e13 100644 --- a/com.woltlab.wcf/userNotificationEvent.xml +++ b/com.woltlab.wcf/userNotificationEvent.xml @@ -144,7 +144,14 @@ needActivation com.woltlab.wcf.user.registration.notification wcf\system\user\notification\event\UserNeedActivationUserNotificationEvent - admin.user.canEnableUser + admin.user.canSearchUser + 0 + + + success + com.woltlab.wcf.user.registration.notification + wcf\system\user\notification\event\UserRegistrationSuccessUserNotificationEvent + admin.user.canSearchUser 0 diff --git a/wcfsetup/install/files/lib/form/RegisterActivationForm.class.php b/wcfsetup/install/files/lib/form/RegisterActivationForm.class.php index 19c68e5425..eac11e35b9 100644 --- a/wcfsetup/install/files/lib/form/RegisterActivationForm.class.php +++ b/wcfsetup/install/files/lib/form/RegisterActivationForm.class.php @@ -12,6 +12,7 @@ use wcf\system\form\builder\field\TextFormField; use wcf\system\form\builder\field\validation\FormFieldValidationError; use wcf\system\form\builder\field\validation\FormFieldValidator; use wcf\system\request\LinkHandler; +use wcf\system\user\command\RegistrationNotification; use wcf\system\WCF; use wcf\util\HeaderUtil; use wcf\util\StringUtil; @@ -146,6 +147,9 @@ final class RegisterActivationForm extends AbstractFormBuilderForm $redirectText = WCF::getLanguage()->getDynamicVariable('wcf.user.registerActivation.success'); } + $command = new RegistrationNotification($this->user); + $command(); + HeaderUtil::delayedRedirect(LinkHandler::getInstance()->getLink(), $redirectText, 10, 'success', true); exit; diff --git a/wcfsetup/install/files/lib/form/RegisterForm.class.php b/wcfsetup/install/files/lib/form/RegisterForm.class.php index c78c85c936..a73204563e 100644 --- a/wcfsetup/install/files/lib/form/RegisterForm.class.php +++ b/wcfsetup/install/files/lib/form/RegisterForm.class.php @@ -25,6 +25,7 @@ use wcf\system\option\user\UserOptionHandler; use wcf\system\request\LinkHandler; use wcf\system\user\authentication\configuration\UserAuthenticationConfigurationFactory; use wcf\system\user\authentication\LoginRedirect; +use wcf\system\user\command\RegistrationNotification; use wcf\system\user\group\assignment\UserGroupAssignmentHandler; use wcf\system\WCF; use wcf\util\HeaderUtil; @@ -445,8 +446,6 @@ class RegisterForm extends UserAddForm $this->message = 'wcf.user.register.success'; UserGroupAssignmentHandler::getInstance()->checkUsers([$user->userID]); - - $this->fireNotificationEvent($user); } elseif (REGISTER_ACTIVATION_METHOD & User::REGISTER_ACTIVATION_USER && empty($this->blacklistMatches)) { // registering via 3rdParty leads to instant activation if ($registerVia3rdParty) { @@ -468,6 +467,9 @@ class RegisterForm extends UserAddForm $this->message = 'wcf.user.register.success.awaitActivation'; } + $command = new RegistrationNotification($user); + $command(); + if ($this->captchaObjectType) { $this->captchaObjectType->getProcessor()->reset(); } @@ -492,40 +494,4 @@ class RegisterForm extends UserAddForm exit; } - - /** - * @param User $user - * @since 5.2 - */ - protected function fireNotificationEvent(User $user) - { - // TODO fire notification if registration don't require activation per email or manuell from an admin - } - - /** - * @return int[] - * @since 5.2 - */ - protected function getRecipientsForNotificationEvent() - { - $sql = "SELECT userID - FROM wcf" . WCF_N . "_user_to_group - WHERE groupID IN ( - SELECT groupID - FROM wcf" . WCF_N . "_user_group_option_value - WHERE optionID IN ( - SELECT optionID - FROM wcf" . WCF_N . "_user_group_option - WHERE optionName = ? - ) - AND optionValue = ? - )"; - $statement = WCF::getDB()->prepareStatement($sql, 100); - $statement->execute([ - 'admin.user.canSearchUser', - 1, - ]); - - return $statement->fetchAll(\PDO::FETCH_COLUMN); - } } diff --git a/wcfsetup/install/files/lib/system/user/command/RegistrationNotification.class.php b/wcfsetup/install/files/lib/system/user/command/RegistrationNotification.class.php new file mode 100644 index 0000000000..3a2e8f8dcb --- /dev/null +++ b/wcfsetup/install/files/lib/system/user/command/RegistrationNotification.class.php @@ -0,0 +1,66 @@ + + * @since 6.1 + */ +final class RegistrationNotification +{ + public function __construct(private readonly User $user) + { + } + + public function __invoke(): void + { + if (!$this->user->requiresAdminActivation() && !$this->user->pendingActivation()) { + return; + } + + $recipientIDs = $this->getRecipientsForNotificationEvent(); + if (!empty($recipientIDs)) { + UserNotificationHandler::getInstance()->fireEvent( + $this->user->requiresAdminActivation() ? 'needActivation' : 'success', + 'com.woltlab.wcf.user.registration.notification', + new UserRegistrationUserNotificationObject($this->user), + $recipientIDs + ); + } + } + + /** + * @return int[] + */ + private function getRecipientsForNotificationEvent(): array + { + $sql = "SELECT userID + FROM wcf1_user_to_group + WHERE groupID IN ( + SELECT groupID + FROM wcf1_user_group_option_value + WHERE optionID IN ( + SELECT optionID + FROM wcf1_user_group_option + WHERE optionName = ? + ) + AND optionValue = ? + )"; + $statement = WCF::getDB()->prepareStatement($sql, 100); + $statement->execute([ + 'admin.user.canSearchUser', + 1, + ]); + + return $statement->fetchAll(\PDO::FETCH_COLUMN); + } +} diff --git a/wcfsetup/install/files/lib/system/user/notification/event/UserNeedActivationUserNotificationEvent.class.php b/wcfsetup/install/files/lib/system/user/notification/event/UserNeedActivationUserNotificationEvent.class.php index 0cca29efdc..d42ebf047f 100644 --- a/wcfsetup/install/files/lib/system/user/notification/event/UserNeedActivationUserNotificationEvent.class.php +++ b/wcfsetup/install/files/lib/system/user/notification/event/UserNeedActivationUserNotificationEvent.class.php @@ -12,7 +12,7 @@ use wcf\system\user\notification\object\UserRegistrationUserNotificationObject; * @author Olaf Braun * @copyright 2001-2024 WoltLab GmbH * @license GNU Lesser General Public License - * @since 6.0 + * @since 6.1 * * @method UserRegistrationUserNotificationObject getUserNotificationObject() */ diff --git a/wcfsetup/install/files/lib/system/user/notification/event/UserRegistrationSuccessUserNotificationEvent.class.php b/wcfsetup/install/files/lib/system/user/notification/event/UserRegistrationSuccessUserNotificationEvent.class.php new file mode 100644 index 0000000000..44afcc99be --- /dev/null +++ b/wcfsetup/install/files/lib/system/user/notification/event/UserRegistrationSuccessUserNotificationEvent.class.php @@ -0,0 +1,76 @@ + + * @since 6.1 + * + * @method UserRegistrationUserNotificationObject getUserNotificationObject() + */ +class UserRegistrationSuccessUserNotificationEvent extends AbstractUserNotificationEvent implements + ITestableUserNotificationEvent +{ + use TTestableUserNotificationEvent; + + #[\Override] + public static function getTestObjects(UserProfile $recipient, UserProfile $author) + { + return [new UserRegistrationUserNotificationObject($author->getDecoratedObject())]; + } + + #[\Override] + public function getTitle(): string + { + return $this->getLanguage()->get('wcf.user.notification.registrationSuccess.title'); + } + + #[\Override] + public function getMessage() + { + return $this->getLanguage()->getDynamicVariable( + 'wcf.user.notification.registrationSuccess.message', + [ + 'author' => $this->author, + 'notification' => $this->notification, + 'userNotificationObject' => $this->getUserNotificationObject(), + ] + ); + } + + #[\Override] + public function getEmailMessage($notificationType = 'instant') + { + return [ + 'template' => 'email_notification_userRegistrationSuccess', + 'application' => 'wcf', + 'variables' => [ + 'notification' => $this->notification, + 'userNotificationObject' => $this->getUserNotificationObject(), + ], + ]; + } + + #[\Override] + public function getLink(): string + { + return LinkHandler::getInstance()->getLink('UserEdit', [ + 'object' => $this->getUserNotificationObject(), + 'isACP' => true, + ]); + } + + #[\Override] + public function getEventHash() + { + return \sha1($this->eventID); + } +} -- 2.20.1