File.php 8.6 KB

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