Switched to Doctrine DBAL

$GLOBALS['TYPO3_DB'] is no longer supported in v9
This commit is contained in:
Marcel Kapfer 2018-08-27 00:08:16 +02:00
parent d52a108c8e
commit 9f77761e02
Signed by: mmk2410
GPG Key ID: CADE6F0C09F21B09
1 changed files with 32 additions and 24 deletions

View File

@ -26,8 +26,10 @@ namespace In2code\In2frontendauthentication\Domain\Repository;
* This copyright notice MUST APPEAR in all copies of the script! * 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\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 * Class Contact
@ -43,11 +45,23 @@ class FeGroupsRepository
*/ */
public function findByCurrentIpAddress() public function findByCurrentIpAddress()
{ {
$rows = $this->getDatabaseConnection()->exec_SELECTgetRows( $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::TABLE_NAME);
'*',
self::TABLE_NAME, $queryBuilder
'deleted = 0 and hidden = 0' . $this->getIpQueryString() ->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)) { if (!empty($rows)) {
return $rows; return $rows;
} }
@ -58,19 +72,22 @@ class FeGroupsRepository
* Build string like * Build string like
* ip_mask like "%1.1.1.1%" and ip_mask like "%2.2.2.2%" * 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) { foreach ($this->getCurrentIpAddresses() as $ipAddress) {
$databaseConnection = $this->getDatabaseConnection(); $queryBuilder->orWhere(
$ipAddress = $databaseConnection->quoteStr($ipAddress, self::TABLE_NAME); $queryBuilder->expr()->like(
$queryString .= 'ip_mask like "%' . $ipAddress . '%" or '; '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] . '.*'; $ips[] = $parts[0] . '.' . $parts[1] . '.' . $parts[2] . '.*';
return $ips; return $ips;
} }
/**
* @return DatabaseConnection
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function getDatabaseConnection()
{
return $GLOBALS['TYPO3_DB'];
}
} }