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(); \OC_JSON::callCheck();
$prefix = (string)$_POST['ldap_serverconfig_chooser']; $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)) { if ($helper->deleteServerConfiguration($prefix)) {
\OC_JSON::success(); \OC_JSON::success();
} else { } else {

View File

@ -29,7 +29,7 @@
\OC_JSON::checkAppEnabled('user_ldap'); \OC_JSON::checkAppEnabled('user_ldap');
\OC_JSON::callCheck(); \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(); $serverConnections = $helper->getServerConfigurationPrefixes();
sort($serverConnections); sort($serverConnections);
$lk = array_pop($serverConnections); $lk = array_pop($serverConnections);

View File

@ -70,7 +70,7 @@ $access = new \OCA\User_LDAP\Access(
$con, $con,
$ldapWrapper, $ldapWrapper,
$userManager, $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->getConfig(),
\OC::$server->getUserManager() \OC::$server->getUserManager()
); );

View File

@ -29,5 +29,5 @@ if ($state === 'doSet') {
\OC::$server->getConfig()->setSystemValue('ldapIgnoreNamingRules', false); \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(); $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(); $helper->setLDAPProvider();

View File

@ -109,7 +109,7 @@ class Search extends Command {
} }
protected function execute(InputInterface $input, OutputInterface $output): int { 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); $configPrefixes = $helper->getServerConfigurationPrefixes(true);
$ldapWrapper = new LDAP(); $ldapWrapper = new LDAP();

View File

@ -60,7 +60,7 @@ class SetConfig extends Command {
} }
protected function execute(InputInterface $input, OutputInterface $output): int { 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(); $availableConfigs = $helper->getServerConfigurationPrefixes();
$configID = $input->getArgument('configID'); $configID = $input->getArgument('configID');
if (!in_array($configID, $availableConfigs)) { if (!in_array($configID, $availableConfigs)) {

View File

@ -48,7 +48,7 @@ class TestConfig extends Command {
} }
protected function execute(InputInterface $input, OutputInterface $output): int { 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(); $availableConfigs = $helper->getServerConfigurationPrefixes();
$configID = $input->getArgument('configID'); $configID = $input->getArgument('configID');
if (!in_array($configID, $availableConfigs)) { if (!in_array($configID, $availableConfigs)) {

View File

@ -120,7 +120,7 @@ class Connection extends LDAPUtility {
if ($memcache->isAvailable()) { if ($memcache->isAvailable()) {
$this->cache = $memcache->createDistributed(); $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, $this->doNotValidate = !in_array($this->configPrefix,
$helper->getServerConfigurationPrefixes()); $helper->getServerConfigurationPrefixes());
} }

View File

@ -35,23 +35,25 @@
namespace OCA\User_LDAP; namespace OCA\User_LDAP;
use OC\Cache\CappedMemoryCache; use OC\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection;
class Helper { class Helper {
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var IDBConnection */
private $connection;
/** @var CappedMemoryCache */ /** @var CappedMemoryCache */
protected $sanitizeDnCache; protected $sanitizeDnCache;
/** public function __construct(IConfig $config,
* Helper constructor. IDBConnection $connection) {
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config; $this->config = $config;
$this->connection = $connection;
$this->sanitizeDnCache = new CappedMemoryCache(10000); $this->sanitizeDnCache = new CappedMemoryCache(10000);
} }
@ -160,30 +162,22 @@ class Helper {
return false; 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)) { if (empty($prefix)) {
$saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\''; $query->andWhere($query->expr()->notLike('configkey', $query->createNamedParameter('s%')));
} }
$query = \OC_DB::prepare(' $deletedRows = $query->execute();
DELETE return $deletedRows !== 0;
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;
} }
/** /**

View File

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

View File

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

View File

@ -87,7 +87,7 @@ abstract class Proxy {
new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(),
$coreUserManager, $coreNotificationManager, $shareManager); $coreUserManager, $coreNotificationManager, $shareManager);
$connector = new Connection($this->ldap, $configPrefix); $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->setUserMapper($userMap);
$access->setGroupMapper($groupMap); $access->setGroupMapper($groupMap);
self::$accesses[$configPrefix] = $access; self::$accesses[$configPrefix] = $access;

View File

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

View File

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

View File

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

View File

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

View File

@ -137,7 +137,7 @@ abstract class AbstractIntegrationTest {
* initializes the test Helper * initializes the test Helper
*/ */
protected function initHelper() { 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()); $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); $ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals( $this->assertEquals(
@ -240,7 +240,7 @@ class LDAPProviderTest extends \Test\TestCase {
$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock()); $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); $ldapProvider = $this->getLDAPProvider($server);
$this->assertEquals( $this->assertEquals(