ExpireSharesJob.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. public function __construct(
  19. ITimeFactory $time,
  20. private IManager $shareManager,
  21. private IDBConnection $db,
  22. ) {
  23. parent::__construct($time);
  24. // Run once a day
  25. $this->setInterval(24 * 60 * 60);
  26. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  27. }
  28. /**
  29. * Makes the background job do its work
  30. *
  31. * @param array $argument unused argument
  32. */
  33. public function run($argument) {
  34. //Current time
  35. $now = new \DateTime();
  36. $now = $now->format('Y-m-d H:i:s');
  37. /*
  38. * Expire file link shares only (for now)
  39. */
  40. $qb = $this->db->getQueryBuilder();
  41. $qb->select('id', 'share_type')
  42. ->from('share')
  43. ->where(
  44. $qb->expr()->andX(
  45. $qb->expr()->orX(
  46. $qb->expr()->eq('share_type', $qb->expr()->literal(IShare::TYPE_LINK)),
  47. $qb->expr()->eq('share_type', $qb->expr()->literal(IShare::TYPE_EMAIL))
  48. ),
  49. $qb->expr()->lte('expiration', $qb->expr()->literal($now)),
  50. $qb->expr()->orX(
  51. $qb->expr()->eq('item_type', $qb->expr()->literal('file')),
  52. $qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
  53. )
  54. )
  55. );
  56. $shares = $qb->executeQuery();
  57. while ($share = $shares->fetch()) {
  58. if ((int)$share['share_type'] === IShare::TYPE_LINK) {
  59. $id = 'ocinternal';
  60. } elseif ((int)$share['share_type'] === IShare::TYPE_EMAIL) {
  61. $id = 'ocMailShare';
  62. }
  63. $id .= ':' . $share['id'];
  64. try {
  65. $share = $this->shareManager->getShareById($id);
  66. $this->shareManager->deleteShare($share);
  67. } catch (ShareNotFound $e) {
  68. // Normally the share gets automatically expired on fetching it
  69. }
  70. }
  71. $shares->closeCursor();
  72. }
  73. }