File.php 8.3 KB

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