Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typed events for all group hooks and legacy events #18322

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@
'OCP\\Group\\Backend\\IIsAdminBackend' => $baseDir . '/lib/public/Group/Backend/IIsAdminBackend.php',
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => $baseDir . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
'OCP\\Group\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/Group/Backend/ISetDisplayNameBackend.php',
'OCP\\Group\\Events\\BeforeGroupCreatedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php',
'OCP\\Group\\Events\\BeforeGroupDeletedEvent' => $baseDir . '/lib/public/Group/Events/BeforeGroupDeletedEvent.php',
'OCP\\Group\\Events\\BeforeUserAddedEvent' => $baseDir . '/lib/public/Group/Events/BeforeUserAddedEvent.php',
'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\\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',
'OCP\\Http\\Client\\IClient' => $baseDir . '/lib/public/Http/Client/IClient.php',
'OCP\\Http\\Client\\IClientService' => $baseDir . '/lib/public/Http/Client/IClientService.php',
Expand Down
8 changes: 8 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Group\\Backend\\IIsAdminBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IIsAdminBackend.php',
'OCP\\Group\\Backend\\IRemoveFromGroupBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/IRemoveFromGroupBackend.php',
'OCP\\Group\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/Group/Backend/ISetDisplayNameBackend.php',
'OCP\\Group\\Events\\BeforeGroupCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupCreatedEvent.php',
'OCP\\Group\\Events\\BeforeGroupDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeGroupDeletedEvent.php',
'OCP\\Group\\Events\\BeforeUserAddedEvent' => __DIR__ . '/../../..' . '/lib/public/Group/Events/BeforeUserAddedEvent.php',
'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\\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',
'OCP\\Http\\Client\\IClient' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IClient.php',
'OCP\\Http\\Client\\IClientService' => __DIR__ . '/../../..' . '/lib/public/Http/Client/IClientService.php',
Expand Down
82 changes: 35 additions & 47 deletions lib/private/Group/Group.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -32,18 +35,25 @@
namespace OC\Group;

use OC\Hooks\PublicEmitter;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Backend\ICountDisabledInGroup;
use OCP\Group\Backend\IGetDisplayNameBackend;
use OCP\Group\Backend\IHideFromCollaborationBackend;
use OCP\Group\Backend\ISetDisplayNameBackend;
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\IGroup;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class Group implements IGroup {

/** @var null|string */
protected $displayName;

Expand All @@ -58,23 +68,30 @@ 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 IEventDispatcher $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;
Expand Down Expand Up @@ -172,27 +189,14 @@ public function addUser(IUser $user) {
return;
}

$this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [
'user' => $user,
]));

if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
}
$this->dispatcher->dispatchTyped(new BeforeUserAddedEvent($this, $user));
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
$backend->addToGroup($user->getUID(), $this->gid);
if ($this->users) {
$this->users[$user->getUID()] = $user;
}

$this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [
'user' => $user,
]));

if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
}
$this->dispatcher->dispatchTyped(new UserAddedEvent($this, $user));
return;
}
}
Expand All @@ -204,26 +208,16 @@ public function addUser(IUser $user) {
* @param \OC\User\User $user
*/
public function removeUser($user) {
$result = false;
$this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [
'user' => $user,
]));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
}
$this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($this, $user));
$removedFromABackend = false;
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
$backend->removeFromGroup($user->getUID(), $this->gid);
$result = true;
$removedFromABackend = true;
}
}
if ($result) {
$this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [
'user' => $user,
]));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
}
if ($removedFromABackend) {
$this->dispatcher->dispatchTyped(new UserRemovedEvent($this, $user));
if ($this->users) {
foreach ($this->users as $index => $groupUser) {
if ($groupUser->getUID() === $user->getUID()) {
Expand Down Expand Up @@ -327,24 +321,18 @@ public function delete() {
return false;
}

$result = false;
$this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preDelete', array($this));
}
$this->dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($this));
$deletedFromABackend = false;
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
$result = true;
$deletedFromABackend = true;
$backend->deleteGroup($this->gid);
}
}
if ($result) {
$this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postDelete', array($this));
}
if ($deletedFromABackend) {
$this->dispatcher->dispatchTyped(new GroupDeletedEvent($this));
}
return $result;
return $deletedFromABackend;
}

/**
Expand Down
58 changes: 24 additions & 34 deletions lib/private/Group/Manager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -40,36 +43,34 @@
namespace OC\Group;

use OC\Hooks\PublicEmitter;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Events\BeforeGroupCreatedEvent;
use OCP\Group\Events\GroupCreatedEvent;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\Group\Events\UserAddedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\GroupInterface;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class Manager
*
* Hooks available in scope \OC\Group:
* - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
* - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
* - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
* - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
* - preDelete(\OC\Group\Group $group)
* - postDelete(\OC\Group\Group $group)
* - preCreate(string $groupId)
* - postCreate(\OC\Group\Group $group)
*
* @package OC\Group
*/
class Manager extends PublicEmitter implements IGroupManager {

/** @var GroupInterface[] */
private $backends = [];

/** @var \OC\User\Manager */
private $userManager;
/** @var EventDispatcherInterface */

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

/** @var ILogger */
private $logger;

Expand All @@ -84,36 +85,25 @@ class Manager extends PublicEmitter implements IGroupManager {

/**
* @param \OC\User\Manager $userManager
* @param EventDispatcherInterface $dispatcher
* @param IEventDispatcher $dispatcher
* @param ILogger $logger
*/
public function __construct(\OC\User\Manager $userManager,
EventDispatcherInterface $dispatcher,
IEventDispatcher $dispatcher,
ILogger $logger) {
$this->userManager = $userManager;
$this->dispatcher = $dispatcher;
$this->logger = $logger;

$cachedGroups = &$this->cachedGroups;
$cachedUserGroups = &$this->cachedUserGroups;
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
unset($cachedGroups[$group->getGID()]);
$cachedUserGroups = [];
$dispatcher->addListener(GroupDeletedEvent::class, function(GroupDeletedEvent $event) {
unset($this->cachedGroups[$event->getGroup()->getGID()]);
$this->cachedUserGroups = [];
});
$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroups = [];
$dispatcher->addListener(UserAddedEvent::class, function() {
$this->cachedUserGroups = [];
});
$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroups = [];
$dispatcher->addListener(UserRemovedEvent::class, function() {
$this->cachedUserGroups = [];
});
}

Expand Down Expand Up @@ -220,12 +210,12 @@ public function createGroup($gid) {
} else if ($group = $this->get($gid)) {
return $group;
} else {
$this->emit('\OC\Group', 'preCreate', [$gid]);
$this->dispatcher->dispatchTyped(new BeforeGroupCreatedEvent($gid));
foreach ($this->backends as $backend) {
if ($backend->implementsActions(Backend::CREATE_GROUP)) {
if ($backend->createGroup($gid)) {
$group = $this->getGroupObject($gid);
$this->emit('\OC\Group', 'postCreate', [$group]);
$this->dispatcher->dispatchTyped(new GroupCreatedEvent($group));
return $group;
}
}
Expand Down
Loading