Merge pull request #22775 from nextcloud/fix/noid/stable19-ldap-group-update-events

[stable19] makes LDAP's group updater dispatch type events
This commit is contained in:
Roeland Jago Douma 2020-09-11 08:45:49 +02:00 committed by GitHub
commit 9e88559e94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 7 deletions

View File

@ -43,6 +43,9 @@ 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\UserAddedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\ILogger;
class UpdateGroups extends \OC\BackgroundJob\TimedJob {
@ -93,6 +96,11 @@ class UpdateGroups extends \OC\BackgroundJob\TimedJob {
* @param string[] $groups
*/
private static function handleKnownGroups($groups) {
/** @var IEventDispatcher $dispatcher */
$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`
@ -104,15 +112,19 @@ 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);
$hasChanged = true;
}
foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
\OCP\Util::emitHook('OC_User', 'post_addToGroup', ['uid' => $addedUser, 'gid' => $group]);
$userObject = $userManager->get($addedUser);
$dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
\OCP\Util::writeLog('user_ldap',
'bgJ "updateGroups" "'.$addedUser.'" added to "'.$group.'".',
ILogger::INFO);

View File

@ -901,7 +901,6 @@ 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 */

View 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']);
}
}