Skip to content

Commit

Permalink
Merge pull request #23765 from nextcloud/techdebt/noid/sudadmin-event…
Browse files Browse the repository at this point in the history
…s-into-typed-events

Add typed events for adding and removing a subadmin
  • Loading branch information
MorrisJobke authored Oct 29, 2020
2 parents 28e2551 + 765ee60 commit 7ed11b6
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 27 deletions.
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@
'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',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Group/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
namespace OC\Group;

use OC\Hooks\PublicEmitter;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\GroupInterface;
use OCP\IGroup;
use OCP\IGroupManager;
Expand Down Expand Up @@ -416,7 +417,8 @@ public function getSubAdmin() {
$this->subAdmin = new \OC\SubAdmin(
$this->userManager,
$this,
\OC::$server->getDatabaseConnection()
\OC::$server->getDatabaseConnection(),
\OC::$server->get(IEventDispatcher::class)
);
}

Expand Down
18 changes: 15 additions & 3 deletions lib/private/SubAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,17 +53,22 @@ class SubAdmin extends PublicEmitter implements ISubAdmin {
/** @var IDBConnection */
private $dbConn;

/** @var IEventDispatcher */
private $eventDispatcher;

/**
* @param IUserManager $userManager
* @param IGroupManager $groupManager
* @param IDBConnection $dbConn
*/
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);
Expand All @@ -85,8 +93,10 @@ public function createSubAdmin(IUser $user, IGroup $group): void {
])
->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);
}

/**
Expand All @@ -102,8 +112,10 @@ public function deleteSubAdmin(IUser $user, IGroup $group): void {
->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);
}

/**
Expand Down
67 changes: 67 additions & 0 deletions lib/public/Group/Events/SubAdminAddedEvent.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
67 changes: 67 additions & 0 deletions lib/public/Group/Events/SubAdminRemovedEvent.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 7ed11b6

Please sign in to comment.