Skip to content

Commit

Permalink
Merge pull request #1153 from nextcloud/bugfix/noid/share-error
Browse files Browse the repository at this point in the history
Avoid throwing on other share types than user
  • Loading branch information
juliushaertl committed Nov 10, 2023
2 parents a6aaf32 + 1b9e787 commit f282151
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public function register(IRegistrationContext $context): void {
);
} else {
// FIXME: Remove once Nextcloud 28 is the minimum supported version
\OCP\Server::get(IEventDispatcher::class)->addListener('OCP\Share::preShare', function (GenericEvent $event) {
\OCP\Server::get(IEventDispatcher::class)->addListener('OCP\Share::preShare', function ($event) {
if (!$event instanceof GenericEvent) {
return;
}

/** @var IShare $share */
$share = $event->getSubject();

Check warning on line 46 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Call to deprecated function \OCP\EventDispatcher\GenericEvent::getSubject() defined at vendor/nextcloud/ocp/OCP/EventDispatcher/GenericEvent.php:70

Check warning on line 46 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Call to deprecated function \OCP\EventDispatcher\GenericEvent::getSubject() defined at vendor/nextcloud/ocp/OCP/EventDispatcher/GenericEvent.php:70

Expand Down
43 changes: 28 additions & 15 deletions lib/AppInfo/BeforeShareCreatedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
use OCP\Files\File;
use OCP\Share\Events\BeforeShareCreatedEvent;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

class BeforeShareCreatedListener implements IEventListener {
private SettingsService $settings;
private NoteUtil $noteUtil;

public function __construct(SettingsService $settings, NoteUtil $noteUtil) {
public function __construct(SettingsService $settings, NoteUtil $noteUtil, LoggerInterface $logger) {
$this->settings = $settings;
$this->noteUtil = $noteUtil;
$this->logger = $logger;

Check warning on line 23 in lib/AppInfo/BeforeShareCreatedListener.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Reference to undeclared property \OCA\Notes\AppInfo\BeforeShareCreatedListener->logger (Did you mean $logger)

Check warning on line 23 in lib/AppInfo/BeforeShareCreatedListener.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Reference to undeclared property \OCA\Notes\AppInfo\BeforeShareCreatedListener->logger (Did you mean $logger)
}

public function handle(Event $event): void {
Expand All @@ -31,22 +33,33 @@ public function handle(Event $event): void {

public function overwriteShareTarget(IShare $share): void {
$itemType = $share->getNode() instanceof File ? 'file' : 'folder';
$fileSourcePath = $share->getNode()->getPath();
$itemTarget = $share->getTarget();
$uidOwner = $share->getSharedBy();
$ownerPath = $this->noteUtil->getRoot()->getUserFolder($uidOwner)->getPath();
$ownerNotesPath = $ownerPath . '/' . $this->settings->get($uidOwner, 'notesPath');

$receiver = $share->getSharedWith();
$receiverPath = $this->noteUtil->getRoot()->getUserFolder($receiver)->getPath();
$receiverNotesInternalPath = $this->settings->get($receiver, 'notesPath');
$receiverNotesPath = $receiverPath . '/' . $receiverNotesInternalPath;
$this->noteUtil->getOrCreateFolder($receiverNotesPath);

if ($itemType !== 'file' || strpos($fileSourcePath, $ownerNotesPath) !== 0) {

if ($share->getShareType() !== IShare::TYPE_USER) {
return;
}

$share->setTarget('/' . $receiverNotesInternalPath . $itemTarget);
try {
$fileSourcePath = $share->getNode()->getPath();
$itemTarget = $share->getTarget();
$uidOwner = $share->getSharedBy();
$ownerPath = $this->noteUtil->getRoot()->getUserFolder($uidOwner)->getPath();
$ownerNotesPath = $ownerPath . '/' . $this->settings->get($uidOwner, 'notesPath');

$receiver = $share->getSharedWith();
$receiverPath = $this->noteUtil->getRoot()->getUserFolder($receiver)->getPath();
$receiverNotesInternalPath = $this->settings->get($receiver, 'notesPath');
$receiverNotesPath = $receiverPath . '/' . $receiverNotesInternalPath;
$this->noteUtil->getOrCreateFolder($receiverNotesPath);

if ($itemType !== 'file' || strpos($fileSourcePath, $ownerNotesPath) !== 0) {
return;
}

$share->setTarget('/' . $receiverNotesInternalPath . $itemTarget);
} catch (\Throwable $e) {
$this->logger->error('Failed to overwrite share target for notes', [

Check warning on line 60 in lib/AppInfo/BeforeShareCreatedListener.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Reference to undeclared property \OCA\Notes\AppInfo\BeforeShareCreatedListener->logger
'exception' => $e,
]);
}
}
}

0 comments on commit f282151

Please sign in to comment.