aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2019-02-22 13:07:26 +0100
committerJulius Härtl <jus@bitgrid.net>2019-03-01 20:56:59 +0100
commit01b4db62fbc4230cff953a2385d305b149744b86 (patch)
tree788b5edf5acba069bd6996466ab9b1b30a6804d4 /lib/private
parent55ae7fa2a48ce38e2807a40632f282740c0e8117 (diff)
downloadnextcloud-server-01b4db62fbc4230cff953a2385d305b149744b86.tar.gz
nextcloud-server-01b4db62fbc4230cff953a2385d305b149744b86.zip
Add dispatcher events to User and Group objects
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/Files/View.php2
-rw-r--r--lib/private/Group/Group.php66
-rw-r--r--lib/private/Group/Manager.php63
-rw-r--r--lib/private/Server.php7
-rw-r--r--lib/private/User/Manager.php15
-rw-r--r--lib/private/User/User.php47
6 files changed, 113 insertions, 87 deletions
diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php
index 6066aed411f..f12e4609e08 100644
--- a/lib/private/Files/View.php
+++ b/lib/private/Files/View.php
@@ -1299,7 +1299,7 @@ class View {
if ($owner instanceof IUser) {
return $owner;
} else {
- return new User($ownerId, null);
+ return new User($ownerId, null, \OC::$server->getEventDispatcher());
}
}
diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php
index df8de7af5d5..3876c575968 100644
--- a/lib/private/Group/Group.php
+++ b/lib/private/Group/Group.php
@@ -31,55 +31,50 @@
namespace OC\Group;
use OCP\Group\Backend\IHideFromCollaborationBackend;
+use OC\Hooks\PublicEmitter;
use OCP\GroupInterface;
use OCP\IGroup;
use OCP\IUser;
use OCP\Group\Backend\ICountDisabledInGroup;
+use OCP\IUserManager;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Symfony\Component\EventDispatcher\GenericEvent;
class Group implements IGroup {
/** @var null|string */
protected $displayName;
- /**
- * @var string $id
- */
+ /** @var string */
private $gid;
- /**
- * @var \OC\User\User[] $users
- */
+ /** @var \OC\User\User[] */
private $users = array();
- /**
- * @var bool $usersLoaded
- */
+ /** @var bool */
private $usersLoaded;
- /**
- * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend
- */
+ /** @var Backend[] */
private $backends;
-
- /**
- * @var \OC\Hooks\PublicEmitter $emitter
- */
+ /** @var EventDispatcherInterface */
+ private $dispatcher;
+ /** @var \OC\User\Manager|IUserManager */
+ private $userManager;
+ /** @var PublicEmitter */
private $emitter;
- /**
- * @var \OC\User\Manager $userManager
- */
- private $userManager;
/**
* @param string $gid
- * @param \OC\Group\Backend[] $backends
- * @param \OC\User\Manager $userManager
- * @param \OC\Hooks\PublicEmitter $emitter
+ * @param Backend[] $backends
+ * @param EventDispatcherInterface $dispatcher
+ * @param IUserManager $userManager
+ * @param PublicEmitter $emitter
* @param string $displayName
*/
- public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
+ public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) {
$this->gid = $gid;
$this->backends = $backends;
+ $this->dispatcher = $dispatcher;
$this->userManager = $userManager;
$this->emitter = $emitter;
$this->displayName = $displayName;
@@ -151,6 +146,10 @@ class Group implements IGroup {
return;
}
+ $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [
+ 'user' => $user,
+ ]));
+
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
}
@@ -160,6 +159,11 @@ class Group implements IGroup {
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));
}
@@ -175,6 +179,9 @@ class Group implements IGroup {
*/
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));
}
@@ -185,6 +192,9 @@ class Group implements IGroup {
}
}
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));
}
@@ -292,6 +302,7 @@ class Group implements IGroup {
}
$result = false;
+ $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preDelete', array($this));
}
@@ -301,8 +312,11 @@ class Group implements IGroup {
$backend->deleteGroup($this->gid);
}
}
- if ($result and $this->emitter) {
- $this->emitter->emit('\OC\Group', 'postDelete', array($this));
+ if ($result) {
+ $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this));
+ if ($this->emitter) {
+ $this->emitter->emit('\OC\Group', 'postDelete', array($this));
+ }
}
return $result;
}
diff --git a/lib/private/Group/Manager.php b/lib/private/Group/Manager.php
index f1bb6357d0a..7567f719b0a 100644
--- a/lib/private/Group/Manager.php
+++ b/lib/private/Group/Manager.php
@@ -44,6 +44,7 @@ use OCP\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUser;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Class Manager
@@ -61,39 +62,37 @@ use OCP\IUser;
* @package OC\Group
*/
class Manager extends PublicEmitter implements IGroupManager {
- /**
- * @var GroupInterface[] $backends
- */
- private $backends = array();
+ /** @var GroupInterface[] */
+ private $backends = [];
- /**
- * @var \OC\User\Manager $userManager
- */
+ /** @var \OC\User\Manager */
private $userManager;
+ /** @var EventDispatcherInterface */
+ private $dispatcher;
+ /** @var ILogger */
+ private $logger;
- /**
- * @var \OC\Group\Group[]
- */
- private $cachedGroups = array();
+ /** @var \OC\Group\Group[] */
+ private $cachedGroups = [];
- /**
- * @var \OC\Group\Group[]
- */
- private $cachedUserGroups = array();
+ /** @var \OC\Group\Group[] */
+ private $cachedUserGroups = [];
/** @var \OC\SubAdmin */
private $subAdmin = null;
- /** @var ILogger */
- private $logger;
-
/**
* @param \OC\User\Manager $userManager
+ * @param EventDispatcherInterface $dispatcher
* @param ILogger $logger
*/
- public function __construct(\OC\User\Manager $userManager, ILogger $logger) {
+ public function __construct(\OC\User\Manager $userManager,
+ EventDispatcherInterface $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) {
@@ -101,19 +100,19 @@ class Manager extends PublicEmitter implements IGroupManager {
* @var \OC\Group\Group $group
*/
unset($cachedGroups[$group->getGID()]);
- $cachedUserGroups = array();
+ $cachedUserGroups = [];
});
$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
- $cachedUserGroups = array();
+ $cachedUserGroups = [];
});
$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
- $cachedUserGroups = array();
+ $cachedUserGroups = [];
});
}
@@ -144,7 +143,7 @@ class Manager extends PublicEmitter implements IGroupManager {
}
public function clearBackends() {
- $this->backends = array();
+ $this->backends = [];
$this->clearCaches();
}
@@ -158,8 +157,8 @@ class Manager extends PublicEmitter implements IGroupManager {
protected function clearCaches() {
- $this->cachedGroups = array();
- $this->cachedUserGroups = array();
+ $this->cachedGroups = [];
+ $this->cachedUserGroups = [];
}
/**
@@ -179,7 +178,7 @@ class Manager extends PublicEmitter implements IGroupManager {
* @return \OCP\IGroup
*/
protected function getGroupObject($gid, $displayName = null) {
- $backends = array();
+ $backends = [];
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) {
$groupData = $backend->getGroupDetails($gid);
@@ -197,7 +196,7 @@ class Manager extends PublicEmitter implements IGroupManager {
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->dispatcher, $this->userManager, $this, $displayName);
return $this->cachedGroups[$gid];
}
@@ -239,7 +238,7 @@ class Manager extends PublicEmitter implements IGroupManager {
* @return \OC\Group\Group[]
*/
public function search($search, $limit = null, $offset = null) {
- $groups = array();
+ $groups = [];
foreach ($this->backends as $backend) {
$groupIds = $backend->getGroups($search, $limit, $offset);
foreach ($groupIds as $groupId) {
@@ -276,7 +275,7 @@ class Manager extends PublicEmitter implements IGroupManager {
if (isset($this->cachedUserGroups[$uid])) {
return $this->cachedUserGroups[$uid];
}
- $groups = array();
+ $groups = [];
foreach ($this->backends as $backend) {
$groupIds = $backend->getUserGroups($uid);
if (is_array($groupIds)) {
@@ -351,11 +350,11 @@ class Manager extends PublicEmitter implements IGroupManager {
public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
$group = $this->get($gid);
if(is_null($group)) {
- return array();
+ return [];
}
$search = trim($search);
- $groupUsers = array();
+ $groupUsers = [];
if(!empty($search)) {
// only user backends have the capability to do a complex search for users
@@ -384,7 +383,7 @@ class Manager extends PublicEmitter implements IGroupManager {
$groupUsers = $group->searchUsers('', $limit, $offset);
}
- $matchingUsers = array();
+ $matchingUsers = [];
foreach($groupUsers as $groupUser) {
$matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName();
}
diff --git a/lib/private/Server.php b/lib/private/Server.php
index 8a49a8a8a94..68aba3645cf 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -316,14 +316,13 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias('LazyRootFolder', \OCP\Files\IRootFolder::class);
$this->registerService(\OC\User\Manager::class, function (Server $c) {
- $config = $c->getConfig();
- return new \OC\User\Manager($config);
+ return new \OC\User\Manager($c->getConfig(), $c->getEventDispatcher());
});
$this->registerAlias('UserManager', \OC\User\Manager::class);
$this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class);
$this->registerService(\OCP\IGroupManager::class, function (Server $c) {
- $groupManager = new \OC\Group\Manager($this->getUserManager(), $this->getLogger());
+ $groupManager = new \OC\Group\Manager($this->getUserManager(), $c->getEventDispatcher(), $this->getLogger());
$groupManager->listen('\OC\Group', 'preCreate', function ($gid) {
\OC_Hook::emit('OC_Group', 'pre_createGroup', array('run' => true, 'gid' => $gid));
});
@@ -1095,6 +1094,8 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias(\OCP\Collaboration\AutoComplete\IManager::class, \OC\Collaboration\AutoComplete\Manager::class);
+ $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class);
+
$this->registerService('SettingsManager', function (Server $c) {
$manager = new \OC\Settings\Manager(
$c->getLogger(),
diff --git a/lib/private/User/Manager.php b/lib/private/User/Manager.php
index f1a2029a7d5..62f02915c39 100644
--- a/lib/private/User/Manager.php
+++ b/lib/private/User/Manager.php
@@ -39,6 +39,7 @@ use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\UserInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Class Manager
@@ -68,16 +69,14 @@ class Manager extends PublicEmitter implements IUserManager {
*/
private $cachedUsers = array();
- /**
- * @var \OCP\IConfig $config
- */
+ /** @var IConfig */
private $config;
+ /** @var EventDispatcherInterface */
+ private $dispatcher;
- /**
- * @param \OCP\IConfig $config
- */
- public function __construct(IConfig $config) {
+ public function __construct(IConfig $config, EventDispatcherInterface $dispatcher) {
$this->config = $config;
+ $this->dispatcher = $dispatcher;
$cachedUsers = &$this->cachedUsers;
$this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) {
/** @var \OC\User\User $user */
@@ -156,7 +155,7 @@ class Manager extends PublicEmitter implements IUserManager {
return $this->cachedUsers[$uid];
}
- $user = new User($uid, $backend, $this, $this->config);
+ $user = new User($uid, $backend, $this->dispatcher, $this, $this->config);
if ($cacheUser) {
$this->cachedUsers[$uid] = $user;
}
diff --git a/lib/private/User/User.php b/lib/private/User/User.php
index 06dd47b0887..17fa022b1b7 100644
--- a/lib/private/User/User.php
+++ b/lib/private/User/User.php
@@ -42,30 +42,34 @@ use OCP\IUser;
use OCP\IConfig;
use OCP\UserInterface;
use \OCP\IUserBackend;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Symfony\Component\EventDispatcher\GenericEvent;
class User implements IUser {
- /** @var string $uid */
+ /** @var string */
private $uid;
- /** @var string $displayName */
+ /** @var string */
private $displayName;
- /** @var UserInterface $backend */
+ /** @var UserInterface|null */
private $backend;
+ /** @var EventDispatcherInterface */
+ private $dispatcher;
- /** @var bool $enabled */
+ /** @var bool */
private $enabled;
- /** @var Emitter|Manager $emitter */
+ /** @var Emitter|Manager */
private $emitter;
- /** @var string $home */
+ /** @var string */
private $home;
- /** @var int $lastLogin */
+ /** @var int */
private $lastLogin;
- /** @var \OCP\IConfig $config */
+ /** @var \OCP\IConfig */
private $config;
/** @var IAvatarManager */
@@ -74,16 +78,10 @@ class User implements IUser {
/** @var IURLGenerator */
private $urlGenerator;
- /**
- * @param string $uid
- * @param UserInterface $backend
- * @param \OC\Hooks\Emitter $emitter
- * @param IConfig|null $config
- * @param IURLGenerator $urlGenerator
- */
- public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $urlGenerator = null) {
+ public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) {
$this->uid = $uid;
$this->backend = $backend;
+ $this->dispatcher = $dispatcher;
$this->emitter = $emitter;
if(is_null($config)) {
$config = \OC::$server->getConfig();
@@ -115,7 +113,7 @@ class User implements IUser {
public function getDisplayName() {
if (!isset($this->displayName)) {
$displayName = '';
- if ($this->backend and $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) {
+ if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) {
// get display name and strip whitespace from the beginning and end of it
$backendDisplayName = $this->backend->getDisplayName($this->uid);
if (is_string($backendDisplayName)) {
@@ -199,6 +197,7 @@ class User implements IUser {
* @return bool
*/
public function delete() {
+ $this->dispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this));
if ($this->emitter) {
$this->emitter->emit('\OC\User', 'preDelete', array($this));
}
@@ -243,6 +242,7 @@ class User implements IUser {
$accountManager = \OC::$server->query(AccountManager::class);
$accountManager->deleteUser($this);
+ $this->dispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this));
if ($this->emitter) {
$this->emitter->emit('\OC\User', 'postDelete', array($this));
}
@@ -258,11 +258,19 @@ class User implements IUser {
* @return bool
*/
public function setPassword($password, $recoveryPassword = null) {
+ $this->dispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [
+ 'password' => $password,
+ 'recoveryPassword' => $recoveryPassword,
+ ]));
if ($this->emitter) {
$this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword));
}
if ($this->backend->implementsActions(Backend::SET_PASSWORD)) {
$result = $this->backend->setPassword($this->uid, $password);
+ $this->dispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [
+ 'password' => $password,
+ 'recoveryPassword' => $recoveryPassword,
+ ]));
if ($this->emitter) {
$this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword));
}
@@ -455,6 +463,11 @@ class User implements IUser {
}
public function triggerChange($feature, $value = null, $oldValue = null) {
+ $this->dispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [
+ 'feature' => $feature,
+ 'value' => $value,
+ 'oldValue' => $oldValue,
+ ]));
if ($this->emitter) {
$this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue));
}