Use query builder instead of OC_DB in user_ldap

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-11-06 11:25:28 +01:00
parent 2c6bbe783a
commit 5fba0e562f
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
19 changed files with 40 additions and 46 deletions

View File

@ -32,7 +32,7 @@
\OC_JSON::callCheck();
$prefix = (string)$_POST['ldap_serverconfig_chooser'];
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
if ($helper->deleteServerConfiguration($prefix)) {
\OC_JSON::success();
} else {

View File

@ -29,7 +29,7 @@
\OC_JSON::checkAppEnabled('user_ldap');
\OC_JSON::callCheck();
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$serverConnections = $helper->getServerConfigurationPrefixes();
sort($serverConnections);
$lk = array_pop($serverConnections);

View File

@ -70,7 +70,7 @@ $access = new \OCA\User_LDAP\Access(
$con,
$ldapWrapper,
$userManager,
new \OCA\User_LDAP\Helper(\OC::$server->getConfig()),
new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
\OC::$server->getConfig(),
\OC::$server->getUserManager()
);

View File

@ -29,5 +29,5 @@ if ($state === 'doSet') {
\OC::$server->getConfig()->setSystemValue('ldapIgnoreNamingRules', false);
}
$helper = new \OCA\User_LDAP\Helper($config);
$helper = new \OCA\User_LDAP\Helper($config, \OC::$server->getDatabaseConnection());
$helper->setLDAPProvider();

View File

@ -23,5 +23,5 @@
*
*/
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$helper->setLDAPProvider();

View File

@ -109,7 +109,7 @@ class Search extends Command {
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$helper = new Helper($this->ocConfig);
$helper = new Helper($this->ocConfig, \OC::$server->getDatabaseConnection());
$configPrefixes = $helper->getServerConfigurationPrefixes(true);
$ldapWrapper = new LDAP();

View File

@ -60,7 +60,7 @@ class SetConfig extends Command {
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$helper = new Helper(\OC::$server->getConfig());
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$availableConfigs = $helper->getServerConfigurationPrefixes();
$configID = $input->getArgument('configID');
if (!in_array($configID, $availableConfigs)) {

View File

@ -48,7 +48,7 @@ class TestConfig extends Command {
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$helper = new Helper(\OC::$server->getConfig());
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$availableConfigs = $helper->getServerConfigurationPrefixes();
$configID = $input->getArgument('configID');
if (!in_array($configID, $availableConfigs)) {

View File

@ -120,7 +120,7 @@ class Connection extends LDAPUtility {
if ($memcache->isAvailable()) {
$this->cache = $memcache->createDistributed();
}
$helper = new Helper(\OC::$server->getConfig());
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$this->doNotValidate = !in_array($this->configPrefix,
$helper->getServerConfigurationPrefixes());
}

View File

@ -35,23 +35,25 @@
namespace OCA\User_LDAP;
use OC\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
class Helper {
/** @var IConfig */
private $config;
/** @var IDBConnection */
private $connection;
/** @var CappedMemoryCache */
protected $sanitizeDnCache;
/**
* Helper constructor.
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
public function __construct(IConfig $config,
IDBConnection $connection) {
$this->config = $config;
$this->connection = $connection;
$this->sanitizeDnCache = new CappedMemoryCache(10000);
}
@ -160,30 +162,22 @@ class Helper {
return false;
}
$saveOtherConfigurations = '';
$query = $this->connection->getQueryBuilder();
$query->delete('appconfig')
->where($query->expr()->eq('appid', $query->createNamedParameter('user_ldap')))
->andWhere($query->expr()->notIn('configkey', $query->createNamedParameter([
'enabled',
'installed_version',
'types',
'bgjUpdateGroupsLastRun',
]), IQueryBuilder::PARAM_STR_ARRAY));
if (empty($prefix)) {
$saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\'';
$query->andWhere($query->expr()->notLike('configkey', $query->createNamedParameter('s%')));
}
$query = \OC_DB::prepare('
DELETE
FROM `*PREFIX*appconfig`
WHERE `configkey` LIKE ?
' . $saveOtherConfigurations . '
AND `appid` = \'user_ldap\'
AND `configkey` NOT IN (\'enabled\', \'installed_version\', \'types\', \'bgjUpdateGroupsLastRun\')
');
$delRows = $query->execute([$prefix . '%']);
if ($delRows === null) {
return false;
}
if ($delRows === 0) {
return false;
}
return true;
$deletedRows = $query->execute();
return $deletedRows !== 0;
}
/**

View File

@ -88,7 +88,7 @@ class CleanUp extends TimedJob {
if (isset($arguments['helper'])) {
$this->ldapHelper = $arguments['helper'];
} else {
$this->ldapHelper = new Helper(\OC::$server->getConfig());
$this->ldapHelper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
}
if (isset($arguments['ocConfig'])) {

View File

@ -308,7 +308,7 @@ class Sync extends TimedJob {
if (isset($argument['helper'])) {
$this->ldapHelper = $argument['helper'];
} else {
$this->ldapHelper = new Helper($this->config);
$this->ldapHelper = new Helper($this->config, \OC::$server->getDatabaseConnection());
}
if (isset($argument['ldapWrapper'])) {

View File

@ -87,7 +87,7 @@ abstract class Proxy {
new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(),
$coreUserManager, $coreNotificationManager, $shareManager);
$connector = new Connection($this->ldap, $configPrefix);
$access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig, $coreUserManager);
$access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig, \OC::$server->getDatabaseConnection()), $ocConfig, $coreUserManager);
$access->setUserMapper($userMap);
$access->setGroupMapper($groupMap);
self::$accesses[$configPrefix] = $access;

View File

@ -48,7 +48,7 @@ class Admin implements ISettings {
* @return TemplateResponse
*/
public function getForm() {
$helper = new Helper(\OC::$server->getConfig());
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$prefixes = $helper->getServerConfigurationPrefixes();
if (count($prefixes) === 0) {
$newPrefix = $helper->getNextServerConfigurationPrefix();

View File

@ -739,7 +739,7 @@ class Wizard extends LDAPUtility {
//this did not help :(
//Let's see whether we can parse the Host URL and convert the domain to
//a base DN
$helper = new Helper(\OC::$server->getConfig());
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$domain = $helper->getDomainFromURL($this->configuration->ldapHost);
if (!$domain) {
return false;

View File

@ -118,7 +118,7 @@ class AccessTest extends TestCase {
$this->createMock(INotificationManager::class),
$this->shareManager])
->getMock();
$helper = new Helper(\OC::$server->getConfig());
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
return [$lw, $connector, $um, $helper];
}

View File

@ -40,7 +40,7 @@ class HelperTest extends \Test\TestCase {
parent::setUp();
$this->config = $this->createMock(IConfig::class);
$this->helper = new Helper($this->config);
$this->helper = new Helper($this->config, \OC::$server->getDatabaseConnection());
}
public function testGetServerConfigurationPrefixes() {

View File

@ -137,7 +137,7 @@ abstract class AbstractIntegrationTest {
* initializes the test Helper
*/
protected function initHelper() {
$this->helper = new Helper(\OC::$server->getConfig());
$this->helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
}
/**

View File

@ -224,7 +224,7 @@ class LDAPProviderTest extends \Test\TestCase {
$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals(
@ -240,7 +240,7 @@ class LDAPProviderTest extends \Test\TestCase {
$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
$ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals(