mirror of
https://github.com/nextcloud/server.git
synced 2026-06-13 18:50:47 -04:00
planned refactorings for OC 8
This commit is contained in:
parent
3ca70d647a
commit
61ed363f82
6 changed files with 83 additions and 55 deletions
|
|
@ -10,6 +10,17 @@ use OCA\user_ldap\lib\Helper;
|
|||
use OCA\user_ldap\lib\LDAP;
|
||||
use OCA\user_ldap\User_Proxy;
|
||||
use OCA\User_LDAP\Mapping\UserMapping;
|
||||
use OCA\User_LDAP\lib\User\DeletedUsersIndex;
|
||||
|
||||
$dbConnection = \OC::$server->getDatabaseConnection();
|
||||
$userMapping = new UserMapping($dbConnection);
|
||||
$helper = new Helper();
|
||||
$uBackend = new User_Proxy(
|
||||
$helper->getServerConfigurationPrefixes(true),
|
||||
new LDAP()
|
||||
);
|
||||
$deletedUsersIndex = new DeletedUsersIndex(
|
||||
\OC::$server->getConfig(), $dbConnection, $userMapping);
|
||||
|
||||
$application->add(new OCA\user_ldap\Command\ShowConfig());
|
||||
$application->add(new OCA\user_ldap\Command\SetConfig());
|
||||
|
|
@ -17,13 +28,6 @@ $application->add(new OCA\user_ldap\Command\TestConfig());
|
|||
$application->add(new OCA\user_ldap\Command\CreateEmptyConfig());
|
||||
$application->add(new OCA\user_ldap\Command\DeleteConfig());
|
||||
$application->add(new OCA\user_ldap\Command\Search());
|
||||
$userMapping = new UserMapping(\OC::$server->getDatabaseConnection());
|
||||
$application->add(new OCA\user_ldap\Command\ShowRemnants($userMapping));
|
||||
$helper = new Helper();
|
||||
$uBackend = new User_Proxy(
|
||||
$helper->getServerConfigurationPrefixes(true),
|
||||
new LDAP()
|
||||
);
|
||||
$application->add(new OCA\user_ldap\Command\CheckUser(
|
||||
$uBackend, $helper, \OC::$server->getConfig()
|
||||
));
|
||||
$uBackend, $helper, $deletedUsersIndex, $userMapping));
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||
|
||||
use OCA\user_ldap\lib\user\User;
|
||||
use OCA\User_LDAP\lib\user\Manager;
|
||||
use OCA\User_LDAP\lib\User\DeletedUsersIndex;
|
||||
use OCA\user_ldap\lib\Helper;
|
||||
use OCA\user_ldap\User_Proxy;
|
||||
|
||||
|
|
@ -26,18 +27,22 @@ class CheckUser extends Command {
|
|||
/** @var \OCA\User_LDAP\lib\Helper */
|
||||
protected $helper;
|
||||
|
||||
/** @var \OCP\IConfig */
|
||||
protected $config;
|
||||
/** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */
|
||||
protected $dui;
|
||||
|
||||
/** @var \OCA\User_LDAP\Mapping\UserMapping */
|
||||
protected $mapping;
|
||||
|
||||
/**
|
||||
* @param OCA\user_ldap\User_Proxy $uBackend
|
||||
* @param OCA\User_LDAP\lib\Helper $helper
|
||||
* @param OCP\IConfig $config
|
||||
*/
|
||||
public function __construct(User_Proxy $uBackend, Helper $helper, \OCP\IConfig $config) {
|
||||
public function __construct(User_Proxy $uBackend, Helper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
|
||||
$this->backend = $uBackend;
|
||||
$this->helper = $helper;
|
||||
$this->config = $config;
|
||||
$this->dui = $dui;
|
||||
$this->mapping = $mapping;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
|
@ -70,10 +75,7 @@ class CheckUser extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
// TODO FIXME consolidate next line in DeletedUsersIndex
|
||||
// (impractical now, because of class dependencies)
|
||||
$this->config->setUserValue($uid, 'user_ldap', 'isDeleted', '1');
|
||||
|
||||
$this->dui->markUser($uid);
|
||||
$output->writeln('The user does not exists on LDAP anymore.');
|
||||
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
|
||||
. $uid . '"');
|
||||
|
|
@ -86,22 +88,11 @@ class CheckUser extends Command {
|
|||
* checks whether a user is actually mapped
|
||||
* @param string $ocName the username as used in ownCloud
|
||||
* @throws \Exception
|
||||
* @return bool
|
||||
* @return true
|
||||
*/
|
||||
protected function confirmUserIsMapped($ocName) {
|
||||
//TODO FIXME this should go to Mappings in OC 8
|
||||
$db = \OC::$server->getDatabaseConnection();
|
||||
$query = $db->prepare('
|
||||
SELECT
|
||||
`ldap_dn` AS `dn`
|
||||
FROM `*PREFIX*ldap_user_mapping`
|
||||
WHERE `owncloud_name` = ?'
|
||||
);
|
||||
|
||||
$query->execute(array($ocName));
|
||||
$result = $query->fetchColumn();
|
||||
|
||||
if($result === false) {
|
||||
$dn = $this->mapping->getDNByName($ocName);
|
||||
if ($dn === false) {
|
||||
throw new \Exception('The given user is not a recognized LDAP user.');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ namespace OCA\User_LDAP\Jobs;
|
|||
use \OCA\user_ldap\User_Proxy;
|
||||
use \OCA\user_ldap\lib\Helper;
|
||||
use \OCA\user_ldap\lib\LDAP;
|
||||
use \OCA\User_LDAP\lib\User\DeletedUsersIndex;
|
||||
use \OCA\User_LDAP\Mapping\UserMapping;
|
||||
|
||||
/**
|
||||
* Class CleanUp
|
||||
|
|
@ -45,6 +47,12 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
|
|||
*/
|
||||
protected $ldapHelper;
|
||||
|
||||
/** @var \OCA\User_LDAP\Mapping\UserMapping */
|
||||
protected $mapping;
|
||||
|
||||
/** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */
|
||||
protected $dui;
|
||||
|
||||
/**
|
||||
* @var int $defaultIntervalMin default interval in minutes
|
||||
*/
|
||||
|
|
@ -92,6 +100,19 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
|
|||
} else {
|
||||
$this->db = \OC::$server->getDatabaseConnection();
|
||||
}
|
||||
|
||||
if(isset($arguments['mapping'])) {
|
||||
$this->mapping = $arguments['mapping'];
|
||||
} else {
|
||||
$this->mapping = new UserMapping($this->db);
|
||||
}
|
||||
|
||||
if(isset($arguments['deletedUsersIndex'])) {
|
||||
$this->dui = $arguments['deletedUsersIndex'];
|
||||
} else {
|
||||
$this->dui = new DeletedUsersIndex(
|
||||
$this->ocConfig, $this->db, $this->mapping);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -104,7 +125,7 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
|
|||
if(!$this->isCleanUpAllowed()) {
|
||||
return;
|
||||
}
|
||||
$users = $this->getMappedUsers($this->limit, $this->getOffset());
|
||||
$users = $this->mapping->getList($this->limit, $this->getOffset());
|
||||
if(!is_array($users)) {
|
||||
//something wrong? Let's start from the beginning next time and
|
||||
//abort
|
||||
|
|
@ -169,33 +190,11 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
|
|||
private function checkUser($user) {
|
||||
if($this->userBackend->userExistsOnLDAP($user['name'])) {
|
||||
//still available, all good
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO FIXME consolidate next line in DeletedUsersIndex
|
||||
// (impractical now, because of class dependencies)
|
||||
$this->ocConfig->setUserValue($user['name'], 'user_ldap', 'isDeleted', '1');
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a batch of users from the mappings table
|
||||
* @param int $limit
|
||||
* @param int $offset
|
||||
* @return array
|
||||
*/
|
||||
public function getMappedUsers($limit, $offset) {
|
||||
$query = $this->db->prepare('
|
||||
SELECT
|
||||
`ldap_dn` AS `dn`,
|
||||
`owncloud_name` AS `name`,
|
||||
`directory_uuid` AS `uuid`
|
||||
FROM `*PREFIX*ldap_user_mapping`',
|
||||
$limit,
|
||||
$offset
|
||||
);
|
||||
|
||||
$query->execute();
|
||||
return $query->fetchAll();
|
||||
$this->dui->markUser($user['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -152,6 +152,27 @@ abstract class AbstractMapping {
|
|||
return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets a piece of the mapping list
|
||||
* @param int $offset
|
||||
* @param int $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getList($offset = null, $limit = null) {
|
||||
$query = $this->dbc->prepare('
|
||||
SELECT
|
||||
`ldap_dn` AS `dn`,
|
||||
`owncloud_name` AS `name`,
|
||||
`directory_uuid` AS `uuid`
|
||||
FROM `*PREFIX*ldap_user_mapping`',
|
||||
$limit,
|
||||
$offset
|
||||
);
|
||||
|
||||
$query->execute();
|
||||
return $query->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* attempts to map the given entry
|
||||
* @param string $fdn fully distinguished name (from LDAP)
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ class DeletedUsersIndex {
|
|||
foreach($deletedUsers as $user) {
|
||||
$userObjects[] = new OfflineUser($user, $this->config, $this->db, $this->mapping);
|
||||
}
|
||||
$this->deletedUsers = $userObjects;
|
||||
|
||||
return $this->deletedUsers;
|
||||
}
|
||||
|
|
@ -97,4 +98,12 @@ class DeletedUsersIndex {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* marks a user as deleted
|
||||
* @param string ocName
|
||||
*/
|
||||
public function markUser($ocName) {
|
||||
$this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,10 @@ class Test_CleanUp extends \PHPUnit_Framework_TestCase {
|
|||
$this->getMockBuilder('\OCA\user_ldap\User_Proxy')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$mocks['deletedUsersIndex'] =
|
||||
$this->getMockBuilder('\OCA\user_ldap\lib\user\deletedUsersIndex')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$mocks['ocConfig'] = $this->getMock('\OCP\IConfig');
|
||||
$mocks['db'] = $this->getMock('\OCP\IDBConnection');
|
||||
$mocks['helper'] = $this->getMock('\OCA\user_ldap\lib\Helper');
|
||||
|
|
|
|||
Loading…
Reference in a new issue