]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix!: Remove symfony EventDispatcherInterface from Group
authorJoas Schilling <coding@schilljs.com>
Wed, 26 Jul 2023 19:56:07 +0000 (21:56 +0200)
committerJoas Schilling <coding@schilljs.com>
Thu, 27 Jul 2023 21:13:37 +0000 (23:13 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Group/Group.php
lib/private/Group/Manager.php
lib/private/Server.php
tests/lib/Group/GroupTest.php
tests/lib/Group/ManagerTest.php

index efc21ad7c0dc01f5209b88624f627c430498e10a..441ee64604d5de4070948577dccd322cd8d8d268 100644 (file)
@@ -34,6 +34,13 @@ namespace OC\Group;
 
 use OC\Hooks\PublicEmitter;
 use OC\User\LazyUser;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Group\Events\BeforeGroupDeletedEvent;
+use OCP\Group\Events\BeforeUserAddedEvent;
+use OCP\Group\Events\BeforeUserRemovedEvent;
+use OCP\Group\Events\GroupDeletedEvent;
+use OCP\Group\Events\UserAddedEvent;
+use OCP\Group\Events\UserRemovedEvent;
 use OCP\GroupInterface;
 use OCP\Group\Backend\ICountDisabledInGroup;
 use OCP\Group\Backend\IGetDisplayNameBackend;
@@ -46,8 +53,6 @@ use OCP\Group\Events\GroupChangedEvent;
 use OCP\IGroup;
 use OCP\IUser;
 use OCP\IUserManager;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\EventDispatcher\GenericEvent;
 
 class Group implements IGroup {
        /** @var null|string  */
@@ -64,23 +69,14 @@ class Group implements IGroup {
 
        /** @var Backend[] */
        private $backends;
-       /** @var EventDispatcherInterface */
+       /** @var IEventDispatcher */
        private $dispatcher;
        /** @var \OC\User\Manager|IUserManager  */
        private $userManager;
        /** @var PublicEmitter */
        private $emitter;
 
-
-       /**
-        * @param string $gid
-        * @param Backend[] $backends
-        * @param EventDispatcherInterface $dispatcher
-        * @param IUserManager $userManager
-        * @param PublicEmitter $emitter
-        * @param string $displayName
-        */
-       public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) {
+       public function __construct(string $gid, array $backends, IEventDispatcher $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) {
                $this->gid = $gid;
                $this->backends = $backends;
                $this->dispatcher = $dispatcher;
@@ -112,12 +108,12 @@ class Group implements IGroup {
        public function setDisplayName(string $displayName): bool {
                $displayName = trim($displayName);
                if ($displayName !== '') {
-                       $this->dispatcher->dispatch(new BeforeGroupChangedEvent($this, 'displayName', $displayName, $this->displayName));
+                       $this->dispatcher->dispatchTyped(new BeforeGroupChangedEvent($this, 'displayName', $displayName, $this->displayName));
                        foreach ($this->backends as $backend) {
                                if (($backend instanceof ISetDisplayNameBackend)
                                        && $backend->setDisplayName($this->gid, $displayName)) {
                                        $this->displayName = $displayName;
-                                       $this->dispatcher->dispatch(new GroupChangedEvent($this, 'displayName', $displayName, ''));
+                                       $this->dispatcher->dispatchTyped(new GroupChangedEvent($this, 'displayName', $displayName, ''));
                                        return true;
                                }
                        }
@@ -180,9 +176,7 @@ class Group implements IGroup {
                        return;
                }
 
-               $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [
-                       'user' => $user,
-               ]));
+               $this->dispatcher->dispatchTyped(new BeforeUserAddedEvent($this, $user));
 
                if ($this->emitter) {
                        $this->emitter->emit('\OC\Group', 'preAddUser', [$this, $user]);
@@ -194,9 +188,7 @@ class Group implements IGroup {
                                        $this->users[$user->getUID()] = $user;
                                }
 
-                               $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [
-                                       'user' => $user,
-                               ]));
+                               $this->dispatcher->dispatchTyped(new UserAddedEvent($this, $user));
 
                                if ($this->emitter) {
                                        $this->emitter->emit('\OC\Group', 'postAddUser', [$this, $user]);
@@ -213,9 +205,7 @@ class Group implements IGroup {
         */
        public function removeUser($user) {
                $result = false;
-               $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [
-                       'user' => $user,
-               ]));
+               $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($this, $user));
                if ($this->emitter) {
                        $this->emitter->emit('\OC\Group', 'preRemoveUser', [$this, $user]);
                }
@@ -226,9 +216,7 @@ class Group implements IGroup {
                        }
                }
                if ($result) {
-                       $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [
-                               'user' => $user,
-                       ]));
+                       $this->dispatcher->dispatchTyped(new UserRemovedEvent($this, $user));
                        if ($this->emitter) {
                                $this->emitter->emit('\OC\Group', 'postRemoveUser', [$this, $user]);
                        }
@@ -352,7 +340,7 @@ class Group implements IGroup {
                }
 
                $result = false;
-               $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this));
+               $this->dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($this));
                if ($this->emitter) {
                        $this->emitter->emit('\OC\Group', 'preDelete', [$this]);
                }
