2012-08-22 12:31:58 +04:00
|
|
|
|
<?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>
|
2020-03-31 11:49:10 +03:00
|
|
|
|
* @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>
|
2017-11-06 17:56:42 +03:00
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
|
* @author Roger Szabo <roger.szabo@web.de>
|
2016-03-01 19:25:15 +03:00
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2012-08-22 12:31:58 +04:00
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
|
* @license AGPL-3.0
|
2012-08-22 12:31:58 +04:00
|
|
|
|
*
|
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.
|
2012-08-22 12:31:58 +04:00
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
2012-08-22 12:31:58 +04:00
|
|
|
|
* 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.
|
2012-08-22 12:31:58 +04:00
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2012-08-22 12:31:58 +04:00
|
|
|
|
*
|
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
|
2016-05-12 17:46:41 +03:00
|
|
|
|
namespace OCA\User_LDAP\Jobs;
|
2012-08-22 12:31:58 +04:00
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
|
use OC\BackgroundJob\TimedJob;
|
|
|
|
|
use OCA\User_LDAP\Group_Proxy;
|
2020-07-07 22:17:25 +03:00
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2020-09-10 13:57:10 +03:00
|
|
|
|
use OCP\Group\Events\UserAddedEvent;
|
2020-07-07 22:17:25 +03:00
|
|
|
|
use OCP\Group\Events\UserRemovedEvent;
|
2018-04-25 16:22:28 +03:00
|
|
|
|
use OCP\ILogger;
|
2014-12-20 18:09:04 +03:00
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
|
class UpdateGroups extends TimedJob {
|
|
|
|
|
private $groupsFromDB;
|
2012-08-22 12:31:58 +04:00
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
|
/** @var Group_Proxy */
|
|
|
|
|
private $groupBackend;
|
2012-08-22 12:31:58 +04:00
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
|
public function __construct(Group_Proxy $groupBackend) {
|
|
|
|
|
$this->interval = $this->getRefreshInterval();
|
|
|
|
|
$this->groupBackend = $groupBackend;
|
2013-04-21 01:27:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
2014-05-13 15:29:25 +04:00
|
|
|
|
* @param mixed $argument
|
2014-05-11 17:17:27 +04:00
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
|
public function run($argument) {
|
2020-10-22 12:25:33 +03:00
|
|
|
|
$this->updateGroups();
|
2013-04-21 01:27:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
|
public function updateGroups() {
|
2018-04-25 16:22:28 +03:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
|
$knownGroups = array_keys($this->getKnownGroups());
|
|
|
|
|
$actualGroups = $this->groupBackend->getGroups();
|
2012-08-22 12:31:58 +04:00
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
|
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.',
|
2018-04-25 16:22:28 +03:00
|
|
|
|
ILogger::INFO);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
|
$this->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
|
|
|
|
|
$this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
|
|
|
|
|
$this->handleRemovedGroups(array_diff($knownGroups, $actualGroups));
|
2012-08-22 12:31:58 +04:00
|
|
|
|
|
2018-04-25 16:22:28 +03:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2020-10-22 12:25:33 +03:00
|
|
|
|
private function getRefreshInterval() {
|
2012-08-22 14:04:32 +04:00
|
|
|
|
//defaults to every hour
|
2018-01-13 16:25:04 +03:00
|
|
|
|
return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
|
2012-08-22 14:04:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
2014-05-15 16:12:17 +04:00
|
|
|
|
* @param string[] $groups
|
2014-05-11 17:17:27 +04:00
|
|
|
|
*/
|
2020-10-22 12:25:33 +03:00
|
|
|
|
private function handleKnownGroups($groups) {
|
2020-09-10 13:57:10 +03:00
|
|
|
|
/** @var IEventDispatcher $dispatcher */
|
2020-07-07 22:17:25 +03:00
|
|
|
|
$dispatcher = \OC::$server->query(IEventDispatcher::class);
|
|
|
|
|
$groupManager = \OC::$server->getGroupManager();
|
|
|
|
|
$userManager = \OC::$server->getUserManager();
|
|
|
|
|
|
2018-04-25 16:22:28 +03:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', ILogger::DEBUG);
|
2018-03-26 14:07:12 +03:00
|
|
|
|
$query = \OC_DB::prepare('
|
2012-08-25 03:52:27 +04:00
|
|
|
|
UPDATE `*PREFIX*ldap_group_members`
|
|
|
|
|
SET `owncloudusers` = ?
|
|
|
|
|
WHERE `owncloudname` = ?
|
2012-08-22 12:31:58 +04:00
|
|
|
|
');
|
2020-10-22 12:25:33 +03:00
|
|
|
|
if (!is_array($this->groupsFromDB)) {
|
|
|
|
|
$this->getKnownGroups();
|
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
|
foreach ($groups as $group) {
|
2020-10-22 12:25:33 +03:00
|
|
|
|
$knownUsers = unserialize($this->groupsFromDB[$group]['owncloudusers']);
|
|
|
|
|
$actualUsers = $this->groupBackend->usersInGroup($group);
|
2014-05-16 00:47:28 +04:00
|
|
|
|
$hasChanged = false;
|
2020-07-07 22:17:25 +03:00
|
|
|
|
|
|
|
|
|
$groupObject = $groupManager->get($group);
|
2020-04-10 15:19:56 +03:00
|
|
|
|
foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
|
2020-07-07 22:17:25 +03:00
|
|
|
|
$userObject = $userManager->get($removedUser);
|
|
|
|
|
$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.'".',
|
2018-04-25 16:22:28 +03:00
|
|
|
|
ILogger::INFO);
|
2014-05-16 00:47:28 +04:00
|
|
|
|
$hasChanged = true;
|
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
|
foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
|
2020-09-10 13:57:10 +03:00
|
|
|
|
$userObject = $userManager->get($addedUser);
|
|
|
|
|
$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.'".',
|
2018-04-25 16:22:28 +03:00
|
|
|
|
ILogger::INFO);
|
2014-05-16 00:47:28 +04:00
|
|
|
|
$hasChanged = true;
|
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
|
if ($hasChanged) {
|
2020-03-26 11:30:18 +03:00
|
|
|
|
$query->execute([serialize($actualUsers), $group]);
|
2014-05-16 00:47:28 +04:00
|
|
|
|
}
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
2013-02-15 01:16:48 +04:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – FINISHED dealing with known Groups.',
|
2018-04-25 16:22:28 +03:00
|
|
|
|
ILogger::DEBUG);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
2014-05-15 16:12:17 +04:00
|
|
|
|
* @param string[] $createdGroups
|
2014-05-11 17:17:27 +04:00
|
|
|
|
*/
|
2020-10-22 12:25:33 +03:00
|
|
|
|
private function handleCreatedGroups($createdGroups) {
|
2018-04-25 16:22:28 +03:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
|
2018-03-26 14:07:12 +03:00
|
|
|
|
$query = \OC_DB::prepare('
|
2012-08-22 12:31:58 +04:00
|
|
|
|
INSERT
|
2012-08-25 03:52:27 +04:00
|
|
|
|
INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
|
2012-08-22 12:31:58 +04:00
|
|
|
|
VALUES (?, ?)
|
|
|
|
|
');
|
2020-04-10 15:19:56 +03:00
|
|
|
|
foreach ($createdGroups as $createdGroup) {
|
2013-02-15 01:16:48 +04:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
|
2018-04-25 16:22:28 +03:00
|
|
|
|
ILogger::INFO);
|
2020-10-22 12:25:33 +03:00
|
|
|
|
$users = serialize($this->groupBackend->usersInGroup($createdGroup));
|
2020-03-26 11:30:18 +03:00
|
|
|
|
$query->execute([$createdGroup, $users]);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
2013-02-15 01:16:48 +04:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – FINISHED dealing with created Groups.',
|
2018-04-25 16:22:28 +03:00
|
|
|
|
ILogger::DEBUG);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
2014-05-15 16:12:17 +04:00
|
|
|
|
* @param string[] $removedGroups
|
2014-05-11 17:17:27 +04:00
|
|
|
|
*/
|
2020-10-22 12:25:33 +03:00
|
|
|
|
private function handleRemovedGroups($removedGroups) {
|
2018-04-25 16:22:28 +03:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
|
2018-03-26 14:07:12 +03:00
|
|
|
|
$query = \OC_DB::prepare('
|
2012-08-22 12:31:58 +04:00
|
|
|
|
DELETE
|
2012-08-25 03:52:27 +04:00
|
|
|
|
FROM `*PREFIX*ldap_group_members`
|
|
|
|
|
WHERE `owncloudname` = ?
|
2012-08-22 12:31:58 +04:00
|
|
|
|
');
|
2020-04-10 15:19:56 +03:00
|
|
|
|
foreach ($removedGroups as $removedGroup) {
|
2013-02-15 01:16:48 +04:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
|
2018-04-25 16:22:28 +03:00
|
|
|
|
ILogger::INFO);
|
2020-03-26 11:30:18 +03:00
|
|
|
|
$query->execute([$removedGroup]);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
2013-02-15 01:16:48 +04:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – FINISHED dealing with removed groups.',
|
2018-04-25 16:22:28 +03:00
|
|
|
|
ILogger::DEBUG);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-10-22 12:25:33 +03:00
|
|
|
|
private function getKnownGroups() {
|
|
|
|
|
if (is_array($this->groupsFromDB)) {
|
|
|
|
|
$this->groupsFromDB;
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
2018-03-26 14:07:12 +03:00
|
|
|
|
$query = \OC_DB::prepare('
|
2012-08-25 03:52:27 +04:00
|
|
|
|
SELECT `owncloudname`, `owncloudusers`
|
|
|
|
|
FROM `*PREFIX*ldap_group_members`
|
2012-08-22 12:31:58 +04:00
|
|
|
|
');
|
|
|
|
|
$result = $query->execute()->fetchAll();
|
2020-10-22 12:25:33 +03:00
|
|
|
|
$this->groupsFromDB = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
|
foreach ($result as $dataset) {
|
2020-10-22 12:25:33 +03:00
|
|
|
|
$this->groupsFromDB[$dataset['owncloudname']] = $dataset;
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 12:25:33 +03:00
|
|
|
|
return $this->groupsFromDB;
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
2013-02-15 01:16:48 +04:00
|
|
|
|
}
|