1
0

Manager.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author blizzz <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. namespace OCA\WorkflowEngine;
  31. use Doctrine\DBAL\Exception;
  32. use OCP\Cache\CappedMemoryCache;
  33. use OCA\WorkflowEngine\AppInfo\Application;
  34. use OCA\WorkflowEngine\Check\FileMimeType;
  35. use OCA\WorkflowEngine\Check\FileName;
  36. use OCA\WorkflowEngine\Check\FileSize;
  37. use OCA\WorkflowEngine\Check\FileSystemTags;
  38. use OCA\WorkflowEngine\Check\RequestRemoteAddress;
  39. use OCA\WorkflowEngine\Check\RequestTime;
  40. use OCA\WorkflowEngine\Check\RequestURL;
  41. use OCA\WorkflowEngine\Check\RequestUserAgent;
  42. use OCA\WorkflowEngine\Check\UserGroupMembership;
  43. use OCA\WorkflowEngine\Entity\File;
  44. use OCA\WorkflowEngine\Helper\ScopeContext;
  45. use OCA\WorkflowEngine\Service\Logger;
  46. use OCA\WorkflowEngine\Service\RuleMatcher;
  47. use OCP\AppFramework\QueryException;
  48. use OCP\DB\QueryBuilder\IQueryBuilder;
  49. use OCP\EventDispatcher\IEventDispatcher;
  50. use OCP\ICacheFactory;
  51. use OCP\IConfig;
  52. use OCP\IDBConnection;
  53. use OCP\IL10N;
  54. use OCP\IServerContainer;
  55. use OCP\IUserSession;
  56. use OCP\WorkflowEngine\Events\RegisterChecksEvent;
  57. use OCP\WorkflowEngine\Events\RegisterEntitiesEvent;
  58. use OCP\WorkflowEngine\Events\RegisterOperationsEvent;
  59. use OCP\WorkflowEngine\ICheck;
  60. use OCP\WorkflowEngine\IComplexOperation;
  61. use OCP\WorkflowEngine\IEntity;
  62. use OCP\WorkflowEngine\IEntityEvent;
  63. use OCP\WorkflowEngine\IManager;
  64. use OCP\WorkflowEngine\IOperation;
  65. use OCP\WorkflowEngine\IRuleMatcher;
  66. use Psr\Log\LoggerInterface;
  67. class Manager implements IManager {
  68. /** @var array[] */
  69. protected $operations = [];
  70. /** @var array[] */
  71. protected $checks = [];
  72. /** @var IEntity[] */
  73. protected $registeredEntities = [];
  74. /** @var IOperation[] */
  75. protected $registeredOperators = [];
  76. /** @var ICheck[] */
  77. protected $registeredChecks = [];
  78. /** @var CappedMemoryCache<int[]> */
  79. protected CappedMemoryCache $operationsByScope;
  80. public function __construct(
  81. protected IDBConnection $connection,
  82. protected IServerContainer $container,
  83. protected IL10N $l,
  84. protected LoggerInterface $logger,
  85. protected IUserSession $session,
  86. private IEventDispatcher $dispatcher,
  87. private IConfig $config,
  88. private ICacheFactory $cacheFactory,
  89. ) {
  90. $this->operationsByScope = new CappedMemoryCache(64);
  91. }
  92. public function getRuleMatcher(): IRuleMatcher {
  93. return new RuleMatcher(
  94. $this->session,
  95. $this->container,
  96. $this->l,
  97. $this,
  98. $this->container->query(Logger::class)
  99. );
  100. }
  101. public function getAllConfiguredEvents() {
  102. $cache = $this->cacheFactory->createDistributed('flow');
  103. $cached = $cache->get('events');
  104. if ($cached !== null) {
  105. return $cached;
  106. }
  107. $query = $this->connection->getQueryBuilder();
  108. $query->select('class', 'entity')
  109. ->selectAlias($query->expr()->castColumn('events', IQueryBuilder::PARAM_STR), 'events')
  110. ->from('flow_operations')
  111. ->where($query->expr()->neq('events', $query->createNamedParameter('[]'), IQueryBuilder::PARAM_STR))
  112. ->groupBy('class', 'entity', $query->expr()->castColumn('events', IQueryBuilder::PARAM_STR));
  113. $result = $query->execute();
  114. $operations = [];
  115. while ($row = $result->fetch()) {
  116. $eventNames = \json_decode($row['events']);
  117. $operation = $row['class'];
  118. $entity = $row['entity'];
  119. $operations[$operation] = $operations[$row['class']] ?? [];
  120. $operations[$operation][$entity] = $operations[$operation][$entity] ?? [];
  121. $operations[$operation][$entity] = array_unique(array_merge($operations[$operation][$entity], $eventNames ?? []));
  122. }
  123. $result->closeCursor();
  124. $cache->set('events', $operations, 3600);
  125. return $operations;
  126. }
  127. /**
  128. * @param string $operationClass
  129. * @return ScopeContext[]
  130. */
  131. public function getAllConfiguredScopesForOperation(string $operationClass): array {
  132. static $scopesByOperation = [];
  133. if (isset($scopesByOperation[$operationClass])) {
  134. return $scopesByOperation[$operationClass];
  135. }
  136. try {
  137. /** @var IOperation $operation */
  138. $operation = $this->container->query($operationClass);
  139. } catch (QueryException $e) {
  140. return [];
  141. }
  142. $query = $this->connection->getQueryBuilder();
  143. $query->selectDistinct('s.type')
  144. ->addSelect('s.value')
  145. ->from('flow_operations', 'o')
  146. ->leftJoin('o', 'flow_operations_scope', 's', $query->expr()->eq('o.id', 's.operation_id'))
  147. ->where($query->expr()->eq('o.class', $query->createParameter('operationClass')));
  148. $query->setParameters(['operationClass' => $operationClass]);
  149. $result = $query->execute();
  150. $scopesByOperation[$operationClass] = [];
  151. while ($row = $result->fetch()) {
  152. $scope = new ScopeContext($row['type'], $row['value']);
  153. if (!$operation->isAvailableForScope((int) $row['type'])) {
  154. continue;
  155. }
  156. $scopesByOperation[$operationClass][$scope->getHash()] = $scope;
  157. }
  158. return $scopesByOperation[$operationClass];
  159. }
  160. public function getAllOperations(ScopeContext $scopeContext): array {
  161. if (isset($this->operations[$scopeContext->getHash()])) {
  162. return $this->operations[$scopeContext->getHash()];
  163. }
  164. $query = $this->connection->getQueryBuilder();
  165. $query->select('o.*')
  166. ->selectAlias('s.type', 'scope_type')
  167. ->selectAlias('s.value', 'scope_actor_id')
  168. ->from('flow_operations', 'o')
  169. ->leftJoin('o', 'flow_operations_scope', 's', $query->expr()->eq('o.id', 's.operation_id'))
  170. ->where($query->expr()->eq('s.type', $query->createParameter('scope')));
  171. if ($scopeContext->getScope() === IManager::SCOPE_USER) {
  172. $query->andWhere($query->expr()->eq('s.value', $query->createParameter('scopeId')));
  173. }
  174. $query->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]);
  175. $result = $query->execute();
  176. $this->operations[$scopeContext->getHash()] = [];
  177. while ($row = $result->fetch()) {
  178. try {
  179. /** @var IOperation $operation */
  180. $operation = $this->container->query($row['class']);
  181. } catch (QueryException $e) {
  182. continue;
  183. }
  184. if (!$operation->isAvailableForScope((int) $row['scope_type'])) {
  185. continue;
  186. }
  187. if (!isset($this->operations[$scopeContext->getHash()][$row['class']])) {
  188. $this->operations[$scopeContext->getHash()][$row['class']] = [];
  189. }
  190. $this->operations[$scopeContext->getHash()][$row['class']][] = $row;
  191. }
  192. return $this->operations[$scopeContext->getHash()];
  193. }
  194. public function getOperations(string $class, ScopeContext $scopeContext): array {
  195. if (!isset($this->operations[$scopeContext->getHash()])) {
  196. $this->getAllOperations($scopeContext);
  197. }
  198. return $this->operations[$scopeContext->getHash()][$class] ?? [];
  199. }
  200. /**
  201. * @param int $id
  202. * @return array
  203. * @throws \UnexpectedValueException
  204. */
  205. protected function getOperation($id) {
  206. $query = $this->connection->getQueryBuilder();
  207. $query->select('*')
  208. ->from('flow_operations')
  209. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  210. $result = $query->execute();
  211. $row = $result->fetch();
  212. $result->closeCursor();
  213. if ($row) {
  214. return $row;
  215. }
  216. throw new \UnexpectedValueException($this->l->t('Operation #%s does not exist', [$id]));
  217. }
  218. protected function insertOperation(
  219. string $class,
  220. string $name,
  221. array $checkIds,
  222. string $operation,
  223. string $entity,
  224. array $events
  225. ): int {
  226. $query = $this->connection->getQueryBuilder();
  227. $query->insert('flow_operations')
  228. ->values([
  229. 'class' => $query->createNamedParameter($class),
  230. 'name' => $query->createNamedParameter($name),
  231. 'checks' => $query->createNamedParameter(json_encode(array_unique($checkIds))),
  232. 'operation' => $query->createNamedParameter($operation),
  233. 'entity' => $query->createNamedParameter($entity),
  234. 'events' => $query->createNamedParameter(json_encode($events))
  235. ]);
  236. $query->execute();
  237. $this->cacheFactory->createDistributed('flow')->remove('events');
  238. return $query->getLastInsertId();
  239. }
  240. /**
  241. * @param string $class
  242. * @param string $name
  243. * @param array[] $checks
  244. * @param string $operation
  245. * @return array The added operation
  246. * @throws \UnexpectedValueException
  247. * @throw Exception
  248. */
  249. public function addOperation(
  250. string $class,
  251. string $name,
  252. array $checks,
  253. string $operation,
  254. ScopeContext $scope,
  255. string $entity,
  256. array $events
  257. ) {
  258. $this->validateOperation($class, $name, $checks, $operation, $scope, $entity, $events);
  259. $this->connection->beginTransaction();
  260. try {
  261. $checkIds = [];
  262. foreach ($checks as $check) {
  263. $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']);
  264. }
  265. $id = $this->insertOperation($class, $name, $checkIds, $operation, $entity, $events);
  266. $this->addScope($id, $scope);
  267. $this->connection->commit();
  268. } catch (Exception $e) {
  269. $this->connection->rollBack();
  270. throw $e;
  271. }
  272. return $this->getOperation($id);
  273. }
  274. protected function canModify(int $id, ScopeContext $scopeContext):bool {
  275. if (isset($this->operationsByScope[$scopeContext->getHash()])) {
  276. return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true);
  277. }
  278. $qb = $this->connection->getQueryBuilder();
  279. $qb = $qb->select('o.id')
  280. ->from('flow_operations', 'o')
  281. ->leftJoin('o', 'flow_operations_scope', 's', $qb->expr()->eq('o.id', 's.operation_id'))
  282. ->where($qb->expr()->eq('s.type', $qb->createParameter('scope')));
  283. if ($scopeContext->getScope() !== IManager::SCOPE_ADMIN) {
  284. $qb->where($qb->expr()->eq('s.value', $qb->createParameter('scopeId')));
  285. }
  286. $qb->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]);
  287. $result = $qb->execute();
  288. $operations = [];
  289. while (($opId = $result->fetchOne()) !== false) {
  290. $operations[] = (int)$opId;
  291. }
  292. $this->operationsByScope[$scopeContext->getHash()] = $operations;
  293. $result->closeCursor();
  294. return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true);
  295. }
  296. /**
  297. * @param int $id
  298. * @param string $name
  299. * @param array[] $checks
  300. * @param string $operation
  301. * @return array The updated operation
  302. * @throws \UnexpectedValueException
  303. * @throws \DomainException
  304. * @throws Exception
  305. */
  306. public function updateOperation(
  307. int $id,
  308. string $name,
  309. array $checks,
  310. string $operation,
  311. ScopeContext $scopeContext,
  312. string $entity,
  313. array $events
  314. ): array {
  315. if (!$this->canModify($id, $scopeContext)) {
  316. throw new \DomainException('Target operation not within scope');
  317. };
  318. $row = $this->getOperation($id);
  319. $this->validateOperation($row['class'], $name, $checks, $operation, $scopeContext, $entity, $events);
  320. $checkIds = [];
  321. try {
  322. $this->connection->beginTransaction();
  323. foreach ($checks as $check) {
  324. $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']);
  325. }
  326. $query = $this->connection->getQueryBuilder();
  327. $query->update('flow_operations')
  328. ->set('name', $query->createNamedParameter($name))
  329. ->set('checks', $query->createNamedParameter(json_encode(array_unique($checkIds))))
  330. ->set('operation', $query->createNamedParameter($operation))
  331. ->set('entity', $query->createNamedParameter($entity))
  332. ->set('events', $query->createNamedParameter(json_encode($events)))
  333. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  334. $query->execute();
  335. $this->connection->commit();
  336. } catch (Exception $e) {
  337. $this->connection->rollBack();
  338. throw $e;
  339. }
  340. unset($this->operations[$scopeContext->getHash()]);
  341. $this->cacheFactory->createDistributed('flow')->remove('events');
  342. return $this->getOperation($id);
  343. }
  344. /**
  345. * @param int $id
  346. * @return bool
  347. * @throws \UnexpectedValueException
  348. * @throws Exception
  349. * @throws \DomainException
  350. */
  351. public function deleteOperation($id, ScopeContext $scopeContext) {
  352. if (!$this->canModify($id, $scopeContext)) {
  353. throw new \DomainException('Target operation not within scope');
  354. };
  355. $query = $this->connection->getQueryBuilder();
  356. try {
  357. $this->connection->beginTransaction();
  358. $result = (bool)$query->delete('flow_operations')
  359. ->where($query->expr()->eq('id', $query->createNamedParameter($id)))
  360. ->execute();
  361. if ($result) {
  362. $qb = $this->connection->getQueryBuilder();
  363. $result &= (bool)$qb->delete('flow_operations_scope')
  364. ->where($qb->expr()->eq('operation_id', $qb->createNamedParameter($id)))
  365. ->execute();
  366. }
  367. $this->connection->commit();
  368. } catch (Exception $e) {
  369. $this->connection->rollBack();
  370. throw $e;
  371. }
  372. if (isset($this->operations[$scopeContext->getHash()])) {
  373. unset($this->operations[$scopeContext->getHash()]);
  374. }
  375. $this->cacheFactory->createDistributed('flow')->remove('events');
  376. return $result;
  377. }
  378. protected function validateEvents(string $entity, array $events, IOperation $operation) {
  379. try {
  380. /** @var IEntity $instance */
  381. $instance = $this->container->query($entity);
  382. } catch (QueryException $e) {
  383. throw new \UnexpectedValueException($this->l->t('Entity %s does not exist', [$entity]));
  384. }
  385. if (!$instance instanceof IEntity) {
  386. throw new \UnexpectedValueException($this->l->t('Entity %s is invalid', [$entity]));
  387. }
  388. if (empty($events)) {
  389. if (!$operation instanceof IComplexOperation) {
  390. throw new \UnexpectedValueException($this->l->t('No events are chosen.'));
  391. }
  392. return;
  393. }
  394. $availableEvents = [];
  395. foreach ($instance->getEvents() as $event) {
  396. /** @var IEntityEvent $event */
  397. $availableEvents[] = $event->getEventName();
  398. }
  399. $diff = array_diff($events, $availableEvents);
  400. if (!empty($diff)) {
  401. throw new \UnexpectedValueException($this->l->t('Entity %s has no event %s', [$entity, array_shift($diff)]));
  402. }
  403. }
  404. /**
  405. * @param string $class
  406. * @param string $name
  407. * @param array[] $checks
  408. * @param string $operation
  409. * @param ScopeContext $scope
  410. * @param string $entity
  411. * @param array $events
  412. * @throws \UnexpectedValueException
  413. */
  414. public function validateOperation($class, $name, array $checks, $operation, ScopeContext $scope, string $entity, array $events) {
  415. try {
  416. /** @var IOperation $instance */
  417. $instance = $this->container->query($class);
  418. } catch (QueryException $e) {
  419. throw new \UnexpectedValueException($this->l->t('Operation %s does not exist', [$class]));
  420. }
  421. if (!($instance instanceof IOperation)) {
  422. throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class]));
  423. }
  424. if (!$instance->isAvailableForScope($scope->getScope())) {
  425. throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class]));
  426. }
  427. $this->validateEvents($entity, $events, $instance);
  428. if (count($checks) === 0) {
  429. throw new \UnexpectedValueException($this->l->t('At least one check needs to be provided'));
  430. }
  431. if (strlen((string)$operation) > IManager::MAX_OPERATION_VALUE_BYTES) {
  432. throw new \UnexpectedValueException($this->l->t('The provided operation data is too long'));
  433. }
  434. $instance->validateOperation($name, $checks, $operation);
  435. foreach ($checks as $check) {
  436. if (!is_string($check['class'])) {
  437. throw new \UnexpectedValueException($this->l->t('Invalid check provided'));
  438. }
  439. try {
  440. /** @var ICheck $instance */
  441. $instance = $this->container->query($check['class']);
  442. } catch (QueryException $e) {
  443. throw new \UnexpectedValueException($this->l->t('Check %s does not exist', [$class]));
  444. }
  445. if (!($instance instanceof ICheck)) {
  446. throw new \UnexpectedValueException($this->l->t('Check %s is invalid', [$class]));
  447. }
  448. if (!empty($instance->supportedEntities())
  449. && !in_array($entity, $instance->supportedEntities())
  450. ) {
  451. throw new \UnexpectedValueException($this->l->t('Check %s is not allowed with this entity', [$class]));
  452. }
  453. if (strlen((string)$check['value']) > IManager::MAX_CHECK_VALUE_BYTES) {
  454. throw new \UnexpectedValueException($this->l->t('The provided check value is too long'));
  455. }
  456. $instance->validateCheck($check['operator'], $check['value']);
  457. }
  458. }
  459. /**
  460. * @param int[] $checkIds
  461. * @return array[]
  462. */
  463. public function getChecks(array $checkIds) {
  464. $checkIds = array_map('intval', $checkIds);
  465. $checks = [];
  466. foreach ($checkIds as $i => $checkId) {
  467. if (isset($this->checks[$checkId])) {
  468. $checks[$checkId] = $this->checks[$checkId];
  469. unset($checkIds[$i]);
  470. }
  471. }
  472. if (empty($checkIds)) {
  473. return $checks;
  474. }
  475. $query = $this->connection->getQueryBuilder();
  476. $query->select('*')
  477. ->from('flow_checks')
  478. ->where($query->expr()->in('id', $query->createNamedParameter($checkIds, IQueryBuilder::PARAM_INT_ARRAY)));
  479. $result = $query->execute();
  480. while ($row = $result->fetch()) {
  481. $this->checks[(int) $row['id']] = $row;
  482. $checks[(int) $row['id']] = $row;
  483. }
  484. $result->closeCursor();
  485. $checkIds = array_diff($checkIds, array_keys($checks));
  486. if (!empty($checkIds)) {
  487. $missingCheck = array_pop($checkIds);
  488. throw new \UnexpectedValueException($this->l->t('Check #%s does not exist', $missingCheck));
  489. }
  490. return $checks;
  491. }
  492. /**
  493. * @param string $class
  494. * @param string $operator
  495. * @param string $value
  496. * @return int Check unique ID
  497. */
  498. protected function addCheck($class, $operator, $value) {
  499. $hash = md5($class . '::' . $operator . '::' . $value);
  500. $query = $this->connection->getQueryBuilder();
  501. $query->select('id')
  502. ->from('flow_checks')
  503. ->where($query->expr()->eq('hash', $query->createNamedParameter($hash)));
  504. $result = $query->execute();
  505. if ($row = $result->fetch()) {
  506. $result->closeCursor();
  507. return (int) $row['id'];
  508. }
  509. $query = $this->connection->getQueryBuilder();
  510. $query->insert('flow_checks')
  511. ->values([
  512. 'class' => $query->createNamedParameter($class),
  513. 'operator' => $query->createNamedParameter($operator),
  514. 'value' => $query->createNamedParameter($value),
  515. 'hash' => $query->createNamedParameter($hash),
  516. ]);
  517. $query->execute();
  518. return $query->getLastInsertId();
  519. }
  520. protected function addScope(int $operationId, ScopeContext $scope): void {
  521. $query = $this->connection->getQueryBuilder();
  522. $insertQuery = $query->insert('flow_operations_scope');
  523. $insertQuery->values([
  524. 'operation_id' => $query->createNamedParameter($operationId),
  525. 'type' => $query->createNamedParameter($scope->getScope()),
  526. 'value' => $query->createNamedParameter($scope->getScopeId()),
  527. ]);
  528. $insertQuery->execute();
  529. }
  530. public function formatOperation(array $operation): array {
  531. $checkIds = json_decode($operation['checks'], true);
  532. $checks = $this->getChecks($checkIds);
  533. $operation['checks'] = [];
  534. foreach ($checks as $check) {
  535. // Remove internal values
  536. unset($check['id']);
  537. unset($check['hash']);
  538. $operation['checks'][] = $check;
  539. }
  540. $operation['events'] = json_decode($operation['events'], true) ?? [];
  541. return $operation;
  542. }
  543. /**
  544. * @return IEntity[]
  545. */
  546. public function getEntitiesList(): array {
  547. $this->dispatcher->dispatchTyped(new RegisterEntitiesEvent($this));
  548. return array_values(array_merge($this->getBuildInEntities(), $this->registeredEntities));
  549. }
  550. /**
  551. * @return IOperation[]
  552. */
  553. public function getOperatorList(): array {
  554. $this->dispatcher->dispatchTyped(new RegisterOperationsEvent($this));
  555. return array_merge($this->getBuildInOperators(), $this->registeredOperators);
  556. }
  557. /**
  558. * @return ICheck[]
  559. */
  560. public function getCheckList(): array {
  561. $this->dispatcher->dispatchTyped(new RegisterChecksEvent($this));
  562. return array_merge($this->getBuildInChecks(), $this->registeredChecks);
  563. }
  564. public function registerEntity(IEntity $entity): void {
  565. $this->registeredEntities[get_class($entity)] = $entity;
  566. }
  567. public function registerOperation(IOperation $operator): void {
  568. $this->registeredOperators[get_class($operator)] = $operator;
  569. }
  570. public function registerCheck(ICheck $check): void {
  571. $this->registeredChecks[get_class($check)] = $check;
  572. }
  573. /**
  574. * @return IEntity[]
  575. */
  576. protected function getBuildInEntities(): array {
  577. try {
  578. return [
  579. File::class => $this->container->query(File::class),
  580. ];
  581. } catch (QueryException $e) {
  582. $this->logger->error($e->getMessage(), ['exception' => $e]);
  583. return [];
  584. }
  585. }
  586. /**
  587. * @return IOperation[]
  588. */
  589. protected function getBuildInOperators(): array {
  590. try {
  591. return [
  592. // None yet
  593. ];
  594. } catch (QueryException $e) {
  595. $this->logger->error($e->getMessage(), ['exception' => $e]);
  596. return [];
  597. }
  598. }
  599. /**
  600. * @return ICheck[]
  601. */
  602. protected function getBuildInChecks(): array {
  603. try {
  604. return [
  605. $this->container->query(FileMimeType::class),
  606. $this->container->query(FileName::class),
  607. $this->container->query(FileSize::class),
  608. $this->container->query(FileSystemTags::class),
  609. $this->container->query(RequestRemoteAddress::class),
  610. $this->container->query(RequestTime::class),
  611. $this->container->query(RequestURL::class),
  612. $this->container->query(RequestUserAgent::class),
  613. $this->container->query(UserGroupMembership::class),
  614. ];
  615. } catch (QueryException $e) {
  616. $this->logger->error($e->getMessage(), ['exception' => $e]);
  617. return [];
  618. }
  619. }
  620. public function isUserScopeEnabled(): bool {
  621. return $this->config->getAppValue(Application::APP_ID, 'user_scope_disabled', 'no') === 'no';
  622. }
  623. }