|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright 2024 Christopher Ng <[email protected]> |
| 7 | + * |
| 8 | + * @author Christopher Ng <[email protected]> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace OCA\Files_DownloadLimit\Dav; |
| 28 | + |
| 29 | +use OCA\DAV\Connector\Sabre\File as SabreFile; |
| 30 | +use OCA\Files_DownloadLimit\Db\Limit; |
| 31 | +use OCA\Files_DownloadLimit\Db\LimitMapper; |
| 32 | +use OCA\Files_DownloadLimit\LimitList; |
| 33 | +use OCP\AppFramework\Db\DoesNotExistException; |
| 34 | +use OCP\Files\File; |
| 35 | +use OCP\IUser; |
| 36 | +use OCP\IUserSession; |
| 37 | +use OCP\Share\IManager as IShareManager; |
| 38 | +use OCP\Share\IShare; |
| 39 | +use Psr\Log\LoggerInterface; |
| 40 | +use Sabre\DAV\INode; |
| 41 | +use Sabre\DAV\PropFind; |
| 42 | +use Sabre\DAV\Server; |
| 43 | +use Sabre\DAV\ServerPlugin; |
| 44 | + |
| 45 | +class PropFindPlugin extends ServerPlugin { |
| 46 | + public const DOWNLOAD_LIMITS_PROPERTY = '{http://nextcloud.org/ns}share-download-limits'; |
| 47 | + |
| 48 | + public function __construct( |
| 49 | + private IShareManager $shareManager, |
| 50 | + private IUserSession $userSession, |
| 51 | + private LimitMapper $limitMapper, |
| 52 | + private LoggerInterface $logger, |
| 53 | + ) { |
| 54 | + } |
| 55 | + |
| 56 | + public function initialize(Server $server): void { |
| 57 | + $server->on('propFind', [$this, 'propFind']); |
| 58 | + } |
| 59 | + |
| 60 | + public function propFind(PropFind $propFind, INode $node) { |
| 61 | + if (!in_array(static::DOWNLOAD_LIMITS_PROPERTY, $propFind->getRequestedProperties(), true)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (!($node instanceof SabreFile)) { // Only allowed on files |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $propFind->handle( |
| 70 | + static::DOWNLOAD_LIMITS_PROPERTY, |
| 71 | + function () use ($node) { |
| 72 | + $user = $this->userSession->getUser(); |
| 73 | + if (!($user instanceof IUser)) { |
| 74 | + return new LimitList([]); |
| 75 | + } |
| 76 | + |
| 77 | + $externalShares = $this->getSharesForTypes( |
| 78 | + $user, |
| 79 | + $node->getNode(), |
| 80 | + [ |
| 81 | + IShare::TYPE_LINK, |
| 82 | + IShare::TYPE_EMAIL, |
| 83 | + ], |
| 84 | + ); |
| 85 | + |
| 86 | + /** @var Limit[] $limits */ |
| 87 | + $limits = array_values(array_filter(array_map(function (IShare $share) { |
| 88 | + try { |
| 89 | + return $this->limitMapper->get($share->getToken()); |
| 90 | + } catch (DoesNotExistException $e) { |
| 91 | + // Allow as no limit has been set |
| 92 | + return null; |
| 93 | + } |
| 94 | + }, $externalShares))); |
| 95 | + |
| 96 | + return new LimitList($limits); |
| 97 | + }, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param int[] $types |
| 103 | + * @return IShare[] |
| 104 | + */ |
| 105 | + private function getSharesForTypes(IUser $user, File $file, array $types) { |
| 106 | + /** @var IShare[] $shares */ |
| 107 | + $shares = []; |
| 108 | + foreach ($types as $type) { |
| 109 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($user->getUID(), $type, $file, false, -1, 0)); |
| 110 | + } |
| 111 | + return $shares; |
| 112 | + } |
| 113 | +} |
0 commit comments