2012-08-22 08:31:58 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-07-21 14:49:16 +00:00
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2017-11-06 14:56:42 +00:00
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
|
* @author Roger Szabo <roger.szabo@web.de>
|
|
|
|
|
* @author root <root@localhost.localdomain>
|
2016-03-01 16:25:15 +00:00
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2017-11-06 14:56:42 +00:00
|
|
|
|
* @author Vinicius Cubas Brand <vinicius@eita.org.br>
|
2012-08-22 08:31:58 +00:00
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
|
* @license AGPL-3.0
|
2012-08-22 08:31:58 +00:00
|
|
|
|
*
|
2015-03-26 10:44:34 +00: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 08:31:58 +00:00
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
2012-08-22 08:31:58 +00:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
2012-08-22 08:31:58 +00:00
|
|
|
|
*
|
2015-03-26 10:44:34 +00: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/>
|
2012-08-22 08:31:58 +00:00
|
|
|
|
*
|
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
|
2016-05-12 14:46:41 +00:00
|
|
|
|
namespace OCA\User_LDAP\Jobs;
|
2012-08-22 08:31:58 +00:00
|
|
|
|
|
2016-05-12 14:46:41 +00:00
|
|
|
|
use OCA\User_LDAP\Access;
|
|
|
|
|
use OCA\User_LDAP\Connection;
|
2016-05-12 14:31:35 +00:00
|
|
|
|
use OCA\User_LDAP\FilesystemHelper;
|
|
|
|
|
use OCA\User_LDAP\Helper;
|
|
|
|
|
use OCA\User_LDAP\LDAP;
|
|
|
|
|
use OCA\User_LDAP\LogWrapper;
|
2014-12-20 15:09:04 +00:00
|
|
|
|
use OCA\User_LDAP\Mapping\GroupMapping;
|
2015-01-06 16:50:06 +00:00
|
|
|
|
use OCA\User_LDAP\Mapping\UserMapping;
|
2016-05-12 14:16:06 +00:00
|
|
|
|
use OCA\User_LDAP\User\Manager;
|
2018-04-25 13:22:28 +00:00
|
|
|
|
use OCP\ILogger;
|
2014-12-20 15:09:04 +00:00
|
|
|
|
|
2016-05-12 14:46:41 +00:00
|
|
|
|
class UpdateGroups extends \OC\BackgroundJob\TimedJob {
|
2012-08-22 08:31:58 +00:00
|
|
|
|
static private $groupsFromDB;
|
|
|
|
|
|
|
|
|
|
static private $groupBE;
|
|
|
|
|
|
2013-04-20 21:27:46 +00:00
|
|
|
|
public function __construct(){
|
|
|
|
|
$this->interval = self::getRefreshInterval();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
|
/**
|
2014-05-13 11:29:25 +00:00
|
|
|
|
* @param mixed $argument
|
2014-05-11 13:17:27 +00:00
|
|
|
|
*/
|
2013-04-20 21:27:46 +00:00
|
|
|
|
public function run($argument){
|
2016-05-12 14:46:41 +00:00
|
|
|
|
self::updateGroups();
|
2013-04-20 21:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 08:31:58 +00:00
|
|
|
|
static public function updateGroups() {
|
2018-04-25 13:22:28 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
|
2012-08-22 08:31:58 +00:00
|
|
|
|
|
|
|
|
|
$knownGroups = array_keys(self::getKnownGroups());
|
|
|
|
|
$actualGroups = self::getGroupBE()->getGroups();
|
|
|
|
|
|
|
|
|
|
if(empty($actualGroups) && empty($knownGroups)) {
|
2013-02-14 21:16:48 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
|
2018-04-25 13:22:28 +00:00
|
|
|
|
ILogger::INFO);
|
2012-08-22 08:31:58 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
|
|
|
|
|
self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
|
|
|
|
|
self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
|
|
|
|
|
|
2018-04-25 13:22:28 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
|
2012-08-22 08:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2012-08-22 10:04:32 +00:00
|
|
|
|
static private function getRefreshInterval() {
|
|
|
|
|
//defaults to every hour
|
2018-01-13 13:25:04 +00:00
|
|
|
|
return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
|
2012-08-22 10:04:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
|
/**
|
2014-05-15 12:12:17 +00:00
|
|
|
|
* @param string[] $groups
|
2014-05-11 13:17:27 +00:00
|
|
|
|
*/
|
2012-08-22 08:31:58 +00:00
|
|
|
|
static private function handleKnownGroups($groups) {
|
2018-04-25 13:22:28 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', ILogger::DEBUG);
|
2018-03-26 11:07:12 +00:00
|
|
|
|
$query = \OC_DB::prepare('
|
2012-08-24 23:52:27 +00:00
|
|
|
|
UPDATE `*PREFIX*ldap_group_members`
|
|
|
|
|
SET `owncloudusers` = ?
|
|
|
|
|
WHERE `owncloudname` = ?
|
2012-08-22 08:31:58 +00:00
|
|
|
|
');
|
|
|
|
|
foreach($groups as $group) {
|
|
|
|
|
//we assume, that self::$groupsFromDB has been retrieved already
|
2014-05-15 20:47:28 +00:00
|
|
|
|
$knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
|
|
|
|
|
$actualUsers = self::getGroupBE()->usersInGroup($group);
|
|
|
|
|
$hasChanged = false;
|
|
|
|
|
foreach(array_diff($knownUsers, $actualUsers) as $removedUser) {
|
|
|
|
|
\OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group));
|
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-02-14 21:16:48 +00:00
|
|
|
|
'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
|
2018-04-25 13:22:28 +00:00
|
|
|
|
ILogger::INFO);
|
2014-05-15 20:47:28 +00:00
|
|
|
|
$hasChanged = true;
|
|
|
|
|
}
|
|
|
|
|
foreach(array_diff($actualUsers, $knownUsers) as $addedUser) {
|
|
|
|
|
\OCP\Util::emitHook('OC_User', 'post_addToGroup', array('uid' => $addedUser, 'gid' => $group));
|
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
2013-02-14 21:16:48 +00:00
|
|
|
|
'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
|
2018-04-25 13:22:28 +00:00
|
|
|
|
ILogger::INFO);
|
2014-05-15 20:47:28 +00:00
|
|
|
|
$hasChanged = true;
|
|
|
|
|
}
|
|
|
|
|
if($hasChanged) {
|
2012-08-22 08:31:58 +00:00
|
|
|
|
$query->execute(array(serialize($actualUsers), $group));
|
2014-05-15 20:47:28 +00:00
|
|
|
|
}
|
2012-08-22 08:31:58 +00:00
|
|
|
|
}
|
2013-02-14 21:16:48 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – FINISHED dealing with known Groups.',
|
2018-04-25 13:22:28 +00:00
|
|
|
|
ILogger::DEBUG);
|
2012-08-22 08:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
|
/**
|
2014-05-15 12:12:17 +00:00
|
|
|
|
* @param string[] $createdGroups
|
2014-05-11 13:17:27 +00:00
|
|
|
|
*/
|
2012-08-22 08:31:58 +00:00
|
|
|
|
static private function handleCreatedGroups($createdGroups) {
|
2018-04-25 13:22:28 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
|
2018-03-26 11:07:12 +00:00
|
|
|
|
$query = \OC_DB::prepare('
|
2012-08-22 08:31:58 +00:00
|
|
|
|
INSERT
|
2012-08-24 23:52:27 +00:00
|
|
|
|
INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
|
2012-08-22 08:31:58 +00:00
|
|
|
|
VALUES (?, ?)
|
|
|
|
|
');
|
|
|
|
|
foreach($createdGroups as $createdGroup) {
|
2013-02-14 21:16:48 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
|
2018-04-25 13:22:28 +00:00
|
|
|
|
ILogger::INFO);
|
2012-08-22 08:31:58 +00:00
|
|
|
|
$users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
|
2014-05-15 20:47:28 +00:00
|
|
|
|
$query->execute(array($createdGroup, $users));
|
2012-08-22 08:31:58 +00:00
|
|
|
|
}
|
2013-02-14 21:16:48 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – FINISHED dealing with created Groups.',
|
2018-04-25 13:22:28 +00:00
|
|
|
|
ILogger::DEBUG);
|
2012-08-22 08:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
|
/**
|
2014-05-15 12:12:17 +00:00
|
|
|
|
* @param string[] $removedGroups
|
2014-05-11 13:17:27 +00:00
|
|
|
|
*/
|
2012-08-22 08:31:58 +00:00
|
|
|
|
static private function handleRemovedGroups($removedGroups) {
|
2018-04-25 13:22:28 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
|
2018-03-26 11:07:12 +00:00
|
|
|
|
$query = \OC_DB::prepare('
|
2012-08-22 08:31:58 +00:00
|
|
|
|
DELETE
|
2012-08-24 23:52:27 +00:00
|
|
|
|
FROM `*PREFIX*ldap_group_members`
|
|
|
|
|
WHERE `owncloudname` = ?
|
2012-08-22 08:31:58 +00:00
|
|
|
|
');
|
|
|
|
|
foreach($removedGroups as $removedGroup) {
|
2013-02-14 21:16:48 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
|
2018-04-25 13:22:28 +00:00
|
|
|
|
ILogger::INFO);
|
2014-05-15 20:47:28 +00:00
|
|
|
|
$query->execute(array($removedGroup));
|
2012-08-22 08:31:58 +00:00
|
|
|
|
}
|
2013-02-14 21:16:48 +00:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – FINISHED dealing with removed groups.',
|
2018-04-25 13:22:28 +00:00
|
|
|
|
ILogger::DEBUG);
|
2012-08-22 08:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
|
/**
|
2016-05-12 08:43:34 +00:00
|
|
|
|
* @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy
|
2014-05-11 13:17:27 +00:00
|
|
|
|
*/
|
2012-08-22 08:31:58 +00:00
|
|
|
|
static private function getGroupBE() {
|
|
|
|
|
if(!is_null(self::$groupBE)) {
|
|
|
|
|
return self::$groupBE;
|
|
|
|
|
}
|
2016-10-10 08:18:18 +00:00
|
|
|
|
$helper = new Helper(\OC::$server->getConfig());
|
2014-08-21 15:59:13 +00:00
|
|
|
|
$configPrefixes = $helper->getServerConfigurationPrefixes(true);
|
2013-11-06 15:25:31 +00:00
|
|
|
|
$ldapWrapper = new LDAP();
|
2013-09-10 15:11:02 +00:00
|
|
|
|
if(count($configPrefixes) === 1) {
|
2013-06-18 18:03:59 +00:00
|
|
|
|
//avoid the proxy when there is only one LDAP server configured
|
2015-01-06 23:52:18 +00:00
|
|
|
|
$dbc = \OC::$server->getDatabaseConnection();
|
2016-05-12 14:16:06 +00:00
|
|
|
|
$userManager = new Manager(
|
2014-06-12 11:32:58 +00:00
|
|
|
|
\OC::$server->getConfig(),
|
|
|
|
|
new FilesystemHelper(),
|
|
|
|
|
new LogWrapper(),
|
|
|
|
|
\OC::$server->getAvatarManager(),
|
2015-01-06 23:52:18 +00:00
|
|
|
|
new \OCP\Image(),
|
2016-01-19 14:48:38 +00:00
|
|
|
|
$dbc,
|
2017-03-31 07:16:22 +00:00
|
|
|
|
\OC::$server->getUserManager(),
|
|
|
|
|
\OC::$server->getNotificationManager());
|
2013-11-06 15:25:31 +00:00
|
|
|
|
$connector = new Connection($ldapWrapper, $configPrefixes[0]);
|
2018-03-19 14:53:30 +00:00
|
|
|
|
$ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server->getConfig(), \OC::$server->getUserManager());
|
2015-01-06 23:52:18 +00:00
|
|
|
|
$groupMapper = new GroupMapping($dbc);
|
|
|
|
|
$userMapper = new UserMapping($dbc);
|
2015-01-06 16:50:06 +00:00
|
|
|
|
$ldapAccess->setGroupMapper($groupMapper);
|
|
|
|
|
$ldapAccess->setUserMapper($userMapper);
|
2017-11-02 12:40:38 +00:00
|
|
|
|
self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess, \OC::$server->query('LDAPGroupPluginManager'));
|
2013-06-18 18:03:59 +00:00
|
|
|
|
} else {
|
2017-11-02 12:40:38 +00:00
|
|
|
|
self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper, \OC::$server->query('LDAPGroupPluginManager'));
|
2013-06-18 18:03:59 +00:00
|
|
|
|
}
|
2012-08-22 08:31:58 +00:00
|
|
|
|
|
|
|
|
|
return self::$groupBE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 13:17:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2012-08-22 08:31:58 +00:00
|
|
|
|
static private function getKnownGroups() {
|
|
|
|
|
if(is_array(self::$groupsFromDB)) {
|
|
|
|
|
return self::$groupsFromDB;
|
|
|
|
|
}
|
2018-03-26 11:07:12 +00:00
|
|
|
|
$query = \OC_DB::prepare('
|
2012-08-24 23:52:27 +00:00
|
|
|
|
SELECT `owncloudname`, `owncloudusers`
|
|
|
|
|
FROM `*PREFIX*ldap_group_members`
|
2012-08-22 08:31:58 +00:00
|
|
|
|
');
|
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
|
self::$groupsFromDB = array();
|
|
|
|
|
foreach($result as $dataset) {
|
2014-05-15 20:47:28 +00:00
|
|
|
|
self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
|
2012-08-22 08:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$groupsFromDB;
|
|
|
|
|
}
|
2013-02-14 21:16:48 +00:00
|
|
|
|
}
|