1
0

LegacyHooks.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Share20;
  7. use OCP\EventDispatcher\IEventDispatcher;
  8. use OCP\Files\File;
  9. use OCP\Share;
  10. use OCP\Share\Events\BeforeShareCreatedEvent;
  11. use OCP\Share\Events\BeforeShareDeletedEvent;
  12. use OCP\Share\Events\ShareCreatedEvent;
  13. use OCP\Share\Events\ShareDeletedEvent;
  14. use OCP\Share\Events\ShareDeletedFromSelfEvent;
  15. use OCP\Share\IShare;
  16. class LegacyHooks {
  17. /** @var IEventDispatcher */
  18. private $eventDispatcher;
  19. public function __construct(IEventDispatcher $eventDispatcher) {
  20. $this->eventDispatcher = $eventDispatcher;
  21. $this->eventDispatcher->addListener(BeforeShareDeletedEvent::class, function (BeforeShareDeletedEvent $event) {
  22. $this->preUnshare($event);
  23. });
  24. $this->eventDispatcher->addListener(ShareDeletedEvent::class, function (ShareDeletedEvent $event) {
  25. $this->postUnshare($event);
  26. });
  27. $this->eventDispatcher->addListener(ShareDeletedFromSelfEvent::class, function (ShareDeletedFromSelfEvent $event) {
  28. $this->postUnshareFromSelf($event);
  29. });
  30. $this->eventDispatcher->addListener(BeforeShareCreatedEvent::class, function (BeforeShareCreatedEvent $event) {
  31. $this->preShare($event);
  32. });
  33. $this->eventDispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) {
  34. $this->postShare($event);
  35. });
  36. }
  37. public function preUnshare(BeforeShareDeletedEvent $e) {
  38. $share = $e->getShare();
  39. $formatted = $this->formatHookParams($share);
  40. \OC_Hook::emit(Share::class, 'pre_unshare', $formatted);
  41. }
  42. public function postUnshare(ShareDeletedEvent $e) {
  43. $share = $e->getShare();
  44. $formatted = $this->formatHookParams($share);
  45. $formatted['deletedShares'] = [$formatted];
  46. \OC_Hook::emit(Share::class, 'post_unshare', $formatted);
  47. }
  48. public function postUnshareFromSelf(ShareDeletedFromSelfEvent $e) {
  49. $share = $e->getShare();
  50. $formatted = $this->formatHookParams($share);
  51. $formatted['itemTarget'] = $formatted['fileTarget'];
  52. $formatted['unsharedItems'] = [$formatted];
  53. \OC_Hook::emit(Share::class, 'post_unshareFromSelf', $formatted);
  54. }
  55. private function formatHookParams(IShare $share) {
  56. // Prepare hook
  57. $shareType = $share->getShareType();
  58. $sharedWith = '';
  59. if ($shareType === IShare::TYPE_USER ||
  60. $shareType === IShare::TYPE_GROUP ||
  61. $shareType === IShare::TYPE_REMOTE) {
  62. $sharedWith = $share->getSharedWith();
  63. }
  64. $hookParams = [
  65. 'id' => $share->getId(),
  66. 'itemType' => $share->getNodeType(),
  67. 'itemSource' => $share->getNodeId(),
  68. 'shareType' => $shareType,
  69. 'shareWith' => $sharedWith,
  70. 'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
  71. 'uidOwner' => $share->getSharedBy(),
  72. 'fileSource' => $share->getNodeId(),
  73. 'fileTarget' => $share->getTarget()
  74. ];
  75. return $hookParams;
  76. }
  77. public function preShare(BeforeShareCreatedEvent $e) {
  78. $share = $e->getShare();
  79. // Pre share hook
  80. $run = true;
  81. $error = '';
  82. $preHookData = [
  83. 'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
  84. 'itemSource' => $share->getNode()->getId(),
  85. 'shareType' => $share->getShareType(),
  86. 'uidOwner' => $share->getSharedBy(),
  87. 'permissions' => $share->getPermissions(),
  88. 'fileSource' => $share->getNode()->getId(),
  89. 'expiration' => $share->getExpirationDate(),
  90. 'token' => $share->getToken(),
  91. 'itemTarget' => $share->getTarget(),
  92. 'shareWith' => $share->getSharedWith(),
  93. 'run' => &$run,
  94. 'error' => &$error,
  95. ];
  96. \OC_Hook::emit(Share::class, 'pre_shared', $preHookData);
  97. if ($run === false) {
  98. $e->setError($error);
  99. $e->stopPropagation();
  100. }
  101. return $e;
  102. }
  103. public function postShare(ShareCreatedEvent $e) {
  104. $share = $e->getShare();
  105. $postHookData = [
  106. 'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
  107. 'itemSource' => $share->getNode()->getId(),
  108. 'shareType' => $share->getShareType(),
  109. 'uidOwner' => $share->getSharedBy(),
  110. 'permissions' => $share->getPermissions(),
  111. 'fileSource' => $share->getNode()->getId(),
  112. 'expiration' => $share->getExpirationDate(),
  113. 'token' => $share->getToken(),
  114. 'id' => $share->getId(),
  115. 'shareWith' => $share->getSharedWith(),
  116. 'itemTarget' => $share->getTarget(),
  117. 'fileTarget' => $share->getTarget(),
  118. 'path' => $share->getNode()->getPath(),
  119. ];
  120. \OC_Hook::emit(Share::class, 'post_shared', $postHookData);
  121. }
  122. }