diff options
Diffstat (limited to 'apps/files_trashbin/lib/Sabre')
-rw-r--r-- | apps/files_trashbin/lib/Sabre/AbstractTrash.php | 19 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/RootCollection.php | 13 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashHome.php | 18 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashRoot.php | 21 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Sabre/TrashbinPlugin.php | 73 |
5 files changed, 95 insertions, 49 deletions
diff --git a/apps/files_trashbin/lib/Sabre/AbstractTrash.php b/apps/files_trashbin/lib/Sabre/AbstractTrash.php index e46a7d5d638..f032395437b 100644 --- a/apps/files_trashbin/lib/Sabre/AbstractTrash.php +++ b/apps/files_trashbin/lib/Sabre/AbstractTrash.php @@ -8,21 +8,18 @@ declare(strict_types=1); */ namespace OCA\Files_Trashbin\Sabre; +use OCA\Files_Trashbin\Service\ConfigService; use OCA\Files_Trashbin\Trash\ITrashItem; use OCA\Files_Trashbin\Trash\ITrashManager; use OCP\Files\FileInfo; use OCP\IUser; +use Sabre\DAV\Exception\Forbidden; abstract class AbstractTrash implements ITrash { - /** @var ITrashItem */ - protected $data; - - /** @var ITrashManager */ - protected $trashManager; - - public function __construct(ITrashManager $trashManager, ITrashItem $data) { - $this->trashManager = $trashManager; - $this->data = $data; + public function __construct( + protected ITrashManager $trashManager, + protected ITrashItem $data, + ) { } public function getFilename(): string { @@ -78,6 +75,10 @@ abstract class AbstractTrash implements ITrash { } public function delete() { + if (!ConfigService::getDeleteFromTrashEnabled()) { + throw new Forbidden('Not allowed to delete items from the trash bin'); + } + $this->trashManager->removeItem($this->data); } diff --git a/apps/files_trashbin/lib/Sabre/RootCollection.php b/apps/files_trashbin/lib/Sabre/RootCollection.php index f626bfd7ee1..8886dae0895 100644 --- a/apps/files_trashbin/lib/Sabre/RootCollection.php +++ b/apps/files_trashbin/lib/Sabre/RootCollection.php @@ -10,22 +10,19 @@ namespace OCA\Files_Trashbin\Sabre; use OCA\Files_Trashbin\Trash\ITrashManager; use OCP\IConfig; +use OCP\IUserSession; +use OCP\Server; use Sabre\DAV\INode; use Sabre\DAVACL\AbstractPrincipalCollection; use Sabre\DAVACL\PrincipalBackend; class RootCollection extends AbstractPrincipalCollection { - /** @var ITrashManager */ - private $trashManager; - public function __construct( - ITrashManager $trashManager, + private ITrashManager $trashManager, PrincipalBackend\BackendInterface $principalBackend, - IConfig $config + IConfig $config, ) { parent::__construct($principalBackend, 'principals/users'); - - $this->trashManager = $trashManager; $this->disableListing = !$config->getSystemValue('debug', false); } @@ -41,7 +38,7 @@ class RootCollection extends AbstractPrincipalCollection { */ public function getChildForPrincipal(array $principalInfo): TrashHome { [, $name] = \Sabre\Uri\split($principalInfo['uri']); - $user = \OC::$server->getUserSession()->getUser(); + $user = Server::get(IUserSession::class)->getUser(); if (is_null($user) || $name !== $user->getUID()) { throw new \Sabre\DAV\Exception\Forbidden(); } diff --git a/apps/files_trashbin/lib/Sabre/TrashHome.php b/apps/files_trashbin/lib/Sabre/TrashHome.php index edea2744e6f..fc291c76f17 100644 --- a/apps/files_trashbin/lib/Sabre/TrashHome.php +++ b/apps/files_trashbin/lib/Sabre/TrashHome.php @@ -15,23 +15,11 @@ use Sabre\DAV\Exception\NotFound; use Sabre\DAV\ICollection; class TrashHome implements ICollection { - /** @var ITrashManager */ - private $trashManager; - - /** @var array */ - private $principalInfo; - - /** @var IUser */ - private $user; - public function __construct( - array $principalInfo, - ITrashManager $trashManager, - IUser $user + private array $principalInfo, + private ITrashManager $trashManager, + private IUser $user, ) { - $this->principalInfo = $principalInfo; - $this->trashManager = $trashManager; - $this->user = $user; } public function delete() { diff --git a/apps/files_trashbin/lib/Sabre/TrashRoot.php b/apps/files_trashbin/lib/Sabre/TrashRoot.php index ff2ea3aaa02..dd89583d9a1 100644 --- a/apps/files_trashbin/lib/Sabre/TrashRoot.php +++ b/apps/files_trashbin/lib/Sabre/TrashRoot.php @@ -8,8 +8,10 @@ declare(strict_types=1); */ namespace OCA\Files_Trashbin\Sabre; +use OCA\Files_Trashbin\Service\ConfigService; use OCA\Files_Trashbin\Trash\ITrashItem; use OCA\Files_Trashbin\Trash\ITrashManager; +use OCA\Files_Trashbin\Trashbin; use OCP\Files\FileInfo; use OCP\IUser; use Sabre\DAV\Exception\Forbidden; @@ -18,19 +20,18 @@ use Sabre\DAV\ICollection; class TrashRoot implements ICollection { - /** @var IUser */ - private $user; - - /** @var ITrashManager */ - private $trashManager; - - public function __construct(IUser $user, ITrashManager $trashManager) { - $this->user = $user; - $this->trashManager = $trashManager; + public function __construct( + private IUser $user, + private ITrashManager $trashManager, + ) { } public function delete() { - \OCA\Files_Trashbin\Trashbin::deleteAll(); + if (!ConfigService::getDeleteFromTrashEnabled()) { + throw new Forbidden('Not allowed to delete items from the trash bin'); + } + + Trashbin::deleteAll(); foreach ($this->trashManager->listTrashRoot($this->user) as $trashItem) { $this->trashManager->removeItem($trashItem); } diff --git a/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php b/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php index ffa6aa6ae6d..54bb1326966 100644 --- a/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php +++ b/apps/files_trashbin/lib/Sabre/TrashbinPlugin.php @@ -8,8 +8,12 @@ declare(strict_types=1); */ namespace OCA\Files_Trashbin\Sabre; +use OC\Files\FileInfo; +use OC\Files\View; use OCA\DAV\Connector\Sabre\FilesPlugin; +use OCA\Files_Trashbin\Trash\ITrashItem; use OCP\IPreview; +use Psr\Log\LoggerInterface; use Sabre\DAV\INode; use Sabre\DAV\PropFind; use Sabre\DAV\Server; @@ -24,17 +28,15 @@ class TrashbinPlugin extends ServerPlugin { public const TRASHBIN_TITLE = '{http://nextcloud.org/ns}trashbin-title'; public const TRASHBIN_DELETED_BY_ID = '{http://nextcloud.org/ns}trashbin-deleted-by-id'; public const TRASHBIN_DELETED_BY_DISPLAY_NAME = '{http://nextcloud.org/ns}trashbin-deleted-by-display-name'; + public const TRASHBIN_BACKEND = '{http://nextcloud.org/ns}trashbin-backend'; /** @var Server */ private $server; - /** @var IPreview */ - private $previewManager; - public function __construct( - IPreview $previewManager + private IPreview $previewManager, + private View $view, ) { - $this->previewManager = $previewManager; } public function initialize(Server $server) { @@ -42,6 +44,7 @@ class TrashbinPlugin extends ServerPlugin { $this->server->on('propFind', [$this, 'propFind']); $this->server->on('afterMethod:GET', [$this,'httpGet']); + $this->server->on('beforeMove', [$this, 'beforeMove']); } @@ -74,6 +77,11 @@ class TrashbinPlugin extends ServerPlugin { return $node->getDeletedBy()?->getDisplayName(); }); + // Pass the real filename as the DAV display name + $propFind->handle(FilesPlugin::DISPLAYNAME_PROPERTYNAME, function () use ($node) { + return $node->getFilename(); + }); + $propFind->handle(FilesPlugin::SIZE_PROPERTYNAME, function () use ($node) { return $node->getSize(); }); @@ -96,13 +104,21 @@ class TrashbinPlugin extends ServerPlugin { return $node->getFileId(); }); - $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { - return $this->previewManager->isAvailable($node->getFileInfo()); + $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node): string { + return $this->previewManager->isAvailable($node->getFileInfo()) ? 'true' : 'false'; }); $propFind->handle(FilesPlugin::MOUNT_TYPE_PROPERTYNAME, function () { return ''; }); + + $propFind->handle(self::TRASHBIN_BACKEND, function () use ($node) { + $fileInfo = $node->getFileInfo(); + if (!($fileInfo instanceof ITrashItem)) { + return ''; + } + return $fileInfo->getTrashBackend()::class; + }); } /** @@ -118,4 +134,47 @@ class TrashbinPlugin extends ServerPlugin { $response->addHeader('Content-Disposition', 'attachment; filename="' . $node->getFilename() . '"'); } } + + /** + * Check if a user has available space before attempting to + * restore from trashbin unless they have unlimited quota. + * + * @param string $sourcePath + * @param string $destinationPath + * @return bool + */ + public function beforeMove(string $sourcePath, string $destinationPath): bool { + try { + $node = $this->server->tree->getNodeForPath($sourcePath); + $destinationNodeParent = $this->server->tree->getNodeForPath(dirname($destinationPath)); + } catch (\Sabre\DAV\Exception $e) { + \OCP\Server::get(LoggerInterface::class) + ->error($e->getMessage(), ['app' => 'files_trashbin', 'exception' => $e]); + return true; + } + + // Check if a file is being restored before proceeding + if (!$node instanceof ITrash || !$destinationNodeParent instanceof RestoreFolder) { + return true; + } + + $fileInfo = $node->getFileInfo(); + if (!$fileInfo instanceof ITrashItem) { + return true; + } + $restoreFolder = dirname($fileInfo->getOriginalLocation()); + $freeSpace = $this->view->free_space($restoreFolder); + if ($freeSpace === FileInfo::SPACE_NOT_COMPUTED + || $freeSpace === FileInfo::SPACE_UNKNOWN + || $freeSpace === FileInfo::SPACE_UNLIMITED) { + return true; + } + $filesize = $fileInfo->getSize(); + if ($freeSpace < $filesize) { + $this->server->httpResponse->setStatus(507); + return false; + } + + return true; + } } |