2019-08-09 14:24:48 +03:00
|
|
|
<?php
|
2020-04-09 12:50:14 +03:00
|
|
|
|
2019-08-09 14:24:48 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\WorkflowEngine\Entity;
|
|
|
|
|
2019-11-06 12:54:37 +03:00
|
|
|
use OCP\EventDispatcher\Event;
|
2019-11-27 15:59:34 +03:00
|
|
|
use OCP\EventDispatcher\GenericEvent;
|
2019-11-29 13:25:25 +03:00
|
|
|
use OCP\Files\InvalidPathException;
|
2019-09-10 00:59:31 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2019-11-27 15:59:34 +03:00
|
|
|
use OCP\Files\Node;
|
|
|
|
use OCP\Files\NotFoundException;
|
2019-08-09 14:24:48 +03:00
|
|
|
use OCP\IL10N;
|
2019-11-06 12:54:37 +03:00
|
|
|
use OCP\ILogger;
|
2019-08-09 14:24:48 +03:00
|
|
|
use OCP\IURLGenerator;
|
2020-08-05 14:00:30 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2019-11-27 19:22:26 +03:00
|
|
|
use OCP\IUserSession;
|
2019-11-27 15:59:34 +03:00
|
|
|
use OCP\Share\IManager as ShareManager;
|
2019-11-27 19:22:26 +03:00
|
|
|
use OCP\SystemTag\ISystemTag;
|
|
|
|
use OCP\SystemTag\ISystemTagManager;
|
2019-09-10 00:59:31 +03:00
|
|
|
use OCP\SystemTag\MapperEvent;
|
2020-08-05 14:00:30 +03:00
|
|
|
use OCP\WorkflowEngine\EntityContext\IContextPortation;
|
2019-11-27 19:22:26 +03:00
|
|
|
use OCP\WorkflowEngine\EntityContext\IDisplayText;
|
2020-08-07 01:14:50 +03:00
|
|
|
use OCP\WorkflowEngine\EntityContext\IIcon;
|
2019-11-29 13:25:25 +03:00
|
|
|
use OCP\WorkflowEngine\EntityContext\IUrl;
|
2019-09-03 13:31:29 +03:00
|
|
|
use OCP\WorkflowEngine\GenericEntityEvent;
|
2019-08-09 14:24:48 +03:00
|
|
|
use OCP\WorkflowEngine\IEntity;
|
2019-09-09 18:17:39 +03:00
|
|
|
use OCP\WorkflowEngine\IRuleMatcher;
|
2019-08-09 14:24:48 +03:00
|
|
|
|
2020-08-07 01:14:50 +03:00
|
|
|
class File implements IEntity, IDisplayText, IUrl, IIcon, IContextPortation {
|
2019-11-27 19:22:26 +03:00
|
|
|
private const EVENT_NAMESPACE = '\OCP\Files::';
|
2019-08-09 14:24:48 +03:00
|
|
|
|
|
|
|
/** @var IL10N */
|
|
|
|
protected $l10n;
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
2019-09-10 00:59:31 +03:00
|
|
|
/** @var IRootFolder */
|
2019-11-06 12:54:37 +03:00
|
|
|
protected $root;
|
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
2019-11-27 15:59:34 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $eventName;
|
|
|
|
/** @var Event */
|
|
|
|
protected $event;
|
|
|
|
/** @var ShareManager */
|
|
|
|
private $shareManager;
|
2019-11-27 19:22:26 +03:00
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
/** @var ISystemTagManager */
|
|
|
|
private $tagManager;
|
2020-08-05 14:00:30 +03:00
|
|
|
/** @var ?Node */
|
|
|
|
private $node;
|
|
|
|
/** @var ?IUser */
|
|
|
|
private $actingUser = null;
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
2019-11-27 15:59:34 +03:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
IL10N $l10n,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
IRootFolder $root,
|
|
|
|
ILogger $logger,
|
2019-11-27 19:22:26 +03:00
|
|
|
ShareManager $shareManager,
|
|
|
|
IUserSession $userSession,
|
2020-08-05 14:00:30 +03:00
|
|
|
ISystemTagManager $tagManager,
|
|
|
|
IUserManager $userManager
|
2019-11-27 15:59:34 +03:00
|
|
|
) {
|
2019-08-09 14:24:48 +03:00
|
|
|
$this->l10n = $l10n;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2019-09-10 00:59:31 +03:00
|
|
|
$this->root = $root;
|
2019-11-06 12:54:37 +03:00
|
|
|
$this->logger = $logger;
|
2019-11-27 15:59:34 +03:00
|
|
|
$this->shareManager = $shareManager;
|
2019-11-27 19:22:26 +03:00
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->tagManager = $tagManager;
|
2020-08-05 14:00:30 +03:00
|
|
|
$this->userManager = $userManager;
|
2019-08-09 14:24:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string {
|
|
|
|
return $this->l10n->t('File');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIcon(): string {
|
|
|
|
return $this->urlGenerator->imagePath('core', 'categories/files.svg');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEvents(): array {
|
|
|
|
return [
|
2019-11-27 15:59:34 +03:00
|
|
|
new GenericEntityEvent($this->l10n->t('File created'), self::EVENT_NAMESPACE . 'postCreate'),
|
|
|
|
new GenericEntityEvent($this->l10n->t('File updated'), self::EVENT_NAMESPACE . 'postWrite'),
|
|
|
|
new GenericEntityEvent($this->l10n->t('File renamed'), self::EVENT_NAMESPACE . 'postRename'),
|
|
|
|
new GenericEntityEvent($this->l10n->t('File deleted'), self::EVENT_NAMESPACE . 'postDelete'),
|
|
|
|
new GenericEntityEvent($this->l10n->t('File accessed'), self::EVENT_NAMESPACE . 'postTouch'),
|
|
|
|
new GenericEntityEvent($this->l10n->t('File copied'), self::EVENT_NAMESPACE . 'postCopy'),
|
2019-11-06 12:54:37 +03:00
|
|
|
new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN),
|
2019-08-09 14:24:48 +03:00
|
|
|
];
|
|
|
|
}
|
2019-09-09 18:17:39 +03:00
|
|
|
|
2019-11-06 12:54:37 +03:00
|
|
|
public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void {
|
|
|
|
if (!$event instanceof GenericEvent && !$event instanceof MapperEvent) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-27 15:59:34 +03:00
|
|
|
$this->eventName = $eventName;
|
|
|
|
$this->event = $event;
|
2020-08-05 14:00:30 +03:00
|
|
|
$this->actingUser = $this->actingUser ?? $this->userSession->getUser();
|
2019-11-27 15:59:34 +03:00
|
|
|
try {
|
|
|
|
$node = $this->getNode();
|
|
|
|
$ruleMatcher->setEntitySubject($this, $node);
|
2019-12-17 17:00:44 +03:00
|
|
|
$ruleMatcher->setFileInfo($node->getStorage(), $node->getInternalPath());
|
2019-11-27 15:59:34 +03:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isLegitimatedForUserId(string $uid): bool {
|
|
|
|
try {
|
|
|
|
$node = $this->getNode();
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($node->getOwner()->getUID() === $uid) {
|
2019-11-27 15:59:34 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$acl = $this->shareManager->getAccessList($node, true, true);
|
|
|
|
return array_key_exists($uid, $acl['users']);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
protected function getNode(): Node {
|
2020-08-05 14:00:30 +03:00
|
|
|
if ($this->node) {
|
|
|
|
return $this->node;
|
|
|
|
}
|
2019-11-27 15:59:34 +03:00
|
|
|
if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
switch ($this->eventName) {
|
|
|
|
case self::EVENT_NAMESPACE . 'postCreate':
|
|
|
|
case self::EVENT_NAMESPACE . 'postWrite':
|
|
|
|
case self::EVENT_NAMESPACE . 'postDelete':
|
|
|
|
case self::EVENT_NAMESPACE . 'postTouch':
|
|
|
|
return $this->event->getSubject();
|
|
|
|
case self::EVENT_NAMESPACE . 'postRename':
|
|
|
|
case self::EVENT_NAMESPACE . 'postCopy':
|
|
|
|
return $this->event->getSubject()[1];
|
2019-09-10 00:59:31 +03:00
|
|
|
case MapperEvent::EVENT_ASSIGN:
|
2019-11-27 15:59:34 +03:00
|
|
|
if (!$this->event instanceof MapperEvent || $this->event->getObjectType() !== 'files') {
|
|
|
|
throw new NotFoundException();
|
2019-09-10 00:59:31 +03:00
|
|
|
}
|
2019-11-27 15:59:34 +03:00
|
|
|
$nodes = $this->root->getById((int)$this->event->getObjectId());
|
2020-08-05 14:00:30 +03:00
|
|
|
if (is_array($nodes) && isset($nodes[0])) {
|
|
|
|
$this->node = $nodes[0];
|
|
|
|
return $this->node;
|
2019-09-10 00:59:31 +03:00
|
|
|
}
|
|
|
|
break;
|
2019-09-09 18:17:39 +03:00
|
|
|
}
|
2019-11-27 15:59:34 +03:00
|
|
|
throw new NotFoundException();
|
2019-09-09 18:17:39 +03:00
|
|
|
}
|
2019-11-27 19:22:26 +03:00
|
|
|
|
|
|
|
public function getDisplayText(int $verbosity = 0): string {
|
|
|
|
try {
|
|
|
|
$node = $this->getNode();
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = [
|
2020-08-05 14:00:30 +03:00
|
|
|
$this->actingUser ? $this->actingUser->getDisplayName() : $this->l10n->t('Someone'),
|
2019-11-27 19:22:26 +03:00
|
|
|
$node->getName()
|
|
|
|
];
|
|
|
|
|
|
|
|
switch ($this->eventName) {
|
|
|
|
case self::EVENT_NAMESPACE . 'postCreate':
|
|
|
|
return $this->l10n->t('%s created %s', $options);
|
|
|
|
case self::EVENT_NAMESPACE . 'postWrite':
|
|
|
|
return $this->l10n->t('%s modified %s', $options);
|
|
|
|
case self::EVENT_NAMESPACE . 'postDelete':
|
|
|
|
return $this->l10n->t('%s deleted %s', $options);
|
|
|
|
case self::EVENT_NAMESPACE . 'postTouch':
|
|
|
|
return $this->l10n->t('%s accessed %s', $options);
|
|
|
|
case self::EVENT_NAMESPACE . 'postRename':
|
|
|
|
return $this->l10n->t('%s renamed %s', $options);
|
|
|
|
case self::EVENT_NAMESPACE . 'postCopy':
|
|
|
|
return $this->l10n->t('%s copied %s', $options);
|
|
|
|
case MapperEvent::EVENT_ASSIGN:
|
|
|
|
$tagNames = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->event instanceof MapperEvent) {
|
2019-11-27 19:22:26 +03:00
|
|
|
$tagIDs = $this->event->getTags();
|
|
|
|
$tagObjects = $this->tagManager->getTagsByIds($tagIDs);
|
|
|
|
foreach ($tagObjects as $systemTag) {
|
|
|
|
/** @var ISystemTag $systemTag */
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($systemTag->isUserVisible()) {
|
2019-11-27 19:22:26 +03:00
|
|
|
$tagNames[] = $systemTag->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$filename = array_pop($options);
|
|
|
|
$tagString = implode(', ', $tagNames);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($tagString === '') {
|
2019-11-27 19:22:26 +03:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
array_push($options, $tagString, $filename);
|
|
|
|
return $this->l10n->t('%s assigned %s to %s', $options);
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 13:25:25 +03:00
|
|
|
|
|
|
|
public function getUrl(): string {
|
|
|
|
try {
|
|
|
|
return $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $this->getNode()->getId()]);
|
|
|
|
} catch (InvalidPathException $e) {
|
|
|
|
return '';
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 14:00:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function exportContextIDs(): array {
|
|
|
|
$nodeOwner = $this->getNode()->getOwner();
|
|
|
|
$actingUserId = null;
|
|
|
|
if ($this->actingUser instanceof IUser) {
|
|
|
|
$actingUserId = $this->actingUser->getUID();
|
|
|
|
} elseif ($this->userSession->getUser() instanceof IUser) {
|
|
|
|
$actingUserId = $this->userSession->getUser()->getUID();
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'eventName' => $this->eventName,
|
|
|
|
'nodeId' => $this->getNode()->getId(),
|
|
|
|
'nodeOwnerId' => $nodeOwner ? $nodeOwner->getUID() : null,
|
|
|
|
'actingUserId' => $actingUserId,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function importContextIDs(array $contextIDs): void {
|
|
|
|
$this->eventName = $contextIDs['eventName'];
|
|
|
|
if ($contextIDs['nodeOwnerId'] !== null) {
|
|
|
|
$userFolder = $this->root->getUserFolder($contextIDs['nodeOwnerId']);
|
|
|
|
$nodes = $userFolder->getById($contextIDs['nodeId']);
|
|
|
|
} else {
|
|
|
|
$nodes = $this->root->getById($contextIDs['nodeId']);
|
|
|
|
}
|
|
|
|
$this->node = $nodes[0] ?? null;
|
|
|
|
if ($contextIDs['actingUserId']) {
|
|
|
|
$this->actingUser = $this->userManager->get($contextIDs['actingUserId']);
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 01:14:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getIconUrl(): string {
|
|
|
|
return $this->getIcon();
|
|
|
|
}
|
2019-08-09 14:24:48 +03:00
|
|
|
}
|