You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyHooks.php 5.2KB

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