propagationmanager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files_Sharing\Propagation;
  23. use OC\Files\Filesystem;
  24. use OC\Files\View;
  25. use OCP\IConfig;
  26. use OCP\IUserSession;
  27. /**
  28. * Keep track of all change and share propagators by owner
  29. */
  30. class PropagationManager {
  31. /**
  32. * @var \OCP\IUserSession
  33. */
  34. private $userSession;
  35. /**
  36. * @var \OCP\IConfig
  37. */
  38. private $config;
  39. /**
  40. * Change propagators for share owner
  41. *
  42. * @var \OC\Files\Cache\ChangePropagator[]
  43. */
  44. private $changePropagators = [];
  45. /**
  46. * Recipient propagators
  47. *
  48. * @var \OCA\Files_Sharing\Propagation\RecipientPropagator[]
  49. */
  50. private $sharePropagators = [];
  51. public function __construct(IUserSession $userSession, IConfig $config) {
  52. $this->userSession = $userSession;
  53. $this->config = $config;
  54. }
  55. /**
  56. * @param string $user
  57. * @return \OC\Files\Cache\ChangePropagator
  58. */
  59. public function getChangePropagator($user) {
  60. $activeUser = $this->userSession->getUser();
  61. // for the local user we want to propagator from the active view, not any cached one
  62. if ($activeUser && $activeUser->getUID() === $user && Filesystem::getView() instanceof View) {
  63. // it's important that we take the existing propagator here to make sure we can listen to external changes
  64. $this->changePropagators[$user] = Filesystem::getView()->getUpdater()->getPropagator();
  65. }
  66. if (isset($this->changePropagators[$user])) {
  67. return $this->changePropagators[$user];
  68. }
  69. $view = new View('/' . $user . '/files');
  70. $this->changePropagators[$user] = $view->getUpdater()->getPropagator();
  71. return $this->changePropagators[$user];
  72. }
  73. /**
  74. * @param string $user
  75. * @return \OCA\Files_Sharing\Propagation\RecipientPropagator
  76. */
  77. public function getSharePropagator($user) {
  78. if (isset($this->sharePropagators[$user])) {
  79. return $this->sharePropagators[$user];
  80. }
  81. $this->sharePropagators[$user] = new RecipientPropagator($user, $this->getChangePropagator($user), $this->config, $this);
  82. return $this->sharePropagators[$user];
  83. }
  84. /**
  85. * Attach the recipient propagator for $user to the change propagator of a share owner to mark shares as dirty when the owner makes a change to a share
  86. *
  87. * @param string $shareOwner
  88. * @param string $user
  89. */
  90. public function listenToOwnerChanges($shareOwner, $user) {
  91. $sharePropagator = $this->getSharePropagator($user);
  92. $ownerPropagator = $this->getChangePropagator($shareOwner);
  93. $sharePropagator->attachToPropagator($ownerPropagator, $shareOwner);
  94. }
  95. /**
  96. * To be called from setupFS trough a hook
  97. *
  98. * Sets up listening to changes made to shares owned by the current user
  99. */
  100. public function globalSetup() {
  101. $user = $this->userSession->getUser();
  102. if (!$user) {
  103. return;
  104. }
  105. $recipientPropagator = $this->getSharePropagator($user->getUID());
  106. $watcher = new ChangeWatcher(Filesystem::getView(), $recipientPropagator);
  107. // for marking shares owned by the active user as dirty when a file inside them changes
  108. $this->listenToOwnerChanges($user->getUID(), $user->getUID());
  109. \OC_Hook::connect('OC_Filesystem', 'post_write', $watcher, 'writeHook');
  110. \OC_Hook::connect('OC_Filesystem', 'post_delete', $watcher, 'writeHook');
  111. \OC_Hook::connect('OC_Filesystem', 'post_rename', $watcher, 'renameHook');
  112. }
  113. }