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

[local] feat: Store user which has downloaded file in activity feed #46533

Closed
Closed
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
37 changes: 22 additions & 15 deletions apps/files_sharing/lib/Activity/Providers/Downloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) {

if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
$event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
if (!isset($parsedParameters['remote-address-hash']['type'])) {
$subject = $this->l->t('{file} downloaded via public link');
$this->setSubjects($event, $subject, $parsedParameters);
if (isset($parsedParameters['actor'])) {
$subject = $this->l->t('{file} downloaded via public link by {actor}');
} else {
$subject = $this->l->t('{file} downloaded via public link');
$this->setSubjects($event, $subject, $parsedParameters);
}

$this->setSubjects($event, $subject, $parsedParameters);
if (isset($parsedParameters['remote-address-hash']['type'])) {
$event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
}
} elseif ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
Expand Down Expand Up @@ -92,20 +94,25 @@ protected function getParsedParameters(IEvent $event) {
switch ($subject) {
case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
$parsedParameters = [
'file' => $this->getFile($parameters[0], $event),
];

if (isset($parameters[1])) {
return [
'file' => $this->getFile($parameters[0], $event),
'remote-address-hash' => [
'type' => 'highlight',
'id' => $parameters[1],
'name' => $parameters[1],
'link' => '',
],
$parsedParameters['remote-address-hash'] = [
'type' => 'highlight',
'id' => $parameters[1],
'name' => $parameters[1],
'link' => '',
];
}
return [
'file' => $this->getFile($parameters[0], $event),
];

if (isset($parameters[2])) {
$parsedParameters['actor'] = $this->getUser($parameters[2]);
}

return $parsedParameters;

case self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED:
case self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED:
return [
Expand Down
11 changes: 8 additions & 3 deletions apps/files_sharing/lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use OCP\Share;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IPublicShareTemplateFactory;
use OCP\Share\IShare;
use OCP\Template;

/**
* @package OCA\Files_Sharing\Controllers
*/
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class ShareController extends AuthPublicShareController {
protected ?Share\IShare $share = null;
private ?IUser $currentUser = null;

public const SHARE_ACCESS = 'access';
public const SHARE_AUTH = 'auth';
Expand All @@ -68,8 +70,11 @@
protected ISecureRandom $secureRandom,
protected Defaults $defaults,
private IPublicShareTemplateFactory $publicShareTemplateFactory,
IUserSession $userSession,
) {
parent::__construct($appName, $request, $session, $urlGenerator);

$this->currentUser = $userSession->getUser();
}

/**
Expand Down Expand Up @@ -428,7 +433,7 @@
&& !isset($downloadStartSecret[32])
&& preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) {
// FIXME: set on the response once we use an actual app framework response
setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/');

Check failure on line 436 in apps/files_sharing/lib/Controller/ShareController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedCookie

apps/files_sharing/lib/Controller/ShareController.php:436:35: TaintedCookie: Detected tainted cookie (see https://psalm.dev/257)
}

$this->emitAccessShareHook($share);
Expand Down Expand Up @@ -511,11 +516,11 @@
} else {
if ($node instanceof \OCP\Files\File) {
$subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED;
$parameters[] = $remoteAddressHash;
} else {
$subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED;
$parameters[] = $remoteAddressHash;
}
$parameters[] = $remoteAddressHash;
$parameters[] = $this->currentUser?->getUID();
}

$this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath);
Expand Down
6 changes: 6 additions & 0 deletions apps/files_sharing/tests/Controller/ShareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IPublicShareTemplateFactory;
Expand Down Expand Up @@ -84,6 +85,8 @@ class ShareControllerTest extends \Test\TestCase {
private $defaults;
/** @var IPublicShareTemplateFactory|MockObject */
private $publicShareTemplateFactory;
/** @var IUserSession|MockObject */
private $userSession;

protected function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -124,6 +127,8 @@ protected function setUp(): void {
)
);

$this->userSession = $this->createMock(IUserSession::class);

$this->shareController = new \OCA\Files_Sharing\Controller\ShareController(
$this->appName,
$this->createMock(IRequest::class),
Expand All @@ -142,6 +147,7 @@ protected function setUp(): void {
$this->secureRandom,
$this->defaults,
$this->publicShareTemplateFactory,
$this->userSession,
);


Expand Down
Loading