LegacyHooks.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Share20;
  24. use OCP\Share\IShare;
  25. use Symfony\Component\EventDispatcher\EventDispatcher;
  26. use Symfony\Component\EventDispatcher\GenericEvent;
  27. class LegacyHooks {
  28. /** @var EventDispatcher */
  29. private $eventDispatcher;
  30. /**
  31. * LegacyHooks constructor.
  32. *
  33. * @param EventDispatcher $eventDispatcher
  34. */
  35. public function __construct(EventDispatcher $eventDispatcher) {
  36. $this->eventDispatcher = $eventDispatcher;
  37. $this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']);
  38. $this->eventDispatcher->addListener('OCP\Share::postUnshare', [$this, 'postUnshare']);
  39. }
  40. /**
  41. * @param GenericEvent $e
  42. */
  43. public function preUnshare(GenericEvent $e) {
  44. /** @var IShare $share */
  45. $share = $e->getSubject();
  46. $formatted = $this->formatHookParams($share);
  47. \OC_Hook::emit('OCP\Share', 'pre_unshare', $formatted);
  48. }
  49. /**
  50. * @param GenericEvent $e
  51. */
  52. public function postUnshare(GenericEvent $e) {
  53. /** @var IShare $share */
  54. $share = $e->getSubject();
  55. $formatted = $this->formatHookParams($share);
  56. /** @var IShare[] $deletedShares */
  57. $deletedShares = $e->getArgument('deletedShares');
  58. $formattedDeletedShares = array_map(function($share) {
  59. return $this->formatHookParams($share);
  60. }, $deletedShares);
  61. $formatted['deletedShares'] = $formattedDeletedShares;
  62. \OC_Hook::emit('OCP\Share', 'pre_unshare', $formatted);
  63. }
  64. private function formatHookParams(IShare $share) {
  65. // Prepare hook
  66. $shareType = $share->getShareType();
  67. $sharedWith = '';
  68. if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
  69. $shareType === \OCP\Share::SHARE_TYPE_GROUP ||
  70. $shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
  71. $sharedWith = $share->getSharedWith();
  72. }
  73. $hookParams = [
  74. 'id' => $share->getId(),
  75. 'itemType' => $share->getNodeType(),
  76. 'itemSource' => $share->getNodeId(),
  77. 'shareType' => $shareType,
  78. 'shareWith' => $sharedWith,
  79. 'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
  80. 'uidOwner' => $share->getSharedBy(),
  81. 'fileSource' => $share->getNodeId(),
  82. 'fileTarget' => $share->getTarget()
  83. ];
  84. return $hookParams;
  85. }
  86. }