From 9f77761e02c7cbc8e3e520f63737d940222212b3 Mon Sep 17 00:00:00 2001 From: Marcel Kapfer Date: Mon, 27 Aug 2018 00:08:16 +0200 Subject: [PATCH] Switched to Doctrine DBAL $GLOBALS['TYPO3_DB'] is no longer supported in v9 --- .../Domain/Repository/FeGroupsRepository.php | 56 +++++++++++-------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/Classes/Domain/Repository/FeGroupsRepository.php b/Classes/Domain/Repository/FeGroupsRepository.php index 9c707ed..d7a10b6 100644 --- a/Classes/Domain/Repository/FeGroupsRepository.php +++ b/Classes/Domain/Repository/FeGroupsRepository.php @@ -26,8 +26,10 @@ namespace In2code\In2frontendauthentication\Domain\Repository; * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ -use TYPO3\CMS\Core\Database\DatabaseConnection; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; +use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction; /** * Class Contact @@ -43,11 +45,23 @@ class FeGroupsRepository */ public function findByCurrentIpAddress() { - $rows = $this->getDatabaseConnection()->exec_SELECTgetRows( - '*', - self::TABLE_NAME, - 'deleted = 0 and hidden = 0' . $this->getIpQueryString() - ); + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::TABLE_NAME); + + $queryBuilder + ->getRestrictions() + ->removeAll() + ->add(GeneralUtility::makeInstance(DeletedRestriction::class)) + ->add(GeneralUtility::makeInstance(HiddenRestriction::class)); + + $queryBuilder + ->select('*') + ->from(self::TABLE_NAME); + + $this->getIpQueryString($queryBuilder); + + $rows = $queryBuilder->execute() + ->fetchAll(); + if (!empty($rows)) { return $rows; } @@ -58,19 +72,22 @@ class FeGroupsRepository * Build string like * ip_mask like "%1.1.1.1%" and ip_mask like "%2.2.2.2%" * - * @return string + * @param QueryBuilder $queryBuilder */ - protected function getIpQueryString() + protected function getIpQueryString(&$queryBuilder) { - $queryString = ' and ('; foreach ($this->getCurrentIpAddresses() as $ipAddress) { - $databaseConnection = $this->getDatabaseConnection(); - $ipAddress = $databaseConnection->quoteStr($ipAddress, self::TABLE_NAME); - $queryString .= 'ip_mask like "%' . $ipAddress . '%" or '; + $queryBuilder->orWhere( + $queryBuilder->expr()->like( + 'ip_mask', + $queryBuilder->createNamedParameter( + '%' . + $queryBuilder->escapeLikeWildcards($ipAddress) . + '%' + ) + ) + ); } - $queryString = rtrim($queryString, ' or '); - $queryString .= ')'; - return $queryString; } /** @@ -90,13 +107,4 @@ class FeGroupsRepository $ips[] = $parts[0] . '.' . $parts[1] . '.' . $parts[2] . '.*'; return $ips; } - - /** - * @return DatabaseConnection - * @SuppressWarnings(PHPMD.Superglobals) - */ - protected function getDatabaseConnection() - { - return $GLOBALS['TYPO3_DB']; - } }