diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2019-09-09 16:53:59 +0200 |
---|---|---|
committer | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2019-09-09 22:56:05 +0200 |
commit | 849d025d093d1c80d3820901d20bf6a664c6af02 (patch) | |
tree | 5abb7efdee81ac197dac61dd860f6b4c8becaf51 /apps/workflowengine/lib | |
parent | 4cd931fcc6bb7f794fd14ddb75c7867823449e79 (diff) | |
download | nextcloud-server-849d025d093d1c80d3820901d20bf6a664c6af02.tar.gz nextcloud-server-849d025d093d1c80d3820901d20bf6a664c6af02.zip |
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 <blizzz@arthur-schiwon.de>
Diffstat (limited to 'apps/workflowengine/lib')
-rw-r--r-- | apps/workflowengine/lib/Check/AbstractStringCheck.php | 5 | ||||
-rw-r--r-- | apps/workflowengine/lib/Check/FileSize.php | 5 | ||||
-rw-r--r-- | apps/workflowengine/lib/Check/RequestRemoteAddress.php | 5 | ||||
-rw-r--r-- | apps/workflowengine/lib/Check/RequestTime.php | 5 | ||||
-rw-r--r-- | apps/workflowengine/lib/Check/TFileCheck.php (renamed from apps/workflowengine/lib/Check/AFileCheck.php) | 2 | ||||
-rw-r--r-- | apps/workflowengine/lib/Check/UserGroupMembership.php | 5 | ||||
-rw-r--r-- | apps/workflowengine/lib/Manager.php | 97 | ||||
-rw-r--r-- | apps/workflowengine/lib/Service/RuleMatcher.php | 137 |
8 files changed, 142 insertions, 119 deletions
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/AFileCheck.php b/apps/workflowengine/lib/Check/TFileCheck.php index 45d251b1659..44a39aadfde 100644 --- a/apps/workflowengine/lib/Check/AFileCheck.php +++ b/apps/workflowengine/lib/Check/TFileCheck.php @@ -28,9 +28,7 @@ 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 */ 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() { @@ -635,24 +566,6 @@ class Manager implements IManager, IEntityAware { } /** - * @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[] */ public function getEntitiesList(): array { 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 @@ +<?php +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\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'])); + } + } +} |