Merge pull request #23765 from nextcloud/techdebt/noid/sudadmin-events-into-typed-events

Add typed events for adding and removing a subadmin
This commit is contained in:
Morris Jobke 2020-10-29 11:18:01 +01:00 committed by GitHub
commit 7ed11b6dfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 187 additions and 27 deletions

View File

@ -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',

View File

@ -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',

View File

@ -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)
);
}

View File

@ -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);
}
/**

View File

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Morris Jobke <hey@morrisjobke.de>
*
* @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\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;
}
}

View File

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Morris Jobke <hey@morrisjobke.de>
*
* @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\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;
}
}

View File

@ -21,6 +21,10 @@
namespace Test;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Events\SubAdminAddedEvent;
use OCP\Group\Events\SubAdminRemovedEvent;
/**
* @group DB
*/
@ -35,12 +39,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 +55,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 +108,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 +133,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 +149,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 +165,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 +181,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 +196,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 +207,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 +217,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 +240,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 +253,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 +265,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,22 +277,22 @@ 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];
$g = $this->groups[0];
$count = 0;
$subAdmin->listen('\OC\SubAdmin', 'postCreateSubAdmin', function ($user, $group) use ($test, $u, $g, &$count) {
$test->assertEquals($u->getUID(), $user->getUID());
$test->assertEquals($g->getGID(), $group->getGID());
$this->eventDispatcher->addListener(SubAdminAddedEvent::class, function (SubAdminAddedEvent $event) use ($test, $u, $g, &$count) {
$test->assertEquals($u->getUID(), $event->getUser()->getUID());
$test->assertEquals($g->getGID(), $event->getGroup()->getGID());
$count++;
});
$subAdmin->listen('\OC\SubAdmin', 'postDeleteSubAdmin', function ($user, $group) use ($test, $u, $g, &$count) {
$test->assertEquals($u->getUID(), $user->getUID());
$test->assertEquals($g->getGID(), $group->getGID());
$this->eventDispatcher->addListener(SubAdminRemovedEvent::class, function ($event) use ($test, $u, $g, &$count) {
$test->assertEquals($u->getUID(), $event->getUser()->getUID());
$test->assertEquals($g->getGID(), $event->getGroup()->getGID());
$count++;
});