1
0

File.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\WorkflowEngine\Entity;
  8. use OC\Files\Config\UserMountCache;
  9. use OCP\EventDispatcher\Event;
  10. use OCP\EventDispatcher\GenericEvent;
  11. use OCP\Files\InvalidPathException;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\Mount\IMountManager;
  14. use OCP\Files\Node;
  15. use OCP\Files\NotFoundException;
  16. use OCP\IL10N;
  17. use OCP\IURLGenerator;
  18. use OCP\IUser;
  19. use OCP\IUserManager;
  20. use OCP\IUserSession;
  21. use OCP\SystemTag\ISystemTag;
  22. use OCP\SystemTag\ISystemTagManager;
  23. use OCP\SystemTag\MapperEvent;
  24. use OCP\WorkflowEngine\EntityContext\IContextPortation;
  25. use OCP\WorkflowEngine\EntityContext\IDisplayText;
  26. use OCP\WorkflowEngine\EntityContext\IIcon;
  27. use OCP\WorkflowEngine\EntityContext\IUrl;
  28. use OCP\WorkflowEngine\GenericEntityEvent;
  29. use OCP\WorkflowEngine\IEntity;
  30. use OCP\WorkflowEngine\IRuleMatcher;
  31. class File implements IEntity, IDisplayText, IUrl, IIcon, IContextPortation {
  32. private const EVENT_NAMESPACE = '\OCP\Files::';
  33. /** @var string */
  34. protected $eventName;
  35. /** @var Event */
  36. protected $event;
  37. /** @var ?Node */
  38. private $node;
  39. /** @var ?IUser */
  40. private $actingUser = null;
  41. /** @var UserMountCache */
  42. private $userMountCache;
  43. public function __construct(
  44. protected IL10N $l10n,
  45. protected IURLGenerator $urlGenerator,
  46. protected IRootFolder $root,
  47. private IUserSession $userSession,
  48. private ISystemTagManager $tagManager,
  49. private IUserManager $userManager,
  50. UserMountCache $userMountCache,
  51. private IMountManager $mountManager,
  52. ) {
  53. $this->userMountCache = $userMountCache;
  54. }
  55. public function getName(): string {
  56. return $this->l10n->t('File');
  57. }
  58. public function getIcon(): string {
  59. return $this->urlGenerator->imagePath('core', 'categories/files.svg');
  60. }
  61. public function getEvents(): array {
  62. return [
  63. new GenericEntityEvent($this->l10n->t('File created'), self::EVENT_NAMESPACE . 'postCreate'),
  64. new GenericEntityEvent($this->l10n->t('File updated'), self::EVENT_NAMESPACE . 'postWrite'),
  65. new GenericEntityEvent($this->l10n->t('File renamed'), self::EVENT_NAMESPACE . 'postRename'),
  66. new GenericEntityEvent($this->l10n->t('File deleted'), self::EVENT_NAMESPACE . 'postDelete'),
  67. new GenericEntityEvent($this->l10n->t('File accessed'), self::EVENT_NAMESPACE . 'postTouch'),
  68. new GenericEntityEvent($this->l10n->t('File copied'), self::EVENT_NAMESPACE . 'postCopy'),
  69. new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN),
  70. ];
  71. }
  72. public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void {
  73. if (!$event instanceof GenericEvent && !$event instanceof MapperEvent) {
  74. return;
  75. }
  76. $this->eventName = $eventName;
  77. $this->event = $event;
  78. $this->actingUser = $this->actingUser ?? $this->userSession->getUser();
  79. try {
  80. $node = $this->getNode();
  81. $ruleMatcher->setEntitySubject($this, $node);
  82. $ruleMatcher->setFileInfo($node->getStorage(), $node->getInternalPath());
  83. } catch (NotFoundException $e) {
  84. // pass
  85. }
  86. }
  87. public function isLegitimatedForUserId(string $userId): bool {
  88. try {
  89. $node = $this->getNode();
  90. if ($node->getOwner()?->getUID() === $userId) {
  91. return true;
  92. }
  93. if ($this->eventName === self::EVENT_NAMESPACE . 'postDelete') {
  94. // At postDelete, the file no longer exists. Check for parent folder instead.
  95. $fileId = $node->getParentId();
  96. } else {
  97. $fileId = $node->getId();
  98. }
  99. $mountInfos = $this->userMountCache->getMountsForFileId($fileId, $userId);
  100. foreach ($mountInfos as $mountInfo) {
  101. $mount = $this->mountManager->getMountFromMountInfo($mountInfo);
  102. if ($mount && $mount->getStorage() && !empty($mount->getStorage()->getCache()->get($fileId))) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. } catch (NotFoundException $e) {
  108. return false;
  109. }
  110. }
  111. /**
  112. * @throws NotFoundException
  113. */
  114. protected function getNode(): Node {
  115. if ($this->node) {
  116. return $this->node;
  117. }
  118. if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) {
  119. throw new NotFoundException();
  120. }
  121. switch ($this->eventName) {
  122. case self::EVENT_NAMESPACE . 'postCreate':
  123. case self::EVENT_NAMESPACE . 'postWrite':
  124. case self::EVENT_NAMESPACE . 'postDelete':
  125. case self::EVENT_NAMESPACE . 'postTouch':
  126. return $this->event->getSubject();
  127. case self::EVENT_NAMESPACE . 'postRename':
  128. case self::EVENT_NAMESPACE . 'postCopy':
  129. return $this->event->getSubject()[1];
  130. case MapperEvent::EVENT_ASSIGN:
  131. if (!$this->event instanceof MapperEvent || $this->event->getObjectType() !== 'files') {
  132. throw new NotFoundException();
  133. }
  134. $nodes = $this->root->getById((int)$this->event->getObjectId());
  135. if (is_array($nodes) && isset($nodes[0])) {
  136. $this->node = $nodes[0];
  137. return $this->node;
  138. }
  139. break;
  140. }
  141. throw new NotFoundException();
  142. }
  143. public function getDisplayText(int $verbosity = 0): string {
  144. try {
  145. $node = $this->getNode();
  146. } catch (NotFoundException $e) {
  147. return '';
  148. }
  149. $options = [
  150. $this->actingUser ? $this->actingUser->getDisplayName() : $this->l10n->t('Someone'),
  151. $node->getName()
  152. ];
  153. switch ($this->eventName) {
  154. case self::EVENT_NAMESPACE . 'postCreate':
  155. return $this->l10n->t('%s created %s', $options);
  156. case self::EVENT_NAMESPACE . 'postWrite':
  157. return $this->l10n->t('%s modified %s', $options);
  158. case self::EVENT_NAMESPACE . 'postDelete':
  159. return $this->l10n->t('%s deleted %s', $options);
  160. case self::EVENT_NAMESPACE . 'postTouch':
  161. return $this->l10n->t('%s accessed %s', $options);
  162. case self::EVENT_NAMESPACE . 'postRename':
  163. return $this->l10n->t('%s renamed %s', $options);
  164. case self::EVENT_NAMESPACE . 'postCopy':
  165. return $this->l10n->t('%s copied %s', $options);
  166. case MapperEvent::EVENT_ASSIGN:
  167. $tagNames = [];
  168. if ($this->event instanceof MapperEvent) {
  169. $tagIDs = $this->event->getTags();
  170. $tagObjects = $this->tagManager->getTagsByIds($tagIDs);
  171. foreach ($tagObjects as $systemTag) {
  172. /** @var ISystemTag $systemTag */
  173. if ($systemTag->isUserVisible()) {
  174. $tagNames[] = $systemTag->getName();
  175. }
  176. }
  177. }
  178. $filename = array_pop($options);
  179. $tagString = implode(', ', $tagNames);
  180. if ($tagString === '') {
  181. return '';
  182. }
  183. array_push($options, $tagString, $filename);
  184. return $this->l10n->t('%s assigned %s to %s', $options);
  185. }
  186. }
  187. public function getUrl(): string {
  188. try {
  189. return $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $this->getNode()->getId()]);
  190. } catch (InvalidPathException $e) {
  191. return '';
  192. } catch (NotFoundException $e) {
  193. return '';
  194. }
  195. }
  196. /**
  197. * @inheritDoc
  198. */
  199. public function exportContextIDs(): array {
  200. $nodeOwner = $this->getNode()->getOwner();
  201. $actingUserId = null;
  202. if ($this->actingUser instanceof IUser) {
  203. $actingUserId = $this->actingUser->getUID();
  204. } elseif ($this->userSession->getUser() instanceof IUser) {
  205. $actingUserId = $this->userSession->getUser()->getUID();
  206. }
  207. return [
  208. 'eventName' => $this->eventName,
  209. 'nodeId' => $this->getNode()->getId(),
  210. 'nodeOwnerId' => $nodeOwner ? $nodeOwner->getUID() : null,
  211. 'actingUserId' => $actingUserId,
  212. ];
  213. }
  214. /**
  215. * @inheritDoc
  216. */
  217. public function importContextIDs(array $contextIDs): void {
  218. $this->eventName = $contextIDs['eventName'];
  219. if ($contextIDs['nodeOwnerId'] !== null) {
  220. $userFolder = $this->root->getUserFolder($contextIDs['nodeOwnerId']);
  221. $nodes = $userFolder->getById($contextIDs['nodeId']);
  222. } else {
  223. $nodes = $this->root->getById($contextIDs['nodeId']);
  224. }
  225. $this->node = $nodes[0] ?? null;
  226. if ($contextIDs['actingUserId']) {
  227. $this->actingUser = $this->userManager->get($contextIDs['actingUserId']);
  228. }
  229. }
  230. /**
  231. * @inheritDoc
  232. */
  233. public function getIconUrl(): string {
  234. return $this->getIcon();
  235. }
  236. }