nextcloud/apps/user_ldap/lib/Jobs/UpdateGroups.php

246 lines
8.0 KiB
PHP
Raw Normal View History

<?php
/**
2016-07-21 17:49:16 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
2016-05-26 20:56:05 +03:00
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
2015-03-26 13:44:34 +03:00
* @author Bart Visscher <bartv@thisnet.nl>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
2016-07-21 17:49:16 +03:00
* @author Joas Schilling <coding@schilljs.com>
2015-03-26 13:44:34 +03:00
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
2016-05-26 20:56:05 +03:00
* @author Lukas Reschke <lukas@statuscode.ch>
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
2016-07-21 19:13:36 +03:00
* @author Robin Appelman <robin@icewind.nl>
2016-01-12 17:02:16 +03:00
* @author Robin McCorkell <robin@mccorkell.me.uk>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Roger Szabo <roger.szabo@web.de>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vinicius Cubas Brand <vinicius@eita.org.br>
*
2015-03-26 13:44:34 +03:00
* @license AGPL-3.0
*
2015-03-26 13:44:34 +03:00
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
2015-03-26 13:44:34 +03:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2015-03-26 13:44:34 +03:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
2015-03-26 13:44:34 +03:00
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
2016-05-12 17:46:41 +03:00
namespace OCA\User_LDAP\Jobs;
2016-05-12 17:46:41 +03:00
use OCA\User_LDAP\Access;
use OCA\User_LDAP\Connection;
2016-05-12 17:31:35 +03:00
use OCA\User_LDAP\FilesystemHelper;
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\LogWrapper;
use OCA\User_LDAP\Mapping\GroupMapping;
use OCA\User_LDAP\Mapping\UserMapping;
2016-05-12 17:16:06 +03:00
use OCA\User_LDAP\User\Manager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Events\UserAddedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\ILogger;
use OCP\IUser;
2016-05-12 17:46:41 +03:00
class UpdateGroups extends \OC\BackgroundJob\TimedJob {
private static $groupsFromDB;
private static $groupBE;
public function __construct() {
2013-04-21 01:27:46 +04:00
$this->interval = self::getRefreshInterval();
}
/**
2014-05-13 15:29:25 +04:00
* @param mixed $argument
*/
public function run($argument) {
2016-05-12 17:46:41 +03:00
self::updateGroups();
2013-04-21 01:27:46 +04:00
}
public static function updateGroups() {
\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
$knownGroups = array_keys(self::getKnownGroups());
$actualGroups = self::getGroupBE()->getGroups();
if (empty($actualGroups) && empty($knownGroups)) {
2013-02-15 01:16:48 +04:00
\OCP\Util::writeLog('user_ldap',
'bgJ "updateGroups" groups do not seem to be configured properly, aborting.',
ILogger::INFO);
return;
}
self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" Finished.', ILogger::DEBUG);
}
/**
* @return int
*/
private static function getRefreshInterval() {
//defaults to every hour
return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
}
/**
2014-05-15 16:12:17 +04:00
* @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`
SET `owncloudusers` = ?
WHERE `owncloudname` = ?
');
foreach ($groups as $group) {
//we assume, that self::$groupsFromDB has been retrieved already
2014-05-16 00:47:28 +04:00
$knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
$actualUsers = self::getGroupBE()->usersInGroup($group);
$hasChanged = false;
$groupObject = $groupManager->get($group);
foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
$userObject = $userManager->get($removedUser);
if ($userObject instanceof IUser) {
$dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
}
2014-05-16 00:47:28 +04:00
\OCP\Util::writeLog('user_ldap',
2013-02-15 01:16:48 +04:00
'bgJ "updateGroups" "'.$removedUser.'" removed from "'.$group.'".',
ILogger::INFO);
2014-05-16 00:47:28 +04:00
$hasChanged = true;
}
foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
$userObject = $userManager->get($addedUser);
if ($userObject instanceof IUser) {
$dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
}
2014-05-16 00:47:28 +04:00
\OCP\Util::writeLog('user_ldap',
2013-02-15 01:16:48 +04:00
'bgJ "updateGroups" "'.$addedUser.'" added to "'.$group.'".',
ILogger::INFO);
2014-05-16 00:47:28 +04:00
$hasChanged = true;
}
if ($hasChanged) {
$query->execute([serialize($actualUsers), $group]);
2014-05-16 00:47:28 +04:00
}
}
2013-02-15 01:16:48 +04:00
\OCP\Util::writeLog('user_ldap',
'bgJ "updateGroups" FINISHED dealing with known Groups.',
ILogger::DEBUG);
}
/**
2014-05-15 16:12:17 +04:00
* @param string[] $createdGroups
*/
private static function handleCreatedGroups($createdGroups) {
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" dealing with created Groups.', ILogger::DEBUG);
$query = \OC_DB::prepare('
INSERT
INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
VALUES (?, ?)
');
foreach ($createdGroups as $createdGroup) {
2013-02-15 01:16:48 +04:00
\OCP\Util::writeLog('user_ldap',
'bgJ "updateGroups" new group "'.$createdGroup.'" found.',
ILogger::INFO);
$users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
$query->execute([$createdGroup, $users]);
}
2013-02-15 01:16:48 +04:00
\OCP\Util::writeLog('user_ldap',
'bgJ "updateGroups" FINISHED dealing with created Groups.',
ILogger::DEBUG);
}
/**
2014-05-15 16:12:17 +04:00
* @param string[] $removedGroups
*/
private static function handleRemovedGroups($removedGroups) {
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" dealing with removed groups.', ILogger::DEBUG);
$query = \OC_DB::prepare('
DELETE
FROM `*PREFIX*ldap_group_members`
WHERE `owncloudname` = ?
');
foreach ($removedGroups as $removedGroup) {
2013-02-15 01:16:48 +04:00
\OCP\Util::writeLog('user_ldap',
'bgJ "updateGroups" group "'.$removedGroup.'" was removed.',
ILogger::INFO);
$query->execute([$removedGroup]);
}
2013-02-15 01:16:48 +04:00
\OCP\Util::writeLog('user_ldap',
'bgJ "updateGroups" FINISHED dealing with removed groups.',
ILogger::DEBUG);
}
/**
* @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy
*/
private static function getGroupBE() {
if (!is_null(self::$groupBE)) {
return self::$groupBE;
}
$helper = new Helper(\OC::$server->getConfig());
LDAP User Cleanup: Port from stable7 without further adjustements LDAP User Cleanup background job for user clean up adjust user backend for clean up register background job remove dead code dependency injection make Helper non-static for proper testing check whether it is OK to run clean up job. Do not forget to pass arguments. use correct method to get the config from server methods can be private, proper indirect testing is given no automatic user deletion make limit readable for test purposes make method less complex add first tests let preferences accept limit and offset for getUsersForValue DI via constructor does not work for background jobs after detecting, now we have retrieving deleted users and their details we need this method to be public for now finalize export method, add missing getter clean up namespaces and get rid of unnecessary files helper is not static anymore cleanup according to scrutinizer add cli tool to show deleted users uses are necessary after recent namespace change also remove user from mappings table on deletion add occ command to delete users fix use statement improve output big fixes / improvements PHP doc return true in userExists early for cleaning up deleted users bump version control state and interval with one config.php setting, now ldapUserCleanupInterval. 0 will disable it. enabled by default. improve doc rename cli method to be consistent with others introduce ldapUserCleanupInterval in sample config don't show last login as unix epoche start when no login happend less log output consistent namespace for OfflineUser rename GarbageCollector to DeletedUsersIndex and move it to user subdir fix unit tests add tests for deleteUser more test adjustements Conflicts: apps/user_ldap/ajax/clearMappings.php apps/user_ldap/appinfo/app.php apps/user_ldap/lib/access.php apps/user_ldap/lib/helper.php apps/user_ldap/tests/helper.php core/register_command.php lib/private/preferences.php lib/private/user.php add ldap:check-user to check user existance on the fly Conflicts: apps/user_ldap/lib/helper.php forgotten file PHPdoc fixes, no code change and don't forget to adjust tests
2014-08-21 19:59:13 +04:00
$configPrefixes = $helper->getServerConfigurationPrefixes(true);
$ldapWrapper = new LDAP();
if (count($configPrefixes) === 1) {
//avoid the proxy when there is only one LDAP server configured
2015-01-07 02:52:18 +03:00
$dbc = \OC::$server->getDatabaseConnection();
2016-05-12 17:16:06 +03:00
$userManager = new Manager(
2014-06-12 15:32:58 +04:00
\OC::$server->getConfig(),
new FilesystemHelper(),
new LogWrapper(),
\OC::$server->getAvatarManager(),
2015-01-07 02:52:18 +03:00
new \OCP\Image(),
$dbc,
\OC::$server->getUserManager(),
\OC::$server->getNotificationManager());
$connector = new Connection($ldapWrapper, $configPrefixes[0]);
$ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server->getConfig(), \OC::$server->getUserManager());
2015-01-07 02:52:18 +03:00
$groupMapper = new GroupMapping($dbc);
$userMapper = new UserMapping($dbc);
$ldapAccess->setGroupMapper($groupMapper);
$ldapAccess->setUserMapper($userMapper);
self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess, \OC::$server->query('LDAPGroupPluginManager'));
} else {
self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper, \OC::$server->query('LDAPGroupPluginManager'));
}
return self::$groupBE;
}
/**
* @return array
*/
private static function getKnownGroups() {
if (is_array(self::$groupsFromDB)) {
return self::$groupsFromDB;
}
$query = \OC_DB::prepare('
SELECT `owncloudname`, `owncloudusers`
FROM `*PREFIX*ldap_group_members`
');
$result = $query->execute()->fetchAll();
self::$groupsFromDB = [];
foreach ($result as $dataset) {
2014-05-16 00:47:28 +04:00
self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
}
return self::$groupsFromDB;
}
2013-02-15 01:16:48 +04:00
}