diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2021-09-10 09:31:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-10 09:31:18 +0200 |
commit | 90b47ab7f0aeb1af91a4639d9f863c1eb2d0ca2b (patch) | |
tree | 4ddc11ebe66a7ccbb045cecd2a3dff9d0b94b84c /apps/files_sharing/lib | |
parent | b0bf898e18b4a7be973442d9ec6761c3457d928b (diff) | |
parent | c56c317d82e6015739549a606578e963578f4de6 (diff) | |
download | nextcloud-server-90b47ab7f0aeb1af91a4639d9f863c1eb2d0ca2b.tar.gz nextcloud-server-90b47ab7f0aeb1af91a4639d9f863c1eb2d0ca2b.zip |
Merge pull request #28764 from nextcloud/feat/sharing-link-event
Diffstat (limited to 'apps/files_sharing/lib')
-rw-r--r-- | apps/files_sharing/lib/Controller/ShareController.php | 34 | ||||
-rw-r--r-- | apps/files_sharing/lib/Event/ShareLinkAccessedEvent.php | 68 |
2 files changed, 100 insertions, 2 deletions
diff --git a/apps/files_sharing/lib/Controller/ShareController.php b/apps/files_sharing/lib/Controller/ShareController.php index 95c3303ae74..614dae7ffba 100644 --- a/apps/files_sharing/lib/Controller/ShareController.php +++ b/apps/files_sharing/lib/Controller/ShareController.php @@ -48,6 +48,7 @@ use OC_Util; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\Files_Sharing\Activity\Providers\Downloads; use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; +use OCA\Files_Sharing\Event\ShareLinkAccessedEvent; use OCA\Viewer\Event\LoadViewer; use OCP\Accounts\IAccountManager; use OCP\AppFramework\AuthPublicShareController; @@ -162,6 +163,10 @@ class ShareController extends AuthPublicShareController { $this->shareManager = $shareManager; } + public const SHARE_ACCESS = 'access'; + public const SHARE_AUTH = 'auth'; + public const SHARE_DOWNLOAD = 'download'; + /** * @PublicPage * @NoCSRFRequired @@ -233,6 +238,7 @@ class ShareController extends AuthPublicShareController { protected function authFailed() { $this->emitAccessShareHook($this->share, 403, 'Wrong password'); + $this->emitShareAccessEvent($this->share, self::SHARE_AUTH, 403, 'Wrong password'); } /** @@ -242,10 +248,13 @@ class ShareController extends AuthPublicShareController { * otherwise token * @param int $errorCode * @param string $errorMessage + * * @throws \OCP\HintException * @throws \OC\ServerNotAvailableException + * + * @deprecated use OCP\Files_Sharing\Event\ShareLinkAccessedEvent */ - protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { + protected function emitAccessShareHook($share, int $errorCode = 200, string $errorMessage = '') { $itemType = $itemSource = $uidOwner = ''; $token = $share; $exception = null; @@ -260,20 +269,34 @@ class ShareController extends AuthPublicShareController { $exception = $e; } } + \OC_Hook::emit(Share::class, 'share_link_access', [ 'itemType' => $itemType, 'itemSource' => $itemSource, 'uidOwner' => $uidOwner, 'token' => $token, 'errorCode' => $errorCode, - 'errorMessage' => $errorMessage, + 'errorMessage' => $errorMessage ]); + if (!is_null($exception)) { throw $exception; } } /** + * Emit a ShareLinkAccessedEvent event when a share is accessed, downloaded, auth... + */ + protected function emitShareAccessEvent(IShare $share, string $step = '', int $errorCode = 200, string $errorMessage = ''): void { + if ($step !== self::SHARE_ACCESS && + $step !== self::SHARE_AUTH && + $step !== self::SHARE_DOWNLOAD) { + return; + } + $this->eventDispatcher->dispatchTyped(new ShareLinkAccessedEvent($share, $step, $errorCode, $errorMessage)); + } + + /** * Validate the permissions of the share * * @param Share\IShare $share @@ -312,6 +335,7 @@ class ShareController extends AuthPublicShareController { try { $share = $this->shareManager->getShareByToken($this->getToken()); } catch (ShareNotFound $e) { + // The share does not exists, we do not emit an ShareLinkAccessedEvent $this->emitAccessShareHook($this->getToken(), 404, 'Share not found'); throw new NotFoundException(); } @@ -326,10 +350,12 @@ class ShareController extends AuthPublicShareController { try { if ($shareNode instanceof \OCP\Files\File && $path !== '') { $this->emitAccessShareHook($share, 404, 'Share not found'); + $this->emitShareAccessEvent($share, self::SHARE_ACCESS, 404, 'Share not found'); throw new NotFoundException(); } } catch (\Exception $e) { $this->emitAccessShareHook($share, 404, 'Share not found'); + $this->emitShareAccessEvent($share, self::SHARE_ACCESS, 404, 'Share not found'); throw $e; } @@ -371,6 +397,7 @@ class ShareController extends AuthPublicShareController { $folderNode = $shareNode->get($path); } catch (\OCP\Files\NotFoundException $e) { $this->emitAccessShareHook($share, 404, 'Share not found'); + $this->emitShareAccessEvent($share, self::SHARE_ACCESS, 404, 'Share not found'); throw new NotFoundException(); } @@ -534,6 +561,7 @@ class ShareController extends AuthPublicShareController { $response->setContentSecurityPolicy($csp); $this->emitAccessShareHook($share); + $this->emitShareAccessEvent($share, self::SHARE_ACCESS); return $response; } @@ -596,6 +624,7 @@ class ShareController extends AuthPublicShareController { $node = $node->get($path); } catch (NotFoundException $e) { $this->emitAccessShareHook($share, 404, 'Share not found'); + $this->emitShareAccessEvent($share, self::SHARE_DOWNLOAD, 404, 'Share not found'); return new NotFoundResponse(); } } @@ -637,6 +666,7 @@ class ShareController extends AuthPublicShareController { } $this->emitAccessShareHook($share); + $this->emitShareAccessEvent($share, self::SHARE_DOWNLOAD); $server_params = [ 'head' => $this->request->getMethod() === 'HEAD' ]; diff --git a/apps/files_sharing/lib/Event/ShareLinkAccessedEvent.php b/apps/files_sharing/lib/Event/ShareLinkAccessedEvent.php new file mode 100644 index 00000000000..490ada1eef2 --- /dev/null +++ b/apps/files_sharing/lib/Event/ShareLinkAccessedEvent.php @@ -0,0 +1,68 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Files_Sharing\Event; + +use OCP\EventDispatcher\Event; +use OCP\Share\IShare; + +class ShareLinkAccessedEvent extends Event { + /** @var IShare */ + private $share; + + /** @var string */ + private $step; + + /** @var int */ + private $errorCode; + + /** @var string */ + private $errorMessage; + + public function __construct(IShare $share, string $step = '', int $errorCode = 200, string $errorMessage = '') { + parent::__construct(); + $this->share = $share; + $this->step = $step; + $this->errorCode = $errorCode; + $this->errorMessage = $errorMessage; + } + + public function getShare(): IShare { + return $this->share; + } + + public function getStep(): string { + return $this->step; + } + + public function getErrorCode(): int { + return $this->errorCode; + } + + public function getErrorMessage(): string { + return $this->errorMessage; + } +} |