@@ -362,7 +350,7 @@ class Group implements IGroup {
                        }
                }
                if ($result) {
-                       $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this));
+                       $this->dispatcher->dispatchTyped(new GroupDeletedEvent($this));
                        if ($this->emitter) {
                                $this->emitter->emit('\OC\Group', 'postDelete', [$this]);
                        }
index 0672e519e3668aa1d702b4aab56267de86daad39..c43b5165a791aaa67c5744d56be2e585fb65746a 100644 (file)
@@ -41,13 +41,14 @@ namespace OC\Group;
 
 use OC\Hooks\PublicEmitter;
 use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Group\Events\BeforeGroupCreatedEvent;
+use OCP\Group\Events\GroupCreatedEvent;
 use OCP\GroupInterface;
 use OCP\ICacheFactory;
 use OCP\IGroup;
 use OCP\IGroupManager;
 use OCP\IUser;
 use Psr\Log\LoggerInterface;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 /**
  * Class Manager
@@ -70,8 +71,7 @@ class Manager extends PublicEmitter implements IGroupManager {
 
        /** @var \OC\User\Manager */
        private $userManager;
-       /** @var EventDispatcherInterface */
-       private $dispatcher;
+       private IEventDispatcher $dispatcher;
        private LoggerInterface $logger;
 
        /** @var \OC\Group\Group[] */
