1
0

ExpireSharesJob.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\IJob;
  28. use OCP\BackgroundJob\TimedJob;
  29. use OCP\IDBConnection;
  30. use OCP\Share\Exceptions\ShareNotFound;
  31. use OCP\Share\IManager;
  32. use OCP\Share\IShare;
  33. /**
  34. * Delete all shares that are expired
  35. */
  36. class ExpireSharesJob extends TimedJob {
  37. /** @var IManager */
  38. private $shareManager;
  39. /** @var IDBConnection */
  40. private $db;
  41. public function __construct(ITimeFactory $time, IManager $shareManager, IDBConnection $db) {
  42. $this->shareManager = $shareManager;
  43. $this->db = $db;
  44. parent::__construct($time);
  45. // Run once a day
  46. $this->setInterval(24 * 60 * 60);
  47. $this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
  48. }
  49. /**
  50. * Makes the background job do its work
  51. *
  52. * @param array $argument unused argument
  53. */
  54. public function run($argument) {
  55. //Current time
  56. $now = new \DateTime();
  57. $now = $now->format('Y-m-d H:i:s');
  58. /*
  59. * Expire file link shares only (for now)
  60. */
  61. $qb = $this->db->getQueryBuilder();
  62. $qb->select('id', 'share_type')
  63. ->from('share')
  64. ->where(
  65. $qb->expr()->andX(
  66. $qb->expr()->orX(
  67. $qb->expr()->eq('share_type', $qb->expr()->literal(IShare::TYPE_LINK)),
  68. $qb->expr()->eq('share_type', $qb->expr()->literal(IShare::TYPE_EMAIL))
  69. ),
  70. $qb->expr()->lte('expiration', $qb->expr()->literal($now)),
  71. $qb->expr()->orX(
  72. $qb->expr()->eq('item_type', $qb->expr()->literal('file')),
  73. $qb->expr()->eq('item_type', $qb->expr()->literal('folder'))
  74. )
  75. )
  76. );
  77. $shares = $qb->executeQuery();
  78. while ($share = $shares->fetch()) {
  79. if ((int)$share['share_type'] === IShare::TYPE_LINK) {
  80. $id = 'ocinternal';
  81. } elseif ((int)$share['share_type'] === IShare::TYPE_EMAIL) {
  82. $id = 'ocMailShare';
  83. }
  84. $id .= ':' . $share['id'];
  85. try {
  86. $share = $this->shareManager->getShareById($id);
  87. $this->shareManager->deleteShare($share);
  88. } catch (ShareNotFound $e) {
  89. // Normally the share gets automatically expired on fetching it
  90. }
  91. }
  92. $shares->closeCursor();
  93. }
  94. }