1
0

ExpireSharesJob.php 2.2 KB

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