1
0

CleanupJob.php 979 B

1234567891011121314151617181920212223242526272829303132333435363738
  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\IJob;
  10. use OCP\BackgroundJob\TimedJob;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\IDBConnection;
  13. class CleanupJob extends TimedJob {
  14. public function __construct(
  15. ITimeFactory $time,
  16. private IDBConnection $connection,
  17. ) {
  18. parent::__construct($time);
  19. // Run once a day
  20. $this->setInterval(3600 * 24);
  21. $this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
  22. }
  23. protected function run($argument): void {
  24. // Delete all entries more than 48 hours old
  25. $time = $this->time->getTime() - (48 * 3600);
  26. $qb = $this->connection->getQueryBuilder();
  27. $qb->delete('bruteforce_attempts')
  28. ->where($qb->expr()->lt('occurred', $qb->createNamedParameter($time), IQueryBuilder::PARAM_INT));
  29. $qb->execute();
  30. }
  31. }