Skip to content

Commit

Permalink
Merge pull request #30996 from owncloud/group-events
Browse files Browse the repository at this point in the history
Throw symfony events on group actions
  • Loading branch information
Vincent Petry authored Apr 4, 2018
2 parents 54cd970 + 452cf85 commit 01ad9dd
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 200 deletions.
14 changes: 13 additions & 1 deletion lib/private/Group/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

use OCP\IGroup;
use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class Group implements IGroup {
/**
Expand Down Expand Up @@ -63,17 +65,21 @@ class Group implements IGroup {
*/
private $userManager;

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

/**
* @param string $gid
* @param \OC\Group\Backend[] $backends
* @param \OC\User\Manager $userManager
* @param \OC\Hooks\PublicEmitter $emitter
* @param string $displayName
*/
public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
public function __construct($gid, $backends, $userManager, EventDispatcherInterface $eventDispatcher, $emitter = null, $displayName = null) {
$this->gid = $gid;
$this->backends = $backends;
$this->userManager = $userManager;
$this->eventDispatcher = $eventDispatcher;
$this->emitter = $emitter;
$this->displayName = $displayName;
}
Expand Down Expand Up @@ -147,6 +153,7 @@ public function addUser($user) {
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preAddUser', [$this, $user]);
}
$this->eventDispatcher->dispatch('group.preAddUser', new GenericEvent($this, ['user' => $user]));
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
$backend->addToGroup($user->getUID(), $this->gid);
Expand All @@ -156,6 +163,7 @@ public function addUser($user) {
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postAddUser', [$this, $user]);
}
$this->eventDispatcher->dispatch('group.postAddUser', new GenericEvent($this, ['user' => $user]));
return;
}
}
Expand All @@ -171,6 +179,7 @@ public function removeUser($user) {
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preRemoveUser', [$this, $user]);
}
$this->eventDispatcher->dispatch('group.preRemoveUser', new GenericEvent($this, ['user' => $user]));
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);
Expand All @@ -181,6 +190,7 @@ public function removeUser($user) {
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postRemoveUser', [$this, $user]);
}
$this->eventDispatcher->dispatch('group.postRemoveUser', new GenericEvent($this, ['user' => $user]));
if ($this->users) {
foreach ($this->users as $index => $groupUser) {
if ($groupUser->getUID() === $user->getUID()) {
Expand Down Expand Up @@ -268,6 +278,7 @@ public function delete() {
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preDelete', [$this]);
}
$this->eventDispatcher->dispatch('group.preDelete', new GenericEvent($this));
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
$result = true;
Expand All @@ -277,6 +288,7 @@ public function delete() {
if ($result and $this->emitter) {
$this->emitter->emit('\OC\Group', 'postDelete', [$this]);
}
$this->eventDispatcher->dispatch('group.postDelete', new GenericEvent($this));
return $result;
}

Expand Down
12 changes: 10 additions & 2 deletions lib/private/Group/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
use OCP\GroupInterface;
use OCP\IGroupManager;
use OCP\Util\UserSearch;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class Manager
Expand Down Expand Up @@ -85,13 +87,17 @@ class Manager extends PublicEmitter implements IGroupManager {
/** @var \OC\SubAdmin */
private $subAdmin = null;

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

/**
* @param \OC\User\Manager $userManager
* @param UserSearch $userSearch
*/
public function __construct(UserManager $userManager, UserSearch $userSearch) {
public function __construct(UserManager $userManager, UserSearch $userSearch, EventDispatcherInterface $eventDispatcher) {
$this->userManager = $userManager;
$this->userSearch = $userSearch;
$this->eventDispatcher = $eventDispatcher;
$cachedGroups = & $this->cachedGroups;
$cachedUserGroups = & $this->cachedUserGroups;
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
Expand Down Expand Up @@ -186,7 +192,7 @@ protected function getGroupObject($gid, $displayName = null) {
if (count($backends) === 0) {
return null;
}
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this, $displayName);
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this->eventDispatcher, $this, $displayName);
return $this->cachedGroups[$gid];
}

Expand All @@ -209,11 +215,13 @@ public function createGroup($gid) {
return $group;
} else {
$this->emit('\OC\Group', 'preCreate', [$gid]);
$this->eventDispatcher->dispatch('group.preCreate', new GenericEvent(null, ['gid' => $gid]));
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) {
$backend->createGroup($gid);
$group = $this->getGroupObject($gid);
$this->emit('\OC\Group', 'postCreate', [$group]);
$this->eventDispatcher->dispatch('group.postCreate', new GenericEvent($group, ['gid' => $gid]));
return $group;
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,10 @@ public function __construct($webRoot, \OC\Config $config) {
$this->getUserManager(),
new UserSearch(
$c->getConfig()
)
),
$this->getEventDispatcher()
);

$groupManager->listen('\OC\Group', 'preCreate', function ($gid) {
\OC_Hook::emit('OC_Group', 'pre_createGroup', ['run' => true, 'gid' => $gid]);
});
Expand Down
12 changes: 6 additions & 6 deletions tests/lib/App/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public function testNotEnableIfNotInstalled() {

public function testEnableAppForGroups() {
$groups = [
new Group('group1', [], null),
new Group('group2', [], null)
new Group('group1', [], null, $this->eventDispatcher),
new Group('group2', [], null, $this->eventDispatcher)
];
$this->expectClearCache();
$this->manager->enableAppForGroups('test', $groups);
Expand All @@ -191,8 +191,8 @@ public function dataEnableAppForGroupsAllowedTypes() {
*/
public function testEnableAppForGroupsAllowedTypes(array $appInfo) {
$groups = [
new Group('group1', [], null),
new Group('group2', [], null)
new Group('group1', [], null, $this->eventDispatcher),
new Group('group2', [], null, $this->eventDispatcher)
];
$this->expectClearCache();

Expand Down Expand Up @@ -236,8 +236,8 @@ public function dataEnableAppForGroupsForbiddenTypes() {
*/
public function testEnableAppForGroupsForbiddenTypes($type) {
$groups = [
new Group('group1', [], null),
new Group('group2', [], null)
new Group('group1', [], null, $this->eventDispatcher),
new Group('group2', [], null, $this->eventDispatcher)
];

/** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
Expand Down
Loading

0 comments on commit 01ad9dd

Please sign in to comment.