Add session variable so that the user can delete the previous avatar
authorCyperghost <olaf_schmitz_1@t-online.de>
Wed, 6 Nov 2024 11:48:28 +0000 (12:48 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Wed, 6 Nov 2024 11:48:28 +0000 (12:48 +0100)
wcfsetup/install/files/lib/system/file/processor/UserAvatarFileProcessor.class.php

index 2f2a6c25237568922bcc6faad7d797d2c11f2fce..5140d0af57a4762293b06fab756a9eb096f15e1e 100644 (file)
@@ -21,6 +21,8 @@ use wcf\util\FileUtil;
  */
 final class UserAvatarFileProcessor extends AbstractFileProcessor
 {
+    private const SESSION_VARIABLE = 'wcf_user_avatar_processor_%d';
+
     #[\Override]
     public function getObjectTypeName(): string
     {
@@ -58,6 +60,12 @@ final class UserAvatarFileProcessor extends AbstractFileProcessor
             return;
         }
 
+        // Save the `fileID` in the session variable so that the current user can delete it the old avatar
+        if ($user->avatarFileID !== null) {
+            WCF::getSession()->register(\sprintf(self::SESSION_VARIABLE, $$user->avatarFileID), TIME_NOW);
+            WCF::getSession()->update();
+        }
+
         (new UserEditor($user))->update([
             'avatarFileID' => $file->fileID,
         ]);
@@ -111,7 +119,9 @@ final class UserAvatarFileProcessor extends AbstractFileProcessor
     {
         $user = $this->getUserByFile($file);
         if ($user === null) {
-            return false;
+            return WCF::getSession()->getVar(
+                \sprintf(self::SESSION_VARIABLE, $file->fileID)
+            ) !== null;
         }
 
         return $this->canEditAvatar($user);
@@ -140,6 +150,13 @@ final class UserAvatarFileProcessor extends AbstractFileProcessor
     #[\Override]
     public function delete(array $fileIDs, array $thumbnailIDs): void
     {
+        \array_map(
+            static fn(int $fileID) => WCF::getSession()->unregister(
+                \sprintf(self::SESSION_VARIABLE, $fileID)
+            ),
+            $fileIDs
+        );
+
         $conditionBuilder = new PreparedStatementConditionBuilder();
         $conditionBuilder->add('avatarFileID IN (?)', [$fileIDs]);