Manager.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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\Files\Storage\IStorage;
  51. use OCP\IConfig;
  52. use OCP\IDBConnection;
  53. use OCP\IL10N;
  54. use OCP\ILogger;
  55. use OCP\IServerContainer;
  56. use OCP\IUserSession;
  57. use OCP\WorkflowEngine\Events\RegisterChecksEvent;
  58. use OCP\WorkflowEngine\Events\RegisterEntitiesEvent;
  59. use OCP\WorkflowEngine\Events\RegisterOperationsEvent;
  60. use OCP\WorkflowEngine\ICheck;
  61. use OCP\WorkflowEngine\IComplexOperation;
  62. use OCP\WorkflowEngine\IEntity;
  63. use OCP\WorkflowEngine\IEntityEvent;
  64. use OCP\WorkflowEngine\IManager;
  65. use OCP\WorkflowEngine\IOperation;
  66. use OCP\WorkflowEngine\IRuleMatcher;
  67. use Symfony\Component\EventDispatcher\EventDispatcherInterface as LegacyDispatcher;
  68. use Symfony\Component\EventDispatcher\GenericEvent;
  69. class Manager implements IManager {
  70. /** @var IStorage */
  71. protected $storage;
  72. /** @var string */
  73. protected $path;
  74. /** @var object */
  75. protected $entity;
  76. /** @var array[] */
  77. protected $operations = [];
  78. /** @var array[] */
  79. protected $checks = [];
  80. /** @var IDBConnection */
  81. protected $connection;
  82. /** @var IServerContainer|\OC\Server */
  83. protected $container;
  84. /** @var IL10N */
  85. protected $l;
  86. /** @var LegacyDispatcher */
  87. protected $legacyEventDispatcher;
  88. /** @var IEntity[] */
  89. protected $registeredEntities = [];
  90. /** @var IOperation[] */
  91. protected $registeredOperators = [];
  92. /** @var ICheck[] */
  93. protected $registeredChecks = [];
  94. /** @var ILogger */
  95. protected $logger;
  96. /** @var CappedMemoryCache<int[]> */
  97. protected CappedMemoryCache $operationsByScope;
  98. /** @var IUserSession */
  99. protected $session;
  100. /** @var IEventDispatcher */
  101. private $dispatcher;
  102. /** @var IConfig */
  103. private $config;
  104. public function __construct(
  105. IDBConnection $connection,
  106. IServerContainer $container,
  107. IL10N $l,
  108. LegacyDispatcher $eventDispatcher,
  109. ILogger $logger,
  110. IUserSession $session,
  111. IEventDispatcher $dispatcher,
  112. IConfig $config
  113. ) {
  114. $this->connection = $connection;
  115. $this->container = $container;
  116. $this->l = $l;
  117. $this->legacyEventDispatcher = $eventDispatcher;
  118. $this->logger = $logger;
  119. $this->operationsByScope = new CappedMemoryCache(64);
  120. $this->session = $session;
  121. $this->dispatcher = $dispatcher;
  122. $this->config = $config;
  123. }
  124. public function getRuleMatcher(): IRuleMatcher {
  125. return new RuleMatcher(
  126. $this->session,
  127. $this->container,
  128. $this->l,
  129. $this,
  130. $this->container->query(Logger::class)
  131. );
  132. }
  133. public function getAllConfiguredEvents() {
  134. $query = $this->connection->getQueryBuilder();
  135. $query->select('class', 'entity')
  136. ->selectAlias($query->expr()->castColumn('events', IQueryBuilder::PARAM_STR), 'events')
  137. ->from('flow_operations')
  138. ->where($query->expr()->neq('events', $query->createNamedParameter('[]'), IQueryBuilder::PARAM_STR))
  139. ->groupBy('class', 'entity', $query->expr()->castColumn('events', IQueryBuilder::PARAM_STR));
  140. $result = $query->execute();
  141. $operations = [];
  142. while ($row = $result->fetch()) {
  143. $eventNames = \json_decode($row['events']);
  144. $operation = $row['class'];
  145. $entity = $row['entity'];
  146. $operations[$operation] = $operations[$row['class']] ?? [];
  147. $operations[$operation][$entity] = $operations[$operation][$entity] ?? [];
  148. $operations[$operation][$entity] = array_unique(array_merge($operations[$operation][$entity], $eventNames ?? []));
  149. }
  150. $result->closeCursor();
  151. return $operations;
  152. }
  153. /**
  154. * @param string $operationClass
  155. * @return ScopeContext[]
  156. */
  157. public function getAllConfiguredScopesForOperation(string $operationClass): array {
  158. static $scopesByOperation = [];
  159. if (isset($scopesByOperation[$operationClass])) {
  160. return $scopesByOperation[$operationClass];
  161. }
  162. $query = $this->connection->getQueryBuilder();
  163. $query->selectDistinct('s.type')
  164. ->addSelect('s.value')
  165. ->from('flow_operations', 'o')
  166. ->leftJoin('o', 'flow_operations_scope', 's', $query->expr()->eq('o.id', 's.operation_id'))
  167. ->where($query->expr()->eq('o.class', $query->createParameter('operationClass')));
  168. $query->setParameters(['operationClass' => $operationClass]);
  169. $result = $query->execute();
  170. $scopesByOperation[$operationClass] = [];
  171. while ($row = $result->fetch()) {
  172. $scope = new ScopeContext($row['type'], $row['value']);
  173. $scopesByOperation[$operationClass][$scope->getHash()] = $scope;
  174. }
  175. return $scopesByOperation[$operationClass];
  176. }
  177. public function getAllOperations(ScopeContext $scopeContext): array {
  178. if (isset($this->operations[$scopeContext->getHash()])) {
  179. return $this->operations[$scopeContext->getHash()];
  180. }
  181. $query = $this->connection->getQueryBuilder();
  182. $query->select('o.*')
  183. ->selectAlias('s.type', 'scope_type')
  184. ->selectAlias('s.value', 'scope_actor_id')
  185. ->from('flow_operations', 'o')
  186. ->leftJoin('o', 'flow_operations_scope', 's', $query->expr()->eq('o.id', 's.operation_id'))
  187. ->where($query->expr()->eq('s.type', $query->createParameter('scope')));
  188. if ($scopeContext->getScope() === IManager::SCOPE_USER) {
  189. $query->andWhere($query->expr()->eq('s.value', $query->createParameter('scopeId')));
  190. }
  191. $query->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]);
  192. $result = $query->execute();
  193. $this->operations[$scopeContext->getHash()] = [];
  194. while ($row = $result->fetch()) {
  195. if (!isset($this->operations[$scopeContext->getHash()][$row['class']])) {
  196. $this->operations[$scopeContext->getHash()][$row['class']] = [];
  197. }
  198. $this->operations[$scopeContext->getHash()][$row['class']][] = $row;
  199. }
  200. return $this->operations[$scopeContext->getHash()];
  201. }
  202. public function getOperations(string $class, ScopeContext $scopeContext): array {
  203. if (!isset($this->operations[$scopeContext->getHash()])) {
  204. $this->getAllOperations($scopeContext);
  205. }
  206. return $this->operations[$scopeContext->getHash()][$class] ?? [];
  207. }
  208. /**
  209. * @param int $id
  210. * @return array
  211. * @throws \UnexpectedValueException
  212. */
  213. protected function getOperation($id) {
  214. $query = $this->connection->getQueryBuilder();
  215. $query->select('*')
  216. ->from('flow_operations')
  217. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  218. $result = $query->execute();
  219. $row = $result->fetch();
  220. $result->closeCursor();
  221. if ($row) {
  222. return $row;
  223. }
  224. throw new \UnexpectedValueException($this->l->t('Operation #%s does not exist', [$id]));
  225. }
  226. protected function insertOperation(
  227. string $class,
  228. string $name,
  229. array $checkIds,
  230. string $operation,
  231. string $entity,
  232. array $events
  233. ): int {
  234. $query = $this->connection->getQueryBuilder();
  235. $query->insert('flow_operations')
  236. ->values([
  237. 'class' => $query->createNamedParameter($class),
  238. 'name' => $query->createNamedParameter($name),
  239. 'checks' => $query->createNamedParameter(json_encode(array_unique($checkIds))),
  240. 'operation' => $query->createNamedParameter($operation),
  241. 'entity' => $query->createNamedParameter($entity),
  242. 'events' => $query->createNamedParameter(json_encode($events))
  243. ]);
  244. $query->execute();
  245. return $query->getLastInsertId();
  246. }
  247. /**
  248. * @param string $class
  249. * @param string $name
  250. * @param array[] $checks
  251. * @param string $operation
  252. * @return array The added operation
  253. * @throws \UnexpectedValueException
  254. * @throw Exception
  255. */
  256. public function addOperation(
  257. string $class,
  258. string $name,
  259. array $checks,
  260. string $operation,
  261. ScopeContext $scope,
  262. string $entity,
  263. array $events
  264. ) {
  265. $this->validateOperation($class, $name, $checks, $operation, $entity, $events);
  266. $this->connection->beginTransaction();
  267. try {
  268. $checkIds = [];
  269. foreach ($checks as $check) {
  270. $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']);
  271. }
  272. $id = $this->insertOperation($class, $name, $checkIds, $operation, $entity, $events);
  273. $this->addScope($id, $scope);
  274. $this->connection->commit();
  275. } catch (Exception $e) {
  276. $this->connection->rollBack();
  277. throw $e;
  278. }
  279. return $this->getOperation($id);
  280. }
  281. protected function canModify(int $id, ScopeContext $scopeContext):bool {
  282. if (isset($this->operationsByScope[$scopeContext->getHash()])) {
  283. return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true);
  284. }
  285. $qb = $this->connection->getQueryBuilder();
  286. $qb = $qb->select('o.id')
  287. ->from('flow_operations', 'o')
  288. ->leftJoin('o', 'flow_operations_scope', 's', $qb->expr()->eq('o.id', 's.operation_id'))
  289. ->where($qb->expr()->eq('s.type', $qb->createParameter('scope')));
  290. if ($scopeContext->getScope() !== IManager::SCOPE_ADMIN) {
  291. $qb->where($qb->expr()->eq('s.value', $qb->createParameter('scopeId')));
  292. }
  293. $qb->setParameters(['scope' => $scopeContext->getScope(), 'scopeId' => $scopeContext->getScopeId()]);
  294. $result = $qb->execute();
  295. $operations = [];
  296. while (($opId = $result->fetchOne()) !== false) {
  297. $operations[] = (int)$opId;
  298. }
  299. $this->operationsByScope[$scopeContext->getHash()] = $operations;
  300. $result->closeCursor();
  301. return in_array($id, $this->operationsByScope[$scopeContext->getHash()], true);
  302. }
  303. /**
  304. * @param int $id
  305. * @param string $name
  306. * @param array[] $checks
  307. * @param string $operation
  308. * @return array The updated operation
  309. * @throws \UnexpectedValueException
  310. * @throws \DomainException
  311. * @throws Exception
  312. */
  313. public function updateOperation(
  314. int $id,
  315. string $name,
  316. array $checks,
  317. string $operation,
  318. ScopeContext $scopeContext,
  319. string $entity,
  320. array $events
  321. ): array {
  322. if (!$this->canModify($id, $scopeContext)) {
  323. throw new \DomainException('Target operation not within scope');
  324. };
  325. $row = $this->getOperation($id);
  326. $this->validateOperation($row['class'], $name, $checks, $operation, $entity, $events);
  327. $checkIds = [];
  328. try {
  329. $this->connection->beginTransaction();
  330. foreach ($checks as $check) {
  331. $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']);
  332. }
  333. $query = $this->connection->getQueryBuilder();
  334. $query->update('flow_operations')
  335. ->set('name', $query->createNamedParameter($name))
  336. ->set('checks', $query->createNamedParameter(json_encode(array_unique($checkIds))))
  337. ->set('operation', $query->createNamedParameter($operation))
  338. ->set('entity', $query->createNamedParameter($entity))
  339. ->set('events', $query->createNamedParameter(json_encode($events)))
  340. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  341. $query->execute();
  342. $this->connection->commit();
  343. } catch (Exception $e) {
  344. $this->connection->rollBack();
  345. throw $e;
  346. }
  347. unset($this->operations[$scopeContext->getHash()]);
  348. return $this->getOperation($id);
  349. }
  350. /**
  351. * @param int $id
  352. * @return bool
  353. * @throws \UnexpectedValueException
  354. * @throws Exception
  355. * @throws \DomainException
  356. */
  357. public function deleteOperation($id, ScopeContext $scopeContext) {
  358. if (!$this->canModify($id, $scopeContext)) {
  359. throw new \DomainException('Target operation not within scope');
  360. };
  361. $query = $this->connection->getQueryBuilder();
  362. try {
  363. $this->connection->beginTransaction();
  364. $result = (bool)$query->delete('flow_operations')
  365. ->where($query->expr()->eq('id', $query->createNamedParameter($id)))
  366. ->execute();
  367. if ($result) {
  368. $qb = $this->connection->getQueryBuilder();
  369. $result &= (bool)$qb->delete('flow_operations_scope')
  370. ->where($qb->expr()->eq('operation_id', $qb->createNamedParameter($id)))
  371. ->execute();
  372. }
  373. $this->connection->commit();
  374. } catch (Exception $e) {
  375. $this->connection->rollBack();
  376. throw $e;
  377. }
  378. if (isset($this->operations[$scopeContext->getHash()])) {
  379. unset($this->operations[$scopeContext->getHash()]);
  380. }
  381. return $result;
  382. }
  383. protected function validateEvents(string $entity, array $events, IOperation $operation) {
  384. try {
  385. /** @var IEntity $instance */
  386. $instance = $this->container->query($entity);
  387. } catch (QueryException $e) {
  388. throw new \UnexpectedValueException($this->l->t('Entity %s does not exist', [$entity]));
  389. }
  390. if (!$instance instanceof IEntity) {
  391. throw new \UnexpectedValueException($this->l->t('Entity %s is invalid', [$entity]));
  392. }
  393. if (empty($events)) {
  394. if (!$operation instanceof IComplexOperation) {
  395. throw new \UnexpectedValueException($this->l->t('No events are chosen.'));
  396. }
  397. return;
  398. }
  399. $availableEvents = [];
  400. foreach ($instance->getEvents() as $event) {
  401. /** @var IEntityEvent $event */
  402. $availableEvents[] = $event->getEventName();
  403. }
  404. $diff = array_diff($events, $availableEvents);
  405. if (!empty($diff)) {
  406. throw new \UnexpectedValueException($this->l->t('Entity %s has no event %s', [$entity, array_shift($diff)]));
  407. }
  408. }
  409. /**
  410. * @param string $class
  411. * @param string $name
  412. * @param array[] $checks
  413. * @param string $operation
  414. * @throws \UnexpectedValueException
  415. */
  416. public function validateOperation($class, $name, array $checks, $operation, string $entity, array $events) {
  417. try {
  418. /** @var IOperation $instance */
  419. $instance = $this->container->query($class);
  420. } catch (QueryException $e) {
  421. throw new \UnexpectedValueException($this->l->t('Operation %s does not exist', [$class]));
  422. }
  423. if (!($instance instanceof IOperation)) {
  424. throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class]));
  425. }
  426. $this->validateEvents($entity, $events, $instance);
  427. if (count($checks) === 0) {
  428. throw new \UnexpectedValueException($this->l->t('At least one check needs to be provided'));
  429. }
  430. if (strlen((string)$operation) > IManager::MAX_OPERATION_VALUE_BYTES) {
  431. throw new \UnexpectedValueException($this->l->t('The provided operation data is too long'));
  432. }
  433. $instance->validateOperation($name, $checks, $operation);
  434. foreach ($checks as $check) {
  435. if (!is_string($check['class'])) {
  436. throw new \UnexpectedValueException($this->l->t('Invalid check provided'));
  437. }
  438. try {
  439. /** @var ICheck $instance */
  440. $instance = $this->container->query($check['class']);
  441. } catch (QueryException $e) {
  442. throw new \UnexpectedValueException($this->l->t('Check %s does not exist', [$class]));
  443. }
  444. if (!($instance instanceof ICheck)) {
  445. throw new \UnexpectedValueException($this->l->t('Check %s is invalid', [$class]));
  446. }
  447. if (!empty($instance->supportedEntities())
  448. && !in_array($entity, $instance->supportedEntities())
  449. ) {
  450. throw new \UnexpectedValueException($this->l->t('Check %s is not allowed with this entity', [$class]));
  451. }
  452. if (strlen((string)$check['value']) > IManager::MAX_CHECK_VALUE_BYTES) {
  453. throw new \UnexpectedValueException($this->l->t('The provided check value is too long'));
  454. }
  455. $instance->validateCheck($check['operator'], $check['value']);
  456. }
  457. }
  458. /**
  459. * @param int[] $checkIds
  460. * @return array[]
  461. */
  462. public function getChecks(array $checkIds) {
  463. $checkIds = array_map('intval', $checkIds);
  464. $checks = [];
  465. foreach ($checkIds as $i => $checkId) {
  466. if (isset($this->checks[$checkId])) {
  467. $checks[$checkId] = $this->checks[$checkId];
  468. unset($checkIds[$i]);
  469. }
  470. }
  471. if (empty($checkIds)) {
  472. return $checks;
  473. }
  474. $query = $this->connection->getQueryBuilder();
  475. $query->select('*')
  476. ->from('flow_checks')
  477. ->where($query->expr()->in('id', $query->createNamedParameter($checkIds, IQueryBuilder::PARAM_INT_ARRAY)));
  478. $result = $query->execute();
  479. while ($row = $result->fetch()) {
  480. $this->checks[(int) $row['id']] = $row;
  481. $checks[(int) $row['id']] = $row;
  482. }
  483. $result->closeCursor();
  484. $checkIds = array_diff($checkIds, array_keys($checks));
  485. if (!empty($checkIds)) {
  486. $missingCheck = array_pop($checkIds);
  487. throw new \UnexpectedValueException($this->l->t('Check #%s does not exist', $missingCheck));
  488. }
  489. return $checks;
  490. }
  491. /**
  492. * @param string $class
  493. * @param string $operator
  494. * @param string $value
  495. * @return int Check unique ID
  496. */
  497. protected function addCheck($class, $operator, $value) {
  498. $hash = md5($class . '::' . $operator . '::' . $value);
  499. $query = $this->connection->getQueryBuilder();
  500. $query->select('id')
  501. ->from('flow_checks')
  502. ->where($query->expr()->eq('hash', $query->createNamedParameter($hash)));
  503. $result = $query->execute();
  504. if ($row = $result->fetch()) {
  505. $result->closeCursor();
  506. return (int) $row['id'];
  507. }
  508. $query = $this->connection->getQueryBuilder();
  509. $query->insert('flow_checks')
  510. ->values([
  511. 'class' => $query->createNamedParameter($class),
  512. 'operator' => $query->createNamedParameter($operator),
  513. 'value' => $query->createNamedParameter($value),
  514. 'hash' => $query->createNamedParameter($hash),
  515. ]);
  516. $query->execute();
  517. return $query->getLastInsertId();
  518. }
  519. protected function addScope(int $operationId, ScopeContext $scope): void {
  520. $query = $this->connection->getQueryBuilder();
  521. $insertQuery = $query->insert('flow_operations_scope');
  522. $insertQuery->values([
  523. 'operation_id' => $query->createNamedParameter($operationId),
  524. 'type' => $query->createNamedParameter($scope->getScope()),
  525. 'value' => $query->createNamedParameter($scope->getScopeId()),
  526. ]);
  527. $insertQuery->execute();
  528. }
  529. public function formatOperation(array $operation): array {
  530. $checkIds = json_decode($operation['checks'], true);
  531. $checks = $this->getChecks($checkIds);
  532. $operation['checks'] = [];
  533. foreach ($checks as $check) {
  534. // Remove internal values
  535. unset($check['id']);
  536. unset($check['hash']);
  537. $operation['checks'][] = $check;
  538. }
  539. $operation['events'] = json_decode($operation['events'], true) ?? [];
  540. return $operation;
  541. }
  542. /**
  543. * @return IEntity[]
  544. */
  545. public function getEntitiesList(): array {
  546. $this->dispatcher->dispatchTyped(new RegisterEntitiesEvent($this));
  547. $this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_ENTITY, new GenericEvent($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. $this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_OPERATION, new GenericEvent($this));
  556. return array_merge($this->getBuildInOperators(), $this->registeredOperators);
  557. }
  558. /**
  559. * @return ICheck[]
  560. */
  561. public function getCheckList(): array {
  562. $this->dispatcher->dispatchTyped(new RegisterChecksEvent($this));
  563. $this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_CHECK, new GenericEvent($this));
  564. return array_merge($this->getBuildInChecks(), $this->registeredChecks);
  565. }
  566. public function registerEntity(IEntity $entity): void {
  567. $this->registeredEntities[get_class($entity)] = $entity;
  568. }
  569. public function registerOperation(IOperation $operator): void {
  570. $this->registeredOperators[get_class($operator)] = $operator;
  571. }
  572. public function registerCheck(ICheck $check): void {
  573. $this->registeredChecks[get_class($check)] = $check;
  574. }
  575. /**
  576. * @return IEntity[]
  577. */
  578. protected function getBuildInEntities(): array {
  579. try {
  580. return [
  581. File::class => $this->container->query(File::class),
  582. ];
  583. } catch (QueryException $e) {
  584. $this->logger->logException($e);
  585. return [];
  586. }
  587. }
  588. /**
  589. * @return IOperation[]
  590. */
  591. protected function getBuildInOperators(): array {
  592. try {
  593. return [
  594. // None yet
  595. ];
  596. } catch (QueryException $e) {
  597. $this->logger->logException($e);
  598. return [];
  599. }
  600. }
  601. /**
  602. * @return ICheck[]
  603. */
  604. protected function getBuildInChecks(): array {
  605. try {
  606. return [
  607. $this->container->query(FileMimeType::class),
  608. $this->container->query(FileName::class),
  609. $this->container->query(FileSize::class),
  610. $this->container->query(FileSystemTags::class),
  611. $this->container->query(RequestRemoteAddress::class),
  612. $this->container->query(RequestTime::class),
  613. $this->container->query(RequestURL::class),
  614. $this->container->query(RequestUserAgent::class),
  615. $this->container->query(UserGroupMembership::class),
  616. ];
  617. } catch (QueryException $e) {
  618. $this->logger->logException($e);
  619. return [];
  620. }
  621. }
  622. public function isUserScopeEnabled(): bool {
  623. return $this->config->getAppValue(Application::APP_ID, 'user_scope_disabled', 'no') === 'no';
  624. }
  625. }