Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable22] Emit event on link share action #29732

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/files_sharing/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'OCA\\Files_Sharing\\Controller\\ShareesAPIController' => $baseDir . '/../lib/Controller/ShareesAPIController.php',
'OCA\\Files_Sharing\\DeleteOrphanedSharesJob' => $baseDir . '/../lib/DeleteOrphanedSharesJob.php',
'OCA\\Files_Sharing\\Event\\BeforeTemplateRenderedEvent' => $baseDir . '/../lib/Event/BeforeTemplateRenderedEvent.php',
'OCA\\Files_Sharing\\Event\\ShareLinkAccessedEvent' => $baseDir . '/../lib/Event/ShareLinkAccessedEvent.php',
'OCA\\Files_Sharing\\Exceptions\\BrokenPath' => $baseDir . '/../lib/Exceptions/BrokenPath.php',
'OCA\\Files_Sharing\\Exceptions\\S2SException' => $baseDir . '/../lib/Exceptions/S2SException.php',
'OCA\\Files_Sharing\\Exceptions\\SharingRightsException' => $baseDir . '/../lib/Exceptions/SharingRightsException.php',
Expand Down
1 change: 1 addition & 0 deletions apps/files_sharing/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ComposerStaticInitFiles_Sharing
'OCA\\Files_Sharing\\Controller\\ShareesAPIController' => __DIR__ . '/..' . '/../lib/Controller/ShareesAPIController.php',
'OCA\\Files_Sharing\\DeleteOrphanedSharesJob' => __DIR__ . '/..' . '/../lib/DeleteOrphanedSharesJob.php',
'OCA\\Files_Sharing\\Event\\BeforeTemplateRenderedEvent' => __DIR__ . '/..' . '/../lib/Event/BeforeTemplateRenderedEvent.php',
'OCA\\Files_Sharing\\Event\\ShareLinkAccessedEvent' => __DIR__ . '/..' . '/../lib/Event/ShareLinkAccessedEvent.php',
'OCA\\Files_Sharing\\Exceptions\\BrokenPath' => __DIR__ . '/..' . '/../lib/Exceptions/BrokenPath.php',
'OCA\\Files_Sharing\\Exceptions\\S2SException' => __DIR__ . '/..' . '/../lib/Exceptions/S2SException.php',
'OCA\\Files_Sharing\\Exceptions\\SharingRightsException' => __DIR__ . '/..' . '/../lib/Exceptions/SharingRightsException.php',
Expand Down
34 changes: 32 additions & 2 deletions apps/files_sharing/lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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;
Expand Down Expand Up @@ -162,6 +163,10 @@ public function __construct(string $appName,
$this->shareManager = $shareManager;
}

public const SHARE_ACCESS = 'access';
public const SHARE_AUTH = 'auth';
public const SHARE_DOWNLOAD = 'download';

/**
* @PublicPage
* @NoCSRFRequired
Expand Down Expand Up @@ -233,6 +238,7 @@ protected function authSucceeded() {

protected function authFailed() {
$this->emitAccessShareHook($this->share, 403, 'Wrong password');
$this->emitShareAccessEvent($this->share, self::SHARE_AUTH, 403, 'Wrong password');
}

/**
Expand All @@ -242,10 +248,13 @@ protected function authFailed() {
* otherwise token
* @param int $errorCode
* @param string $errorMessage
*
* @throws \OC\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;
Expand All @@ -260,19 +269,33 @@ protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage =
$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
*
Expand Down Expand Up @@ -312,6 +335,7 @@ public function showShare($path = ''): TemplateResponse {
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();
}
Expand All @@ -326,10 +350,12 @@ public function showShare($path = ''): TemplateResponse {
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;
}

Expand Down Expand Up @@ -371,6 +397,7 @@ public function showShare($path = ''): TemplateResponse {
$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();
}

Expand Down Expand Up @@ -534,6 +561,7 @@ public function showShare($path = ''): TemplateResponse {
$response->setContentSecurityPolicy($csp);

$this->emitAccessShareHook($share);
$this->emitShareAccessEvent($share, self::SHARE_ACCESS);

return $response;
}
Expand Down Expand Up @@ -596,6 +624,7 @@ public function downloadShare($token, $files = null, $path = '', $downloadStartS
$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();
}
}
Expand Down Expand Up @@ -637,6 +666,7 @@ public function downloadShare($token, $files = null, $path = '', $downloadStartS
}

$this->emitAccessShareHook($share);
$this->emitShareAccessEvent($share, self::SHARE_DOWNLOAD);

$server_params = [ 'head' => $this->request->getMethod() === 'HEAD' ];

Expand Down
68 changes: 68 additions & 0 deletions apps/files_sharing/lib/Event/ShareLinkAccessedEvent.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 3 additions & 3 deletions apps/files_sharing/tests/Controller/ShareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public function testShowShare() {
return null;
});

$this->eventDispatcher->expects($this->once())
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatchTyped')
->with(
$this->callback(function ($event) use ($share) {
Expand Down Expand Up @@ -450,7 +450,7 @@ public function testShowShareWithPrivateName() {
return null;
});

$this->eventDispatcher->expects($this->once())
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatchTyped')
->with(
$this->callback(function ($event) use ($share) {
Expand Down Expand Up @@ -605,7 +605,7 @@ public function testShowShareHideDownload() {
return null;
});

$this->eventDispatcher->expects($this->once())
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatchTyped')
->with(
$this->callback(function ($event) use ($share) {
Expand Down