Skip to content

Commit

Permalink
fix: fix deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jürgen Kußmann committed Oct 11, 2024
1 parent 6ab8d75 commit d17fc67
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 182 deletions.
111 changes: 0 additions & 111 deletions Classes/Domain/Repository/Entry.php

This file was deleted.

95 changes: 84 additions & 11 deletions Classes/Domain/Repository/EntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,97 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use Aoe\FeloginBruteforceProtection\Domain\Model\Entry;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;

class EntryRepository extends Repository
{
public function initializeObject(): void
private const ENTRY_DB_TABLE = 'tx_feloginbruteforceprotection_domain_model_entry';

/**
* We don't use the extbase-logic to do the DB-query - because we must do the DB-query BEFORE the FE-user is initialised - and
* this would lead to the TYPO3-deprecation 'Using extbase in a context without TypoScript. Will stop working with TYPO3 v13.'
*/
public function findOneEntryByIdentifier(string $identifier): ?Entry
{
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::ENTRY_DB_TABLE);
$records = $queryBuilder->select('*')
->from(self::ENTRY_DB_TABLE)
->where(
$queryBuilder->expr()
->eq('identifier', $queryBuilder->createNamedParameter($identifier, \PDO::PARAM_STR)),
)
->executeQuery()
->fetchAllAssociative();

if (count($records) === 1) {
$entry = new Entry();
$entry->_setProperty('uid', $records[0]['uid']);
$entry->_setProperty('pid', $records[0]['pid']);
$entry->_setProperty('tstamp', $records[0]['tstamp']);
$entry->_setProperty('crdate', $records[0]['crdate']);
$entry->_setProperty('identifier', $records[0]['identifier']);
$entry->_setProperty('failures', $records[0]['failures']);
return $entry;
}

return null;
}

/**
* We don't use the extbase-logic to do the DB-query - because we must do the DB-query BEFORE the FE-user is initialised - and
* this would lead to the TYPO3-deprecation 'Using extbase in a context without TypoScript. Will stop working with TYPO3 v13.'
*/
public function createEntry(Entry $entry): void
{
/** @var Typo3QuerySettings $defaultQuerySettings */
$defaultQuerySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
// don't add the pid constraint
$defaultQuerySettings->setRespectStoragePage(false);
// don't add fields from enable columns constraint
$defaultQuerySettings->setIgnoreEnableFields(true);
// don't add sys_language_uid constraint
$defaultQuerySettings->setRespectSysLanguage(false);
$this->setDefaultQuerySettings($defaultQuerySettings);
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::ENTRY_DB_TABLE);
$queryBuilder->insert(self::ENTRY_DB_TABLE)
->values([
'pid' => 0,
'tstamp' => $entry->getTstamp(),
'crdate' => $entry->getCrdate(),
'identifier' => $entry->getIdentifier(),
'failures' => $entry->getFailures(),
])
->executeStatement();
}

/**
* We don't use the extbase-logic to do the DB-query - because we must do the DB-query BEFORE the FE-user is initialised - and
* this would lead to the TYPO3-deprecation 'Using extbase in a context without TypoScript. Will stop working with TYPO3 v13.'
*/
public function updateEntry(Entry $entry): void
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::ENTRY_DB_TABLE);
$queryBuilder->update(self::ENTRY_DB_TABLE)
->set('failures', $entry->getFailures())
->set('tstamp', $entry->getTstamp())
->where(
$queryBuilder->expr()
->eq('identifier', $queryBuilder->createNamedParameter($entry->getIdentifier(), \PDO::PARAM_STR)),
)
->executeStatement();
}

/**
* We don't use the extbase-logic to do the DB-query - because we must do the DB-query BEFORE the FE-user is initialised - and
* this would lead to the TYPO3-deprecation 'Using extbase in a context without TypoScript. Will stop working with TYPO3 v13.'
*/
public function removeEntry(Entry $entry): void
{
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::ENTRY_DB_TABLE);
$queryBuilder->delete(self::ENTRY_DB_TABLE)
->where(
$queryBuilder->expr()
->eq('identifier', $queryBuilder->createNamedParameter($entry->getIdentifier(), \PDO::PARAM_STR)),
)
->executeStatement();
}

