File.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Jonas Meurer <jonas@freesources.org>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\WorkflowEngine\Entity;
  27. use OC\Files\Config\UserMountCache;
  28. use OC\Files\Mount\Manager as MountManager;
  29. use OCP\EventDispatcher\Event;
  30. use OCP\EventDispatcher\GenericEvent;
  31. use OCP\Files\InvalidPathException;
  32. use OCP\Files\IRootFolder;
  33. use OCP\Files\Node;
  34. use OCP\Files\NotFoundException;
  35. use OCP\IL10N;
  36. use OCP\ILogger;
  37. use OCP\IURLGenerator;
  38. use OCP\IUser;
  39. use OCP\IUserManager;
  40. use OCP\IUserSession;
  41. use OCP\SystemTag\ISystemTag;
  42. use OCP\SystemTag\ISystemTagManager;
  43. use OCP\SystemTag\MapperEvent;
  44. use OCP\WorkflowEngine\EntityContext\IContextPortation;
  45. use OCP\WorkflowEngine\EntityContext\IDisplayText;
  46. use OCP\WorkflowEngine\EntityContext\IIcon;
  47. use OCP\WorkflowEngine\EntityContext\IUrl;
  48. use OCP\WorkflowEngine\GenericEntityEvent;
  49. use OCP\WorkflowEngine\IEntity;
  50. use OCP\WorkflowEngine\IRuleMatcher;
  51. class File implements IEntity, IDisplayText, IUrl, IIcon, IContextPortation {
  52. private const EVENT_NAMESPACE = '\OCP\Files::';
  53. /** @var IL10N */
  54. protected $l10n;
  55. /** @var IURLGenerator */
  56. protected $urlGenerator;
  57. /** @var IRootFolder */
  58. protected $root;
  59. /** @var ILogger */
  60. protected $logger;
  61. /** @var string */
  62. protected $eventName;
  63. /** @var Event */
  64. protected $event;
  65. /** @var IUserSession */
  66. private $userSession;
  67. /** @var ISystemTagManager */
  68. private $tagManager;
  69. /** @var ?Node */
  70. private $node;
  71. /** @var ?IUser */
  72. private $actingUser = null;
  73. /** @var IUserManager */
  74. private $userManager;
  75. /** @var UserMountCache */
  76. private $userMountCache;
  77. /** @var MountManager */
  78. private $mountManager;
  79. public function __construct(
  80. IL10N $l10n,
  81. IURLGenerator $urlGenerator,
  82. IRootFolder $root,
  83. ILogger $logger,
  84. IUserSession $userSession,
  85. ISystemTagManager $tagManager,
  86. IUserManager $userManager,
  87. UserMountCache $userMountCache,
  88. MountManager $mountManager
  89. ) {
  90. $this->l10n = $l10n;
  91. $this->urlGenerator = $urlGenerator;
  92. $this->root = $root;
  93. $this->logger = $logger;
  94. $this->userSession = $userSession;
  95. $this->tagManager = $tagManager;
  96. $this->userManager = $userManager;
  97. $this->userMountCache = $userMountCache;
  98. $this->mountManager = $mountManager;
  99. }
  100. public function getName(): string {
  101. return $this->l10n->t('File');
  102. }
  103. public function getIcon(): string {
  104. return $this->urlGenerator->imagePath('core', 'categories/files.svg');
  105. }
  106. public function getEvents(): array {
  107. return [
  108. new GenericEntityEvent($this->l10n->t('File created'), self::EVENT_NAMESPACE . 'postCreate'),
  109. new GenericEntityEvent($this->l10n->t('File updated'), self::EVENT_NAMESPACE . 'postWrite'),
  110. new GenericEntityEvent($this->l10n->t('File renamed'), self::EVENT_NAMESPACE . 'postRename'),
  111. new GenericEntityEvent($this->l10n->t('File deleted'), self::EVENT_NAMESPACE . 'postDelete'),
  112. new GenericEntityEvent($this->l10n->t('File accessed'), self::EVENT_NAMESPACE . 'postTouch'),
  113. new GenericEntityEvent($this->l10n->t('File copied'), self::EVENT_NAMESPACE . 'postCopy'),
  114. new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN),
  115. ];
  116. }
  117. public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void {
  118. if (!$event instanceof GenericEvent && !$event instanceof MapperEvent) {
  119. return;
  120. }
  121. $this->eventName = $eventName;
  122. $this->event = $event;
  123. $this->actingUser = $this->actingUser ?? $this->userSession->getUser();
  124. try {
  125. $node = $this->getNode();
  126. $ruleMatcher->setEntitySubject($this, $node);
  127. $ruleMatcher->setFileInfo($node->getStorage(), $node->getInternalPath());
  128. } catch (NotFoundException $e) {
  129. // pass
  130. }
  131. }
  132. public function isLegitimatedForUserId(string $userId): bool {
  133. try {
  134. $node = $this->getNode();
  135. if ($node->getOwner()?->getUID() === $userId) {
  136. return true;
  137. }
  138. if ($this->eventName === self::EVENT_NAMESPACE . 'postDelete') {
  139. // At postDelete, the file no longer exists. Check for parent folder instead.
  140. $fileId = $node->getParent()->getId();
  141. } else {
  142. $fileId = $node->getId();
  143. }
  144. $mountInfos = $this->userMountCache->getMountsForFileId($fileId, $userId);
  145. foreach ($mountInfos as $mountInfo) {
  146. $mount = $this->mountManager->getMountFromMountInfo($mountInfo);
  147. if ($mount && $mount->getStorage() && !empty($mount->getStorage()->getCache()->get($fileId))) {
  148. return true;
  149. }
  150. }
  151. return false;
  152. } catch (NotFoundException $e) {
  153. return false;
  154. }
  155. }
  156. /**
  157. * @throws NotFoundException
  158. */
  159. protected function getNode(): Node {
  160. if ($this->node) {
  161. return $this->node;
  162. }
  163. if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) {
  164. throw new NotFoundException();
  165. }
  166. switch ($this->eventName) {
  167. case self::EVENT_NAMESPACE . 'postCreate':
  168. case self::EVENT_NAMESPACE . 'postWrite':
  169. case self::EVENT_NAMESPACE . 'postDelete':
  170. case self::EVENT_NAMESPACE . 'postTouch':
  171. return $this->event->getSubject();
  172. case self::EVENT_NAMESPACE . 'postRename':
  173. case self::EVENT_NAMESPACE . 'postCopy':
  174. return $this->event->getSubject()[1];
  175. case MapperEvent::EVENT_ASSIGN:
  176. if (!$this->event instanceof MapperEvent || $this->event->getObjectType() !== 'files') {
  177. throw new NotFoundException();
  178. }
  179. $nodes = $this->root->getById((int)$this->event->getObjectId());
  180. if (is_array($nodes) && isset($nodes[0])) {
  181. $this->node = $nodes[0];
  182. return $this->node;
  183. }
  184. break;
  185. }
  186. throw new NotFoundException();
  187. }
  188. public function getDisplayText(int $verbosity = 0): string {
  189. try {
  190. $node = $this->getNode();
  191. } catch (NotFoundException $e) {
  192. return '';
  193. }
  194. $options = [
  195. $this->actingUser ? $this->actingUser->getDisplayName() : $this->l10n->t('Someone'),
  196. $node->getName()
  197. ];
  198. switch ($this->eventName) {
  199. case self::EVENT_NAMESPACE . 'postCreate':
  200. return $this->l10n->t('%s created %s', $options);
  201. case self::EVENT_NAMESPACE . 'postWrite':
  202. return $this->l10n->t('%s modified %s', $options);
  203. case self::EVENT_NAMESPACE . 'postDelete':
  204. return $this->l10n->t('%s deleted %s', $options);
  205. case self::EVENT_NAMESPACE . 'postTouch':
  206. return $this->l10n->t('%s accessed %s', $options);
  207. case self::EVENT_NAMESPACE . 'postRename':
  208. return $this->l10n->t('%s renamed %s', $options);
  209. case self::EVENT_NAMESPACE . 'postCopy':
  210. return $this->l10n->t('%s copied %s', $options);
  211. case MapperEvent::EVENT_ASSIGN:
  212. $tagNames = [];
  213. if ($this->event instanceof MapperEvent) {
  214. $tagIDs = $this->event->getTags();
  215. $tagObjects = $this->tagManager->getTagsByIds($tagIDs);
  216. foreach ($tagObjects as $systemTag) {
  217. /** @var ISystemTag $systemTag */
  218. if ($systemTag->isUserVisible()) {
  219. $tagNames[] = $systemTag->getName();
  220. }
  221. }
  222. }
  223. $filename = array_pop($options);
  224. $tagString = implode(', ', $tagNames);
  225. if ($tagString === '') {
  226. return '';
  227. }
  228. array_push($options, $tagString, $filename);
  229. return $this->l10n->t('%s assigned %s to %s', $options);
  230. }
  231. }
  232. public function getUrl(): string {
  233. try {
  234. return $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $this->getNode()->getId()]);
  235. } catch (InvalidPathException $e) {
  236. return '';
  237. } catch (NotFoundException $e) {
  238. return '';
  239. }
  240. }
  241. /**
  242. * @inheritDoc
  243. */
  244. public function exportContextIDs(): array {
  245. $nodeOwner = $this->getNode()->getOwner();
  246. $actingUserId = null;
  247. if ($this->actingUser instanceof IUser) {
  248. $actingUserId = $this->actingUser->getUID();
  249. } elseif ($this->userSession->getUser() instanceof IUser) {
  250. $actingUserId = $this->userSession->getUser()->getUID();
  251. }
  252. return [
  253. 'eventName' => $this->eventName,
  254. 'nodeId' => $this->getNode()->getId(),
  255. 'nodeOwnerId' => $nodeOwner ? $nodeOwner->getUID() : null,
  256. 'actingUserId' => $actingUserId,
  257. ];
  258. }
  259. /**
  260. * @inheritDoc
  261. */
  262. public function importContextIDs(array $contextIDs): void {
  263. $this->eventName = $contextIDs['eventName'];
  264. if ($contextIDs['nodeOwnerId'] !== null) {
  265. $userFolder = $this->root->getUserFolder($contextIDs['nodeOwnerId']);
  266. $nodes = $userFolder->getById($contextIDs['nodeId']);
  267. } else {
  268. $nodes = $this->root->getById($contextIDs['nodeId']);
  269. }
  270. $this->node = $nodes[0] ?? null;
  271. if ($contextIDs['actingUserId']) {
  272. $this->actingUser = $this->userManager->get($contextIDs['actingUserId']);
  273. }
  274. }
  275. /**
  276. * @inheritDoc
  277. */
  278. public function getIconUrl(): string {
  279. return $this->getIcon();
  280. }
  281. }