From c2a52813e2cfadc43b096927ee9d9db3d9ac5c84 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 5 Sep 2019 15:52:11 +0200 Subject: extends ICheck with scope and entity support, provide them as initialState Signed-off-by: Arthur Schiwon --- apps/workflowengine/lib/Check/AbstractStringCheck.php | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'apps/workflowengine/lib/Check/AbstractStringCheck.php') diff --git a/apps/workflowengine/lib/Check/AbstractStringCheck.php b/apps/workflowengine/lib/Check/AbstractStringCheck.php index 0fd728e3496..7a385729e09 100644 --- a/apps/workflowengine/lib/Check/AbstractStringCheck.php +++ b/apps/workflowengine/lib/Check/AbstractStringCheck.php @@ -25,6 +25,7 @@ namespace OCA\WorkflowEngine\Check; use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IManager; abstract class AbstractStringCheck implements ICheck { @@ -101,6 +102,16 @@ abstract class AbstractStringCheck implements ICheck { } } + public function supportedEntities(): array { + // universal by default + return []; + } + + public function isAvailableForScope(int $scope): bool { + // admin only by default + return $scope === IManager::SCOPE_ADMIN; + } + /** * @param string $pattern * @param string $subject -- cgit v1.2.3 From fd2de58503a3c915cf178385db3e9962845719c4 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Mon, 9 Sep 2019 14:27:10 +0200 Subject: seperate setFileInfo from ICheck Signed-off-by: Arthur Schiwon --- .../lib/Check/AbstractStringCheck.php | 8 ---- apps/workflowengine/lib/Check/FileMimeType.php | 3 +- apps/workflowengine/lib/Check/FileName.php | 3 +- apps/workflowengine/lib/Check/FileSize.php | 7 ---- apps/workflowengine/lib/Check/FileSystemTags.php | 3 +- .../lib/Check/RequestRemoteAddress.php | 8 ---- apps/workflowengine/lib/Check/RequestTime.php | 8 ---- .../lib/Check/UserGroupMembership.php | 8 ---- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/public/WorkflowEngine/ICheck.php | 11 +----- lib/public/WorkflowEngine/IFileCheck.php | 44 ++++++++++++++++++++++ 12 files changed, 53 insertions(+), 52 deletions(-) create mode 100644 lib/public/WorkflowEngine/IFileCheck.php (limited to 'apps/workflowengine/lib/Check/AbstractStringCheck.php') diff --git a/apps/workflowengine/lib/Check/AbstractStringCheck.php b/apps/workflowengine/lib/Check/AbstractStringCheck.php index 7a385729e09..edd983ba699 100644 --- a/apps/workflowengine/lib/Check/AbstractStringCheck.php +++ b/apps/workflowengine/lib/Check/AbstractStringCheck.php @@ -42,14 +42,6 @@ abstract class AbstractStringCheck implements ICheck { $this->l = $l; } - /** - * @param IStorage $storage - * @param string $path - */ - public function setFileInfo(IStorage $storage, $path) { - // Nothing changes here with a different path - } - /** * @return string */ diff --git a/apps/workflowengine/lib/Check/FileMimeType.php b/apps/workflowengine/lib/Check/FileMimeType.php index 6d51992fdd0..52fc54fc859 100644 --- a/apps/workflowengine/lib/Check/FileMimeType.php +++ b/apps/workflowengine/lib/Check/FileMimeType.php @@ -27,8 +27,9 @@ use OCP\Files\IMimeTypeDetector; use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\IRequest; +use OCP\WorkflowEngine\IFileCheck; -class FileMimeType extends AbstractStringCheck { +class FileMimeType extends AbstractStringCheck implements IFileCheck { /** @var array */ protected $mimeType; diff --git a/apps/workflowengine/lib/Check/FileName.php b/apps/workflowengine/lib/Check/FileName.php index d9b63ebfed1..76c48b7955e 100644 --- a/apps/workflowengine/lib/Check/FileName.php +++ b/apps/workflowengine/lib/Check/FileName.php @@ -26,8 +26,9 @@ use OCA\WorkflowEngine\Entity\File; use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\IRequest; +use OCP\WorkflowEngine\IFileCheck; -class FileName extends AbstractStringCheck { +class FileName extends AbstractStringCheck implements IFileCheck { /** @var IRequest */ protected $request; diff --git a/apps/workflowengine/lib/Check/FileSize.php b/apps/workflowengine/lib/Check/FileSize.php index 06a45bdcd41..ac1896c6057 100644 --- a/apps/workflowengine/lib/Check/FileSize.php +++ b/apps/workflowengine/lib/Check/FileSize.php @@ -49,13 +49,6 @@ class FileSize implements ICheck { $this->request = $request; } - /** - * @param IStorage $storage - * @param string $path - */ - public function setFileInfo(IStorage $storage, $path) { - } - /** * @param string $operator * @param string $value diff --git a/apps/workflowengine/lib/Check/FileSystemTags.php b/apps/workflowengine/lib/Check/FileSystemTags.php index 12285c9b33e..41b657d53b0 100644 --- a/apps/workflowengine/lib/Check/FileSystemTags.php +++ b/apps/workflowengine/lib/Check/FileSystemTags.php @@ -31,8 +31,9 @@ use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagObjectMapper; use OCP\SystemTag\TagNotFoundException; use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IFileCheck; -class FileSystemTags implements ICheck { +class FileSystemTags implements ICheck, IFileCheck { /** @var array */ protected $fileIds; diff --git a/apps/workflowengine/lib/Check/RequestRemoteAddress.php b/apps/workflowengine/lib/Check/RequestRemoteAddress.php index c386168f597..28e3bd4be09 100644 --- a/apps/workflowengine/lib/Check/RequestRemoteAddress.php +++ b/apps/workflowengine/lib/Check/RequestRemoteAddress.php @@ -44,14 +44,6 @@ class RequestRemoteAddress implements ICheck { $this->request = $request; } - /** - * @param IStorage $storage - * @param string $path - */ - public function setFileInfo(IStorage $storage, $path) { - // A different path doesn't change time, so nothing to do here. - } - /** * @param string $operator * @param string $value diff --git a/apps/workflowengine/lib/Check/RequestTime.php b/apps/workflowengine/lib/Check/RequestTime.php index 79696583cdb..523d251789a 100644 --- a/apps/workflowengine/lib/Check/RequestTime.php +++ b/apps/workflowengine/lib/Check/RequestTime.php @@ -49,14 +49,6 @@ class RequestTime implements ICheck { $this->timeFactory = $timeFactory; } - /** - * @param IStorage $storage - * @param string $path - */ - public function setFileInfo(IStorage $storage, $path) { - // A different path doesn't change time, so nothing to do here. - } - /** * @param string $operator * @param string $value diff --git a/apps/workflowengine/lib/Check/UserGroupMembership.php b/apps/workflowengine/lib/Check/UserGroupMembership.php index 4b06dbd2da4..e9518733f9b 100644 --- a/apps/workflowengine/lib/Check/UserGroupMembership.php +++ b/apps/workflowengine/lib/Check/UserGroupMembership.php @@ -58,14 +58,6 @@ class UserGroupMembership implements ICheck { $this->l = $l; } - /** - * @param IStorage $storage - * @param string $path - */ - public function setFileInfo(IStorage $storage, $path) { - // A different path doesn't change group memberships, so nothing to do here. - } - /** * @param string $operator * @param string $value diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 6673cbe70a7..4078193b18a 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -444,6 +444,7 @@ return array( 'OCP\\WorkflowEngine\\IEntity' => $baseDir . '/lib/public/WorkflowEngine/IEntity.php', 'OCP\\WorkflowEngine\\IEntityAware' => $baseDir . '/lib/public/WorkflowEngine/IEntityAware.php', 'OCP\\WorkflowEngine\\IEntityEvent' => $baseDir . '/lib/public/WorkflowEngine/IEntityEvent.php', + 'OCP\\WorkflowEngine\\IFileCheck' => $baseDir . '/lib/public/WorkflowEngine/IFileCheck.php', 'OCP\\WorkflowEngine\\IManager' => $baseDir . '/lib/public/WorkflowEngine/IManager.php', 'OCP\\WorkflowEngine\\IOperation' => $baseDir . '/lib/public/WorkflowEngine/IOperation.php', 'OCP\\WorkflowEngine\\ISpecificOperation' => $baseDir . '/lib/public/WorkflowEngine/ISpecificOperation.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index d4b74d5c231..4cf093dcaa8 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -478,6 +478,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\WorkflowEngine\\IEntity' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IEntity.php', 'OCP\\WorkflowEngine\\IEntityAware' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IEntityAware.php', 'OCP\\WorkflowEngine\\IEntityEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IEntityEvent.php', + 'OCP\\WorkflowEngine\\IFileCheck' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IFileCheck.php', 'OCP\\WorkflowEngine\\IManager' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IManager.php', 'OCP\\WorkflowEngine\\IOperation' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IOperation.php', 'OCP\\WorkflowEngine\\ISpecificOperation' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/ISpecificOperation.php', diff --git a/lib/public/WorkflowEngine/ICheck.php b/lib/public/WorkflowEngine/ICheck.php index 92ec6f83893..f5586e83d51 100644 --- a/lib/public/WorkflowEngine/ICheck.php +++ b/lib/public/WorkflowEngine/ICheck.php @@ -2,6 +2,7 @@ /** * @copyright Copyright (c) 2016 Morris Jobke * + * @author Arthur Schiwon * @author Morris Jobke * * @license GNU AGPL version 3 or any later version @@ -23,9 +24,6 @@ namespace OCP\WorkflowEngine; - -use OCP\Files\Storage\IStorage; - /** * Interface ICheck * @@ -33,13 +31,6 @@ use OCP\Files\Storage\IStorage; * @since 9.1 */ interface ICheck { - /** - * @param IStorage $storage - * @param string $path - * @since 9.1 - */ - public function setFileInfo(IStorage $storage, $path); - /** * @param string $operator * @param string $value diff --git a/lib/public/WorkflowEngine/IFileCheck.php b/lib/public/WorkflowEngine/IFileCheck.php new file mode 100644 index 00000000000..ce15d6c352c --- /dev/null +++ b/lib/public/WorkflowEngine/IFileCheck.php @@ -0,0 +1,44 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace OCP\WorkflowEngine; + + +use OCP\Files\Storage\IStorage; + +/** + * Interface IFileCheck + * + * @package OCP\WorkflowEngine + * @since 18.0.0 + */ +interface IFileCheck { + /** + * @param IStorage $storage + * @param string $path + * @since 18.0.0 + */ + public function setFileInfo(IStorage $storage, $path); + +} -- cgit v1.2.3 From 4cd931fcc6bb7f794fd14ddb75c7867823449e79 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Mon, 9 Sep 2019 16:04:12 +0200 Subject: require IChecks to receive entity context Signed-off-by: Arthur Schiwon --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + apps/workflowengine/lib/Check/AFileCheck.php | 67 ++++++++++++++++++++++ .../lib/Check/AbstractStringCheck.php | 6 +- apps/workflowengine/lib/Check/FileMimeType.php | 12 ++-- apps/workflowengine/lib/Check/FileName.php | 17 +----- apps/workflowengine/lib/Check/FileSize.php | 5 ++ apps/workflowengine/lib/Check/FileSystemTags.php | 17 +----- .../lib/Check/RequestRemoteAddress.php | 6 +- apps/workflowengine/lib/Check/RequestTime.php | 5 +- apps/workflowengine/lib/Check/RequestUserAgent.php | 1 - .../lib/Check/UserGroupMembership.php | 6 +- lib/public/WorkflowEngine/ICheck.php | 20 +++++++ 13 files changed, 119 insertions(+), 45 deletions(-) create mode 100644 apps/workflowengine/lib/Check/AFileCheck.php (limited to 'apps/workflowengine/lib/Check/AbstractStringCheck.php') diff --git a/apps/workflowengine/composer/composer/autoload_classmap.php b/apps/workflowengine/composer/composer/autoload_classmap.php index 4dab03642dd..6264358788c 100644 --- a/apps/workflowengine/composer/composer/autoload_classmap.php +++ b/apps/workflowengine/composer/composer/autoload_classmap.php @@ -16,6 +16,7 @@ return array( 'OCA\\WorkflowEngine\\Check\\RequestTime' => $baseDir . '/../lib/Check/RequestTime.php', 'OCA\\WorkflowEngine\\Check\\RequestURL' => $baseDir . '/../lib/Check/RequestURL.php', 'OCA\\WorkflowEngine\\Check\\RequestUserAgent' => $baseDir . '/../lib/Check/RequestUserAgent.php', + 'OCA\\WorkflowEngine\\Check\\TFileCheck' => $baseDir . '/../lib/Check/AFileCheck.php', 'OCA\\WorkflowEngine\\Check\\UserGroupMembership' => $baseDir . '/../lib/Check/UserGroupMembership.php', 'OCA\\WorkflowEngine\\Command\\Index' => $baseDir . '/../lib/Command/Index.php', 'OCA\\WorkflowEngine\\Controller\\AWorkflowController' => $baseDir . '/../lib/Controller/AWorkflowController.php', diff --git a/apps/workflowengine/composer/composer/autoload_static.php b/apps/workflowengine/composer/composer/autoload_static.php index 38b1a5b0e76..cd370986f15 100644 --- a/apps/workflowengine/composer/composer/autoload_static.php +++ b/apps/workflowengine/composer/composer/autoload_static.php @@ -31,6 +31,7 @@ class ComposerStaticInitWorkflowEngine 'OCA\\WorkflowEngine\\Check\\RequestTime' => __DIR__ . '/..' . '/../lib/Check/RequestTime.php', 'OCA\\WorkflowEngine\\Check\\RequestURL' => __DIR__ . '/..' . '/../lib/Check/RequestURL.php', 'OCA\\WorkflowEngine\\Check\\RequestUserAgent' => __DIR__ . '/..' . '/../lib/Check/RequestUserAgent.php', + 'OCA\\WorkflowEngine\\Check\\TFileCheck' => __DIR__ . '/..' . '/../lib/Check/AFileCheck.php', 'OCA\\WorkflowEngine\\Check\\UserGroupMembership' => __DIR__ . '/..' . '/../lib/Check/UserGroupMembership.php', 'OCA\\WorkflowEngine\\Command\\Index' => __DIR__ . '/..' . '/../lib/Command/Index.php', 'OCA\\WorkflowEngine\\Controller\\AWorkflowController' => __DIR__ . '/..' . '/../lib/Controller/AWorkflowController.php', diff --git a/apps/workflowengine/lib/Check/AFileCheck.php b/apps/workflowengine/lib/Check/AFileCheck.php new file mode 100644 index 00000000000..45d251b1659 --- /dev/null +++ b/apps/workflowengine/lib/Check/AFileCheck.php @@ -0,0 +1,67 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace OCA\WorkflowEngine\Check; + +use OCA\WorkflowEngine\AppInfo\Application; +use OCA\WorkflowEngine\Entity\File; +use OCP\Files\Node; +use OCP\Files\Storage\IStorage; +use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IEntity; +use OCP\WorkflowEngine\IFileCheck; + +trait TFileCheck { + /** @var IStorage */ + protected $storage; + + /** @var string */ + protected $path; + + /** + * @param IStorage $storage + * @param string $path + * @since 18.0.0 + */ + public function setFileInfo(IStorage $storage, $path) { + $this->storage = $storage; + $this->path = $path; + } + + /** + * @throws \OCP\Files\NotFoundException + */ + public function setEntitySubject(IEntity $entity, $subject): void { + if ($entity instanceof File) { + if (!$subject instanceof Node) { + throw new \UnexpectedValueException( + 'Expected Node subject for File entity, got {class}', + ['app' => Application::APP_ID, 'class' => get_class($subject)] + ); + } + $this->storage = $subject->getStorage(); + $this->path = $subject->getPath(); + } + } +} diff --git a/apps/workflowengine/lib/Check/AbstractStringCheck.php b/apps/workflowengine/lib/Check/AbstractStringCheck.php index edd983ba699..ec9a1945d9c 100644 --- a/apps/workflowengine/lib/Check/AbstractStringCheck.php +++ b/apps/workflowengine/lib/Check/AbstractStringCheck.php @@ -22,9 +22,9 @@ namespace OCA\WorkflowEngine\Check; -use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IEntity; use OCP\WorkflowEngine\IManager; abstract class AbstractStringCheck implements ICheck { @@ -121,4 +121,8 @@ abstract class AbstractStringCheck implements ICheck { $this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject); return $this->matches[$patternHash][$subjectHash]; } + + public function setEntitySubject(IEntity $entity, $subject): void { + // Noop + } } diff --git a/apps/workflowengine/lib/Check/FileMimeType.php b/apps/workflowengine/lib/Check/FileMimeType.php index 52fc54fc859..92375d4d3cd 100644 --- a/apps/workflowengine/lib/Check/FileMimeType.php +++ b/apps/workflowengine/lib/Check/FileMimeType.php @@ -30,6 +30,9 @@ use OCP\IRequest; use OCP\WorkflowEngine\IFileCheck; class FileMimeType extends AbstractStringCheck implements IFileCheck { + use TFileCheck { + setFileInfo as _setFileInfo; + } /** @var array */ protected $mimeType; @@ -40,12 +43,6 @@ class FileMimeType extends AbstractStringCheck implements IFileCheck { /** @var IMimeTypeDetector */ protected $mimeTypeDetector; - /** @var IStorage */ - protected $storage; - - /** @var string */ - protected $path; - /** * @param IL10N $l * @param IRequest $request @@ -62,8 +59,7 @@ class FileMimeType extends AbstractStringCheck implements IFileCheck { * @param string $path */ public function setFileInfo(IStorage $storage, $path) { - $this->storage = $storage; - $this->path = $path; + $this->_setFileInfo($storage, $path); if (!isset($this->mimeType[$this->storage->getId()][$this->path]) || $this->mimeType[$this->storage->getId()][$this->path] === '') { $this->mimeType[$this->storage->getId()][$this->path] = null; diff --git a/apps/workflowengine/lib/Check/FileName.php b/apps/workflowengine/lib/Check/FileName.php index 76c48b7955e..62ee601980b 100644 --- a/apps/workflowengine/lib/Check/FileName.php +++ b/apps/workflowengine/lib/Check/FileName.php @@ -23,22 +23,16 @@ declare(strict_types=1); namespace OCA\WorkflowEngine\Check; use OCA\WorkflowEngine\Entity\File; -use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\IRequest; use OCP\WorkflowEngine\IFileCheck; class FileName extends AbstractStringCheck implements IFileCheck { + use TFileCheck; /** @var IRequest */ protected $request; - /** @var IStorage */ - protected $storage; - - /** @var string */ - protected $path; - /** * @param IL10N $l * @param IRequest $request @@ -48,15 +42,6 @@ class FileName extends AbstractStringCheck implements IFileCheck { $this->request = $request; } - /** - * @param IStorage $storage - * @param string $path - */ - public function setFileInfo(IStorage $storage, $path) { - $this->storage = $storage; - $this->path = $path; - } - /** * @return string */ diff --git a/apps/workflowengine/lib/Check/FileSize.php b/apps/workflowengine/lib/Check/FileSize.php index ac1896c6057..2ad4bb09e01 100644 --- a/apps/workflowengine/lib/Check/FileSize.php +++ b/apps/workflowengine/lib/Check/FileSize.php @@ -28,6 +28,7 @@ use OCP\IL10N; use OCP\IRequest; use OCP\Util; use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IEntity; class FileSize implements ICheck { @@ -118,4 +119,8 @@ class FileSize implements ICheck { public function isAvailableForScope(int $scope): bool { return true; } + + public function setEntitySubject(IEntity $entity, $subject): void { + // NOOP + } } diff --git a/apps/workflowengine/lib/Check/FileSystemTags.php b/apps/workflowengine/lib/Check/FileSystemTags.php index 41b657d53b0..e7e114881ad 100644 --- a/apps/workflowengine/lib/Check/FileSystemTags.php +++ b/apps/workflowengine/lib/Check/FileSystemTags.php @@ -25,7 +25,6 @@ namespace OCA\WorkflowEngine\Check; use OCA\WorkflowEngine\Entity\File; use OCP\Files\Cache\ICache; use OCP\Files\IHomeStorage; -use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagObjectMapper; @@ -34,6 +33,7 @@ use OCP\WorkflowEngine\ICheck; use OCP\WorkflowEngine\IFileCheck; class FileSystemTags implements ICheck, IFileCheck { + use TFileCheck; /** @var array */ protected $fileIds; @@ -50,12 +50,6 @@ class FileSystemTags implements ICheck, IFileCheck { /** @var ISystemTagObjectMapper */ protected $systemTagObjectMapper; - /** @var IStorage */ - protected $storage; - - /** @var string */ - protected $path; - /** * @param IL10N $l * @param ISystemTagManager $systemTagManager @@ -67,15 +61,6 @@ class FileSystemTags implements ICheck, IFileCheck { $this->systemTagObjectMapper = $systemTagObjectMapper; } - /** - * @param IStorage $storage - * @param string $path - */ - public function setFileInfo(IStorage $storage, $path) { - $this->storage = $storage; - $this->path = $path; - } - /** * @param string $operator * @param string $value diff --git a/apps/workflowengine/lib/Check/RequestRemoteAddress.php b/apps/workflowengine/lib/Check/RequestRemoteAddress.php index 28e3bd4be09..0079db4b831 100644 --- a/apps/workflowengine/lib/Check/RequestRemoteAddress.php +++ b/apps/workflowengine/lib/Check/RequestRemoteAddress.php @@ -22,10 +22,10 @@ namespace OCA\WorkflowEngine\Check; -use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\IRequest; use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IEntity; class RequestRemoteAddress implements ICheck { @@ -171,4 +171,8 @@ class RequestRemoteAddress implements ICheck { public function isAvailableForScope(int $scope): bool { return true; } + + public function setEntitySubject(IEntity $entity, $subject): void { + // NOOP + } } diff --git a/apps/workflowengine/lib/Check/RequestTime.php b/apps/workflowengine/lib/Check/RequestTime.php index 523d251789a..bf37bf3d2ba 100644 --- a/apps/workflowengine/lib/Check/RequestTime.php +++ b/apps/workflowengine/lib/Check/RequestTime.php @@ -23,9 +23,9 @@ namespace OCA\WorkflowEngine\Check; use OCP\AppFramework\Utility\ITimeFactory; -use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IEntity; class RequestTime implements ICheck { @@ -135,4 +135,7 @@ class RequestTime implements ICheck { return []; } + public function setEntitySubject(IEntity $entity, $subject): void { + // NOOP + } } diff --git a/apps/workflowengine/lib/Check/RequestUserAgent.php b/apps/workflowengine/lib/Check/RequestUserAgent.php index 1abb56bbe2d..ddc222833dd 100644 --- a/apps/workflowengine/lib/Check/RequestUserAgent.php +++ b/apps/workflowengine/lib/Check/RequestUserAgent.php @@ -24,7 +24,6 @@ namespace OCA\WorkflowEngine\Check; use OCP\IL10N; use OCP\IRequest; -use OCP\WorkflowEngine\IManager; class RequestUserAgent extends AbstractStringCheck { diff --git a/apps/workflowengine/lib/Check/UserGroupMembership.php b/apps/workflowengine/lib/Check/UserGroupMembership.php index e9518733f9b..55f1d017ed0 100644 --- a/apps/workflowengine/lib/Check/UserGroupMembership.php +++ b/apps/workflowengine/lib/Check/UserGroupMembership.php @@ -22,12 +22,12 @@ namespace OCA\WorkflowEngine\Check; -use OCP\Files\Storage\IStorage; use OCP\IGroupManager; use OCP\IL10N; use OCP\IUser; use OCP\IUserSession; use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IEntity; use OCP\WorkflowEngine\IManager; class UserGroupMembership implements ICheck { @@ -114,4 +114,8 @@ class UserGroupMembership implements ICheck { // admin only by default return $scope === IManager::SCOPE_ADMIN; } + + public function setEntitySubject(IEntity $entity, $subject): void { + // NOOP + } } diff --git a/lib/public/WorkflowEngine/ICheck.php b/lib/public/WorkflowEngine/ICheck.php index f5586e83d51..6e9c2fc765e 100644 --- a/lib/public/WorkflowEngine/ICheck.php +++ b/lib/public/WorkflowEngine/ICheck.php @@ -70,4 +70,24 @@ interface ICheck { * @since 18.0.0 */ public function isAvailableForScope(int $scope): bool; + + /** + * Equips the check with a subject fitting the Entity. For instance, an + * entity of File will receive an instance of OCP\Files\Node, or a comment + * entity might get an IComment. + * + * The implementing check must be aware of the incoming type. + * + * If an unsupported subject is passed the implementation MAY throw an + * \UnexpectedValueException. + * + * When an implementation does not depend on a subject being passed to it, + * for example RequestTime, the implemented method SHOULD just pass, without + * any further operation. + * + * @param IEntity $entity + * @param mixed $subject + * @throws \UnexpectedValueException + */ + public function setEntitySubject(IEntity $entity, $subject): void; } -- cgit v1.2.3 From 849d025d093d1c80d3820901d20bf6a664c6af02 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Mon, 9 Sep 2019 16:53:59 +0200 Subject: let a dedicate service serve a stateful process * includes making ICheck not requiring any context setter * and IFileCheck extending the IEntityCheck as entity data can be handed in via Dispatcher Signed-off-by: Arthur Schiwon --- .../composer/composer/autoload_classmap.php | 3 +- .../composer/composer/autoload_static.php | 3 +- apps/workflowengine/lib/Check/AFileCheck.php | 67 ---------- .../lib/Check/AbstractStringCheck.php | 5 - apps/workflowengine/lib/Check/FileSize.php | 5 - .../lib/Check/RequestRemoteAddress.php | 5 - apps/workflowengine/lib/Check/RequestTime.php | 5 - apps/workflowengine/lib/Check/TFileCheck.php | 65 ++++++++++ .../lib/Check/UserGroupMembership.php | 5 - apps/workflowengine/lib/Manager.php | 97 +-------------- apps/workflowengine/lib/Service/RuleMatcher.php | 137 +++++++++++++++++++++ lib/composer/composer/autoload_classmap.php | 3 +- lib/composer/composer/autoload_static.php | 3 +- lib/public/WorkflowEngine/ICheck.php | 20 --- lib/public/WorkflowEngine/IEntityAware.php | 40 ------ lib/public/WorkflowEngine/IEntityCheck.php | 53 ++++++++ lib/public/WorkflowEngine/IFileCheck.php | 6 +- lib/public/WorkflowEngine/IManager.php | 23 +--- lib/public/WorkflowEngine/IRuleMatcher.php | 39 ++++++ 19 files changed, 314 insertions(+), 270 deletions(-) delete mode 100644 apps/workflowengine/lib/Check/AFileCheck.php create mode 100644 apps/workflowengine/lib/Check/TFileCheck.php create mode 100644 apps/workflowengine/lib/Service/RuleMatcher.php delete mode 100644 lib/public/WorkflowEngine/IEntityAware.php create mode 100644 lib/public/WorkflowEngine/IEntityCheck.php create mode 100644 lib/public/WorkflowEngine/IRuleMatcher.php (limited to 'apps/workflowengine/lib/Check/AbstractStringCheck.php') diff --git a/apps/workflowengine/composer/composer/autoload_classmap.php b/apps/workflowengine/composer/composer/autoload_classmap.php index 6264358788c..0de2a50234c 100644 --- a/apps/workflowengine/composer/composer/autoload_classmap.php +++ b/apps/workflowengine/composer/composer/autoload_classmap.php @@ -16,7 +16,7 @@ return array( 'OCA\\WorkflowEngine\\Check\\RequestTime' => $baseDir . '/../lib/Check/RequestTime.php', 'OCA\\WorkflowEngine\\Check\\RequestURL' => $baseDir . '/../lib/Check/RequestURL.php', 'OCA\\WorkflowEngine\\Check\\RequestUserAgent' => $baseDir . '/../lib/Check/RequestUserAgent.php', - 'OCA\\WorkflowEngine\\Check\\TFileCheck' => $baseDir . '/../lib/Check/AFileCheck.php', + 'OCA\\WorkflowEngine\\Check\\TFileCheck' => $baseDir . '/../lib/Check/TFileCheck.php', 'OCA\\WorkflowEngine\\Check\\UserGroupMembership' => $baseDir . '/../lib/Check/UserGroupMembership.php', 'OCA\\WorkflowEngine\\Command\\Index' => $baseDir . '/../lib/Command/Index.php', 'OCA\\WorkflowEngine\\Controller\\AWorkflowController' => $baseDir . '/../lib/Controller/AWorkflowController.php', @@ -28,6 +28,7 @@ return array( 'OCA\\WorkflowEngine\\Manager' => $baseDir . '/../lib/Manager.php', 'OCA\\WorkflowEngine\\Migration\\PopulateNewlyIntroducedDatabaseFields' => $baseDir . '/../lib/Migration/PopulateNewlyIntroducedDatabaseFields.php', 'OCA\\WorkflowEngine\\Migration\\Version2019Date20190808074233' => $baseDir . '/../lib/Migration/Version2019Date20190808074233.php', + 'OCA\\WorkflowEngine\\Service\\RuleMatcher' => $baseDir . '/../lib/Service/RuleMatcher.php', 'OCA\\WorkflowEngine\\Settings\\ASettings' => $baseDir . '/../lib/Settings/ASettings.php', 'OCA\\WorkflowEngine\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php', 'OCA\\WorkflowEngine\\Settings\\Personal' => $baseDir . '/../lib/Settings/Personal.php', diff --git a/apps/workflowengine/composer/composer/autoload_static.php b/apps/workflowengine/composer/composer/autoload_static.php index cd370986f15..0f8a488295b 100644 --- a/apps/workflowengine/composer/composer/autoload_static.php +++ b/apps/workflowengine/composer/composer/autoload_static.php @@ -31,7 +31,7 @@ class ComposerStaticInitWorkflowEngine 'OCA\\WorkflowEngine\\Check\\RequestTime' => __DIR__ . '/..' . '/../lib/Check/RequestTime.php', 'OCA\\WorkflowEngine\\Check\\RequestURL' => __DIR__ . '/..' . '/../lib/Check/RequestURL.php', 'OCA\\WorkflowEngine\\Check\\RequestUserAgent' => __DIR__ . '/..' . '/../lib/Check/RequestUserAgent.php', - 'OCA\\WorkflowEngine\\Check\\TFileCheck' => __DIR__ . '/..' . '/../lib/Check/AFileCheck.php', + 'OCA\\WorkflowEngine\\Check\\TFileCheck' => __DIR__ . '/..' . '/../lib/Check/TFileCheck.php', 'OCA\\WorkflowEngine\\Check\\UserGroupMembership' => __DIR__ . '/..' . '/../lib/Check/UserGroupMembership.php', 'OCA\\WorkflowEngine\\Command\\Index' => __DIR__ . '/..' . '/../lib/Command/Index.php', 'OCA\\WorkflowEngine\\Controller\\AWorkflowController' => __DIR__ . '/..' . '/../lib/Controller/AWorkflowController.php', @@ -43,6 +43,7 @@ class ComposerStaticInitWorkflowEngine 'OCA\\WorkflowEngine\\Manager' => __DIR__ . '/..' . '/../lib/Manager.php', 'OCA\\WorkflowEngine\\Migration\\PopulateNewlyIntroducedDatabaseFields' => __DIR__ . '/..' . '/../lib/Migration/PopulateNewlyIntroducedDatabaseFields.php', 'OCA\\WorkflowEngine\\Migration\\Version2019Date20190808074233' => __DIR__ . '/..' . '/../lib/Migration/Version2019Date20190808074233.php', + 'OCA\\WorkflowEngine\\Service\\RuleMatcher' => __DIR__ . '/..' . '/../lib/Service/RuleMatcher.php', 'OCA\\WorkflowEngine\\Settings\\ASettings' => __DIR__ . '/..' . '/../lib/Settings/ASettings.php', 'OCA\\WorkflowEngine\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php', 'OCA\\WorkflowEngine\\Settings\\Personal' => __DIR__ . '/..' . '/../lib/Settings/Personal.php', diff --git a/apps/workflowengine/lib/Check/AFileCheck.php b/apps/workflowengine/lib/Check/AFileCheck.php deleted file mode 100644 index 45d251b1659..00000000000 --- a/apps/workflowengine/lib/Check/AFileCheck.php +++ /dev/null @@ -1,67 +0,0 @@ - - * - * @author Arthur Schiwon - * - * @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 . - * - */ - -namespace OCA\WorkflowEngine\Check; - -use OCA\WorkflowEngine\AppInfo\Application; -use OCA\WorkflowEngine\Entity\File; -use OCP\Files\Node; -use OCP\Files\Storage\IStorage; -use OCP\WorkflowEngine\ICheck; -use OCP\WorkflowEngine\IEntity; -use OCP\WorkflowEngine\IFileCheck; - -trait TFileCheck { - /** @var IStorage */ - protected $storage; - - /** @var string */ - protected $path; - - /** - * @param IStorage $storage - * @param string $path - * @since 18.0.0 - */ - public function setFileInfo(IStorage $storage, $path) { - $this->storage = $storage; - $this->path = $path; - } - - /** - * @throws \OCP\Files\NotFoundException - */ - public function setEntitySubject(IEntity $entity, $subject): void { - if ($entity instanceof File) { - if (!$subject instanceof Node) { - throw new \UnexpectedValueException( - 'Expected Node subject for File entity, got {class}', - ['app' => Application::APP_ID, 'class' => get_class($subject)] - ); - } - $this->storage = $subject->getStorage(); - $this->path = $subject->getPath(); - } - } -} diff --git a/apps/workflowengine/lib/Check/AbstractStringCheck.php b/apps/workflowengine/lib/Check/AbstractStringCheck.php index ec9a1945d9c..e86ac6bec17 100644 --- a/apps/workflowengine/lib/Check/AbstractStringCheck.php +++ b/apps/workflowengine/lib/Check/AbstractStringCheck.php @@ -24,7 +24,6 @@ namespace OCA\WorkflowEngine\Check; use OCP\IL10N; use OCP\WorkflowEngine\ICheck; -use OCP\WorkflowEngine\IEntity; use OCP\WorkflowEngine\IManager; abstract class AbstractStringCheck implements ICheck { @@ -121,8 +120,4 @@ abstract class AbstractStringCheck implements ICheck { $this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject); return $this->matches[$patternHash][$subjectHash]; } - - public function setEntitySubject(IEntity $entity, $subject): void { - // Noop - } } diff --git a/apps/workflowengine/lib/Check/FileSize.php b/apps/workflowengine/lib/Check/FileSize.php index 2ad4bb09e01..736a3d932fb 100644 --- a/apps/workflowengine/lib/Check/FileSize.php +++ b/apps/workflowengine/lib/Check/FileSize.php @@ -23,7 +23,6 @@ namespace OCA\WorkflowEngine\Check; use OCA\WorkflowEngine\Entity\File; -use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\IRequest; use OCP\Util; @@ -119,8 +118,4 @@ class FileSize implements ICheck { public function isAvailableForScope(int $scope): bool { return true; } - - public function setEntitySubject(IEntity $entity, $subject): void { - // NOOP - } } diff --git a/apps/workflowengine/lib/Check/RequestRemoteAddress.php b/apps/workflowengine/lib/Check/RequestRemoteAddress.php index 0079db4b831..00029771b52 100644 --- a/apps/workflowengine/lib/Check/RequestRemoteAddress.php +++ b/apps/workflowengine/lib/Check/RequestRemoteAddress.php @@ -25,7 +25,6 @@ namespace OCA\WorkflowEngine\Check; use OCP\IL10N; use OCP\IRequest; use OCP\WorkflowEngine\ICheck; -use OCP\WorkflowEngine\IEntity; class RequestRemoteAddress implements ICheck { @@ -171,8 +170,4 @@ class RequestRemoteAddress implements ICheck { public function isAvailableForScope(int $scope): bool { return true; } - - public function setEntitySubject(IEntity $entity, $subject): void { - // NOOP - } } diff --git a/apps/workflowengine/lib/Check/RequestTime.php b/apps/workflowengine/lib/Check/RequestTime.php index bf37bf3d2ba..a6ab4a961ed 100644 --- a/apps/workflowengine/lib/Check/RequestTime.php +++ b/apps/workflowengine/lib/Check/RequestTime.php @@ -25,7 +25,6 @@ namespace OCA\WorkflowEngine\Check; use OCP\AppFramework\Utility\ITimeFactory; use OCP\IL10N; use OCP\WorkflowEngine\ICheck; -use OCP\WorkflowEngine\IEntity; class RequestTime implements ICheck { @@ -134,8 +133,4 @@ class RequestTime implements ICheck { public function supportedEntities(): array { return []; } - - public function setEntitySubject(IEntity $entity, $subject): void { - // NOOP - } } diff --git a/apps/workflowengine/lib/Check/TFileCheck.php b/apps/workflowengine/lib/Check/TFileCheck.php new file mode 100644 index 00000000000..44a39aadfde --- /dev/null +++ b/apps/workflowengine/lib/Check/TFileCheck.php @@ -0,0 +1,65 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace OCA\WorkflowEngine\Check; + +use OCA\WorkflowEngine\AppInfo\Application; +use OCA\WorkflowEngine\Entity\File; +use OCP\Files\Node; +use OCP\Files\Storage\IStorage; +use OCP\WorkflowEngine\IEntity; + +trait TFileCheck { + /** @var IStorage */ + protected $storage; + + /** @var string */ + protected $path; + + /** + * @param IStorage $storage + * @param string $path + * @since 18.0.0 + */ + public function setFileInfo(IStorage $storage, $path) { + $this->storage = $storage; + $this->path = $path; + } + + /** + * @throws \OCP\Files\NotFoundException + */ + public function setEntitySubject(IEntity $entity, $subject): void { + if ($entity instanceof File) { + if (!$subject instanceof Node) { + throw new \UnexpectedValueException( + 'Expected Node subject for File entity, got {class}', + ['app' => Application::APP_ID, 'class' => get_class($subject)] + ); + } + $this->storage = $subject->getStorage(); + $this->path = $subject->getPath(); + } + } +} diff --git a/apps/workflowengine/lib/Check/UserGroupMembership.php b/apps/workflowengine/lib/Check/UserGroupMembership.php index 55f1d017ed0..8839820050a 100644 --- a/apps/workflowengine/lib/Check/UserGroupMembership.php +++ b/apps/workflowengine/lib/Check/UserGroupMembership.php @@ -27,7 +27,6 @@ use OCP\IL10N; use OCP\IUser; use OCP\IUserSession; use OCP\WorkflowEngine\ICheck; -use OCP\WorkflowEngine\IEntity; use OCP\WorkflowEngine\IManager; class UserGroupMembership implements ICheck { @@ -114,8 +113,4 @@ class UserGroupMembership implements ICheck { // admin only by default return $scope === IManager::SCOPE_ADMIN; } - - public function setEntitySubject(IEntity $entity, $subject): void { - // NOOP - } } diff --git a/apps/workflowengine/lib/Manager.php b/apps/workflowengine/lib/Manager.php index 2e78b75aeb8..efe6c387059 100644 --- a/apps/workflowengine/lib/Manager.php +++ b/apps/workflowengine/lib/Manager.php @@ -21,7 +21,6 @@ namespace OCA\WorkflowEngine; -use OC\Files\Storage\Wrapper\Jail; use Doctrine\DBAL\DBALException; use OC\Cache\CappedMemoryCache; use OCA\WorkflowEngine\Check\FileMimeType; @@ -35,6 +34,7 @@ use OCA\WorkflowEngine\Check\RequestUserAgent; use OCA\WorkflowEngine\Check\UserGroupMembership; use OCA\WorkflowEngine\Entity\File; use OCA\WorkflowEngine\Helper\ScopeContext; +use OCA\WorkflowEngine\Service\RuleMatcher; use OCP\AppFramework\QueryException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\Storage\IStorage; @@ -46,14 +46,14 @@ use OCP\IUserSession; use OCP\WorkflowEngine\ICheck; use OCP\WorkflowEngine\IComplexOperation; use OCP\WorkflowEngine\IEntity; -use OCP\WorkflowEngine\IEntityAware; use OCP\WorkflowEngine\IEntityEvent; use OCP\WorkflowEngine\IManager; use OCP\WorkflowEngine\IOperation; +use OCP\WorkflowEngine\IRuleMatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; -class Manager implements IManager, IEntityAware { +class Manager implements IManager { /** @var IStorage */ protected $storage; @@ -122,77 +122,8 @@ class Manager implements IManager, IEntityAware { $this->session = $session; } - /** - * @inheritdoc - */ - public function setFileInfo(IStorage $storage, $path) { - $this->storage = $storage; - - if ($storage->instanceOfStorage(Jail::class)) { - /** @var Jail $storage */ - $path = $storage->getJailedPath($path); - } - $this->path = $path; - } - - /** - * @inheritdoc - */ - public function getMatchingOperations($class, $returnFirstMatchingOperationOnly = true) { - $scopes[] = new ScopeContext(IManager::SCOPE_ADMIN); - $user = $this->session->getUser(); - if($user !== null) { - $scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID()); - } - - $operations = []; - foreach ($scopes as $scope) { - $operations = array_merge($operations, $this->getOperations($class, $scope)); - } - - $matches = []; - foreach ($operations as $operation) { - $checkIds = json_decode($operation['checks'], true); - $checks = $this->getChecks($checkIds); - - foreach ($checks as $check) { - if (!$this->check($check)) { - // Check did not match, continue with the next operation - continue 2; - } - } - - if ($returnFirstMatchingOperationOnly) { - return $operation; - } - $matches[] = $operation; - } - - return $matches; - } - - /** - * @param array $check - * @return bool - */ - protected function check(array $check) { - try { - $checkInstance = $this->container->query($check['class']); - } catch (QueryException $e) { - // Check does not exist, assume it matches. - return true; - } - - if ($checkInstance instanceof IEntityAware && $this->entity !== null) { - $checkInstance->setEntity($this->entity); - return $checkInstance->executeCheck($check['operator'], $check['value']); - } elseif ($checkInstance instanceof ICheck) { - $checkInstance->setFileInfo($this->storage, $this->path); - return $checkInstance->executeCheck($check['operator'], $check['value']); - } else { - // Check is invalid - throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class'])); - } + public function getRuleMatcher(): IRuleMatcher { + return new RuleMatcher($this->session, $this->container, $this->l, $this); } public function getAllConfiguredEvents() { @@ -634,24 +565,6 @@ class Manager implements IManager, IEntityAware { return $operation; } - /** - * @param object $entity - * @since 18.0.0 - */ - public function setEntity($entity) { - if(!is_object($entity)) { - $this->container->getLogger()->logException( - new \InvalidArgumentException('provided entity is not an object'), - [ - 'app' => 'workflowengine', - 'level' => ILogger::ERROR, - ] - ); - return; - } - $this->entity = $entity; - } - /** * @return IEntity[] */ diff --git a/apps/workflowengine/lib/Service/RuleMatcher.php b/apps/workflowengine/lib/Service/RuleMatcher.php new file mode 100644 index 00000000000..bc92eeee818 --- /dev/null +++ b/apps/workflowengine/lib/Service/RuleMatcher.php @@ -0,0 +1,137 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace OCA\WorkflowEngine\Service; + +use OCA\WorkflowEngine\AppInfo\Application; +use OCA\WorkflowEngine\Entity\File; +use OCA\WorkflowEngine\Helper\ScopeContext; +use OCA\WorkflowEngine\Manager; +use OCP\AppFramework\QueryException; +use OCP\Files\Node; +use OCP\Files\Storage\IStorage; +use OCP\IL10N; +use OCP\IServerContainer; +use OCP\IUserSession; +use OCP\WorkflowEngine\ICheck; +use OCP\WorkflowEngine\IEntity; +use OCP\WorkflowEngine\IFileCheck; +use OCP\WorkflowEngine\IManager; +use OCP\WorkflowEngine\IRuleMatcher; + +class RuleMatcher implements IRuleMatcher { + + /** @var IUserSession */ + protected $session; + /** @var IManager */ + protected $manager; + /** @var array */ + protected $contexts; + /** @var IServerContainer */ + protected $container; + /** @var array */ + protected $fileInfo = []; + /** @var IL10N */ + protected $l; + + public function __construct(IUserSession $session, IServerContainer $container, IL10N $l, Manager $manager) { + $this->session = $session; + $this->manager = $manager; + $this->container = $container; + $this->l = $l; + } + + public function setFileInfo(IStorage $storage, string $path): void { + $this->fileInfo['storage'] = $storage; + $this->fileInfo['path'] = $path; + } + + + public function setEntitySubject(IEntity $entity, $subject): void { + $this->contexts[get_class($entity)] = [$entity, $subject]; + } + + public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array { + $scopes[] = new ScopeContext(IManager::SCOPE_ADMIN); + $user = $this->session->getUser(); + if($user !== null) { + $scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID()); + } + + $operations = []; + foreach ($scopes as $scope) { + $operations = array_merge($operations, $this->manager->getOperations($class, $scope)); + } + + $matches = []; + foreach ($operations as $operation) { + $checkIds = json_decode($operation['checks'], true); + $checks = $this->manager->getChecks($checkIds); + + foreach ($checks as $check) { + if (!$this->check($check)) { + // Check did not match, continue with the next operation + continue 2; + } + } + + if ($returnFirstMatchingOperationOnly) { + return $operation; + } + $matches[] = $operation; + } + + return $matches; + } + + /** + * @param array $check + * @return bool + */ + public function check(array $check) { + try { + $checkInstance = $this->container->query($check['class']); + } catch (QueryException $e) { + // Check does not exist, assume it matches. + return true; + } + + if ($checkInstance instanceof ICheck) { + foreach($this->contexts as $entityInfo) { + list($entity, $subject) = $entityInfo; + $checkInstance->setEntitySubject($entity, $subject); + } + return $checkInstance->executeCheck($check['operator'], $check['value']); + } elseif ($checkInstance instanceof IFileCheck) { + if(empty($this->fileInfo)) { + throw new \RuntimeException('Must set file info before running the check'); + } + $checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path']); + return $checkInstance->executeCheck($check['operator'], $check['value']); + } else { + // Check is invalid + throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class'])); + } + } +} diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 4078193b18a..b51b689b629 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -442,11 +442,12 @@ return array( 'OCP\\WorkflowEngine\\ICheck' => $baseDir . '/lib/public/WorkflowEngine/ICheck.php', 'OCP\\WorkflowEngine\\IComplexOperation' => $baseDir . '/lib/public/WorkflowEngine/IComplexOperation.php', 'OCP\\WorkflowEngine\\IEntity' => $baseDir . '/lib/public/WorkflowEngine/IEntity.php', - 'OCP\\WorkflowEngine\\IEntityAware' => $baseDir . '/lib/public/WorkflowEngine/IEntityAware.php', + 'OCP\\WorkflowEngine\\IEntityCheck' => $baseDir . '/lib/public/WorkflowEngine/IEntityCheck.php', 'OCP\\WorkflowEngine\\IEntityEvent' => $baseDir . '/lib/public/WorkflowEngine/IEntityEvent.php', 'OCP\\WorkflowEngine\\IFileCheck' => $baseDir . '/lib/public/WorkflowEngine/IFileCheck.php', 'OCP\\WorkflowEngine\\IManager' => $baseDir . '/lib/public/WorkflowEngine/IManager.php', 'OCP\\WorkflowEngine\\IOperation' => $baseDir . '/lib/public/WorkflowEngine/IOperation.php', + 'OCP\\WorkflowEngine\\IRuleMatcher' => $baseDir . '/lib/public/WorkflowEngine/IRuleMatcher.php', 'OCP\\WorkflowEngine\\ISpecificOperation' => $baseDir . '/lib/public/WorkflowEngine/ISpecificOperation.php', 'OC\\Accounts\\Account' => $baseDir . '/lib/private/Accounts/Account.php', 'OC\\Accounts\\AccountManager' => $baseDir . '/lib/private/Accounts/AccountManager.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 4cf093dcaa8..1bcefc1dc1c 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -476,11 +476,12 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\WorkflowEngine\\ICheck' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/ICheck.php', 'OCP\\WorkflowEngine\\IComplexOperation' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IComplexOperation.php', 'OCP\\WorkflowEngine\\IEntity' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IEntity.php', - 'OCP\\WorkflowEngine\\IEntityAware' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IEntityAware.php', + 'OCP\\WorkflowEngine\\IEntityCheck' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IEntityCheck.php', 'OCP\\WorkflowEngine\\IEntityEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IEntityEvent.php', 'OCP\\WorkflowEngine\\IFileCheck' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IFileCheck.php', 'OCP\\WorkflowEngine\\IManager' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IManager.php', 'OCP\\WorkflowEngine\\IOperation' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IOperation.php', + 'OCP\\WorkflowEngine\\IRuleMatcher' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IRuleMatcher.php', 'OCP\\WorkflowEngine\\ISpecificOperation' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/ISpecificOperation.php', 'OC\\Accounts\\Account' => __DIR__ . '/../../..' . '/lib/private/Accounts/Account.php', 'OC\\Accounts\\AccountManager' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountManager.php', diff --git a/lib/public/WorkflowEngine/ICheck.php b/lib/public/WorkflowEngine/ICheck.php index 6e9c2fc765e..f5586e83d51 100644 --- a/lib/public/WorkflowEngine/ICheck.php +++ b/lib/public/WorkflowEngine/ICheck.php @@ -70,24 +70,4 @@ interface ICheck { * @since 18.0.0 */ public function isAvailableForScope(int $scope): bool; - - /** - * Equips the check with a subject fitting the Entity. For instance, an - * entity of File will receive an instance of OCP\Files\Node, or a comment - * entity might get an IComment. - * - * The implementing check must be aware of the incoming type. - * - * If an unsupported subject is passed the implementation MAY throw an - * \UnexpectedValueException. - * - * When an implementation does not depend on a subject being passed to it, - * for example RequestTime, the implemented method SHOULD just pass, without - * any further operation. - * - * @param IEntity $entity - * @param mixed $subject - * @throws \UnexpectedValueException - */ - public function setEntitySubject(IEntity $entity, $subject): void; } diff --git a/lib/public/WorkflowEngine/IEntityAware.php b/lib/public/WorkflowEngine/IEntityAware.php deleted file mode 100644 index 5ef5066f9ad..00000000000 --- a/lib/public/WorkflowEngine/IEntityAware.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * @author Arthur Schiwon - * - * @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 . - * - */ - -namespace OCP\WorkflowEngine; - -/** - * Interface IEntityAware - * - * @package OCP\WorkflowEngine - * - * @since 18.0.0 - */ -interface IEntityAware { - /** - * @param object $entity - * @since 18.0.0 - */ - public function setEntity($entity); -} diff --git a/lib/public/WorkflowEngine/IEntityCheck.php b/lib/public/WorkflowEngine/IEntityCheck.php new file mode 100644 index 00000000000..82687a052ba --- /dev/null +++ b/lib/public/WorkflowEngine/IEntityCheck.php @@ -0,0 +1,53 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace OCP\WorkflowEngine; + + +use OCP\Files\Storage\IStorage; + +/** + * Interface IFileCheck + * + * @package OCP\WorkflowEngine + * @since 18.0.0 + */ +interface IEntityCheck { + /** + * Equips the check with a subject fitting the Entity. For instance, an + * entity of File will receive an instance of OCP\Files\Node, or a comment + * entity might get an IComment. + * + * The implementing check must be aware of the incoming type. + * + * If an unsupported subject is passed the implementation MAY throw an + * \UnexpectedValueException. + * + * @param IEntity $entity + * @param mixed $subject + * @throws \UnexpectedValueException + */ + public function setEntitySubject(IEntity $entity, $subject): void; + +} diff --git a/lib/public/WorkflowEngine/IFileCheck.php b/lib/public/WorkflowEngine/IFileCheck.php index ce15d6c352c..557ba0f3c10 100644 --- a/lib/public/WorkflowEngine/IFileCheck.php +++ b/lib/public/WorkflowEngine/IFileCheck.php @@ -33,12 +33,10 @@ use OCP\Files\Storage\IStorage; * @package OCP\WorkflowEngine * @since 18.0.0 */ -interface IFileCheck { +interface IFileCheck extends IEntityCheck { /** - * @param IStorage $storage - * @param string $path * @since 18.0.0 */ - public function setFileInfo(IStorage $storage, $path); + public function setFileInfo(IStorage $storage, string $path); } diff --git a/lib/public/WorkflowEngine/IManager.php b/lib/public/WorkflowEngine/IManager.php index 8be47d961e7..78fd718ec9e 100644 --- a/lib/public/WorkflowEngine/IManager.php +++ b/lib/public/WorkflowEngine/IManager.php @@ -23,9 +23,6 @@ namespace OCP\WorkflowEngine; - -use OCP\Files\Storage\IStorage; - /** * Interface IManager * @@ -41,21 +38,6 @@ interface IManager { const EVENT_NAME_REG_ENTITY = 'OCP\WorkflowEngine::registerEntities'; const EVENT_NAME_REG_CHECK = 'OCP\WorkflowEngine::registerChecks'; - /** - * @param IStorage $storage - * @param string $path - * @since 9.1 - */ - public function setFileInfo(IStorage $storage, $path); - - /** - * @param string $class - * @param bool $returnFirstMatchingOperationOnly - * @return array - * @since 9.1 - */ - public function getMatchingOperations($class, $returnFirstMatchingOperationOnly = true); - /** * Listen to `\OCP\WorkflowEngine::EVENT_NAME_REG_ENTITY` at the * EventDispatcher for registering your entities. @@ -79,4 +61,9 @@ interface IManager { * @since 18.0.0 */ public function registerCheck(ICheck $check): void; + + /** + * @since 18.0.0 + */ + public function getRuleMatcher(): IRuleMatcher; } diff --git a/lib/public/WorkflowEngine/IRuleMatcher.php b/lib/public/WorkflowEngine/IRuleMatcher.php new file mode 100644 index 00000000000..5569800edb7 --- /dev/null +++ b/lib/public/WorkflowEngine/IRuleMatcher.php @@ -0,0 +1,39 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace OCP\WorkflowEngine; + +/** + * Class IRuleMatcher + * + * @package OCP\WorkflowEngine + * + * @since 18.0.0 + */ +interface IRuleMatcher extends IFileCheck { + /** + * @since 18.0.0 + */ + public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array; +} -- cgit v1.2.3