From 4173dfe05bd0155eb217dd428ac82091a508567a Mon Sep 17 00:00:00 2001 From: Git'Fellow <12234510+solracsf@users.noreply.github.com> Date: Sat, 25 Nov 2023 23:19:07 +0100 Subject: [PATCH] fix(files_versions): Explicitly check for user or owner before call getUserFolder Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> Fix typo Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> Fix Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> Fix Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> Fix Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> Optimize DI Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> Switch to DI Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> userSession does not contain null Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> Restore phpdoc annots Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> --- .../lib/Listener/FileEventsListener.php | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/apps/files_versions/lib/Listener/FileEventsListener.php b/apps/files_versions/lib/Listener/FileEventsListener.php index 323b92b3920c5..5fe41f76e492e 100644 --- a/apps/files_versions/lib/Listener/FileEventsListener.php +++ b/apps/files_versions/lib/Listener/FileEventsListener.php @@ -59,11 +59,10 @@ use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; use OCP\Files\Node; +use OCP\IUserSession; use Psr\Log\LoggerInterface; class FileEventsListener implements IEventListener { - private IRootFolder $rootFolder; - private IVersionManager $versionManager; /** * @var array */ @@ -76,19 +75,14 @@ class FileEventsListener implements IEventListener { * @var array */ private array $versionsDeleted = []; - private IMimeTypeLoader $mimeTypeLoader; - private LoggerInterface $logger; public function __construct( - IRootFolder $rootFolder, - IVersionManager $versionManager, - IMimeTypeLoader $mimeTypeLoader, - LoggerInterface $logger, + private IRootFolder $rootFolder, + private IVersionManager $versionManager, + private IMimeTypeLoader $mimeTypeLoader, + private IUserSession $userSession, + private LoggerInterface $logger, ) { - $this->rootFolder = $rootFolder; - $this->versionManager = $versionManager; - $this->mimeTypeLoader = $mimeTypeLoader; - $this->logger = $logger; } public function handle(Event $event): void { @@ -354,18 +348,20 @@ public function pre_renameOrCopy_hook(Node $source, Node $target): void { * If no user is connected, try to use the node's owner. */ private function getPathForNode(Node $node): ?string { - try { + $user = $this->userSession->getUser()?->getUID(); + if ($user) { return $this->rootFolder - ->getUserFolder(\OC_User::getUser()) + ->getUserFolder($user) ->getRelativePath($node->getPath()); - } catch (\Throwable $ex) { - $owner = $node->getOwner(); - if ($owner === null) { - return null; - } + } + + $owner = $node->getOwner()?->getUid(); + if ($owner) { return $this->rootFolder - ->getUserFolder($owner->getUid()) + ->getUserFolder($owner) ->getRelativePath($node->getPath()); } + + return null; } }