Allow admin to add attachments to the signature of users
authorCyperghost <olaf_schmitz_1@t-online.de>
Wed, 24 Jan 2024 09:50:46 +0000 (10:50 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Wed, 24 Jan 2024 09:50:46 +0000 (10:50 +0100)
wcfsetup/install/files/lib/acp/form/UserAddForm.class.php
wcfsetup/install/files/lib/acp/form/UserEditForm.class.php
wcfsetup/install/files/lib/system/attachment/SignatureAttachmentObjectType.class.php

index c85deeca436ab4e7a773b0389e0af7f4ae67bdce..f7854adaaa3f7eabcb5678afa69248117cb5d844 100644 (file)
@@ -2,15 +2,19 @@
 
 namespace wcf\acp\form;
 
+use wcf\data\smiley\category\SmileyCategory;
+use wcf\data\smiley\SmileyCache;
 use wcf\data\user\group\UserGroup;
 use wcf\data\user\User;
 use wcf\data\user\UserAction;
 use wcf\form\AbstractForm;
+use wcf\system\attachment\AttachmentHandler;
 use wcf\system\bbcode\BBCodeHandler;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\UserInputException;
 use wcf\system\html\input\HtmlInputProcessor;
 use wcf\system\language\LanguageFactory;
+use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
 use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
 use wcf\util\ArrayUtil;
@@ -124,6 +128,35 @@ class UserAddForm extends UserOptionListForm
      * @var array
      */
     public $optionTree = [];
+    public ?AttachmentHandler $attachmentHandler;
+    public int $attachmentObjectID = 0;
+    public string $attachmentObjectType = 'com.woltlab.wcf.user.signature';
+    public array $defaultSmilies = [];
+    /**
+     * list of smiley categories
+     * @var SmileyCategory[]
+     */
+    public array $smileyCategories = [];
+    public ?string $tmpHash = '';
+
+    #[\Override]
+    public function readParameters()
+    {
+        parent::readParameters();
+
+        if (isset($_REQUEST['tmpHash'])) {
+            $this->tmpHash = $_REQUEST['tmpHash'];
+        }
+        if (empty($this->tmpHash)) {
+            /** @deprecated 5.5 see QuickReplyManager::setTmpHash() */
+            $this->tmpHash = WCF::getSession()->getVar('__wcfAttachmentTmpHash');
+            if ($this->tmpHash === null) {
+                $this->tmpHash = StringUtil::getRandomID();
+            } else {
+                WCF::getSession()->unregister('__wcfAttachmentTmpHash');
+            }
+        }
+    }
 
     /**
      * @inheritDoc
@@ -292,6 +325,7 @@ class UserAddForm extends UserOptionListForm
             'groups' => $this->groupIDs,
             'languageIDs' => $this->visibleLanguages,
             'options' => $saveOptions,
+            'signatureAttachmentHandler' => $this->attachmentHandler,
         ];
 
         if (WCF::getSession()->getPermission('admin.user.canDisableSignature')) {
@@ -302,6 +336,11 @@ class UserAddForm extends UserOptionListForm
 
         $this->objectAction = new UserAction([], 'create', $data);
         $returnValues = $this->objectAction->executeAction();
+
+        $this->htmlInputProcessor->setObjectID($returnValues['returnValues']->userID);
+        MessageEmbeddedObjectManager::getInstance()->registerObjects($this->htmlInputProcessor);
+        $this->attachmentHandler->updateObjectID($returnValues['returnValues']->userID);
+
         $this->saved();
 
         // show empty add form
@@ -321,6 +360,12 @@ class UserAddForm extends UserOptionListForm
         $this->languageID = $this->getDefaultFormLanguageID();
         /** @noinspection PhpUndefinedMethodInspection */
         $this->optionHandler->resetOptionValues();
+        // Reload attachment handler to reset the uploaded attachments.
+        $this->attachmentHandler = new AttachmentHandler(
+            $this->attachmentObjectType,
+            $this->attachmentObjectID,
+            $this->tmpHash
+        );
     }
 
     /**
@@ -403,7 +448,26 @@ class UserAddForm extends UserOptionListForm
      */
     public function readData()
     {
+        if ($this->attachmentObjectType) {
+            $this->attachmentHandler = new AttachmentHandler(
+                $this->attachmentObjectType,
+                $this->attachmentObjectID,
+                $this->tmpHash,
+                0
+            );
+        }
+
         parent::readData();
+        // get default smilies
+        if (MODULE_SMILEY) {
+            $this->smileyCategories = SmileyCache::getInstance()->getVisibleCategories();
+
+            $firstCategory = \reset($this->smileyCategories);
+            if ($firstCategory) {
+                $this->defaultSmilies = SmileyCache::getInstance()
+                    ->getCategorySmilies($firstCategory->categoryID ?: null);
+            }
+        }
 
         $this->readOptionTree();
     }
@@ -442,6 +506,13 @@ class UserAddForm extends UserOptionListForm
             'disableSignature' => $this->disableSignature,
             'disableSignatureReason' => $this->disableSignatureReason,
             'disableSignatureExpires' => $this->disableSignatureExpires,
