1
0

File.php 9.1 KB

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