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>
This commit is contained in:
Arthur Schiwon 2019-09-09 16:53:59 +02:00
parent 4cd931fcc6
commit 849d025d09
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
17 changed files with 213 additions and 169 deletions

View File

@ -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',

View File

@ -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',

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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 */

View File

@ -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
}
}

View File

@ -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[]
*/

View File

@ -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']));
}
}
}

View File

@ -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',

View File

@ -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',

View File

@ -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;
}

View File

@ -0,0 +1,53 @@
<?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 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;
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -25,16 +25,15 @@ declare(strict_types=1);
namespace OCP\WorkflowEngine;
/**
* Interface IEntityAware
* Class IRuleMatcher
*
* @package OCP\WorkflowEngine
*
* @since 18.0.0
*/
interface IEntityAware {
interface IRuleMatcher extends IFileCheck {
/**
* @param object $entity
* @since 18.0.0
*/
public function setEntity($entity);
public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array;
}