@@ -86,7 +86,7 @@ class Manager extends PublicEmitter implements IGroupManager {
        private DisplayNameCache $displayNameCache;
 
        public function __construct(\OC\User\Manager $userManager,
-                                                               EventDispatcherInterface $dispatcher,
+                                                               IEventDispatcher $dispatcher,
                                                                LoggerInterface $logger,
                                                                ICacheFactory $cacheFactory) {
                $this->userManager = $userManager;
@@ -220,11 +220,13 @@ class Manager extends PublicEmitter implements IGroupManager {
                } elseif ($group = $this->get($gid)) {
                        return $group;
                } else {
+                       $this->dispatcher->dispatchTyped(new BeforeGroupCreatedEvent($gid));
                        $this->emit('\OC\Group', 'preCreate', [$gid]);
                        foreach ($this->backends as $backend) {
                                if ($backend->implementsActions(Backend::CREATE_GROUP)) {
                                        if ($backend->createGroup($gid)) {
                                                $group = $this->getGroupObject($gid);
+                                               $this->dispatcher->dispatchTyped(new GroupCreatedEvent($group));
                                                $this->emit('\OC\Group', 'postCreate', [$group]);
                                                return $group;
                                        }
@@ -424,7 +426,7 @@ class Manager extends PublicEmitter implements IGroupManager {
                                $this->userManager,
                                $this,
                                \OC::$server->getDatabaseConnection(),
-                               \OC::$server->get(IEventDispatcher::class)
+                               $this->dispatcher
                        );
                }
 
index 6c7dcb730ebd0d3d1e7d17eaa748b9c1e2160c22..8640fa3e38c1eff2eaa5e8fb2abe62710b01f8c6 100644 (file)
@@ -192,14 +192,6 @@ use OCP\Files\Storage\IStorageFactory;
 use OCP\Files\Template\ITemplateManager;
 use OCP\FullTextSearch\IFullTextSearchManager;
 use OCP\GlobalScale\IConfig;
-use OCP\Group\Events\BeforeGroupCreatedEvent;
-use OCP\Group\Events\BeforeGroupDeletedEvent;
-use OCP\Group\Events\BeforeUserAddedEvent;
-use OCP\Group\Events\BeforeUserRemovedEvent;
-use OCP\Group\Events\GroupCreatedEvent;
-use OCP\Group\Events\GroupDeletedEvent;
-use OCP\Group\Events\UserAddedEvent;
-use OCP\Group\Events\UserRemovedEvent;
 use OCP\Group\ISubAdmin;
 use OCP\Http\Client\IClientService;
 use OCP\IAppConfig;
@@ -253,12 +245,10 @@ use OCP\SystemTag\ISystemTagManager;
 use OCP\SystemTag\ISystemTagObjectMapper;
 use OCP\Talk\IBroker;
 use OCP\Translation\ITranslationManager;
-use OCP\User\Events\BeforePasswordUpdatedEvent;
 use OCP\User\Events\BeforeUserDeletedEvent;
 use OCP\User\Events\BeforeUserLoggedInEvent;
 use OCP\User\Events\BeforeUserLoggedInWithCookieEvent;
 use OCP\User\Events\BeforeUserLoggedOutEvent;
-use OCP\User\Events\PasswordUpdatedEvent;
 use OCP\User\Events\PostLoginEvent;
 use OCP\User\Events\UserChangedEvent;
 use OCP\User\Events\UserLoggedInEvent;
@@ -268,7 +258,6 @@ use Psr\Container\ContainerExceptionInterface;
 use Psr\Container\ContainerInterface;
 use Psr\Log\LoggerInterface;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\EventDispatcher\GenericEvent;
 use OCA\Files_External\Service\UserStoragesService;
 use OCA\Files_External\Service\UserGlobalStoragesService;
 use OCA\Files_External\Service\GlobalStoragesService;
@@ -491,50 +480,10 @@ class Server extends ServerContainer implements IServerContainer {
                $this->registerService(\OCP\IGroupManager::class, function (ContainerInterface $c) {
                        $groupManager = new \OC\Group\Manager(
                                $this->get(IUserManager::class),
-                               $c->get(SymfonyAdapter::class),
+                               $this->get(IEventDispatcher::class),
                                $this->get(LoggerInterface::class),
                                $this->get(ICacheFactory::class)
                        );
-                       $groupManager->listen('\OC\Group', 'preCreate', function ($gid) {
-                               /** @var IEventDispatcher $dispatcher */
-                               $dispatcher = $this->get(IEventDispatcher::class);
-                               $dispatcher->dispatchTyped(new BeforeGroupCreatedEvent($gid));
-                       });
-                       $groupManager->listen('\OC\Group', 'postCreate', function (\OC\Group\Group $group) {
-                               /** @var IEventDispatcher $dispatcher */
-                               $dispatcher = $this->get(IEventDispatcher::class);
-                               $dispatcher->dispatchTyped(new GroupCreatedEvent($group));
-                       });
-                       $groupManager->listen('\OC\Group', 'preDelete', function (\OC\Group\Group $group) {
-                               /** @var IEventDispatcher $dispatcher */
-                               $dispatcher = $this->get(IEventDispatcher::class);
-                               $dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($group));
-                       });
-                       $groupManager->listen('\OC\Group', 'postDelete', function (\OC\Group\Group $group) {
-                               /** @var IEventDispatcher $dispatcher */
-                               $dispatcher = $this->get(IEventDispatcher::class);
-                               $dispatcher->dispatchTyped(new GroupDeletedEvent($group));
-                       });
-                       $groupManager->listen('\OC\Group', 'preAddUser', function (\OC\Group\Group $group, \OC\User\User $user) {
-                               /** @var IEventDispatcher $dispatcher */
-                               $dispatcher = $this->get(IEventDispatcher::class);
-                               $dispatcher->dispatchTyped(new BeforeUserAddedEvent($group, $user));
-                       });
-                       $groupManager->listen('\OC\Group', 'postAddUser', function (\OC\Group\Group $group, \OC\User\User $user) {
-                               /** @var IEventDispatcher $dispatcher */
-                               $dispatcher = $this->get(IEventDispatcher::class);
-                               $dispatcher->dispatchTyped(new UserAddedEvent($group, $user));
-                       });
-                       $groupManager->listen('\OC\Group', 'preRemoveUser', function (\OC\Group\Group $group, \OC\User\User $user) {
-                               /** @var IEventDispatcher $dispatcher */
-                               $dispatcher = $this->get(IEventDispatcher::class);
-                               $dispatcher->dispatchTyped(new BeforeUserRemovedEvent($group, $user));
-                       });
-                       $groupManager->listen('\OC\Group', 'postRemoveUser', function (\OC\Group\Group $group, \OC\User\User $user) {
-                               /** @var IEventDispatcher $dispatcher */
-                               $dispatcher = $this->get(IEventDispatcher::class);
-                               $dispatcher->dispatchTyped(new UserRemovedEvent($group, $user));
-                       });
                        return $groupManager;
                });
                /** @deprecated 19.0.0 */
index 798b997e82a3f19c25624ae6d91332fbc5140179..58bb1c4133b6bcbf6f71a3826a5ed56404456530 100644 (file)
@@ -13,18 +13,14 @@ use OC\User\User;
 use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IUser;
 use PHPUnit\Framework\MockObject\MockObject;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 class GroupTest extends \Test\TestCase {
-       /** @var EventDispatcherInterface|MockObject */
+       /** @var IEventDispatcher|MockObject */
        protected $dispatcher;
-       /** @var EventDispatcherInterface|MockObject */
-       protected $eventDispatcher;
 
        protected function setUp(): void {
                parent::setUp();
-               $this->dispatcher = $this->createMock(EventDispatcherInterface::class);
-               $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
+               $this->dispatcher = $this->createMock(IEventDispatcher::class);
        }
 
        /**
@@ -309,7 +305,7 @@ class GroupTest extends \Test\TestCase {
                $backend->expects($this->once())
                        ->method('searchInGroup')
                        ->with('group1', '2')
-                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]);
+                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);
 
                $users = $group->searchUsers('2');
 
@@ -331,11 +327,11 @@ class GroupTest extends \Test\TestCase {
                $backend1->expects($this->once())
                        ->method('searchInGroup')
                        ->with('group1', '2')
-                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]);
+                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);
                $backend2->expects($this->once())
                        ->method('searchInGroup')
                        ->with('group1', '2')
-                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]);
+                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);
 
                $users = $group->searchUsers('2');
 
@@ -354,7 +350,7 @@ class GroupTest extends \Test\TestCase {
                $backend->expects($this->once())
                        ->method('searchInGroup')
                        ->with('group1', 'user', 1, 1)
-                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]);
+                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);
 
                $users = $group->searchUsers('user', 1, 1);
 
@@ -376,11 +372,11 @@ class GroupTest extends \Test\TestCase {
                $backend1->expects($this->once())
                        ->method('searchInGroup')
                        ->with('group1', 'user', 2, 1)
-                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->eventDispatcher)]);
+                       ->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);
                $backend2->expects($this->once())
                        ->method('searchInGroup')
                        ->with('group1', 'user', 2, 1)
-                       ->willReturn(['user1' => new \OC\User\User('user1', null, $this->eventDispatcher)]);
+                       ->willReturn(['user1' => new \OC\User\User('user1', null, $this->dispatcher)]);
 
                $users = $group->searchUsers('user', 2, 1);
 
index 679223888bbb60c85e4882cc281fbce1581e668d..2887d14acaa179c1402b1b82c0236f63de12819a 100644 (file)
@@ -33,7 +33,6 @@ use OCP\ICacheFactory;
 use OCP\IUser;
 use PHPUnit\Framework\MockObject\MockObject;
 use Psr\Log\LoggerInterface;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Test\TestCase;
 
 interface ISearchableGroupInterface extends ISearchableGroupBackend, GroupInterface {
@@ -42,10 +41,8 @@ interface ISearchableGroupInterface extends ISearchableGroupBackend, GroupInterf
 class ManagerTest extends TestCase {
        /** @var Manager|MockObject */
        protected $userManager;
-       /** @var EventDispatcherInterface|MockObject */
-       protected $dispatcher;
        /** @var IEventDispatcher|MockObject */
-       protected $eventDispatcher;
+       protected $dispatcher;
        /** @var LoggerInterface|MockObject */
        protected $logger;
        /** @var ICacheFactory|MockObject */
@@ -55,8 +52,7 @@ class ManagerTest extends TestCase {
                parent::setUp();
 
                $this->userManager = $this->createMock(Manager::class);
-               $this->dispatcher = $this->createMock(EventDispatcherInterface::class);
-               $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
+               $this->dispatcher = $this->createMock(IEventDispatcher::class);
                $this->logger = $this->createMock(LoggerInterface::class);
                $this->cache = $this->createMock(ICacheFactory::class);
        }
@@ -773,7 +769,7 @@ class ManagerTest extends TestCase {
                $backend->expects($this->once())
                        ->method('searchInGroup')
                        ->with('testgroup', '', 1, 0)
-                       ->willReturn([new User('user2', null, $this->eventDispatcher)]);
+                       ->willReturn([new User('user2', null, $this->dispatcher)]);
 
                $this->userManager->expects($this->never())->method('get');