diff --git a/apps/files_trashbin/lib/Trashbin.php b/apps/files_trashbin/lib/Trashbin.php index dd71e8ec28e46..811c83cdb6b53 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -41,12 +41,16 @@ use OCP\FilesMetadata\IFilesMetadataManager; use OCP\IConfig; use OCP\IDBConnection; +use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserManager; use OCP\Lock\ILockingProvider; use OCP\Lock\LockedException; use OCP\Server; +use OCP\Share\Exceptions\ShareNotFound; use OCP\Util; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; /** @template-implements IEventListener */ @@ -327,13 +331,16 @@ public static function move2trash($file_path, $ownerOnly = false) { } if ($moveSuccessful) { + // there is still a possibility that the file has been deleted by a remote user + $deletedBy = self::overwriteDeletedBy($user); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); $query->insert('files_trash') ->setValue('id', $query->createNamedParameter($filename)) ->setValue('timestamp', $query->createNamedParameter($timestamp)) ->setValue('location', $query->createNamedParameter($location)) ->setValue('user', $query->createNamedParameter($owner)) - ->setValue('deleted_by', $query->createNamedParameter($user)); + ->setValue('deleted_by', $query->createNamedParameter($deletedBy)); $result = $query->executeStatement(); if (!$result) { Server::get(LoggerInterface::class)->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']); @@ -1188,6 +1195,29 @@ private static function getNodeForPath(string $user, string $path, string $baseD } } + /** + * in case the request is authed, and user token is from a federated share + * we use shared_with as initiator of the deletion + */ + private static function overwriteDeletedBy(string $user) { + try { + $request = Server::get(IRequest::class); + /** @psalm-suppress NoInterfaceProperties */ + $token = $request->server['PHP_AUTH_USER'] ?? ''; + if ($token === '') { + return $user; + } + + $federatedShareProvider = Server::get(\OCA\FederatedFileSharing\FederatedShareProvider::class); + $share = $federatedShareProvider->getShareByToken($token); + + return $share->getSharedWith(); + } catch (NotFoundExceptionInterface|ContainerExceptionInterface|ShareNotFound) { + } + + return $user; + } + public function handle(Event $event): void { if ($event instanceof BeforeNodeDeletedEvent) { self::ensureFileScannedHook($event->getNode());