/**
Expand Down
19 changes: 6 additions & 13 deletions Classes/Domain/Service/RestrictionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use Aoe\FeloginBruteforceProtection\Service\Logger\LoggerInterface;
use Aoe\FeloginBruteforceProtection\System\Configuration;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;

class RestrictionService
{
Expand All @@ -47,8 +46,6 @@ class RestrictionService

protected EntryRepository $entryRepository;

protected PersistenceManager $persistenceManager;

protected Entry $entry;

protected bool $clientRestricted;
Expand All @@ -62,7 +59,6 @@ public function __construct(RestrictionIdentifierInterface $restrictionIdentifie
$this->restrictionIdentifier = $restrictionIdentifier;

$this->configuration = GeneralUtility::makeInstance(Configuration::class);
$this->persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
$this->entryRepository = GeneralUtility::makeInstance(EntryRepository::class);
$this->feLoginBruteForceApi = GeneralUtility::makeInstance(FeLoginBruteForceApi::class);
}
Expand All @@ -84,8 +80,7 @@ public function isClientRestricted(): bool
public function removeEntry(): void
{
if ($this->hasEntry()) {
$this->entryRepository->remove($this->entry);
$this->persistenceManager->persistAll();
$this->entryRepository->removeEntry($this->entry);

$this->log('Bruteforce Counter removed', LoggerInterface::SEVERITY_INFO);
}
Expand Down Expand Up @@ -114,7 +109,7 @@ public function checkAndHandleRestriction(): void
}

$this->entry->increaseFailures();
$this->saveEntry();
$this->updateEntry();

$this->restrictionLog();
}
Expand All @@ -127,7 +122,7 @@ public function hasEntry(): bool
public function getEntry(): ?Entry
{
if (!isset($this->entry)) {
$entry = $this->entryRepository->findOneBy(['identifier' => $this->getClientIdentifier()]);
$entry = $this->entryRepository->findOneEntryByIdentifier($this->getClientIdentifier());
if ($entry instanceof Entry) {
$this->entry = $entry;
if ($this->isOutdated($entry)) {
Expand Down Expand Up @@ -198,19 +193,17 @@ private function createEntry(): void
$this->entry->setTstamp(time());
$this->entry->setIdentifier($this->getClientIdentifier());

$this->entryRepository->add($this->entry);
$this->persistenceManager->persistAll();
$this->entryRepository->createEntry($this->entry);
$this->clientRestricted = false;
}

private function saveEntry(): void
private function updateEntry(): void
{
if ($this->entry->getFailures() > 0) {
$this->entry->setTstamp(time());
}

$this->entryRepository->add($this->entry);
$this->persistenceManager->persistAll();
$this->entryRepository->updateEntry($this->entry);
if ($this->hasMaximumNumberOfFailuresReached($this->entry)) {
$this->clientRestricted = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public function testIsNotClientRestrictedWhenMaximumNumbersOfFailureNotReached()

$entryRepository = $this->getAccessibleMock(
EntryRepository::class,
['findOneBy', 'remove'],
['findOneEntryByIdentifier', 'removeEntry'],
[],
'',
false
);
$entryRepository->method('findOneBy')
$entryRepository->method('findOneEntryByIdentifier')
->willReturn($entry);

GeneralUtility::setSingletonInstance(EntryRepository::class, $entryRepository);
Expand Down Expand Up @@ -178,12 +178,12 @@ public function testIsClientRestrictedWithFailures(): void

$entryRepository = $this->getAccessibleMock(
EntryRepository::class,
['findOneBy', 'remove'],
['findOneEntryByIdentifier', 'removeEntry'],
[],
'',
false
);
$entryRepository->method('findOneBy')
$entryRepository->method('findOneEntryByIdentifier')
->willReturn($entry);

GeneralUtility::setSingletonInstance(EntryRepository::class, $entryRepository);
Expand Down Expand Up @@ -216,12 +216,12 @@ public function testIsClientRestrictedWithFailuresAndTimeout(): void

$entryRepository = $this->getAccessibleMock(
EntryRepository::class,
['findOneBy', 'remove'],
['findOneEntryByIdentifier', 'removeEntry'],
[],
'',
false
);
$entryRepository->method('findOneBy')
$entryRepository->method('findOneEntryByIdentifier')
->willReturn($entry);

GeneralUtility::setSingletonInstance(EntryRepository::class, $entryRepository);
Expand Down Expand Up @@ -254,12 +254,12 @@ public function testIsClientRestrictedWithLessFailures(): void

$entryRepository = $this->getAccessibleMock(
EntryRepository::class,
['findOneBy', 'remove'],
['findOneEntryByIdentifier', 'removeEntry'],
[],
'',
false
);
$entryRepository->method('findOneBy')
$entryRepository->method('findOneEntryByIdentifier')
->willReturn($entry);

GeneralUtility::setSingletonInstance(EntryRepository::class, $entryRepository);
Expand Down
Loading

0 comments on commit d17fc67

Please sign in to comment.