Add new command to send notifications for user registration status
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 6 May 2024 10:03:32 +0000 (12:03 +0200)
committerCyperghost <olaf_schmitz_1@t-online.de>
Mon, 6 May 2024 10:03:32 +0000 (12:03 +0200)
com.woltlab.wcf/userNotificationEvent.xml
wcfsetup/install/files/lib/form/RegisterActivationForm.class.php
wcfsetup/install/files/lib/form/RegisterForm.class.php
wcfsetup/install/files/lib/system/user/command/RegistrationNotification.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/user/notification/event/UserNeedActivationUserNotificationEvent.class.php
wcfsetup/install/files/lib/system/user/notification/event/UserRegistrationSuccessUserNotificationEvent.class.php [new file with mode: 0644]

index f5bca57435b6b718ba7aaf815dee8b051ada8579..6d92886e13736630104bc5591996cc2acd1ccaa7 100644 (file)
                        <name>needActivation</name>
                        <objecttype>com.woltlab.wcf.user.registration.notification</objecttype>
                        <classname>wcf\system\user\notification\event\UserNeedActivationUserNotificationEvent</classname>
-                       <permissions>admin.user.canEnableUser</permissions>
+                       <permissions>admin.user.canSearchUser</permissions>
+                       <preset>0</preset>
+               </event>
+               <event>
+                       <name>success</name>
+                       <objecttype>com.woltlab.wcf.user.registration.notification</objecttype>
+                       <classname>wcf\system\user\notification\event\UserRegistrationSuccessUserNotificationEvent</classname>
+                       <permissions>admin.user.canSearchUser</permissions>
                        <preset>0</preset>
                </event>
        </import>
index 19c68e5425a20724ff586c990443c7cc042fc922..eac11e35b9eebf41b71fb31e6d8e9caa1d5aeafd 100644 (file)
@@ -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;
index c78c85c936308f6237a048ce671d208489c0c4c5..a73204563eac725151df73495f365b52281ac6a4 100644 (file)
@@ -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 (file)
index 0000000..3a2e8f8
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+
+namespace wcf\system\user\command;
+
+use wcf\data\user\User;
+use wcf\system\user\notification\object\UserRegistrationUserNotificationObject;
+use wcf\system\user\notification\UserNotificationHandler;
+use wcf\system\WCF;
+
+/**
+ * Send a notification of user registration status.
+ *
+ * @author      Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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);
+    }
+}
index 0cca29efdc86bf0accaf9010b55d2bbd93953a0e..d42ebf047f1bcdd67898a0d348244007d436ff6f 100644 (file)
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
- * @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 (file)
index 0000000..44afcc9
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+
+namespace wcf\system\user\notification\event;
+
+use wcf\data\user\UserProfile;
+use wcf\system\request\LinkHandler;
+use wcf\system\user\notification\object\UserRegistrationUserNotificationObject;
+
+/**
+ * Notification event for users that completed the registration process.
+ *
+ * @author      Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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);
+    }
+}