diff options
author | Raul <raul@nextcloud.com> | 2022-04-21 14:28:41 +0300 |
---|---|---|
committer | Raul <raul@nextcloud.com> | 2022-04-28 15:03:43 +0300 |
commit | ff6e1d0816112b4b6c1fa58ba1d3b44f742080d3 (patch) | |
tree | 0a81a334915d466a811b0c20795fb124b6beab17 /lib | |
parent | 40696f2b5c80dbe11edfed807a24dca9d682d3aa (diff) |
Clear expired tokens in clean-up background job
Signed-off-by: Raul <raul@nextcloud.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Backgroundjobs/Cleanup.php | 20 | ||||
-rw-r--r-- | lib/Db/WopiMapper.php | 20 |
2 files changed, 37 insertions, 3 deletions
diff --git a/lib/Backgroundjobs/Cleanup.php b/lib/Backgroundjobs/Cleanup.php index e5adb55a..08128186 100644 --- a/lib/Backgroundjobs/Cleanup.php +++ b/lib/Backgroundjobs/Cleanup.php @@ -24,7 +24,7 @@ namespace OCA\Richdocuments\Backgroundjobs; use OC\BackgroundJob\TimedJob; -use OCA\Richdocuments\Service\CapabilitiesService; +use OCA\Richdocuments\Db\WopiMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; @@ -32,9 +32,12 @@ class Cleanup extends TimedJob { /** @var IDBConnection */ private $db; + /** @var $wopiMapper */ + private $wopiMapper; - public function __construct(IDBConnection $db) { + public function __construct(IDBConnection $db, WopiMapper $wopiMapper) { $this->db = $db; + $this->wopiMapper = $wopiMapper; $this->setInterval(60*60); } @@ -45,5 +48,18 @@ class Cleanup extends TimedJob { $query->delete('richdocuments_template') ->where($query->expr()->lte('timestamp', $query->createNamedParameter(time() - 60, IQueryBuilder::PARAM_INT))); $query->executeStatement(); + + // Expired WOPI access tokens + $this->cleanUpWopiTokens(); + + } + + private function cleanUpWopiTokens() + { + $tokenIds = $this->wopiMapper->getExpiredTokenIds(1000); + $query = $this->db->getQueryBuilder(); + $query->delete('richdocuments_wopi') + ->where($query->expr()->in('id', $query->createNamedParameter($tokenIds, IQueryBuilder::PARAM_INT_ARRAY))); + $query->executeStatement(); } } diff --git a/lib/Db/WopiMapper.php b/lib/Db/WopiMapper.php index e725177d..3d09eed3 100644 --- a/lib/Db/WopiMapper.php +++ b/lib/Db/WopiMapper.php @@ -24,9 +24,9 @@ namespace OCA\Richdocuments\Db; use OCA\Richdocuments\Exceptions\ExpiredTokenException; use OCA\Richdocuments\Exceptions\UnknownTokenException; -use OCP\AppFramework\Db\TokenExpiredException; use OCP\AppFramework\Db\Mapper; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\ILogger; use OCP\Security\ISecureRandom; @@ -166,4 +166,22 @@ class WopiMapper extends Mapper { return $wopi; } + + /** + * @param int|null $limit + * @param int|null $offset + * @return int[] + * @throws \OCP\DB\Exception + */ + public function getExpiredTokenIds(?int $limit = null, ?int $offset = null) + { + $qb = $this->db->getQueryBuilder(); + $qb->select('id') + ->from('richdocuments_wopi') + ->where($qb->expr()->lt('expiry', $qb->createNamedParameter(time() - 60, IQueryBuilder::PARAM_INT))) + ->setFirstResult($offset) + ->setMaxResults($limit); + + return array_column($qb->executeQuery()->fetchAll(), 'id'); + } } |