2016-07-26 09:16:34 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.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;
|
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
use Doctrine\DBAL\DBALException;
|
|
|
|
use OC\Cache\CappedMemoryCache;
|
2019-09-05 13:52:11 +00:00
|
|
|
use OCA\WorkflowEngine\Check\FileMimeType;
|
|
|
|
use OCA\WorkflowEngine\Check\FileName;
|
|
|
|
use OCA\WorkflowEngine\Check\FileSize;
|
|
|
|
use OCA\WorkflowEngine\Check\FileSystemTags;
|
|
|
|
use OCA\WorkflowEngine\Check\RequestRemoteAddress;
|
|
|
|
use OCA\WorkflowEngine\Check\RequestTime;
|
|
|
|
use OCA\WorkflowEngine\Check\RequestURL;
|
|
|
|
use OCA\WorkflowEngine\Check\RequestUserAgent;
|
|
|
|
use OCA\WorkflowEngine\Check\UserGroupMembership;
|
2019-08-09 11:24:48 +00:00
|
|
|
use OCA\WorkflowEngine\Entity\File;
|
2019-08-19 15:13:47 +00:00
|
|
|
use OCA\WorkflowEngine\Helper\ScopeContext;
|
2019-09-09 14:53:59 +00:00
|
|
|
use OCA\WorkflowEngine\Service\RuleMatcher;
|
2016-07-26 09:16:34 +00:00
|
|
|
use OCP\AppFramework\QueryException;
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\Files\Storage\IStorage;
|
|
|
|
use OCP\IDBConnection;
|
2016-07-27 08:02:53 +00:00
|
|
|
use OCP\IL10N;
|
2019-08-07 10:12:56 +00:00
|
|
|
use OCP\ILogger;
|
2016-07-26 09:16:34 +00:00
|
|
|
use OCP\IServerContainer;
|
2019-08-16 15:17:38 +00:00
|
|
|
use OCP\IUserSession;
|
2016-07-26 09:16:34 +00:00
|
|
|
use OCP\WorkflowEngine\ICheck;
|
2019-08-28 13:00:02 +00:00
|
|
|
use OCP\WorkflowEngine\IComplexOperation;
|
2019-08-09 11:24:48 +00:00
|
|
|
use OCP\WorkflowEngine\IEntity;
|
2019-08-27 15:51:10 +00:00
|
|
|
use OCP\WorkflowEngine\IEntityEvent;
|
2016-07-26 09:16:34 +00:00
|
|
|
use OCP\WorkflowEngine\IManager;
|
2016-08-19 08:34:45 +00:00
|
|
|
use OCP\WorkflowEngine\IOperation;
|
2019-09-09 14:53:59 +00:00
|
|
|
use OCP\WorkflowEngine\IRuleMatcher;
|
2019-08-09 11:24:48 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2016-07-26 09:16:34 +00:00
|
|
|
|
2019-09-09 14:53:59 +00:00
|
|
|
class Manager implements IManager {
|
2016-07-26 09:16:34 +00:00
|
|
|
|
|
|
|
/** @var IStorage */
|
|
|
|
protected $storage;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $path;
|
|
|
|
|
2019-08-07 10:12:56 +00:00
|
|
|
/** @var object */
|
|
|
|
protected $entity;
|
|
|
|
|
2016-07-26 09:16:34 +00:00
|
|
|
/** @var array[] */
|
|
|
|
protected $operations = [];
|
|
|
|
|
|
|
|
/** @var array[] */
|
|
|
|
protected $checks = [];
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/** @var IServerContainer|\OC\Server */
|
|
|
|
protected $container;
|
|
|
|
|
2016-07-27 08:02:53 +00:00
|
|
|
/** @var IL10N */
|
|
|
|
protected $l;
|
|
|
|
|
2019-08-09 11:24:48 +00:00
|
|
|
/** @var EventDispatcherInterface */
|
|
|
|
protected $eventDispatcher;
|
|
|
|
|
|
|
|
/** @var IEntity[] */
|
|
|
|
protected $registeredEntities = [];
|
|
|
|
|
2019-08-27 14:52:00 +00:00
|
|
|
/** @var IOperation[] */
|
2019-08-23 14:56:24 +00:00
|
|
|
protected $registeredOperators = [];
|
|
|
|
|
2019-09-05 13:52:11 +00:00
|
|
|
/** @var ICheck[] */
|
|
|
|
protected $registeredChecks = [];
|
|
|
|
|
2019-08-09 11:24:48 +00:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
2019-08-19 15:13:47 +00:00
|
|
|
|
|
|
|
/** @var CappedMemoryCache */
|
|
|
|
protected $operationsByScope = [];
|
|
|
|
|
2019-08-16 15:17:38 +00:00
|
|
|
/** @var IUserSession */
|
|
|
|
protected $session;
|
2019-08-09 11:24:48 +00:00
|
|
|
|
2016-07-26 09:16:34 +00:00
|
|
|
/**
|
|
|
|
* @param IDBConnection $connection
|
|
|
|
* @param IServerContainer $container
|
2016-07-27 08:02:53 +00:00
|
|
|
* @param IL10N $l
|
2016-07-26 09:16:34 +00:00
|
|
|
*/
|
2019-08-09 11:24:48 +00:00
|
|
|
public function __construct(
|
|
|
|
IDBConnection $connection,
|
|
|
|
IServerContainer $container,
|
|
|
|
IL10N $l,
|
|
|
|
EventDispatcherInterface $eventDispatcher,
|
2019-08-16 15:17:38 +00:00
|
|
|
ILogger $logger,
|
|
|
|
IUserSession $session
|
2019-08-09 11:24:48 +00:00
|
|
|
) {
|
2016-07-26 09:16:34 +00:00
|
|
|
$this->connection = $connection;
|
|
|
|
$this->container = $container;
|
2016-07-27 08:02:53 +00:00
|
|
|
$this->l = $l;
|
2019-08-09 11:24:48 +00:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
|
|
$this->logger = $logger;
|
2019-08-19 15:13:47 +00:00
|
|
|
$this->operationsByScope = new CappedMemoryCache(64);
|
2019-08-16 15:17:38 +00:00
|
|
|
$this->session = $session;
|
2016-07-26 09:16:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 14:53:59 +00:00
|
|
|
public function getRuleMatcher(): IRuleMatcher {
|
|
|
|
return new RuleMatcher($this->session, $this->container, $this->l, $this);
|
2016-07-26 09:16:34 +00:00
|
|
|
}
|
2019-09-03 10:42:57 +00:00
|
|
|
|
|
|
|
public function getAllConfiguredEvents() {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query->selectDistinct('class')
|
|
|
|
->addSelect('entity', 'events')
|
|
|
|
->from('flow_operations')
|
|
|
|
->where($query->expr()->neq('events', $query->createNamedParameter('[]'), IQueryBuilder::PARAM_STR));
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
$operations = [];
|
|
|
|
while($row = $result->fetch()) {
|
|
|
|
$eventNames = \json_decode($row['events']);
|
|
|
|
|
|
|
|
$operation = $row['class'];
|
|
|
|
$entity = $row['entity'];
|
|
|
|
|
|
|
|
$operations[$operation] = $operations[$row['class']] ?? [];
|
|
|
|
$operations[$operation][$entity] = $operations[$operation][$entity] ?? [];
|
|
|
|
|
|
|
|
$operations[$operation][$entity] = array_unique(array_merge($operations[$operation][$entity], $eventNames));
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return $operations;
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
public function getAllOperations(ScopeContext $scopeContext): array {
|
|
|
|
if(isset($this->operations[$scopeContext->getHash()])) {
|
|
|
|
return $this->operations[$scopeContext->getHash()];
|
2019-08-16 15:17:38 +00:00
|
|
|
}
|
2019-08-07 09:01:09 +00:00
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
2019-08-16 15:17:38 +00:00
|
|
|
$query->select('o.*')
|
|
|
|
->from('flow_operations', 'o')
|
|
|
|
->leftJoin('o', 'flow_operations_scope', 's', $query->expr()->eq('o.id', 's.operation_id'))
|
|
|
|
->where($query->expr()->eq('s.type', $query->createParameter('scope')));
|
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
if($scopeContext->getScope() === IManager::SCOPE_USER) {
|
2019-08-16 15:17:38 +00:00
|
|
|
$query->andWhere($query->expr()->eq('s.value', $query->createParameter('scopeId')));
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
$query->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]);
|
2019-08-07 09:01:09 +00:00
|
|
|
$result = $query->execute();
|
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
$this->operations[$scopeContext->getHash()] = [];
|
2019-08-07 09:01:09 +00:00
|
|
|
while ($row = $result->fetch()) {
|
2019-08-19 15:13:47 +00:00
|
|
|
if(!isset($this->operations[$scopeContext->getHash()][$row['class']])) {
|
|
|
|
$this->operations[$scopeContext->getHash()][$row['class']] = [];
|
2019-08-07 09:01:09 +00:00
|
|
|
}
|
2019-08-19 15:13:47 +00:00
|
|
|
$this->operations[$scopeContext->getHash()][$row['class']][] = $row;
|
2019-08-07 09:01:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
return $this->operations[$scopeContext->getHash()];
|
2019-08-07 09:01:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
public function getOperations(string $class, ScopeContext $scopeContext): array {
|
|
|
|
if (!isset($this->operations[$scopeContext->getHash()])) {
|
|
|
|
$this->getAllOperations($scopeContext);
|
2016-07-26 09:16:34 +00:00
|
|
|
}
|
2019-08-19 15:13:47 +00:00
|
|
|
return $this->operations[$scopeContext->getHash()][$class] ?? [];
|
2016-07-26 09:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @return array
|
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
*/
|
|
|
|
protected function getOperation($id) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('flow_operations')
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($id)));
|
|
|
|
$result = $query->execute();
|
|
|
|
$row = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if ($row) {
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
2017-07-24 09:36:20 +00:00
|
|
|
throw new \UnexpectedValueException($this->l->t('Operation #%s does not exist', [$id]));
|
2016-07-26 09:16:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 15:51:10 +00:00
|
|
|
protected function insertOperation(
|
|
|
|
string $class,
|
|
|
|
string $name,
|
|
|
|
array $checkIds,
|
|
|
|
string $operation,
|
2019-08-28 13:00:02 +00:00
|
|
|
string $entity,
|
2019-08-27 15:51:10 +00:00
|
|
|
array $events
|
|
|
|
): int {
|
2019-08-19 15:13:47 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert('flow_operations')
|
|
|
|
->values([
|
|
|
|
'class' => $query->createNamedParameter($class),
|
|
|
|
'name' => $query->createNamedParameter($name),
|
|
|
|
'checks' => $query->createNamedParameter(json_encode(array_unique($checkIds))),
|
|
|
|
'operation' => $query->createNamedParameter($operation),
|
2019-08-28 13:00:02 +00:00
|
|
|
'entity' => $query->createNamedParameter($entity),
|
2019-08-27 15:51:10 +00:00
|
|
|
'events' => $query->createNamedParameter(json_encode($events))
|
2019-08-19 15:13:47 +00:00
|
|
|
]);
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
return $query->getLastInsertId();
|
|
|
|
}
|
|
|
|
|
2016-07-26 09:16:34 +00:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param string $name
|
|
|
|
* @param array[] $checks
|
|
|
|
* @param string $operation
|
|
|
|
* @return array The added operation
|
|
|
|
* @throws \UnexpectedValueException
|
2019-08-19 15:13:47 +00:00
|
|
|
* @throws DBALException
|
2016-07-26 09:16:34 +00:00
|
|
|
*/
|
2019-08-27 15:51:10 +00:00
|
|
|
public function addOperation(
|
|
|
|
string $class,
|
|
|
|
string $name,
|
|
|
|
array $checks,
|
|
|
|
string $operation,
|
|
|
|
ScopeContext $scope,
|
2019-08-28 13:00:02 +00:00
|
|
|
string $entity,
|
2019-08-27 15:51:10 +00:00
|
|
|
array $events
|
|
|
|
) {
|
2019-08-28 13:00:02 +00:00
|
|
|
$this->validateOperation($class, $name, $checks, $operation, $entity, $events);
|
2016-08-19 08:34:45 +00:00
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
$this->connection->beginTransaction();
|
2016-07-26 09:16:34 +00:00
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
try {
|
|
|
|
$checkIds = [];
|
|
|
|
foreach ($checks as $check) {
|
|
|
|
$checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']);
|
|
|
|
}
|
|
|
|
|
2019-08-28 13:00:02 +00:00
|
|
|
$id = $this->insertOperation($class, $name, $checkIds, $operation, $entity, $events);
|
2019-08-19 15:13:47 +00:00
|
|
|
$this->addScope($id, $scope);
|
|
|
|
|
|
|
|
$this->connection->commit();
|
|
|
|
} catch (DBALException $e) {
|
|
|
|
$this->connection->rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
2016-07-26 09:16:34 +00:00
|
|
|
|
|
|
|
return $this->getOperation($id);
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
protected function canModify(int $id, ScopeContext $scopeContext):bool {
|
|
|
|
if(isset($this->operationsByScope[$scopeContext->getHash()])) {
|
|
|
|
return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb = $this->connection->getQueryBuilder();
|
|
|
|
$qb = $qb->select('o.id')
|
|
|
|
->from('flow_operations', 'o')
|
|
|
|
->leftJoin('o', 'flow_operations_scope', 's', $qb->expr()->eq('o.id', 's.operation_id'))
|
|
|
|
->where($qb->expr()->eq('s.type', $qb->createParameter('scope')));
|
|
|
|
|
|
|
|
if($scopeContext->getScope() !== IManager::SCOPE_ADMIN) {
|
|
|
|
$qb->where($qb->expr()->eq('s.value', $qb->createParameter('scopeId')));
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]);
|
|
|
|
$result = $qb->execute();
|
|
|
|
|
|
|
|
$this->operationsByScope[$scopeContext->getHash()] = [];
|
|
|
|
while($opId = $result->fetchColumn(0)) {
|
|
|
|
$this->operationsByScope[$scopeContext->getHash()][] = (int)$opId;
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true);
|
|
|
|
}
|
|
|
|
|
2016-07-26 09:16:34 +00:00
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @param string $name
|
|
|
|
* @param array[] $checks
|
|
|
|
* @param string $operation
|
|
|
|
* @return array The updated operation
|
|
|
|
* @throws \UnexpectedValueException
|
2019-08-19 15:13:47 +00:00
|
|
|
* @throws \DomainException
|
|
|
|
* @throws DBALException
|
2016-07-26 09:16:34 +00:00
|
|
|
*/
|
2019-08-27 15:51:10 +00:00
|
|
|
public function updateOperation(
|
|
|
|
int $id,
|
|
|
|
string $name,
|
|
|
|
array $checks,
|
|
|
|
string $operation,
|
|
|
|
ScopeContext $scopeContext,
|
2019-08-28 13:00:02 +00:00
|
|
|
string $entity,
|
2019-08-27 15:51:10 +00:00
|
|
|
array $events
|
|
|
|
): array {
|
2019-08-19 15:13:47 +00:00
|
|
|
if(!$this->canModify($id, $scopeContext)) {
|
|
|
|
throw new \DomainException('Target operation not within scope');
|
|
|
|
};
|
2016-08-19 08:34:45 +00:00
|
|
|
$row = $this->getOperation($id);
|
2019-08-28 13:00:02 +00:00
|
|
|
$this->validateOperation($row['class'], $name, $checks, $operation, $entity, $events);
|
2016-08-19 08:34:45 +00:00
|
|
|
|
2016-07-26 09:16:34 +00:00
|
|
|
$checkIds = [];
|
2019-08-19 15:13:47 +00:00
|
|
|
try {
|
|
|
|
$this->connection->beginTransaction();
|
|
|
|
foreach ($checks as $check) {
|
|
|
|
$checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']);
|
|
|
|
}
|
2016-07-26 09:16:34 +00:00
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->update('flow_operations')
|
|
|
|
->set('name', $query->createNamedParameter($name))
|
|
|
|
->set('checks', $query->createNamedParameter(json_encode(array_unique($checkIds))))
|
|
|
|
->set('operation', $query->createNamedParameter($operation))
|
2019-08-28 13:00:02 +00:00
|
|
|
->set('entity', $query->createNamedParameter($entity))
|
2019-08-27 15:51:10 +00:00
|
|
|
->set('events', $query->createNamedParameter(json_encode($events)))
|
2019-08-19 15:13:47 +00:00
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($id)));
|
|
|
|
$query->execute();
|
|
|
|
$this->connection->commit();
|
|
|
|
} catch (DBALException $e) {
|
|
|
|
$this->connection->rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
unset($this->operations[$scopeContext->getHash()]);
|
2016-07-26 09:16:34 +00:00
|
|
|
|
|
|
|
return $this->getOperation($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @return bool
|
|
|
|
* @throws \UnexpectedValueException
|
2019-08-19 15:13:47 +00:00
|
|
|
* @throws DBALException
|
|
|
|
* @throws \DomainException
|
2016-07-26 09:16:34 +00:00
|
|
|
*/
|
2019-08-19 15:13:47 +00:00
|
|
|
public function deleteOperation($id, ScopeContext $scopeContext) {
|
|
|
|
if(!$this->canModify($id, $scopeContext)) {
|
|
|
|
throw new \DomainException('Target operation not within scope');
|
|
|
|
};
|
2016-07-26 09:16:34 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2019-08-19 15:13:47 +00:00
|
|
|
try {
|
|
|
|
$this->connection->beginTransaction();
|
|
|
|
$result = (bool)$query->delete('flow_operations')
|
|
|
|
->where($query->expr()->eq('id', $query->createNamedParameter($id)))
|
|
|
|
->execute();
|
|
|
|
if($result) {
|
|
|
|
$qb = $this->connection->getQueryBuilder();
|
|
|
|
$result &= (bool)$qb->delete('flow_operations_scope')
|
2019-08-29 10:41:51 +00:00
|
|
|
->where($qb->expr()->eq('operation_id', $qb->createNamedParameter($id)))
|
2019-08-19 15:13:47 +00:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
$this->connection->commit();
|
|
|
|
} catch (DBALException $e) {
|
|
|
|
$this->connection->rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($this->operations[$scopeContext->getHash()])) {
|
|
|
|
unset($this->operations[$scopeContext->getHash()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2016-07-26 09:16:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 13:00:02 +00:00
|
|
|
protected function validateEvents(string $entity, array $events, IOperation $operation) {
|
|
|
|
try {
|
|
|
|
/** @var IEntity $instance */
|
|
|
|
$instance = $this->container->query($entity);
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
throw new \UnexpectedValueException($this->l->t('Entity %s does not exist', [$entity]));
|
|
|
|
}
|
2019-08-27 15:51:10 +00:00
|
|
|
|
2019-08-28 13:00:02 +00:00
|
|
|
if(!$instance instanceof IEntity) {
|
|
|
|
throw new \UnexpectedValueException($this->l->t('Entity %s is invalid', [$entity]));
|
|
|
|
}
|
2019-08-27 15:51:10 +00:00
|
|
|
|
2019-08-28 13:00:02 +00:00
|
|
|
if(empty($events)) {
|
|
|
|
if(!$operation instanceof IComplexOperation) {
|
|
|
|
throw new \UnexpectedValueException($this->l->t('No events are chosen.'));
|
2019-08-27 15:51:10 +00:00
|
|
|
}
|
2019-08-28 13:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$availableEvents = [];
|
|
|
|
foreach ($instance->getEvents() as $event) {
|
|
|
|
/** @var IEntityEvent $event */
|
|
|
|
$availableEvents[] = $event->getEventName();
|
|
|
|
}
|
|
|
|
|
|
|
|
$diff = array_diff($events, $availableEvents);
|
|
|
|
if(!empty($diff)) {
|
|
|
|
throw new \UnexpectedValueException($this->l->t('Entity %s has no event %s', [$entity, array_shift($diff)]));
|
2019-08-27 15:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 08:34:45 +00:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param string $name
|
|
|
|
* @param array[] $checks
|
|
|
|
* @param string $operation
|
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
*/
|
2019-08-09 10:28:59 +00:00
|
|
|
public function validateOperation($class, $name, array $checks, $operation, string $entity, array $events) {
|
2016-08-19 08:34:45 +00:00
|
|
|
try {
|
|
|
|
/** @var IOperation $instance */
|
|
|
|
$instance = $this->container->query($class);
|
|
|
|
} catch (QueryException $e) {
|
2017-07-24 09:36:20 +00:00
|
|
|
throw new \UnexpectedValueException($this->l->t('Operation %s does not exist', [$class]));
|
2016-08-19 08:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!($instance instanceof IOperation)) {
|
2017-07-24 09:36:20 +00:00
|
|
|
throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class]));
|
2016-08-19 08:34:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 13:00:02 +00:00
|
|
|
$this->validateEvents($entity, $events, $instance);
|
2019-08-27 15:51:10 +00:00
|
|
|
|
2016-08-19 08:34:45 +00:00
|
|
|
$instance->validateOperation($name, $checks, $operation);
|
|
|
|
|
|
|
|
foreach ($checks as $check) {
|
|
|
|
try {
|
|
|
|
/** @var ICheck $instance */
|
|
|
|
$instance = $this->container->query($check['class']);
|
|
|
|
} catch (QueryException $e) {
|
2017-07-24 09:36:20 +00:00
|
|
|
throw new \UnexpectedValueException($this->l->t('Check %s does not exist', [$class]));
|
2016-08-19 08:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!($instance instanceof ICheck)) {
|
2017-07-24 09:36:20 +00:00
|
|
|
throw new \UnexpectedValueException($this->l->t('Check %s is invalid', [$class]));
|
2016-08-19 08:34:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 13:52:11 +00:00
|
|
|
if (!empty($instance->supportedEntities())
|
2019-09-06 12:08:01 +00:00
|
|
|
&& !in_array($entity, $instance->supportedEntities())
|
2019-09-05 13:52:11 +00:00
|
|
|
) {
|
|
|
|
throw new \UnexpectedValueException($this->l->t('Check %s is not allowed with this entity', [$class]));
|
|
|
|
}
|
|
|
|
|
2016-08-19 08:34:45 +00:00
|
|
|
$instance->validateCheck($check['operator'], $check['value']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 09:16:34 +00:00
|
|
|
/**
|
|
|
|
* @param int[] $checkIds
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function getChecks(array $checkIds) {
|
|
|
|
$checkIds = array_map('intval', $checkIds);
|
|
|
|
|
|
|
|
$checks = [];
|
|
|
|
foreach ($checkIds as $i => $checkId) {
|
|
|
|
if (isset($this->checks[$checkId])) {
|
|
|
|
$checks[$checkId] = $this->checks[$checkId];
|
|
|
|
unset($checkIds[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($checkIds)) {
|
|
|
|
return $checks;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('flow_checks')
|
|
|
|
->where($query->expr()->in('id', $query->createNamedParameter($checkIds, IQueryBuilder::PARAM_INT_ARRAY)));
|
|
|
|
$result = $query->execute();
|
|
|
|
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$this->checks[(int) $row['id']] = $row;
|
|
|
|
$checks[(int) $row['id']] = $row;
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
2016-07-27 06:54:34 +00:00
|
|
|
$checkIds = array_diff($checkIds, array_keys($checks));
|
|
|
|
|
|
|
|
if (!empty($checkIds)) {
|
|
|
|
$missingCheck = array_pop($checkIds);
|
2016-07-27 08:02:53 +00:00
|
|
|
throw new \UnexpectedValueException($this->l->t('Check #%s does not exist', $missingCheck));
|
2016-07-27 06:54:34 +00:00
|
|
|
}
|
2016-07-26 09:16:34 +00:00
|
|
|
|
|
|
|
return $checks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param string $operator
|
|
|
|
* @param string $value
|
|
|
|
* @return int Check unique ID
|
|
|
|
*/
|
|
|
|
protected function addCheck($class, $operator, $value) {
|
|
|
|
$hash = md5($class . '::' . $operator . '::' . $value);
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('id')
|
|
|
|
->from('flow_checks')
|
|
|
|
->where($query->expr()->eq('hash', $query->createNamedParameter($hash)));
|
|
|
|
$result = $query->execute();
|
|
|
|
|
|
|
|
if ($row = $result->fetch()) {
|
|
|
|
$result->closeCursor();
|
|
|
|
return (int) $row['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert('flow_checks')
|
|
|
|
->values([
|
|
|
|
'class' => $query->createNamedParameter($class),
|
|
|
|
'operator' => $query->createNamedParameter($operator),
|
|
|
|
'value' => $query->createNamedParameter($value),
|
|
|
|
'hash' => $query->createNamedParameter($hash),
|
|
|
|
]);
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
return $query->getLastInsertId();
|
|
|
|
}
|
2019-08-07 09:01:09 +00:00
|
|
|
|
2019-08-19 15:13:47 +00:00
|
|
|
protected function addScope(int $operationId, ScopeContext $scope): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$insertQuery = $query->insert('flow_operations_scope');
|
|
|
|
$insertQuery->values([
|
|
|
|
'operation_id' => $query->createNamedParameter($operationId),
|
|
|
|
'type' => $query->createNamedParameter($scope->getScope()),
|
|
|
|
'value' => $query->createNamedParameter($scope->getScopeId()),
|
|
|
|
]);
|
|
|
|
$insertQuery->execute();
|
|
|
|
}
|
|
|
|
|
2019-08-07 09:01:09 +00:00
|
|
|
public function formatOperation(array $operation): array {
|
|
|
|
$checkIds = json_decode($operation['checks'], true);
|
|
|
|
$checks = $this->getChecks($checkIds);
|
|
|
|
|
|
|
|
$operation['checks'] = [];
|
|
|
|
foreach ($checks as $check) {
|
|
|
|
// Remove internal values
|
|
|
|
unset($check['id']);
|
|
|
|
unset($check['hash']);
|
|
|
|
|
|
|
|
$operation['checks'][] = $check;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $operation;
|
|
|
|
}
|
2019-08-07 10:12:56 +00:00
|
|
|
|
2019-08-09 11:24:48 +00:00
|
|
|
/**
|
|
|
|
* @return IEntity[]
|
|
|
|
*/
|
2019-08-23 14:56:24 +00:00
|
|
|
public function getEntitiesList(): array {
|
2019-08-27 14:52:00 +00:00
|
|
|
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_ENTITY, new GenericEvent($this));
|
2019-08-09 11:24:48 +00:00
|
|
|
|
|
|
|
return array_merge($this->getBuildInEntities(), $this->registeredEntities);
|
|
|
|
}
|
|
|
|
|
2019-08-23 14:56:24 +00:00
|
|
|
/**
|
2019-08-27 14:52:00 +00:00
|
|
|
* @return IOperation[]
|
2019-08-23 14:56:24 +00:00
|
|
|
*/
|
|
|
|
public function getOperatorList(): array {
|
2019-08-27 14:52:00 +00:00
|
|
|
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_OPERATION, new GenericEvent($this));
|
2019-08-23 14:56:24 +00:00
|
|
|
|
|
|
|
return array_merge($this->getBuildInOperators(), $this->registeredOperators);
|
|
|
|
}
|
|
|
|
|
2019-08-09 11:24:48 +00:00
|
|
|
/**
|
2019-09-05 13:52:11 +00:00
|
|
|
* @return ICheck[]
|
2019-08-09 11:24:48 +00:00
|
|
|
*/
|
2019-09-05 13:52:11 +00:00
|
|
|
public function getCheckList(): array {
|
|
|
|
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_CHECK, new GenericEvent($this));
|
|
|
|
|
|
|
|
return array_merge($this->getBuildInChecks(), $this->registeredChecks);
|
|
|
|
}
|
|
|
|
|
2019-08-09 11:24:48 +00:00
|
|
|
public function registerEntity(IEntity $entity): void {
|
2019-08-27 15:40:00 +00:00
|
|
|
$this->registeredEntities[get_class($entity)] = $entity;
|
2019-08-09 11:24:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:52:00 +00:00
|
|
|
public function registerOperation(IOperation $operator): void {
|
|
|
|
$this->registeredOperators[get_class($operator)] = $operator;
|
2019-08-23 14:56:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 13:52:11 +00:00
|
|
|
public function registerCheck(ICheck $check): void {
|
|
|
|
$this->registeredChecks[get_class($check)] = $check;
|
|
|
|
}
|
|
|
|
|
2019-08-09 11:24:48 +00:00
|
|
|
/**
|
|
|
|
* @return IEntity[]
|
|
|
|
*/
|
|
|
|
protected function getBuildInEntities(): array {
|
|
|
|
try {
|
|
|
|
return [
|
|
|
|
$this->container->query(File::class),
|
|
|
|
];
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
$this->logger->logException($e);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 14:56:24 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-27 14:52:00 +00:00
|
|
|
* @return IOperation[]
|
2019-08-23 14:56:24 +00:00
|
|
|
*/
|
|
|
|
protected function getBuildInOperators(): array {
|
|
|
|
try {
|
|
|
|
return [
|
|
|
|
// None yet
|
|
|
|
];
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
$this->logger->logException($e);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 13:52:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IEntity[]
|
|
|
|
*/
|
|
|
|
protected function getBuildInChecks(): array {
|
|
|
|
try {
|
|
|
|
return [
|
|
|
|
$this->container->query(FileMimeType::class),
|
|
|
|
$this->container->query(FileName::class),
|
|
|
|
$this->container->query(FileSize::class),
|
|
|
|
$this->container->query(FileSystemTags::class),
|
|
|
|
$this->container->query(RequestRemoteAddress::class),
|
|
|
|
$this->container->query(RequestTime::class),
|
|
|
|
$this->container->query(RequestURL::class),
|
|
|
|
$this->container->query(RequestUserAgent::class),
|
|
|
|
$this->container->query(UserGroupMembership::class),
|
|
|
|
];
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
$this->logger->logException($e);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 09:16:34 +00:00
|
|
|
}
|