LegacyHooks.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Pauli Järvinen <pauli.jarvinen@gmail.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Share20;
  28. use OCP\EventDispatcher\IEventDispatcher;
  29. use OCP\Files\File;
  30. use OCP\Share;
  31. use OCP\Share\Events\BeforeShareCreatedEvent;
  32. use OCP\Share\Events\BeforeShareDeletedEvent;
  33. use OCP\Share\Events\ShareCreatedEvent;
  34. use OCP\Share\Events\ShareDeletedEvent;
  35. use OCP\Share\Events\ShareDeletedFromSelfEvent;
  36. use OCP\Share\IShare;
  37. class LegacyHooks {
  38. /** @var IEventDispatcher */
  39. private $eventDispatcher;
  40. public function __construct(IEventDispatcher $eventDispatcher) {
  41. $this->eventDispatcher = $eventDispatcher;
  42. $this->eventDispatcher->addListener(BeforeShareDeletedEvent::class, function (BeforeShareDeletedEvent $event) {
  43. $this->preUnshare($event);
  44. });
  45. $this->eventDispatcher->addListener(ShareDeletedEvent::class, function (ShareDeletedEvent $event) {
  46. $this->postUnshare($event);
  47. });
  48. $this->eventDispatcher->addListener(ShareDeletedFromSelfEvent::class, function (ShareDeletedFromSelfEvent $event) {
  49. $this->postUnshareFromSelf($event);
  50. });
  51. $this->eventDispatcher->addListener(BeforeShareCreatedEvent::class, function (BeforeShareCreatedEvent $event) {
  52. $this->preShare($event);
  53. });
  54. $this->eventDispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) {
  55. $this->postShare($event);
  56. });
  57. }
  58. public function preUnshare(BeforeShareDeletedEvent $e) {
  59. $share = $e->getShare();
  60. $formatted = $this->formatHookParams($share);
  61. \OC_Hook::emit(Share::class, 'pre_unshare', $formatted);
  62. }
  63. public function postUnshare(ShareDeletedEvent $e) {
  64. $share = $e->getShare();
  65. $formatted = $this->formatHookParams($share);
  66. $formatted['deletedShares'] = [$formatted];
  67. \OC_Hook::emit(Share::class, 'post_unshare', $formatted);
  68. }
  69. public function postUnshareFromSelf(ShareDeletedFromSelfEvent $e) {
  70. $share = $e->getShare();
  71. $formatted = $this->formatHookParams($share);
  72. $formatted['itemTarget'] = $formatted['fileTarget'];
  73. $formatted['unsharedItems'] = [$formatted];
  74. \OC_Hook::emit(Share::class, 'post_unshareFromSelf', $formatted);
  75. }
  76. private function formatHookParams(IShare $share) {
  77. // Prepare hook
  78. $shareType = $share->getShareType();
  79. $sharedWith = '';
  80. if ($shareType === IShare::TYPE_USER ||
  81. $shareType === IShare::TYPE_GROUP ||
  82. $shareType === IShare::TYPE_REMOTE) {
  83. $sharedWith = $share->getSharedWith();
  84. }
  85. $hookParams = [
  86. 'id' => $share->getId(),
  87. 'itemType' => $share->getNodeType(),
  88. 'itemSource' => $share->getNodeId(),
  89. 'shareType' => $shareType,
  90. 'shareWith' => $sharedWith,
  91. 'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
  92. 'uidOwner' => $share->getSharedBy(),
  93. 'fileSource' => $share->getNodeId(),
  94. 'fileTarget' => $share->getTarget()
  95. ];
  96. return $hookParams;
  97. }
  98. public function preShare(BeforeShareCreatedEvent $e) {
  99. $share = $e->getShare();
  100. // Pre share hook
  101. $run = true;
  102. $error = '';
  103. $preHookData = [
  104. 'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
  105. 'itemSource' => $share->getNode()->getId(),
  106. 'shareType' => $share->getShareType(),
  107. 'uidOwner' => $share->getSharedBy(),
  108. 'permissions' => $share->getPermissions(),
  109. 'fileSource' => $share->getNode()->getId(),
  110. 'expiration' => $share->getExpirationDate(),
  111. 'token' => $share->getToken(),
  112. 'itemTarget' => $share->getTarget(),
  113. 'shareWith' => $share->getSharedWith(),
  114. 'run' => &$run,
  115. 'error' => &$error,
  116. ];
  117. \OC_Hook::emit(Share::class, 'pre_shared', $preHookData);
  118. if ($run === false) {
  119. $e->setError($error);
  120. $e->stopPropagation();
  121. }
  122. return $e;
  123. }
  124. public function postShare(ShareCreatedEvent $e) {
  125. $share = $e->getShare();
  126. $postHookData = [
  127. 'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
  128. 'itemSource' => $share->getNode()->getId(),
  129. 'shareType' => $share->getShareType(),
  130. 'uidOwner' => $share->getSharedBy(),
  131. 'permissions' => $share->getPermissions(),
  132. 'fileSource' => $share->getNode()->getId(),
  133. 'expiration' => $share->getExpirationDate(),
  134. 'token' => $share->getToken(),
  135. 'id' => $share->getId(),
  136. 'shareWith' => $share->getSharedWith(),
  137. 'itemTarget' => $share->getTarget(),
  138. 'fileTarget' => $share->getTarget(),
  139. 'path' => $share->getNode()->getPath(),
  140. ];
  141. \OC_Hook::emit(Share::class, 'post_shared', $postHookData);
  142. }
  143. }