2012-08-22 12:31:58 +04:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ownCloud – LDAP Background Jobs
|
|
|
|
|
*
|
|
|
|
|
* @author Arthur Schiwon
|
|
|
|
|
* @copyright 2012 Arthur Schiwon blizzz@owncloud.com
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace OCA\user_ldap\lib;
|
|
|
|
|
|
2013-04-21 01:27:46 +04:00
|
|
|
|
class Jobs extends \OC\BackgroundJob\TimedJob {
|
2012-08-22 12:31:58 +04:00
|
|
|
|
static private $groupsFromDB;
|
|
|
|
|
|
|
|
|
|
static private $groupBE;
|
|
|
|
|
static private $connector;
|
|
|
|
|
|
2013-04-21 01:27:46 +04:00
|
|
|
|
public function __construct(){
|
|
|
|
|
$this->interval = self::getRefreshInterval();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
*/
|
2013-04-21 01:27:46 +04:00
|
|
|
|
public function run($argument){
|
|
|
|
|
Jobs::updateGroups();
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 12:31:58 +04:00
|
|
|
|
static public function updateGroups() {
|
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::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.',
|
|
|
|
|
\OCP\Util::INFO);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
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.', \OCP\Util::DEBUG);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2012-08-22 14:04:32 +04:00
|
|
|
|
static private function getRefreshInterval() {
|
|
|
|
|
//defaults to every hour
|
|
|
|
|
return \OCP\Config::getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
*/
|
2012-08-22 12:31:58 +04:00
|
|
|
|
static private function handleKnownGroups($groups) {
|
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG);
|
|
|
|
|
$query = \OCP\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
|
|
|
|
');
|
|
|
|
|
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;
|
|
|
|
|
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-15 01:16:48 +04:00
|
|
|
|
'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
|
|
|
|
|
\OCP\Util::INFO);
|
2014-05-16 00:47:28 +04: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-15 01:16:48 +04:00
|
|
|
|
'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
|
|
|
|
|
\OCP\Util::INFO);
|
2014-05-16 00:47:28 +04:00
|
|
|
|
$hasChanged = true;
|
|
|
|
|
}
|
|
|
|
|
if($hasChanged) {
|
2012-08-22 12:31:58 +04:00
|
|
|
|
$query->execute(array(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.',
|
|
|
|
|
\OCP\Util::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
|
|
|
|
*/
|
2012-08-22 12:31:58 +04:00
|
|
|
|
static private function handleCreatedGroups($createdGroups) {
|
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG);
|
|
|
|
|
$query = \OCP\DB::prepare('
|
|
|
|
|
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 (?, ?)
|
|
|
|
|
');
|
|
|
|
|
foreach($createdGroups as $createdGroup) {
|
2013-02-15 01:16:48 +04:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
|
|
|
|
|
\OCP\Util::INFO);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
$users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
|
2014-05-16 00:47:28 +04:00
|
|
|
|
$query->execute(array($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.',
|
|
|
|
|
\OCP\Util::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
|
|
|
|
*/
|
2012-08-22 12:31:58 +04:00
|
|
|
|
static private function handleRemovedGroups($removedGroups) {
|
|
|
|
|
\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG);
|
|
|
|
|
$query = \OCP\DB::prepare('
|
|
|
|
|
DELETE
|
2012-08-25 03:52:27 +04:00
|
|
|
|
FROM `*PREFIX*ldap_group_members`
|
|
|
|
|
WHERE `owncloudname` = ?
|
2012-08-22 12:31:58 +04:00
|
|
|
|
');
|
|
|
|
|
foreach($removedGroups as $removedGroup) {
|
2013-02-15 01:16:48 +04:00
|
|
|
|
\OCP\Util::writeLog('user_ldap',
|
|
|
|
|
'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
|
|
|
|
|
\OCP\Util::INFO);
|
2014-05-16 00:47:28 +04:00
|
|
|
|
$query->execute(array($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.',
|
|
|
|
|
\OCP\Util::DEBUG);
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
|
|
|
|
* @return \OCA\user_ldap\GROUP_LDAP|\OCA\user_ldap\Group_Proxy
|
|
|
|
|
*/
|
2012-08-22 12:31:58 +04:00
|
|
|
|
static private function getGroupBE() {
|
|
|
|
|
if(!is_null(self::$groupBE)) {
|
|
|
|
|
return self::$groupBE;
|
|
|
|
|
}
|
2013-06-18 22:03:59 +04:00
|
|
|
|
$configPrefixes = Helper::getServerConfigurationPrefixes(true);
|
2013-11-06 19:25:31 +04:00
|
|
|
|
$ldapWrapper = new LDAP();
|
2013-09-10 19:11:02 +04:00
|
|
|
|
if(count($configPrefixes) === 1) {
|
2013-06-18 22:03:59 +04:00
|
|
|
|
//avoid the proxy when there is only one LDAP server configured
|
2014-06-12 15:32:58 +04:00
|
|
|
|
$userManager = new user\Manager(
|
|
|
|
|
\OC::$server->getConfig(),
|
|
|
|
|
new FilesystemHelper(),
|
|
|
|
|
new LogWrapper(),
|
|
|
|
|
\OC::$server->getAvatarManager(),
|
|
|
|
|
new \OCP\Image());
|
2013-11-06 19:25:31 +04:00
|
|
|
|
$connector = new Connection($ldapWrapper, $configPrefixes[0]);
|
2014-06-12 15:32:58 +04:00
|
|
|
|
$ldapAccess = new Access($connector, $ldapWrapper, $userManager);
|
2013-11-06 19:25:31 +04:00
|
|
|
|
self::$groupBE = new \OCA\user_ldap\GROUP_LDAP($ldapAccess);
|
2013-06-18 22:03:59 +04:00
|
|
|
|
} else {
|
2013-09-10 19:11:02 +04:00
|
|
|
|
self::$groupBE = new \OCA\user_ldap\Group_Proxy($configPrefixes, $ldapWrapper);
|
2013-06-18 22:03:59 +04:00
|
|
|
|
}
|
2012-08-22 12:31:58 +04:00
|
|
|
|
|
|
|
|
|
return self::$groupBE;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 17:17:27 +04:00
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2012-08-22 12:31:58 +04:00
|
|
|
|
static private function getKnownGroups() {
|
|
|
|
|
if(is_array(self::$groupsFromDB)) {
|
|
|
|
|
return self::$groupsFromDB;
|
|
|
|
|
}
|
|
|
|
|
$query = \OCP\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();
|
|
|
|
|
self::$groupsFromDB = array();
|
|
|
|
|
foreach($result as $dataset) {
|
2014-05-16 00:47:28 +04:00
|
|
|
|
self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
|
2012-08-22 12:31:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$groupsFromDB;
|
|
|
|
|
}
|
2013-02-15 01:16:48 +04:00
|
|
|
|
}
|