From a6977bad5508d88e0aecd274a43340591abcf001 Mon Sep 17 00:00:00 2001 From: Tobia De Koninck Date: Sun, 13 Aug 2017 16:23:06 +0200 Subject: [PATCH] Update roster according to adding/removing of users to groups --- appinfo/application.php | 3 ++- lib/hooks.php | 29 ++++++++++++++++++++++++++++- lib/rosterpush.php | 18 ++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/appinfo/application.php b/appinfo/application.php index 6a1e10f6..2d1646ed 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -237,7 +237,8 @@ public function __construct(array $urlParams=array()){ $c->query('ServerContainer')->getUserSession(), $c->query('RosterPush'), $c->query('PresenceMapper'), - $c->query('StanzaMapper') + $c->query('StanzaMapper'), + $c->query('ServerContainer')->query('OCP\IGroupManager') ); }); diff --git a/lib/hooks.php b/lib/hooks.php index c9ecba55..ad58d4c7 100644 --- a/lib/hooks.php +++ b/lib/hooks.php @@ -4,6 +4,8 @@ use OCA\OJSXC\Db\PresenceMapper; use OCA\OJSXC\Db\StanzaMapper; +use OCP\IGroup; +use OCP\IGroupManager; use OCP\IUserManager; use OCP\IUser; @@ -32,18 +34,30 @@ class Hooks */ private $stanzaMapper; + /** + * @var RosterPush + */ + private $rosterPush; + + /** + * @var IGroupManager + */ + private $groupManager; + public function __construct( IUserManager $userManager, IUserSession $userSession, RosterPush $rosterPush, PresenceMapper $presenceMapper, - StanzaMapper $stanzaMapper + StanzaMapper $stanzaMapper, + IGroupManager $groupManager ) { $this->userManager = $userManager; $this->userSession = $userSession; $this->rosterPush = $rosterPush; $this->presenceMapper = $presenceMapper; $this->stanzaMapper = $stanzaMapper; + $this->groupManager = $groupManager; } public function register() @@ -51,6 +65,8 @@ public function register() $this->userManager->listen('\OC\User', 'postCreateUser', [$this, 'onCreateUser']); $this->userManager->listen('\OC\User', 'postDelete', [$this, 'onDeleteUser']); $this->userSession->listen('\OC\User', 'changeUser', [$this, 'onChangeUser']); + $this->groupManager->listen('\OC\Group', 'postAddUser', [$this, 'onAddUserToGroup']); + $this->groupManager->listen('\OC\Group', 'postRemoveUser', [$this, 'onRemoveUserFromGroup']); } /** @@ -112,4 +128,15 @@ public function onChangeUser(IUser $user, $feature, $value) $this->onCreateUser($user, ''); } } + + public function onAddUserToGroup(IGroup $group, IUser $user) + { + $this->rosterPush->createOrUpdateRosterItem($user); + } + + public function onRemoveUserFromGroup(IGroup $group, IUser $user) + { + $this->rosterPush->removeRosterItemForUsersInGroup($group, $user->getUID()); + } + } diff --git a/lib/rosterpush.php b/lib/rosterpush.php index 455e709c..1602a26a 100644 --- a/lib/rosterpush.php +++ b/lib/rosterpush.php @@ -5,6 +5,7 @@ use OCA\OJSXC\Db\IQRosterPush; use OCA\OJSXC\Db\IQRosterPushMapper; use OCP\IDBConnection; +use OCP\IGroup; use OCP\IUserManager; use OCP\IUser; @@ -158,4 +159,21 @@ public function refreshRoster() return $stats; } + + public function removeRosterItemForUsersInGroup(IGroup $group, $userId) { + $iq = new IQRosterPush(); + $iq->setJid($userId); + $iq->setSubscription('remove'); + $iq->setFrom(''); + + + foreach ($group->getUsers() as $recipient) { + if ($recipient->getUID() !== $userId) { + $iq->setTo($recipient->getUID()); + $this->iqRosterPushMapper->insert($iq); + } + } + + } + }