From e4f53ff91b985123b171dc23db8246c017af46f6 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Wed, 28 Oct 2020 21:53:04 +0100 Subject: [PATCH] Add typed events for adding and removing a subadmin Signed-off-by: Morris Jobke --- lib/composer/composer/autoload_classmap.php | 2 + lib/composer/composer/autoload_static.php | 2 + lib/private/Group/Manager.php | 4 +- lib/private/SubAdmin.php | 18 ++++- .../Group/Events/SubAdminAddedEvent.php | 67 +++++++++++++++++++ .../Group/Events/SubAdminRemovedEvent.php | 67 +++++++++++++++++++ tests/lib/SubAdminTest.php | 40 ++++++----- 7 files changed, 179 insertions(+), 21 deletions(-) create mode 100644 lib/public/Group/Events/SubAdminAddedEvent.php create mode 100644 lib/public/Group/Events/SubAdminRemovedEvent.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index b3080a117d..f5945a5eb0 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -347,6 +347,8 @@ return array( 'OCP\\Group\\Events\\BeforeUserRemovedEvent' => $baseDir . '/lib/public/Group/Events/BeforeUserRemovedEvent.php', 'OCP\\Group\\Events\\GroupCreatedEvent' => $baseDir . '/lib/public/Group/Events/GroupCreatedEvent.php', 'OCP\\Group\\Events\\GroupDeletedEvent' => $baseDir . '/lib/public/Group/Events/GroupDeletedEvent.php', + 'OCP\\Group\\Events\\SubAdminAddedEvent' => $baseDir . '/lib/public/Group/Events/SubAdminAddedEvent.php', + 'OCP\\Group\\Events\\SubAdminRemovedEvent' => $baseDir . '/lib/public/Group/Events/SubAdminRemovedEvent.php', 'OCP\\Group\\Events\\UserAddedEvent' => $baseDir . '/lib/public/Group/Events/UserAddedEvent.php', 'OCP\\Group\\Events\\UserRemovedEvent' => $baseDir . '/lib/public/Group/Events/UserRemovedEvent.php', 'OCP\\Group\\ISubAdmin' => $baseDir . '/lib/public/Group/ISubAdmin.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 02698a4f6e..7663c43d28 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -376,6 +376,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Group\\Events\\BeforeUserRemovedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeUserRemovedEvent.php', 'OCP\\Group\\Events\\GroupCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/GroupCreatedEvent.php', 'OCP\\Group\\Events\\GroupDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/GroupDeletedEvent.php', + 'OCP\\Group\\Events\\SubAdminAddedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/SubAdminAddedEvent.php', + 'OCP\\Group\\Events\\SubAdminRemovedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/SubAdminRemovedEvent.php', 'OCP\\Group\\Events\\UserAddedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/UserAddedEvent.php', 'OCP\\Group\\Events\\UserRemovedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/UserRemovedEvent.php', 'OCP\\Group\\ISubAdmin' => __DIR__ . '/../../..' . '/lib/public/Group/ISubAdmin.php', diff --git a/lib/private/Group/Manager.php b/lib/private/Group/Manager.php index 6056bcdb3e..3d7c1ff176 100644 --- a/lib/private/Group/Manager.php +++ b/lib/private/Group/Manager.php @@ -41,6 +41,7 @@ namespace OC\Group; use OC\Hooks\PublicEmitter; +use OCP\EventDispatcher\IEventDispatcher; use OCP\GroupInterface; use OCP\IGroup; use OCP\IGroupManager; @@ -416,7 +417,8 @@ class Manager extends PublicEmitter implements IGroupManager { $this->subAdmin = new \OC\SubAdmin( $this->userManager, $this, - \OC::$server->getDatabaseConnection() + \OC::$server->getDatabaseConnection(), + \OC::$server->get(IEventDispatcher::class) ); } diff --git a/lib/private/SubAdmin.php b/lib/private/SubAdmin.php index a9e3855639..3239fb27af 100644 --- a/lib/private/SubAdmin.php +++ b/lib/private/SubAdmin.php @@ -32,6 +32,9 @@ namespace OC; use OC\Hooks\PublicEmitter; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Group\Events\SubAdminAddedEvent; +use OCP\Group\Events\SubAdminRemovedEvent; use OCP\Group\ISubAdmin; use OCP\IDBConnection; use OCP\IGroup; @@ -50,6 +53,9 @@ class SubAdmin extends PublicEmitter implements ISubAdmin { /** @var IDBConnection */ private $dbConn; + /** @var IEventDispatcher */ + private $eventDispatcher; + /** * @param IUserManager $userManager * @param IGroupManager $groupManager @@ -57,10 +63,12 @@ class SubAdmin extends PublicEmitter implements ISubAdmin { */ public function __construct(IUserManager $userManager, IGroupManager $groupManager, - IDBConnection $dbConn) { + IDBConnection $dbConn, + IEventDispatcher $eventDispatcher) { $this->userManager = $userManager; $this->groupManager = $groupManager; $this->dbConn = $dbConn; + $this->eventDispatcher = $eventDispatcher; $this->userManager->listen('\OC\User', 'postDelete', function ($user) { $this->post_deleteUser($user); @@ -85,8 +93,10 @@ class SubAdmin extends PublicEmitter implements ISubAdmin { ]) ->execute(); + /** @depreacted 21.0.0 - use type SubAdminAddedEvent instead */ $this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]); - \OC_Hook::emit("OC_SubAdmin", "post_createSubAdmin", ["gid" => $group->getGID()]); + $event = new SubAdminAddedEvent($group, $user); + $this->eventDispatcher->dispatchTyped($event); } /** @@ -102,8 +112,10 @@ class SubAdmin extends PublicEmitter implements ISubAdmin { ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) ->execute(); + /** @depreacted 21.0.0 - use type SubAdminRemovedEvent instead */ $this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]); - \OC_Hook::emit("OC_SubAdmin", "post_deleteSubAdmin", ["gid" => $group->getGID()]); + $event = new SubAdminRemovedEvent($group, $user); + $this->eventDispatcher->dispatchTyped($event); } /** diff --git a/lib/public/Group/Events/SubAdminAddedEvent.php b/lib/public/Group/Events/SubAdminAddedEvent.php new file mode 100644 index 0000000000..d73e691bbf --- /dev/null +++ b/lib/public/Group/Events/SubAdminAddedEvent.php @@ -0,0 +1,67 @@ + + * + * @author Christoph Wurst + * @author Morris Jobke + * + * @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 . + * + */ + +namespace OCP\Group\Events; + +use OCP\EventDispatcher\Event; +use OCP\IGroup; +use OCP\IUser; + +/** + * @since 21.0.0 + */ +class SubAdminAddedEvent extends Event { + + /** @var IGroup */ + private $group; + + /*** @var IUser */ + private $user; + + /** + * @since 21.0.0 + */ + public function __construct(IGroup $group, IUser $user) { + parent::__construct(); + $this->group = $group; + $this->user = $user; + } + + /** + * @since 21.0.0 + */ + public function getGroup(): IGroup { + return $this->group; + } + + /** + * @since 21.0.0 + */ + public function getUser(): IUser { + return $this->user; + } +} diff --git a/lib/public/Group/Events/SubAdminRemovedEvent.php b/lib/public/Group/Events/SubAdminRemovedEvent.php new file mode 100644 index 0000000000..74f88bd8a3 --- /dev/null +++ b/lib/public/Group/Events/SubAdminRemovedEvent.php @@ -0,0 +1,67 @@ + + * + * @author Christoph Wurst + * @author Morris Jobke + * + * @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 . + * + */ + +namespace OCP\Group\Events; + +use OCP\EventDispatcher\Event; +use OCP\IGroup; +use OCP\IUser; + +/** + * @since 21.0.0 + */ +class SubAdminRemovedEvent extends Event { + + /** @var IGroup */ + private $group; + + /*** @var IUser */ + private $user; + + /** + * @since 21.0.0 + */ + public function __construct(IGroup $group, IUser $user) { + parent::__construct(); + $this->group = $group; + $this->user = $user; + } + + /** + * @since 21.0.0 + */ + public function getGroup(): IGroup { + return $this->group; + } + + /** + * @since 21.0.0 + */ + public function getUser(): IUser { + return $this->user; + } +} diff --git a/tests/lib/SubAdminTest.php b/tests/lib/SubAdminTest.php index 06c319d1f4..0ca2b11679 100644 --- a/tests/lib/SubAdminTest.php +++ b/tests/lib/SubAdminTest.php @@ -21,6 +21,8 @@ namespace Test; +use OCP\EventDispatcher\IEventDispatcher; + /** * @group DB */ @@ -35,12 +37,15 @@ class SubAdminTest extends \Test\TestCase { /** @var \OCP\IDBConnection */ private $dbConn; + /** @var IEventDispatcher */ + private $eventDispatcher; + /** @var \OCP\IUser[] */ private $users; /** @var \OCP\IGroup[] */ private $groups; - + protected function setUp(): void { $this->users = []; $this->groups = []; @@ -48,6 +53,7 @@ class SubAdminTest extends \Test\TestCase { $this->userManager = \OC::$server->getUserManager(); $this->groupManager = \OC::$server->getGroupManager(); $this->dbConn = \OC::$server->getDatabaseConnection(); + $this->eventDispatcher = \OC::$server->get(IEventDispatcher::class); // Create 3 users and 3 groups for ($i = 0; $i < 3; $i++) { @@ -100,7 +106,7 @@ class SubAdminTest extends \Test\TestCase { } public function testCreateSubAdmin() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); // Look for subadmin in the database @@ -125,7 +131,7 @@ class SubAdminTest extends \Test\TestCase { } public function testDeleteSubAdmin() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); @@ -141,12 +147,12 @@ class SubAdminTest extends \Test\TestCase { } public function testGetSubAdminsGroups() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); $subAdmin->createSubAdmin($this->users[0], $this->groups[1]); $result = $subAdmin->getSubAdminsGroups($this->users[0]); - + $this->assertContains($this->groups[0], $result); $this->assertContains($this->groups[1], $result); $this->assertNotContains($this->groups[2], $result); @@ -157,12 +163,12 @@ class SubAdminTest extends \Test\TestCase { } public function testGetGroupsSubAdmins() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); $subAdmin->createSubAdmin($this->users[1], $this->groups[0]); $result = $subAdmin->getGroupsSubAdmins($this->groups[0]); - + $this->assertContains($this->users[0], $result); $this->assertContains($this->users[1], $result); $this->assertNotContains($this->users[2], $result); @@ -173,7 +179,7 @@ class SubAdminTest extends \Test\TestCase { } public function testGetAllSubAdmin() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); $subAdmin->createSubAdmin($this->users[1], $this->groups[1]); @@ -188,7 +194,7 @@ class SubAdminTest extends \Test\TestCase { } public function testIsSubAdminofGroup() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); $this->assertTrue($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[0])); @@ -199,7 +205,7 @@ class SubAdminTest extends \Test\TestCase { } public function testIsSubAdmin() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); $this->assertTrue($subAdmin->isSubAdmin($this->users[0])); @@ -209,14 +215,14 @@ class SubAdminTest extends \Test\TestCase { } public function testIsSubAdminAsAdmin() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $this->groupManager->get('admin')->addUser($this->users[0]); $this->assertTrue($subAdmin->isSubAdmin($this->users[0])); } public function testIsUserAccessible() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $this->groups[0]->addUser($this->users[1]); $this->groups[1]->addUser($this->users[1]); $this->groups[1]->addUser($this->users[2]); @@ -232,12 +238,12 @@ class SubAdminTest extends \Test\TestCase { } public function testIsUserAccessibleAsUser() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1])); } public function testIsUserAccessibleAdmin() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); $this->groupManager->get('admin')->addUser($this->users[1]); @@ -245,7 +251,7 @@ class SubAdminTest extends \Test\TestCase { } public function testPostDeleteUser() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $user = array_shift($this->users); foreach ($this->groups as $group) { @@ -257,7 +263,7 @@ class SubAdminTest extends \Test\TestCase { } public function testPostDeleteGroup() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $group = array_shift($this->groups); foreach ($this->users as $user) { @@ -269,7 +275,7 @@ class SubAdminTest extends \Test\TestCase { } public function testHooks() { - $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn); + $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); $test = $this; $u = $this->users[0];