mirror of
https://github.com/nextcloud/server.git
synced 2026-02-20 00:12:30 -05:00
feat!: Migrate Sharing events to typed events
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
68fc9b48c2
commit
44b810bfbd
10 changed files with 133 additions and 74 deletions
|
|
@ -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']);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
66
lib/public/Share/Events/BeforeShareCreatedEvent.php
Normal file
66
lib/public/Share/Events/BeforeShareCreatedEvent.php
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue