]> source.dussan.org Git - nextcloud-server.git/commitdiff
Migrate OC_Group post_removeFromGroup hook to actual event object
authorMorris Jobke <hey@morrisjobke.de>
Tue, 7 Jul 2020 19:17:25 +0000 (21:17 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Thu, 30 Jul 2020 08:21:08 +0000 (10:21 +0200)
Ref #14552

This adds a BeforeUserRemovedEvent to the LDAP backend because it was missing. It's not really before, but we don't have the before state.

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
apps/user_ldap/lib/Jobs/UpdateGroups.php
lib/base.php
lib/private/Share20/Hooks.php
lib/private/User/User.php

index 58254bf41e9ecd0a5edf578f823cdead82994ce3..035caf0c33c71875e04f8ffe1eb8c26b41a8cee6 100644 (file)
@@ -44,6 +44,8 @@ use OCA\User_LDAP\LogWrapper;
 use OCA\User_LDAP\Mapping\GroupMapping;
 use OCA\User_LDAP\Mapping\UserMapping;
 use OCA\User_LDAP\User\Manager;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Group\Events\UserRemovedEvent;
 use OCP\ILogger;
 
 class UpdateGroups extends \OC\BackgroundJob\TimedJob {
@@ -94,6 +96,10 @@ class UpdateGroups extends \OC\BackgroundJob\TimedJob {
         * @param string[] $groups
         */
        private static function handleKnownGroups($groups) {
+               $dispatcher = \OC::$server->query(IEventDispatcher::class);
+               $groupManager = \OC::$server->getGroupManager();
+               $userManager = \OC::$server->getUserManager();
+
                \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', ILogger::DEBUG);
                $query = \OC_DB::prepare('
                        UPDATE `*PREFIX*ldap_group_members`
@@ -105,8 +111,11 @@ class UpdateGroups extends \OC\BackgroundJob\TimedJob {
                        $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
                        $actualUsers = self::getGroupBE()->usersInGroup($group);
                        $hasChanged = false;
+
+                       $groupObject = $groupManager->get($group);
                        foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
-                               \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', ['uid' => $removedUser, 'gid' => $group]);
+                               $userObject = $userManager->get($removedUser);
+                               $dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
                                \OCP\Util::writeLog('user_ldap',
                                'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
                                        ILogger::INFO);
index a491cb11c80795faeca2c99bbecb627fbc3f50a4..8b8e8e5fe32c2bc82e9452ac6a64867f6ccc17e5 100644 (file)
@@ -61,6 +61,7 @@
  *
  */
 
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\Group\Events\UserRemovedEvent;
 use OCP\ILogger;
 use OCP\Share;
@@ -898,11 +899,10 @@ class OC {
        public static function registerShareHooks() {
                if (\OC::$server->getSystemConfig()->getValue('installed')) {
                        OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser');
-                       OC_Hook::connect('OC_User', 'post_removeFromGroup', Hooks::class, 'post_removeFromGroupLDAP');
                        OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup');
 
-                       /** @var \OCP\EventDispatcher\IEventDispatcher $dispatcher */
-                       $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class);
+                       /** @var IEventDispatcher $dispatcher */
+                       $dispatcher = \OC::$server->get(IEventDispatcher::class);
                        $dispatcher->addServiceListener(UserRemovedEvent::class, \OC\Share20\UserRemovedListener::class);
                }
        }
index 0e41e20a2cd0f921f8a2acfcaeedecec64c49a6d..b596123bbe0cc9e0366db9471ff2bce46ea77c35 100644 (file)
@@ -30,8 +30,4 @@ class Hooks {
        public static function post_deleteGroup($arguments) {
                \OC::$server->getShareManager()->groupDeleted($arguments['gid']);
        }
-
-       public static function post_removeFromGroupLDAP($arguments) {
-               \OC::$server->getShareManager()->userDeletedFromGroup($arguments['uid'], $arguments['gid']);
-       }
 }
index 08bbce4701b103ce3b6dc96f5e170f929ad09cc5..199ddb569dda4f99082c24e494942e20b56a3ac3 100644 (file)
@@ -41,6 +41,9 @@ use OC\Avatar\AvatarManager;
 use OC\Files\Cache\Storage;
 use OC\Hooks\Emitter;
 use OC_Helper;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Group\Events\BeforeUserRemovedEvent;
+use OCP\Group\Events\UserRemovedEvent;
 use OCP\IAvatarManager;
 use OCP\IConfig;
 use OCP\IImage;
@@ -63,6 +66,9 @@ class User implements IUser {
        /** @var EventDispatcherInterface */
        private $dispatcher;
 
+       /** @var IEventDispatcher */
+       private $newDispatcher;
+
        /** @var bool */
        private $enabled;
 
@@ -100,6 +106,8 @@ class User implements IUser {
                if (is_null($this->urlGenerator)) {
                        $this->urlGenerator = \OC::$server->getURLGenerator();
                }
+               // TODO: inject
+               $this->newDispatcher = \OC::$server->query(IEventDispatcher::class);
        }
 
        /**
@@ -219,9 +227,9 @@ class User implements IUser {
                        foreach ($groupManager->getUserGroupIds($this) as $groupId) {
                                $group = $groupManager->get($groupId);
                                if ($group) {
-                                       \OC_Hook::emit("OC_Group", "pre_removeFromGroup", ["run" => true, "uid" => $this->uid, "gid" => $groupId]);
+                                       $this->newDispatcher->dispatchTyped(new BeforeUserRemovedEvent($group, $this));
                                        $group->removeUser($this);
-                                       \OC_Hook::emit("OC_User", "post_removeFromGroup", ["uid" => $this->uid, "gid" => $groupId]);
+                                       $this->newDispatcher->dispatchTyped(new UserRemovedEvent($group, $this));
                                }
                        }
                        // Delete the user's keys in preferences