1
0

ExpireSharesJob.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\TimedJob;
  10. use OCP\IDBConnection;
  11. use OCP\Share\Exceptions\ShareNotFound;
  12. use OCP\Share\IManager;
  13. use OCP\Share\IShare;
  14. /**
  15. * Delete all shares that are expired
  16. */
  17. class ExpireSharesJob extends TimedJob {
  18. /** @var IManager */
  19. private $shareManager;
  20. /** @var IDBConnection */
  21. private $db;
  22. public function __construct(ITimeFactory $time, IManager $shareManager, IDBConnection $db) {
  23. $this->shareManager = $shareManager;
  24. $this->db = $db;
  25. parent::__construct($time);
  26. // Run once a day
  27. $this->setInterval(24 * 60 * 60);
  28. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  29. }
  30. /**
  31. * Makes the background job do its work
  32. *
  33. * @param array $argument unused argument
  34. */
  35. public function run($argument) {
  36. //Current time
  37. $now = new \DateTime();
  38. $now = $now->format('Y-m-d H:i:s');
  39. /*
  40. * Expire file link shares only (for now)
  41. */
  42. $qb = $this->db->getQueryBuilder();
  43. $qb->select('id', 'share_type')
  44. ->from('share')
  45. ->where(
  46. $qb->expr()->andX(
  47. $qb->expr()->orX(
  48. $qb->expr()->eq('share_type', $qb->expr()->literal(IShare::TYPE_LINK)),
  49. $qb->expr()->eq('share_type', $qb->expr()->literal(IShare::TYPE_EMAIL))
  50. ),
  51. $qb->expr()->lte('expiration', $qb->expr()->literal($now)),
  52. $qb->expr()->orX(
  53. $qb->expr()->eq('item_type', $qb->expr()->literal('file')),
  54. $qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
  55. )
  56. )
  57. );
  58. $shares = $qb->executeQuery();
  59. while ($share = $shares->fetch()) {
  60. if ((int)$share['share_type'] === IShare::TYPE_LINK) {
  61. $id = 'ocinternal';
  62. } elseif ((int)$share['share_type'] === IShare::TYPE_EMAIL) {
  63. $id = 'ocMailShare';
  64. }
  65. $id .= ':' . $share['id'];
  66. try {
  67. $share = $this->shareManager->getShareById($id);
  68. $this->shareManager->deleteShare($share);
  69. } catch (ShareNotFound $e) {
  70. // Normally the share gets automatically expired on fetching it
  71. }
  72. }
  73. $shares->closeCursor();
  74. }
  75. }