diff options
-rw-r--r-- | apps/files/lib/Collaboration/Resources/Listener.php | 3 | ||||
-rw-r--r-- | apps/files_sharing/lib/AppInfo/Application.php | 5 | ||||
-rw-r--r-- | apps/files_sharing/lib/Notification/Listener.php | 9 | ||||
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/Share20/LegacyHooks.php | 26 | ||||
-rw-r--r-- | lib/private/Share20/Manager.php | 14 | ||||
-rw-r--r-- | lib/public/Share/Events/BeforeShareCreatedEvent.php | 66 | ||||
-rw-r--r-- | tests/lib/Share20/LegacyHooksTest.php | 21 | ||||
-rw-r--r-- | tests/lib/Share20/ManagerTest.php | 61 |
10 files changed, 133 insertions, 74 deletions
diff --git a/apps/files/lib/Collaboration/Resources/Listener.php b/apps/files/lib/Collaboration/Resources/Listener.php index 28ce5f87897..8de814fd0ea 100644 --- a/apps/files/lib/Collaboration/Resources/Listener.php +++ b/apps/files/lib/Collaboration/Resources/Listener.php @@ -28,10 +28,11 @@ namespace OCA\Files\Collaboration\Resources; use OCP\EventDispatcher\IEventDispatcher; use OCP\Server; use OCP\Collaboration\Resources\IManager; +use OCP\Share\Events\ShareCreatedEvent; class Listener { public static function register(IEventDispatcher $dispatcher): void { - $dispatcher->addListener('OCP\Share::postShare', [self::class, 'shareModification']); + $dispatcher->addListener(ShareCreatedEvent::class, [self::class, 'shareModification']); $dispatcher->addListener('OCP\Share::postUnshare', [self::class, 'shareModification']); $dispatcher->addListener('OCP\Share::postUnshareFromSelf', [self::class, 'shareModification']); } diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 65197e9b270..8f5fe7aca5b 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -143,10 +143,7 @@ class Application extends App implements IBootstrap { }); // notifications api to accept incoming user shares - $dispatcher->addListener('OCP\Share::postShare', function ($event) { - if (!$event instanceof OldGenericEvent) { - return; - } + $dispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) { /** @var Listener $listener */ $listener = $this->getContainer()->query(Listener::class); $listener->shareNotification($event); diff --git a/apps/files_sharing/lib/Notification/Listener.php b/apps/files_sharing/lib/Notification/Listener.php index db7939767d6..b6d30cdb3f5 100644 --- a/apps/files_sharing/lib/Notification/Listener.php +++ b/apps/files_sharing/lib/Notification/Listener.php @@ -31,6 +31,7 @@ use OCP\IGroupManager; use OCP\IUser; use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; +use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\IManager as IShareManager; use OCP\Share\IShare; use Symfony\Component\EventDispatcher\GenericEvent; @@ -54,12 +55,8 @@ class Listener { $this->groupManager = $groupManager; } - /** - * @param GenericEvent $event - */ - public function shareNotification(GenericEvent $event): void { - /** @var IShare $share */ - $share = $event->getSubject(); + public function shareNotification(ShareCreatedEvent $event): void { + $share = $event->getShare(); $notification = $this->instantiateNotification($share); if ($share->getShareType() === IShare::TYPE_USER) { diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 1093b8a2586..451565d8e17 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -585,6 +585,7 @@ return array( 'OCP\\Settings\\ISettings' => $baseDir . '/lib/public/Settings/ISettings.php', 'OCP\\Settings\\ISubAdminSettings' => $baseDir . '/lib/public/Settings/ISubAdminSettings.php', 'OCP\\Share' => $baseDir . '/lib/public/Share.php', + 'OCP\\Share\\Events\\BeforeShareCreatedEvent' => $baseDir . '/lib/public/Share/Events/BeforeShareCreatedEvent.php', 'OCP\\Share\\Events\\ShareCreatedEvent' => $baseDir . '/lib/public/Share/Events/ShareCreatedEvent.php', 'OCP\\Share\\Events\\ShareDeletedEvent' => $baseDir . '/lib/public/Share/Events/ShareDeletedEvent.php', 'OCP\\Share\\Events\\VerifyMountPointEvent' => $baseDir . '/lib/public/Share/Events/VerifyMountPointEvent.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 4a74dda6fab..0d19d59a6c7 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -618,6 +618,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Settings\\ISettings' => __DIR__ . '/../../..' . '/lib/public/Settings/ISettings.php', 'OCP\\Settings\\ISubAdminSettings' => __DIR__ . '/../../..' . '/lib/public/Settings/ISubAdminSettings.php', 'OCP\\Share' => __DIR__ . '/../../..' . '/lib/public/Share.php', + 'OCP\\Share\\Events\\BeforeShareCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Share/Events/BeforeShareCreatedEvent.php', 'OCP\\Share\\Events\\ShareCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Share/Events/ShareCreatedEvent.php', 'OCP\\Share\\Events\\ShareDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Share/Events/ShareDeletedEvent.php', 'OCP\\Share\\Events\\VerifyMountPointEvent' => __DIR__ . '/../../..' . '/lib/public/Share/Events/VerifyMountPointEvent.php', diff --git a/lib/private/Share20/LegacyHooks.php b/lib/private/Share20/LegacyHooks.php index 688b2273384..1146d72a568 100644 --- a/lib/private/Share20/LegacyHooks.php +++ b/lib/private/Share20/LegacyHooks.php @@ -29,6 +29,8 @@ namespace OC\Share20; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; use OCP\Share; +use OCP\Share\Events\BeforeShareCreatedEvent; +use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\IShare; use Symfony\Component\EventDispatcher\GenericEvent; @@ -54,15 +56,11 @@ class LegacyHooks { $this->postUnshareFromSelf($event); } }); - $this->eventDispatcher->addListener('OCP\Share::preShare', function ($event) { - if ($event instanceof GenericEvent) { - $this->preShare($event); - } + $this->eventDispatcher->addListener(BeforeShareCreatedEvent::class, function (BeforeShareCreatedEvent $event) { + $this->preShare($event); }); - $this->eventDispatcher->addListener('OCP\Share::postShare', function ($event) { - if ($event instanceof GenericEvent) { - $this->postShare($event); - } + $this->eventDispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) { + $this->postShare($event); }); } @@ -127,9 +125,8 @@ class LegacyHooks { return $hookParams; } - public function preShare(GenericEvent $e) { - /** @var IShare $share */ - $share = $e->getSubject(); + public function preShare(BeforeShareCreatedEvent $e) { + $share = $e->getShare(); // Pre share hook $run = true; @@ -151,16 +148,15 @@ class LegacyHooks { \OC_Hook::emit(Share::class, 'pre_shared', $preHookData); if ($run === false) { - $e->setArgument('error', $error); + $e->setError($error); $e->stopPropagation(); } return $e; } - public function postShare(GenericEvent $e) { - /** @var IShare $share */ - $share = $e->getSubject(); + public function postShare(ShareCreatedEvent $e) { + $share = $e->getShare(); $postHookData = [ 'itemType' => $share->getNode() instanceof File ? 'file' : 'folder', diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index 3f5dbd7cd28..9799d145a48 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -67,6 +67,7 @@ use OCP\Security\Events\ValidatePasswordPolicyEvent; use OCP\Security\IHasher; use OCP\Security\ISecureRandom; use OCP\Share; +use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\Exceptions\AlreadySharedException; use OCP\Share\Exceptions\GenericShareException; use OCP\Share\Exceptions\ShareNotFound; @@ -806,10 +807,10 @@ class Manager implements IManager { $share->setTarget($target); // Pre share event - $event = new GenericEvent($share); - $this->legacyDispatcher->dispatch('OCP\Share::preShare', $event); - if ($event->isPropagationStopped() && $event->hasArgument('error')) { - throw new \Exception($event->getArgument('error')); + $event = new Share\Events\BeforeShareCreatedEvent($share); + $this->dispatcher->dispatchTyped($event); + if ($event->isPropagationStopped() && $event->getError()) { + throw new \Exception($event->getError()); } $oldShare = $share; @@ -833,10 +834,7 @@ class Manager implements IManager { } // Post share event - $event = new GenericEvent($share); - $this->legacyDispatcher->dispatch('OCP\Share::postShare', $event); - - $this->dispatcher->dispatchTyped(new Share\Events\ShareCreatedEvent($share)); + $this->dispatcher->dispatchTyped(new ShareCreatedEvent($share)); if ($this->config->getSystemValueBool('sharing.enable_share_mail', true) && $share->getShareType() === IShare::TYPE_USER) { diff --git a/lib/public/Share/Events/BeforeShareCreatedEvent.php b/lib/public/Share/Events/BeforeShareCreatedEvent.php new file mode 100644 index 00000000000..f69049b3e8d --- /dev/null +++ b/lib/public/Share/Events/BeforeShareCreatedEvent.php @@ -0,0 +1,66 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.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 OCP\Share\Events; + +use OCP\EventDispatcher\Event; +use OCP\Share\IShare; + +/** + * @since 28.0.0 + */ +class BeforeShareCreatedEvent extends Event { + private ?string $error = null; + + /** + * @since 28.0.0 + */ + public function __construct( + private IShare $share, + ) { + parent::__construct(); + } + + /** + * @since 28.0.0 + */ + public function getShare(): IShare { + return $this->share; + } + + /** + * @since 28.0.0 + */ + public function setError(string $error): void { + $this->error = $error; + } + + /** + * @since 28.0.0 + */ + public function getError(): ?string { + return $this->error; + } +} diff --git a/tests/lib/Share20/LegacyHooksTest.php b/tests/lib/Share20/LegacyHooksTest.php index 54d8e0be725..596fba7bfad 100644 --- a/tests/lib/Share20/LegacyHooksTest.php +++ b/tests/lib/Share20/LegacyHooksTest.php @@ -31,6 +31,8 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\ICacheEntry; use OCP\Files\File; use OCP\IServerContainer; +use OCP\Share\Events\BeforeShareCreatedEvent; +use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\IShare; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -51,9 +53,8 @@ class LegacyHooksTest extends TestCase { $symfonyDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); $logger = $this->createMock(LoggerInterface::class); - $eventDispatcher = new \OC\EventDispatcher\EventDispatcher($symfonyDispatcher, \OC::$server->get(IServerContainer::class), $logger); - $this->eventDispatcher = new SymfonyAdapter($eventDispatcher, $logger); - $this->hooks = new LegacyHooks($eventDispatcher); + $this->eventDispatcher = new \OC\EventDispatcher\EventDispatcher($symfonyDispatcher, \OC::$server->get(IServerContainer::class), $logger); + $this->hooks = new LegacyHooks($this->eventDispatcher); $this->manager = \OC::$server->getShareManager(); } @@ -253,8 +254,8 @@ class LegacyHooksTest extends TestCase { ->method('preShare') ->with($expected); - $event = new GenericEvent($share); - $this->eventDispatcher->dispatch('OCP\Share::preShare', $event); + $event = new BeforeShareCreatedEvent($share); + $this->eventDispatcher->dispatchTyped($event); } public function testPreShareError() { @@ -305,11 +306,11 @@ class LegacyHooksTest extends TestCase { $data['error'] = 'I error'; }); - $event = new GenericEvent($share); - $this->eventDispatcher->dispatch('OCP\Share::preShare', $event); + $event = new BeforeShareCreatedEvent($share); + $this->eventDispatcher->dispatchTyped($event); $this->assertTrue($event->isPropagationStopped()); - $this->assertSame('I error', $event->getArgument('error')); + $this->assertSame('I error', $event->getError()); } public function testPostShare() { @@ -355,7 +356,7 @@ class LegacyHooksTest extends TestCase { ->method('postShare') ->with($expected); - $event = new GenericEvent($share); - $this->eventDispatcher->dispatch('OCP\Share::postShare', $event); + $event = new ShareCreatedEvent($share); + $this->eventDispatcher->dispatchTyped($event); } } diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index a23aa6023af..7a0c23ec2b4 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -51,6 +51,8 @@ use OCP\Mail\IMailer; use OCP\Security\Events\ValidatePasswordPolicyEvent; use OCP\Security\IHasher; use OCP\Security\ISecureRandom; +use OCP\Share\Events\BeforeShareCreatedEvent; +use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\Exceptions\AlreadySharedException; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager; @@ -2317,14 +2319,13 @@ class ManagerTest extends \Test\TestCase { return $share->setId(42); }); - $this->eventDispatcher->expects($this->exactly(2)) - ->method('dispatch') + $this->dispatcher->expects($this->exactly(2)) + ->method('dispatchTyped') ->withConsecutive( // Pre share - [$this->equalTo('OCP\Share::preShare'), - $this->callback(function (GenericEvent $e) use ($path, $date) { - /** @var IShare $share */ - $share = $e->getSubject(); + [ + $this->callback(function (BeforeShareCreatedEvent $e) use ($path, $date) { + $share = $e->getShare(); return $share->getShareType() === IShare::TYPE_LINK && $share->getNode() === $path && @@ -2333,12 +2334,12 @@ class ManagerTest extends \Test\TestCase { $share->getExpirationDate() === $date && $share->getPassword() === 'hashed' && $share->getToken() === 'token'; - })], + }) + ], // Post share - [$this->equalTo('OCP\Share::postShare'), - $this->callback(function (GenericEvent $e) use ($path, $date) { - /** @var IShare $share */ - $share = $e->getSubject(); + [ + $this->callback(function (ShareCreatedEvent $e) use ($path, $date) { + $share = $e->getShare(); return $share->getShareType() === IShare::TYPE_LINK && $share->getNode() === $path && @@ -2349,7 +2350,8 @@ class ManagerTest extends \Test\TestCase { $share->getToken() === 'token' && $share->getId() === '42' && $share->getTarget() === '/target'; - })] + }) + ] ); /** @var IShare $share */ @@ -2424,13 +2426,12 @@ class ManagerTest extends \Test\TestCase { return $share->setId(42); }); - $this->eventDispatcher->expects($this->exactly(2)) - ->method('dispatch') + $this->dispatcher->expects($this->exactly(2)) + ->method('dispatchTyped') ->withConsecutive( - [$this->equalTo('OCP\Share::preShare'), - $this->callback(function (GenericEvent $e) use ($path) { - /** @var IShare $share */ - $share = $e->getSubject(); + [ + $this->callback(function (BeforeShareCreatedEvent $e) use ($path) { + $share = $e->getShare(); return $share->getShareType() === IShare::TYPE_EMAIL && $share->getNode() === $path && @@ -2439,11 +2440,11 @@ class ManagerTest extends \Test\TestCase { $share->getExpirationDate() === null && $share->getPassword() === null && $share->getToken() === 'token'; - })], - [$this->equalTo('OCP\Share::postShare'), - $this->callback(function (GenericEvent $e) use ($path) { - /** @var IShare $share */ - $share = $e->getSubject(); + }) + ], + [ + $this->callback(function (ShareCreatedEvent $e) use ($path) { + $share = $e->getShare(); return $share->getShareType() === IShare::TYPE_EMAIL && $share->getNode() === $path && @@ -2454,7 +2455,8 @@ class ManagerTest extends \Test\TestCase { $share->getToken() === 'token' && $share->getId() === '42' && $share->getTarget() === '/target'; - })], + }) + ], ); /** @var IShare $share */ @@ -2521,13 +2523,12 @@ class ManagerTest extends \Test\TestCase { ->with('/target'); // Pre share - $this->eventDispatcher->expects($this->once()) - ->method('dispatch') + $this->dispatcher->expects($this->once()) + ->method('dispatchTyped') ->with( - $this->equalTo('OCP\Share::preShare'), - $this->isInstanceOf(GenericEvent::class) - )->willReturnCallback(function ($name, GenericEvent $e) { - $e->setArgument('error', 'I won\'t let you share!'); + $this->isInstanceOf(BeforeShareCreatedEvent::class) + )->willReturnCallback(function (BeforeShareCreatedEvent $e) { + $e->setError('I won\'t let you share!'); $e->stopPropagation(); } ); |