Add a command to set the avatar from a user
authorCyperghost <olaf_schmitz_1@t-online.de>
Thu, 7 Nov 2024 11:39:27 +0000 (12:39 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Thu, 7 Nov 2024 11:39:27 +0000 (12:39 +0100)
wcfsetup/install/files/lib/acp/action/UserExportGdprAction.class.php
wcfsetup/install/files/lib/action/UserAvatarAction.class.php
wcfsetup/install/files/lib/system/file/processor/UserAvatarFileProcessor.class.php
wcfsetup/install/files/lib/system/user/command/SetAvatar.class.php [new file with mode: 0644]

index 1a4e6e38fe8b2accd629102325efc1a2ee0940e3..63e9e1b32032e7af5bc050d48c104323cd684676 100644 (file)
@@ -339,7 +339,7 @@ final class UserExportGdprAction extends AbstractAction
             }
         }
 
-        if ($this->user->avatarID) {
+        if ($this->user->avatarFileID !== null || $this->user->avatarID !== null) {
             $data['avatarURL'] = $this->user->getAvatar()->getURL();
         }
 
index 502aa13cc2284aa27e9d25619f2c57b65972c5fa..c42a5019d45946ef35247121c9c359d53d806aa5 100644 (file)
@@ -6,9 +6,7 @@ use Laminas\Diactoros\Response\JsonResponse;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\RequestHandlerInterface;
-use wcf\data\file\FileAction;
 use wcf\data\IStorableObject;
-use wcf\data\user\UserEditor;
 use wcf\data\user\UserProfile;
 use wcf\http\Helper;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
@@ -20,7 +18,7 @@ use wcf\system\form\builder\field\FileProcessorFormField;
 use wcf\system\form\builder\field\RadioButtonFormField;
 use wcf\system\form\builder\IFormDocument;
 use wcf\system\form\builder\Psr15DialogForm;
-use wcf\system\user\storage\UserStorageHandler;
+use wcf\system\user\command\SetAvatar;
 use wcf\system\user\UserProfileHandler;
 use wcf\system\WCF;
 
@@ -77,15 +75,7 @@ final class UserAvatarAction implements RequestHandlerInterface
             // However, if the user wants to delete their avatar and use a standard avatar,
             // this must be saved and the cache reset
             if ($data['avatarType'] === 'none') {
-                if ($user->avatarFileID !== null) {
-                    (new FileAction([$user->avatarFileID], 'delete'))->executeAction();
-                }
-
-                (new UserEditor($user->getDecoratedObject()))->update([
-                    'avatarFileID' => null,
-                ]);
-
-                UserStorageHandler::getInstance()->reset([$user->userID], 'avatar');
+                (new SetAvatar($user->getDecoratedObject()))();
             }
 
             return new JsonResponse([
index f2782755f43f258408b445e7a3fefcaa13a2381e..5154d244583d7fc4f59670e8bb1afd4fbb9d0258 100644 (file)
@@ -4,12 +4,11 @@ namespace wcf\system\file\processor;
 
 use wcf\data\file\File;
 use wcf\data\user\avatar\UserAvatar;
-use wcf\data\user\UserEditor;
 use wcf\data\user\UserProfile;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\UserInputException;
-use wcf\system\user\storage\UserStorageHandler;
+use wcf\system\user\command\SetAvatar;
 use wcf\system\WCF;
 use wcf\util\FileUtil;
 
@@ -66,11 +65,7 @@ final class UserAvatarFileProcessor extends AbstractFileProcessor
             WCF::getSession()->update();
         }
 
-        (new UserEditor($user->getDecoratedObject()))->update([
-            'avatarFileID' => $file->fileID,
-        ]);
-        // reset user storage
-        UserStorageHandler::getInstance()->reset([$user->userID], 'avatar');
+        (new SetAvatar($user->getDecoratedObject(), $file))();
     }
 
     #[\Override]
diff --git a/wcfsetup/install/files/lib/system/user/command/SetAvatar.class.php b/wcfsetup/install/files/lib/system/user/command/SetAvatar.class.php
new file mode 100644 (file)
index 0000000..f1b4ae5
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace wcf\system\user\command;
+
+use wcf\data\file\File;
+use wcf\data\file\FileAction;
+use wcf\data\user\avatar\UserAvatarAction;
+use wcf\data\user\User;
+use wcf\data\user\UserEditor;
+use wcf\system\user\storage\UserStorageHandler;
+
+final class SetAvatar
+{
+    public function __construct(
+        private readonly User $user,
+        private readonly ?File $file = null
+    ) {
+    }
+
+    public function __invoke()
+    {
+        if ($this->file === null) {
+            (new FileAction([$this->user->avatarFileID], 'delete'))->executeAction();
+        }
+
+        // Delete old `UserAvatar` object if provided
+        if ($this->user->avatarID) {
+            (new UserAvatarAction([$this->user->avatarID], 'delete'))->executeAction();
+        }
+
+        (new UserEditor($this->user))->update([
+            'avatarFileID' => $this->file?->fileID,
+            'avatarID' => null,
+        ]);
+
+        UserStorageHandler::getInstance()->reset([$this->user->userID], 'avatar');
+    }
+}