CleanupJob.php 963 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Security\Bruteforce;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\TimedJob;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IDBConnection;
  12. class CleanupJob extends TimedJob {
  13. public function __construct(
  14. ITimeFactory $time,
  15. private IDBConnection $connection,
  16. ) {
  17. parent::__construct($time);
  18. // Run once a day
  19. $this->setInterval(60 * 60 * 24);
  20. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  21. }
  22. protected function run($argument): void {
  23. // Delete all entries more than 48 hours old
  24. $time = $this->time->getTime() - (48 * 3600);
  25. $qb = $this->connection->getQueryBuilder();
  26. $qb->delete('bruteforce_attempts')
  27. ->where($qb->expr()->lt('occurred', $qb->createNamedParameter($time), IQueryBuilder::PARAM_INT));
  28. $qb->executeStatement();
  29. }
  30. }