+            'attachmentHandler' => $this->attachmentHandler,
+            'attachmentObjectID' => $this->attachmentObjectID,
+            'attachmentObjectType' => $this->attachmentObjectType,
+            'attachmentParentObjectID' => 0,
+            'defaultSmilies' => $this->defaultSmilies,
+            'smileyCategories' => $this->smileyCategories,
+            'tmpHash' => $this->tmpHash,
         ]);
     }
 
index e504a96c8e187bd4774eb904ecb84ad8fc00f4f9..9e6f06a62b1745b3341fb2d37e31f0e0ed803366 100755 (executable)
@@ -16,10 +16,12 @@ use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\UserInputException;
+use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
 use wcf\system\moderation\queue\ModerationQueueManager;
 use wcf\system\style\StyleHandler;
 use wcf\system\user\command\SetColorScheme;
 use wcf\system\user\multifactor\Setup;
+use wcf\system\user\signature\SignatureCache;
 use wcf\system\WCF;
 use wcf\util\StringUtil;
 
@@ -180,6 +182,7 @@ class UserEditForm extends UserAddForm
         if (!UserGroup::isAccessibleGroup($this->user->getGroupIDs())) {
             throw new PermissionDeniedException();
         }
+        $this->attachmentObjectID = $this->user->userID;
 
         parent::readParameters();
     }
@@ -391,6 +394,8 @@ class UserEditForm extends UserAddForm
     public function save()
     {
         AbstractForm::save();
+        $this->htmlInputProcessor->setObjectID($this->userID);
+        MessageEmbeddedObjectManager::getInstance()->registerObjects($this->htmlInputProcessor);
 
         // handle avatar
         if ($this->avatarType != 'custom') {
@@ -435,11 +440,13 @@ class UserEditForm extends UserAddForm
                 'languageID' => $this->languageID,
                 'userTitle' => $this->userTitle,
                 'signature' => $this->htmlInputProcessor->getHtml(),
+                'signatureEnableHtml' => 1,
                 'styleID' => $this->styleID,
             ]),
             'groups' => $this->groupIDs,
             'languageIDs' => $this->visibleLanguages,
             'options' => $saveOptions,
+            'signatureAttachmentHandler' => $this->attachmentHandler,
         ];
         // handle changed username
         if (\mb_strtolower($this->username) != \mb_strtolower($this->user->username)) {
@@ -534,6 +541,7 @@ class UserEditForm extends UserAddForm
 
         // reload user
         $this->user = new UserEditor(new User($this->userID));
+        SignatureCache::getInstance()->getSignature($this->user->getDecoratedObject());
 
         // show success message
         WCF::getTPL()->assign('success', true);
index f909fa42872be423a432546cd860ec6b0f4ff084..c90f73b59864c32e4e675aa0c2cf1cbdaab94fc1 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace wcf\system\attachment;
 
+use wcf\data\user\group\UserGroup;
 use wcf\data\user\UserProfile;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\WCF;
@@ -24,11 +25,14 @@ class SignatureAttachmentObjectType extends AbstractAttachmentObjectType
      */
     public function canDownload($objectID)
     {
+        if (!MODULE_USER_SIGNATURE) {
+            return false;
+        }
         if ($objectID) {
             $userProfile = UserProfileRuntimeCache::getInstance()->getObject($objectID);
 
-            if (!MODULE_USER_SIGNATURE) {
-                return false;
+            if ($this->canEditUser($userProfile)) {
+                return true;
             }
             if ($userProfile->disableSignature) {
                 return false;
@@ -40,7 +44,7 @@ class SignatureAttachmentObjectType extends AbstractAttachmentObjectType
             return true;
         }
 
-        return false;
+        return $this->canAddUser();
     }
 
     /**
@@ -56,15 +60,22 @@ class SignatureAttachmentObjectType extends AbstractAttachmentObjectType
      */
     public function canUpload($objectID, $parentObjectID = 0)
     {
-        if (!$objectID || $objectID != WCF::getUser()->userID) {
+        if (!MODULE_USER_SIGNATURE) {
             return false;
         }
 
-        if (!MODULE_USER_SIGNATURE) {
+        if (!$objectID) {
+            return $this->canAddUser();
+        }
+        $userProfile = UserProfileRuntimeCache::getInstance()->getObject($objectID);
+        if ($this->canEditUser($userProfile)) {
+            return true;
+        }
+
+        if ($objectID != WCF::getUser()->userID) {
             return false;
         }
 
-        $userProfile = UserProfileRuntimeCache::getInstance()->getObject($objectID);
         if ($userProfile->disableSignature) {
             return false;
         }
@@ -153,4 +164,15 @@ class SignatureAttachmentObjectType extends AbstractAttachmentObjectType
             }
         }
     }
+
+    private function canAddUser(): bool
+    {
+        return WCF::getSession()->getPermission('admin.user.canAddUser');
+    }
+
+    private function canEditUser(UserProfile $userProfile): bool
+    {
+        return WCF::getSession()->getPermission('admin.user.canEditUser')
+            && UserGroup::isAccessibleGroup($userProfile->getGroupIDs());
+    }
 }