Flow File entity implements IContextPortation

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2020-08-05 13:00:30 +02:00
parent 6c1c9273c0
commit b59efb61bd
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
2 changed files with 60 additions and 8 deletions

View File

@ -34,18 +34,21 @@ use OCP\Files\NotFoundException;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IManager as ShareManager;
use OCP\SystemTag\ISystemTag;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\MapperEvent;
use OCP\WorkflowEngine\EntityContext\IContextPortation;
use OCP\WorkflowEngine\EntityContext\IDisplayText;
use OCP\WorkflowEngine\EntityContext\IUrl;
use OCP\WorkflowEngine\GenericEntityEvent;
use OCP\WorkflowEngine\IEntity;
use OCP\WorkflowEngine\IRuleMatcher;
class File implements IEntity, IDisplayText, IUrl {
class File implements IEntity, IDisplayText, IUrl, IContextPortation {
private const EVENT_NAMESPACE = '\OCP\Files::';
/** @var IL10N */
@ -66,7 +69,12 @@ class File implements IEntity, IDisplayText, IUrl {
private $userSession;
/** @var ISystemTagManager */
private $tagManager;
/** @var ?Node */
private $node;
/** @var ?IUser */
private $actingUser = null;
/** @var IUserManager */
private $userManager;
public function __construct(
IL10N $l10n,
@ -75,7 +83,8 @@ class File implements IEntity, IDisplayText, IUrl {
ILogger $logger,
ShareManager $shareManager,
IUserSession $userSession,
ISystemTagManager $tagManager
ISystemTagManager $tagManager,
IUserManager $userManager
) {
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
@ -84,6 +93,7 @@ class File implements IEntity, IDisplayText, IUrl {
$this->shareManager = $shareManager;
$this->userSession = $userSession;
$this->tagManager = $tagManager;
$this->userManager = $userManager;
}
public function getName(): string {
@ -112,6 +122,7 @@ class File implements IEntity, IDisplayText, IUrl {
}
$this->eventName = $eventName;
$this->event = $event;
$this->actingUser = $this->actingUser ?? $this->userSession->getUser();
try {
$node = $this->getNode();
$ruleMatcher->setEntitySubject($this, $node);
@ -138,6 +149,9 @@ class File implements IEntity, IDisplayText, IUrl {
* @throws NotFoundException
*/
protected function getNode(): Node {
if ($this->node) {
return $this->node;
}
if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) {
throw new NotFoundException();
}
@ -155,8 +169,9 @@ class File implements IEntity, IDisplayText, IUrl {
throw new NotFoundException();
}
$nodes = $this->root->getById((int)$this->event->getObjectId());
if (is_array($nodes) && !empty($nodes)) {
return array_shift($nodes);
if (is_array($nodes) && isset($nodes[0])) {
$this->node = $nodes[0];
return $this->node;
}
break;
}
@ -164,7 +179,6 @@ class File implements IEntity, IDisplayText, IUrl {
}
public function getDisplayText(int $verbosity = 0): string {
$user = $this->userSession->getUser();
try {
$node = $this->getNode();
} catch (NotFoundException $e) {
@ -172,7 +186,7 @@ class File implements IEntity, IDisplayText, IUrl {
}
$options = [
$user ? $user->getDisplayName() : $this->l10n->t('Someone'),
$this->actingUser ? $this->actingUser->getDisplayName() : $this->l10n->t('Someone'),
$node->getName()
];
@ -220,4 +234,40 @@ class File implements IEntity, IDisplayText, IUrl {
return '';
}
}
/**
* @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']);
}
}
}

View File

@ -33,6 +33,7 @@ use OCP\IL10N;
use OCP\ILogger;
use OCP\IServerContainer;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\SystemTag\ISystemTagManager;
use OCP\WorkflowEngine\ICheck;
@ -295,7 +296,8 @@ class ManagerTest extends TestCase {
$this->createMock(ILogger::class),
$this->createMock(\OCP\Share\IManager::class),
$this->createMock(IUserSession::class),
$this->createMock(ISystemTagManager::class)
$this->createMock(ISystemTagManager::class),
$this->createMock(IUserManager::class),
])
->setMethodsExcept(['getEvents'])
->getMock();