RuleMatcher.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\WorkflowEngine\Service;
  8. use OCA\WorkflowEngine\Helper\LogContext;
  9. use OCA\WorkflowEngine\Helper\ScopeContext;
  10. use OCA\WorkflowEngine\Manager;
  11. use OCP\AppFramework\QueryException;
  12. use OCP\Files\Storage\IStorage;
  13. use OCP\IL10N;
  14. use OCP\IServerContainer;
  15. use OCP\IUserSession;
  16. use OCP\WorkflowEngine\ICheck;
  17. use OCP\WorkflowEngine\IEntity;
  18. use OCP\WorkflowEngine\IEntityCheck;
  19. use OCP\WorkflowEngine\IFileCheck;
  20. use OCP\WorkflowEngine\IManager;
  21. use OCP\WorkflowEngine\IOperation;
  22. use OCP\WorkflowEngine\IRuleMatcher;
  23. use RuntimeException;
  24. class RuleMatcher implements IRuleMatcher {
  25. /** @var IUserSession */
  26. protected $session;
  27. /** @var IManager */
  28. protected $manager;
  29. /** @var array */
  30. protected $contexts;
  31. /** @var IServerContainer */
  32. protected $container;
  33. /** @var array */
  34. protected $fileInfo = [];
  35. /** @var IL10N */
  36. protected $l;
  37. /** @var IOperation */
  38. protected $operation;
  39. /** @var IEntity */
  40. protected $entity;
  41. /** @var Logger */
  42. protected $logger;
  43. /** @var string */
  44. protected $eventName;
  45. public function __construct(
  46. IUserSession $session,
  47. IServerContainer $container,
  48. IL10N $l,
  49. Manager $manager,
  50. Logger $logger
  51. ) {
  52. $this->session = $session;
  53. $this->manager = $manager;
  54. $this->container = $container;
  55. $this->l = $l;
  56. $this->logger = $logger;
  57. }
  58. public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
  59. $this->fileInfo['storage'] = $storage;
  60. $this->fileInfo['path'] = $path;
  61. $this->fileInfo['isDir'] = $isDir;
  62. }
  63. public function setEntitySubject(IEntity $entity, $subject): void {
  64. $this->contexts[get_class($entity)] = [$entity, $subject];
  65. }
  66. public function setOperation(IOperation $operation): void {
  67. if ($this->operation !== null) {
  68. throw new RuntimeException('This method must not be called more than once');
  69. }
  70. $this->operation = $operation;
  71. }
  72. public function setEntity(IEntity $entity): void {
  73. if ($this->entity !== null) {
  74. throw new RuntimeException('This method must not be called more than once');
  75. }
  76. $this->entity = $entity;
  77. }
  78. public function setEventName(string $eventName): void {
  79. if ($this->eventName !== null) {
  80. throw new RuntimeException('This method must not be called more than once');
  81. }
  82. $this->eventName = $eventName;
  83. }
  84. public function getEntity(): IEntity {
  85. if ($this->entity === null) {
  86. throw new \LogicException('Entity was not set yet');
  87. }
  88. return $this->entity;
  89. }
  90. public function getFlows(bool $returnFirstMatchingOperationOnly = true): array {
  91. if (!$this->operation) {
  92. throw new RuntimeException('Operation is not set');
  93. }
  94. return $this->getMatchingOperations(get_class($this->operation), $returnFirstMatchingOperationOnly);
  95. }
  96. public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array {
  97. $scopes[] = new ScopeContext(IManager::SCOPE_ADMIN);
  98. $user = $this->session->getUser();
  99. if ($user !== null && $this->manager->isUserScopeEnabled()) {
  100. $scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID());
  101. }
  102. $ctx = new LogContext();
  103. $ctx
  104. ->setScopes($scopes)
  105. ->setEntity($this->entity)
  106. ->setOperation($this->operation);
  107. $this->logger->logFlowRequests($ctx);
  108. $operations = [];
  109. foreach ($scopes as $scope) {
  110. $operations = array_merge($operations, $this->manager->getOperations($class, $scope));
  111. }
  112. if ($this->entity instanceof IEntity) {
  113. /** @var ScopeContext[] $additionalScopes */
  114. $additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class);
  115. foreach ($additionalScopes as $hash => $scopeCandidate) {
  116. if ($scopeCandidate->getScope() !== IManager::SCOPE_USER || in_array($scopeCandidate, $scopes)) {
  117. continue;
  118. }
  119. if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) {
  120. $ctx = new LogContext();
  121. $ctx
  122. ->setScopes([$scopeCandidate])
  123. ->setEntity($this->entity)
  124. ->setOperation($this->operation);
  125. $this->logger->logScopeExpansion($ctx);
  126. $operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate));
  127. }
  128. }
  129. }
  130. $matches = [];
  131. foreach ($operations as $operation) {
  132. $configuredEvents = json_decode($operation['events'], true);
  133. if ($this->eventName !== null && !in_array($this->eventName, $configuredEvents)) {
  134. continue;
  135. }
  136. $checkIds = json_decode($operation['checks'], true);
  137. $checks = $this->manager->getChecks($checkIds);
  138. foreach ($checks as $check) {
  139. if (!$this->check($check)) {
  140. // Check did not match, continue with the next operation
  141. continue 2;
  142. }
  143. }
  144. $ctx = new LogContext();
  145. $ctx
  146. ->setEntity($this->entity)
  147. ->setOperation($this->operation)
  148. ->setConfiguration($operation);
  149. $this->logger->logPassedCheck($ctx);
  150. if ($returnFirstMatchingOperationOnly) {
  151. $ctx = new LogContext();
  152. $ctx
  153. ->setEntity($this->entity)
  154. ->setOperation($this->operation)
  155. ->setConfiguration($operation);
  156. $this->logger->logRunSingle($ctx);
  157. return $operation;
  158. }
  159. $matches[] = $operation;
  160. }
  161. $ctx = new LogContext();
  162. $ctx
  163. ->setEntity($this->entity)
  164. ->setOperation($this->operation);
  165. if (!empty($matches)) {
  166. $ctx->setConfiguration($matches);
  167. $this->logger->logRunAll($ctx);
  168. } else {
  169. $this->logger->logRunNone($ctx);
  170. }
  171. return $matches;
  172. }
  173. /**
  174. * @param array $check
  175. * @return bool
  176. */
  177. public function check(array $check) {
  178. try {
  179. $checkInstance = $this->container->query($check['class']);
  180. } catch (QueryException $e) {
  181. // Check does not exist, assume it matches.
  182. return true;
  183. }
  184. if ($checkInstance instanceof IFileCheck) {
  185. if (empty($this->fileInfo)) {
  186. throw new RuntimeException('Must set file info before running the check');
  187. }
  188. $checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']);
  189. } elseif ($checkInstance instanceof IEntityCheck) {
  190. foreach ($this->contexts as $entityInfo) {
  191. [$entity, $subject] = $entityInfo;
  192. $checkInstance->setEntitySubject($entity, $subject);
  193. }
  194. } elseif (!$checkInstance instanceof ICheck) {
  195. // Check is invalid
  196. throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class']));
  197. }
  198. return $checkInstance->executeCheck($check['operator'], $check['value']);
  199. }
  200. }